Flickering Text

Started by
7 comments, last by Count_Zero 20 years, 11 months ago
Can someone help me out please.. I have got a simple asteroids game now running and have attempted to put some text overlaying the objects to display the score, level and lives values. This is using the work in NeHe''s Tutorial 14. All the main base code is from NeHe''s tutorial 1 for the InitGL and DrawGLFrame functions. If I use glPrint to show one line of text, the display is rock solid. However if I try to show any additional lines then they start flickering - but the first lines displayed doesn''t (only the extra ones printed?). the only change to the glPrint routine is that I''ve added glLoadIdentity() before the translate and the first three parameters are the x, y and z co-ords. The lines of code below shows the object print order.. // * draw the objects * fragments.draw_forward(false); asteroids.draw_forward(false); bullets.draw_forward(true); player.draw_forward(false); player_death.draw_forward(false); // draw all the font text glColor3f(1.0f, 0.5f, 0.0f); glPrint(0.0f, 0.0f, 0.0f, "SCORE:%u", intScore); // Print GL Text To The Screen glColor3f(1.0f, 0.0f, 0.5f); glPrint(0.0f, 0.0f, 0.0f, "LIVES: %u", intCurrentLives); // Print GL Text To The Screen glColor3f(1.0f, 0.5f, 0.5f); glPrint(0.0f, 0.0f, 0.0f, "LEVEL: %u", intLevel); // Print GL Text To The Screen the following code is the routine which does the actual object displaying... struct objects { TriSector data; // my triangle and vertex list bool bAlive; int points; char* object_name; objects* next; objects* prev; }; class object_list { //private: public: objects* first; objects* last; //public: object_list(); void add_object(TriSector t, char* object_name, int pointscore); void draw_forward(bool bRotateFirst); void draw_reverse(); void update_asteroids(float OffSet); void update_player(float OffSet, float angle); void update_bullets(float OffSet); void update_fragments(float OffSet); int check_asteroid_hit(object_list asteroids); void remove_object(objects* thislink); }; void object_list::draw_forward(bool bRotateFirst) { objects* viewlink = first; float alpha; while (viewlink != NULL) { /* draw commands in here */ glLoadIdentity(); // clear current matrix if (bRotateFirst) { glRotatef(viewlink->data.rangle_sum, viewlink->data.rx, viewlink->data.ry, viewlink->data.rz); // rotate the asteroid glTranslatef(viewlink->data.cx,viewlink->data.cy,viewlink->data.cz - 65.0f); // move asteroids position } else { glTranslatef(viewlink->data.cx,viewlink->data.cy,viewlink->data.cz - 65.0f); // move asteroids position glRotatef(viewlink->data.rangle_sum, viewlink->data.rx, viewlink->data.ry, viewlink->data.rz); // rotate the asteroid } glScalef(viewlink->data.sx,viewlink->data.sy,viewlink->data.sz); // make it a huge asteroid glBegin(GL_TRIANGLES); // use alpha for percent of colour. 1.0f = 100% alpha = viewlink->data.alpha_value; for (triLoop = 0; triLoop < viewlink->data.numtriangles ; triLoop++) { for (vertLoop = 0; vertLoop < 3; vertLoop++) { // get the rgb values r_m = viewlink->data.triangle[triLoop].vertex[vertLoop].r; g_m = viewlink->data.triangle[triLoop].vertex[vertLoop].g; b_m = viewlink->data.triangle[triLoop].vertex[vertLoop].b; //a_m = viewlink->data.triangle[triLoop].vertex[vertLoop].a; // get the x,y,z values x_m = viewlink->data.triangle[triLoop].vertex[vertLoop].x; y_m = viewlink->data.triangle[triLoop].vertex[vertLoop].y; z_m = viewlink->data.triangle[triLoop].vertex[vertLoop].z; glColor3f(r_m * alpha, g_m * alpha, b_m * alpha); glVertex3f(x_m, y_m, z_m); } } glEnd(); viewlink = viewlink->next; } } I can''t understand whats going on, but if I move the glPrint comand to before my draw_forward commands then everything flickers. Any thoughts??
Advertisement
Okay maybe this isn''t the problem but if the first 3 parameters for your print function are x,y,z coordinates, why are they all set to (0,0,0)?

quote:Original post by Count_Zero

glPrint(0.0f, 0.0f, 0.0f, "SCORE:%u", intScore);

glPrint(0.0f, 0.0f, 0.0f, "LIVES: %u", intCurrentLives);

glPrint(0.0f, 0.0f, 0.0f, "LEVEL: %u", intLevel);


That''s the only thing I noticed. Would they not all be displayed in the same area and maybe that causes some flickering?
"If all else fails, lower your standards."
Thanks for that.. I have moved them so they are all now on different parts of the screen.. and yes are correct in that the three values are x,y and z - they we''re overlayed giving me a big blob..

glPrint(-10.0f, 10.0f, -40.0f, "...") // top left
glPrint(10.0f, 10.0f, -40.0f, "...") // top right
glPrint(0.0f, -10.0f, -40.0f, "...") // middle bottom

However, the problem is still occuring? the amount of processing shouldn''t be an issue as I''m running on a 21.GHz Athlon?

A sudden thought though! Could this flickering be caused by the refresh rate of the graphics card, and not having it locked to the retrace - I think this is one of the options in the display drivers?
Sorry I don''t know anything about retrace on the drivers. Maybe somebody else here might know if that''ll work.
This might not help at all but I was trying out the text myself awhile ago and got some flickering. What was happening was, I was calculating the FPS, so every second it would try to print the FPS. This was grand on the title bar but when I start using the glPrint() function it would just flash the value onscreen really quick. Just check make sure you''re print statement isn''t stuck in some kind of conditional block statement. Mine was something like if 1 sec passed print, then it cleared it till the next second. Ok, that mightn''t help at all but it''s all I can think of.
Best of luck getting it working!
"If all else fails, lower your standards."
hi!

are you swapping buffers ? i didn''t find that command.
if you''re not swapping buffers that might be the reason for flickering.
Hi.. I''ve had a check and the swapbuffers is definitely working, as this is in the winMain function. Also, the text is the only thing that is flickering as all my other polygon objects are displayed correctly?

I''ve put my main code below as this is the drawGLframe function - my apologies for the length of it, but I just can''t see what wrong? the glPrint() routine is the same one from NeHe''s tutorial as I don''t want to change that until I understand how these display lists work..

GLvoid glPrint(float xVal, float yVal, float zVal, const char *fmt, ...)					// Custom GL "Print" Routine{	float		length=0;								// Used To Find The Length Of The Text	char		text[256];								// Holds Our String	va_list		ap;										// Pointer To List Of Arguments	if (fmt == NULL)									// If There''s No Text		return;											// Do Nothing	va_start(ap, fmt);									// Parses The String For Variables	    vsprintf(text, fmt, ap);						// And Converts Symbols To Actual Numbers	va_end(ap);											// Results Are Stored In Text	glLoadIdentity();	glTranslatef(xVal, yVal, zVal);		// Center Our Text On The Screen	glPushAttrib(GL_LIST_BIT);							// Pushes The Display List Bits	glListBase(base);									// Sets The Base Character to 0	glCallLists(strlen(text), GL_UNSIGNED_BYTE, text);	// Draws The Display List Text	glPopAttrib();										// Pops The Display List Bits}int DrawGLScene(float flMovementOffSet)  // flMovementOffSet == steps[adjust]{	glClear(GL_COLOR_BUFFER_BIT	| GL_DEPTH_BUFFER_BIT); // clear the screen and depth buffer	glLoadIdentity();		// reset the current model view matrix	/// ******* start doing the actual game work here **********	// * check for end of level with both asteroid and fragment lists being empty *	// * this denotes that either start of game, or progressed to next level	if (asteroids.first == NULL && fragments.first == NULL)	{		intLevel = InitLevel(intLevel);		intLevel++;	}	// * check the input from main loop and update variables *	flSize = 0.0f;	xCentre = yCentre = 0.0f;	if (bFires && player.first != NULL)	{		// player is firing, add a new bullet and reset flag		// what about maximum numbers at a time?		bullet.linear_distance = 0.0f;		bullet.rangle = 0.0f;		bullet.rangle_sum = 0.0f;		bullet.bullet_angle = intDirAngle - 90.0f; // so zero is straight up		bullet.life_count = 0;		bullets.add_object(bullet, "BULLET", 0);		bFires = false;	}	// * move all the objects to new positions *	player.update_player(flMovementOffSet, intDirAngle);	asteroids.update_asteroids(flMovementOffSet);	bullets.update_bullets(flMovementOffSet);	fragments.update_fragments(flMovementOffSet);	player_death.update_fragments(flMovementOffSet);	// * check for any collisions *		// bullets that have hit any asteroids	intScore += bullets.check_asteroid_hit(asteroids);	// asteroids that have hit the player	player.check_asteroid_hit(asteroids);	objects* asteroidlink = asteroids.first;	while (asteroidlink != NULL)	{		if (!asteroidlink->bAlive)		{			flSize = (asteroidlink->data.sx - 1.0f);			xCentre = asteroidlink->data.cx;			yCentre = asteroidlink->data.cy;			// add four fragments here for this dead asteroid??			huge_fragment = new_fragment_variables(huge_fragment, asteroidlink->data.sx, xCentre, yCentre);			fragments.add_object(huge_fragment, "HUGE_FRAG", 0);			large_fragment = new_fragment_variables(large_fragment, asteroidlink->data.sx, xCentre, yCentre);			fragments.add_object(large_fragment, "LARGE_FRAG", 0);						medium_fragment = new_fragment_variables(medium_fragment, asteroidlink->data.sx, xCentre, yCentre);			fragments.add_object(medium_fragment, "MED_FRAG", 0);						tiny_fragment = new_fragment_variables(tiny_fragment, asteroidlink->data.sx, xCentre, yCentre);			fragments.add_object(tiny_fragment, "TINY_FRAG", 0);			if (flSize >= 1.0f)			{				huge_asteroid = new_asteroid_variables(huge_asteroid, flSize, xCentre, yCentre);				huge_asteroid.life_count = 0;				asteroids.add_object(huge_asteroid, "ASTEROID", 100 - ((int) flSize * 10));				huge_asteroid = new_asteroid_variables(huge_asteroid, flSize, xCentre, yCentre);				huge_asteroid.life_count = 0;				asteroids.add_object(huge_asteroid, "ASTEROID", 100 - ((int) flSize * 10));			}		}		asteroidlink = asteroidlink->next;	} 	// * draw the objects *	fragments.draw_forward(false);	asteroids.draw_forward(false);	bullets.draw_forward(true);	player.draw_forward(false);	player_death.draw_forward(false);	// draw all the font text	glColor3f(1.0f, 0.5f, 0.0f);	glPrint(-10.0f, 10.0f, -40.0f, "SCORE:%u", intScore);				// Print GL Text To The Screen	glColor3f(1.0f, 0.5f, 0.0f);	glPrint(10.0f, 10.0f, -40.0f, "LIVES: %u", intCurrentLives);				// Print GL Text To The Screen	glColor3f(1.0f, 0.5f, 0.0f);	glPrint(0.0f, -10.0f, -40.0f, "LEVEL: %u", intLevel);				// Print GL Text To The Screen	fragments = tidy_up(fragments);	asteroids = tidy_up(asteroids);	bullets = tidy_up(bullets);	player = tidy_up(player);	player_death = tidy_up(player_death);	// ******* finished doing all the work here ***************	return true;} 


All the tidy_up routine is doing is deleting all the dead objects and removing there pointers..

I have also added the timer code into the winMain to reduce the speed of the game, however if I remove this code it makes no difference to the flickering and everything just runs ludicrously fast.. if I rem out the glPrint() everything is fine?
Try to turn DepthTesting off by
glDisable(GL_DEPTH_TEST);  

before you start to render the text!

BTW: When you have all three values set to 0.0f then the va_list gets confused and the result may be bad.

And: When you set the view to Ortho-Mode you dont need a third value for Z:

Try this functions:
void SetOrtho(){	glMatrixMode(GL_PROJECTION);		// Select Projection	glPushMatrix();						// Push The Matrix	glLoadIdentity();					// Reset The Matrix	glOrtho( 0, resX, resY, 0, -1, 1);	// Select Ortho Mode (640x480)	glMatrixMode(GL_MODELVIEW);			// Select Modelview Matrix	glPushMatrix();						// Push The Matrix	glLoadIdentity();					// Reset The Matrix}// Your 'Draw-Text' code goes here....void SetPerspective(){	glMatrixMode(GL_PROJECTION);		// Select Projection	glPopMatrix();						// Pop The Matrix	glMatrixMode(GL_MODELVIEW);			// Select Modelview	glPopMatrix();						// Pop The Matrix}  




Dark

McNugget next time...

[edited by - DarkKiller on April 30, 2003 5:42:30 AM]
DarkMcNugget next time... ;)
Hi DarkKiller.. You rock my world :o)

I have added glDisable(GL_DEPTH_TEST) just before the glPrint lines, and have got glEnable(GL_DEPTH_TEST) at the beginning of the drawGLframe function.

This has fixed the problem. I can now print text anywhere without any flickering whatsoever..

Many Thanks.
You''re welcome! :D

Dark

McNugget next time...
DarkMcNugget next time... ;)

This topic is closed to new replies.

Advertisement