Thread: problem with my font manager

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    61

    problem with my font manager

    I've written a small font manager, to be used in openGL applications, using the wgl - functions. I have a few problems though, and I can't find the error(s).
    These are the problems :
    - when creating a new font, it doesn't always draw from the first time. I'll try to explain it clearly :
    There is a font that is added when the program is initialised (the standard font).
    * When I change the standard font to another font, the new font won't draw. When I switch back to the standard font, the standard font is drawn.
    * When I change the standard font to a new font, and then change that new font to another new font, this new font is drawn. From this moment, the second new font will always work.

    - one word of the text (in the bottom left corner changes color once every few frames. It happens when I do a full screen redraw. It's weird because I call glColor3f(...) right before drawing the text.

    This is the font code :
    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 ?
    btw : the gui_print_text() function is called every frame.

    Thanks in advance.

  2. #2
    Registered User
    Join Date
    Feb 2005
    Posts
    61
    I found out that when I switch from the standard font to another font, that wglUseFontBitmaps returns 0 instead of 1. And that's probably the error. However, I don't know why it returns 0.

    When I switch back to the standard font, it always returns 1, and when I change to a new font from another new font, it also returns 1.

    so :
    standard -> new => returns 0
    new -> standard => returns 1
    new -> new => returns 1

    Does anyone know why this is ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  2. Templated Generic Resource Manager, WIP..
    By Shamino in forum C++ Programming
    Replies: 13
    Last Post: 02-19-2006, 06:29 PM
  3. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM