works around GLUT bugs related to monospace glyphs

This commit is contained in:
tosh 2013-04-17 00:39:35 +02:00
parent dc4dc887ac
commit e811410f77

View file

@ -13,6 +13,13 @@
#include "world.h"
#include "Util.h"
using namespace std;
// no clue which versions are affected...
#define WORKAROUND_BROKEN_GLUT_STROKES
// see http://www.opengl.org/resources/libraries/glut/spec3/node78.html
static float MONO_STROKE_WIDTH_GLUT = 104.76;
// Return the azimuth angle in degrees between two points.
float azimuth_to(glm::vec3 head_pos, glm::vec3 source_pos) {
return atan2(head_pos.x - source_pos.x, head_pos.z - source_pos.z) * 180.0f / PIf;
@ -107,12 +114,17 @@ double diffclock(timeval *clock1,timeval *clock2)
return diffms;
}
int widthText(float scale, int mono, char *string) {
int widthText(float scale, int mono, char const* string) {
int width = 0;
if (!mono) {
width = scale * glutStrokeLength(GLUT_STROKE_ROMAN, (const unsigned char *) string);
} else {
#ifndef WORKAROUND_BROKEN_GLUT_STROKES
width = scale * glutStrokeLength(GLUT_STROKE_MONO_ROMAN, (const unsigned char *) string);
#else
// return value is unreliable, so just calculate it
width = scale * float(strlen(string)) * MONO_STROKE_WIDTH_GLUT;
#endif
}
return width;
}
@ -134,8 +146,27 @@ void drawtext(int x, int y, float scale, float rotate, float thick, int mono,
len = (int) strlen(string);
for (i = 0; i < len; i++)
{
if (!mono) glutStrokeCharacter(GLUT_STROKE_ROMAN, int(string[i]));
else glutStrokeCharacter(GLUT_STROKE_MONO_ROMAN, int(string[i]));
if (!mono) {
glutStrokeCharacter(GLUT_STROKE_ROMAN, int(string[i]));
} else {
#ifdef WORKAROUND_BROKEN_GLUT_STROKES
if (string[i] != 'm') {
#endif
glutStrokeCharacter(GLUT_STROKE_MONO_ROMAN, int(string[i]));
#ifdef WORKAROUND_BROKEN_GLUT_STROKES
} else {
// some glut implementations have a broken 'm'...
unsigned char tmpStr[2];
tmpStr[0] = string[i];
tmpStr[1] = '\0';
float scale = MONO_STROKE_WIDTH_GLUT / glutStrokeLength(GLUT_STROKE_ROMAN, tmpStr);
glScalef(scale, 1.0f, 1.0f);
glutStrokeCharacter(GLUT_STROKE_ROMAN, int(string[i]));
// stay humble on the stack - might be in projection mode
glScalef(1.0f / scale, 1.0f, 1.0f);
}
#endif
}
}
glPopMatrix();