Code:
typedef struct Font{
struct Font *prev, *next; //for inclusion in linked lists
HFONT font;
int size;
int base;
int inUse;
}Font;
Font *initFont(void){
Font *font;
font = calloc(sizeof(Font));
return font;
}
void freeFont(Font *font){
DeleteObject(font->font);
free(font);
}
void freeFontList(struct List *list){
Font *font;
while(font = list->first){
glDeleteLists(font->base, 96);
LIST_removeFirst(list);
freeFont(font);
}
LIST_free(list);
}
Font *addFont(int size){
Font *f, *t;
t = G->font->first;
while (t){
if (t->size == size) return 0; //font already exists
t = t->next;
}
f = initFont();
LIST_append(G->font, f);
f->base = glGenLists(96);
f->font = CreateFont(size, 0, 0, 0, 500, FALSE, FALSE, FALSE,
ANSI_CHARSET, OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS,
ANTIALIASED_QUALITY, FF_DONTCARE|DEFAULT_PITCH, "Courier New");
f->size = size;
return f;
}
void removeFont(int size){
Font *t, *f;
f = G->font->first;
while(f){
t = f->next;
if (f->size == size){
LIST_remove(G->font, f);
freeFont(f);
}
f = t;
}
}
void printText (const char *input, ...){
char text[256];
va_list ap;
if (input == NULL) return;
va_start(ap, input);
vsprintf(text, input, ap);
va_end(ap);
glPushAttrib(GL_LIST_BIT);
glListBase(G->curFont->base - 32);
glCallLists(strlen(text), GL_UNSIGNED_BYTE, text);
glPopAttrib();
}
Font *getFont(int size){
Font *f;
f = G->font->first;
while(f){
if (f->size == size) return f;
f = f->next;
}
return 0;
}
//////////////////////////////////////////////////////////////////////////
//the function that is called to print text on a window
void gui_print_text(Window *win, int x, int y, const char *text, ...){
Font *f;
int w, h;
if (!win) return;
if (!text) return;
if (!G->curFont) return;
f = G->curFont;
if (!f->inUse){
HFONT oldfont;
oldfont = (HFONT)SelectObject(win->winInfo->hdc, f->font);
wglUseFontBitmaps(win->winInfo->hdc, 32, 96, f->base);
SelectObject(win->winInfo->hdc, oldfont);
f->inUse = 1;
}
wglMakeCurrent(win->winInfo->hdc, win->winInfo->hrc);
w = win->clientRect->w;
h = win->clientRect->h;
glViewport(0, 0, w, h);
glScissor(0, 0, w, h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho(0, w, 0, h, -1, 1);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
glRasterPos2f((float)x, (float)y);
glColor3f(0.0f, 0.0f, 0.0f);
printText(text);
}
void gui_set_font_size(int size){
Font *font, *t, *temp;
font = getFont(size);
if (!font){
t = addFont(size);
G->curFont->inUse = 0;
G->curFont = t;
G->curFont->inUse = 0;
}
else{
if (G->curFont != font){
G->curFont = font;
font->inUse = 0;
}
}
}
Can anyone see a mistake ?