Skip to main content
gamedev.net
🔒 Locked

another text & font problem

Started by pex22 Apr 7, 2005 at 8:57 AM 4 replies 3.5k views
Original Post
pex22
pex22
Hi. I can't see my text when printing (with wglUseFontBitmaps, i think the picture freeze (not the loop) when i tried with wglUseFontOutlines). Here's my code: Idle loop:
// Device d=init(win); Text text;
			d.color(RGB(100,0,0));
			//d.scale(V(1,1,0));
			//d.vertex(V(0,0,-6));
			glRasterPos2f(0,0);
			text.render("Time");
			d.swap();

Text::Text():
		logfont.lfHeight = -12;        // setup font characteristics
		logfont.lfWidth = 0;
		logfont.lfEscapement = 0;
		logfont.lfOrientation = 0;
		logfont.lfWeight = FW_NORMAL;
		logfont.lfItalic = FALSE;
		logfont.lfUnderline = TRUE;
		logfont.lfStrikeOut = FALSE;
		logfont.lfCharSet = ANSI_CHARSET;
		logfont.lfOutPrecision = OUT_DEFAULT_PRECIS;
		logfont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
		logfont.lfQuality = PROOF_QUALITY;
		logfont.lfPitchAndFamily = FF_DONTCARE|DEFAULT_PITCH;
		strcpy(logfont.lfFaceName,"Courier New");
		list=0;
		created=0;

Text::render(str,...):
		va_list ap;
		va_start(ap,str);
		vsprintf(str,str,ap);
		va_end(ap);
		if (!created)
			create();
		glPushAttrib(GL_LIST_BIT);
		glListBase(list);
		glCallLists(strlen(str),GL_UNSIGNED_BYTE,str);
		glPopAttrib();
		this->str = str;
		//if (!list&&!glIsList(list)) puts("NOT A LIST!"); // Printed if not commented!

Text::create():
		HFONT h=CreateFontIndirect(&logfont);
		SelectObject(wglGetCurrentDC(),h);
		list=glGenLists(256);
		//wglUseFontOutlines(wglGetCurrentDC(),0,256,list,0.0f,0.1f,WGL_FONT_POLYGONS,gmf);
		wglUseFontBitmaps(wglGetCurrentDC(),0,256,list);
		created=1;


Before the text drawing, i rendered a rectangle so i guess the rendering contex is fine, isnt it? (However, i cant see it if i draw the text!) What am I missing here? (in both Outlines and Bitmaps, its the same code. the only difference is that this i have to uncomment and this to comment for Outlines instead of Bitmaps). thanks, pex.
pex.
pex22
pex22
bump.
anyone?
pex.
Ludi83
Ludi83
Could you post some OpenGL code, where you draw the text. Perhaps, you aren't in Ortho mode or something else. Otherwise look at nehe tutorial 13(or 12 or 14), it's about text rendering
pex22
pex22
Ok. I have looked again at NeHe tutorial on bitmap fonts and it works, but only in position [0,0].
Here's the code:
Text class:
	void Text::render(Vector v,Vector v2,char* str,...)	{		char a[256];		va_list ap;		va_start(ap,str);		vsprintf(a,str,ap);		va_end(ap);		if (!created||hDC!=wglGetCurrentDC())			create();		Device d(LN_OPENGL);		d.start();//glPushMatrix		d.color(v);//glBegin(triangles),glColor3f		d.finish();//glEnd,glPopMatrix,glLoadIdentity		d.translate(V(0,0,-1));//glTranslatef		d.pos(v2);//glRasterPos2f		glPushAttrib(GL_LIST_BIT);		glListBase(list);		glCallLists(strlen(a),GL_UNSIGNED_BYTE,a);		glPopAttrib();	}	void Text::create()	{		hDC=wglGetCurrentDC();		HFONT h=CreateFontIndirect(&logfont);		SelectObject(hDC,h);		list=glGenLists(256);		wglUseFontBitmaps(hDC,0,256,list);		created=1;	}

Font creation:
	Text::Text()	{		logfont.lfHeight = -32;        // setup font characteristics		logfont.lfWidth = 0;		logfont.lfEscapement = 0;		logfont.lfOrientation = 0;		logfont.lfWeight = FW_NORMAL;		logfont.lfItalic = FALSE;		logfont.lfUnderline = FALSE;		logfont.lfStrikeOut = FALSE;		logfont.lfCharSet = ANSI_CHARSET;		logfont.lfOutPrecision = OUT_DEFAULT_PRECIS;		logfont.lfClipPrecision = CLIP_DEFAULT_PRECIS;		logfont.lfQuality = ANTIALIASED_QUALITY;		logfont.lfPitchAndFamily = FF_DONTCARE|DEFAULT_PITCH;		strcpy(logfont.lfFaceName,"Courier New");		list=0;		created=0;	}	Text text;	text.setEscapement(50);

Camera: pos[0,0,0] direction[0,0,-1]
OpenGL: win.opengl(LN_RGBA|LN_DOUBLE|LN_DEPTH,16); // 16 color & depth bits
i handled LN_DEPTH by:
			glClearDepth(1.0f);			glEnable(GL_DEPTH_TEST);			glDepthFunc(GL_LEQUAL);

Drawing code:
		case LN_DISPLAY:		case LN_IDLE:			d.clear();			cam.update();			if (int g=glGetError())				text.render(V(256,0,0),V(0,0),"OPENGL ERROR! %d",g);			d.start(LN_QUADS);			d.rotate(rotate,V(0,0,rotate));			d.translate(V(0,0));			d.color(RGB(0,0,50));			d.vertex(V(0.87f,1,-5));			d.color(RGB(0,50,0));			d.vertex(V(0.87f,-0.5,-5));			d.color(RGB(50,0,0));			d.vertex(V(-0.87f,-0.5,-5));			d.color(RGB(50,50,0));			d.vertex(V(-0.87f,1,-5));			d.finish();			text.render(V(100,0,0),V(0,0),"FPS: %.f",updateFPS());			d.swap();			rotate+=1.0;			break;

If I try to draw some text in position that isn't [0,0], i see the text running away from me (from left to right) in the bottom of the window and disappear.
it ran away when i had some drawing mistake (didnt use glTranslatef before glRasterPos2f or something), so its just an error that i cant find.
not a big deal. ok it is but it could be anything else.

Thanks,
pex.
pex.
Ludi83
Ludi83
Okay,

those peaces of code really didn't help, because you are only calling some mehtods of some classes, that I didn't know, but I am trying to help.

First question: Are you only trying to draw text?

If your text runs out of the screen, than you perhaps don't call glLoadIdentity() after your glClear()/glOrtho(). If you don't want to post the rest of you code here on gamedev, then mail me, and I will look at you code.
pex22
pex22
First answer: Yes, in this code:
		case LN_DISPLAY:		case LN_IDLE:			d.clear();			text.render(V(100,0,0),V(0,0),"FPS: %.f",updateFPS());			d.swap();			break;

LN_IDLE is the message i get from the window system that tells me i can draw the text (the idle function).
this is in the game loop, handling everything.
but i still have the same problem.

btw, Did you mean about the Device class?
here's what each one is calling:
Device d(LN_OPENGL):just tells 'd' that im using OpenGL.Device::clear():		glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);		glClearColor(0.0f,0.0f,0.0f,0.0f);Device::swap():                SwapBuffers();Device::start():                glPushMatrix();Device::color():                glBegin(GL_TRIANGLES);                glColor3f(Vector);Device::finish():                glEnd();                glPopMatrix();                glLoadIdentity();Device::translate():                glTranslatef(Vector);Device::pos():                glRasterPos2f(Vector (using only the X and Y of the vector));Device::resize():		int width=win->getWidth();		int height=win->getHeight();		glViewport(0,0,width,height);		glMatrixMode(GL_PROJECTION);		glLoadIdentity();		gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,.1f,100.0f);		glMatrixMode(GL_MODELVIEW);		glLoadIdentity();V(float,float,float z=0.0,float w=-1.0f) function:same as Vector(float,float,float,float).it return a Vector object that made up from these 4 parameters.

Variables:
list - the Display List of the text
d - a Device object
text - the Text object
logfont - Details on the font to be created
hDC - last time used HDC handle (if it is not the current HDC then the window has toggled to fullscreen or something so the font has to be build again).
Init code:
call Device::resize() in the beginning of the program and each time it being resized.
VSync is set to OFF. (the problem still happen when it is ON)
and as i said before:
win.opengl(LN_RGBA|LN_DOUBLE|LN_DEPTH,16); // 16 color & depth bits
before everything, i allocate memory for some pointers.
Is the opengl creation necessary? because drawing things like triangles and such work fine.
anyway, here it is:
int Ln::Window::opengl(int wt,int cBits,int dBits,int sBits){	LnLog->title(cname,"Using OpenGL");	int r=1;	int iFormat = 0;	int flags	= 0;	hDC = GetWindowDC(hwnd);	if (hDC==0)	{		r=0;		goto RETURN;	}	PIXELFORMATDESCRIPTOR pfd;	ZeroMemory( &pfd, sizeof( pfd ) );	pfd.nSize = sizeof( pfd );	pfd.nVersion = 1;	flags=PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL;	if (wt & LN_DOUBLE)		flags|=LN_DOUBLE;	pfd.dwFlags = flags;	if (wt & LN_RGBA)		pfd.iPixelType = PFD_TYPE_RGBA;	pfd.cColorBits = cBits;	pfd.cDepthBits = dBits;	pfd.cStencilBits = sBits;	pfd.iLayerType = PFD_MAIN_PLANE;	iFormat = ChoosePixelFormat( hDC, &pfd );	if (iFormat==0)	{		r=0;		goto RETURN;	}	if (!SetPixelFormat(hDC,iFormat,&pfd))	{		r=0;		goto RETURN;	}	hRC = wglCreateContext(hDC);	if (hRC==0)	{		r=0;		goto RETURN;	}	if (!wglMakeCurrent(hDC,hRC))	{		r=0;		goto RETURN;	}	opengled=wt;	oglcbits=cBits;	ogldbits=dBits;	oglsbits=sBits;RETURN:	LnLog->add(GetLastError());	isogl=r;	if (r)	{		if (wt&LN_DEPTH)		{			glClearDepth(1.0f);			glEnable(GL_DEPTH_TEST);			glDepthFunc(GL_LEQUAL);		}		gfxmode=LN_OPENGL;	}	else		gfxmode=LN_NONE;	return r;}


And im not using ortho mode in this code. thats why i translated to [0,0,-1] before glRasterPos2f (d.pos(vec2);) (something that NeHe said. did i miss something out there?).
The rest code is long..so if this post doesnt help i'll email.

Thanks v.much,
pex.
pex.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.