Static text in a 2D game using a Camera

Started by jeffrey1994, August 12, 2013, 19:28:06

Previous topic - Next topic

jeffrey1994

I've been working for the past few days on my very first OpenGL game, which is based of Minecraft but in 2D.

Since I'm using a chunk system and want to display statistics(also things like FPS) on my game I need to draw text on my screen.
But today I started implementing a camera(for now only on the X-axis) but I ran into a problem where the text will be moved since I'm changing the glOrtho() to change the position of the Camera.

I've been looking for solutions on how to draw static text, but the only thing I can find is people that do this by switching between a gluPerspective and glOrtho(). Since I'm always using a glOrtho() I don't know how to implement this.. also I'm not that experienced with OpenGL yet :-\

In this video you can see what I mean:
http://www.youtube.com/watch?v=JWfdQhW12kg

----
So basically I'm looking for a way to draw text on my screen and whenever I move my Camera I want to have the text stay in the same position. If anybody knows a simple solution or a handy way of implementing this please share.

Cornix

You have to first draw all game sprites, and then, at the end of the update cycle draw the text.
Before you draw the text you reset the projection matrix (which is set by glOrtho) by loading the identity (or other static matrix).

jeffrey1994

Thank you for your answer and I tried doing what you said and this is what I came up with.

Currently this is what I changed:

Old:
private void drawDebugInformation()
	{		
		// top-left
		fontRenderer.drawString(5, 5, "FPS: " + FPS);
		fontRenderer.drawString(5, 20, "Vertices: " + totalNumberOfVertices);
		fontRenderer.drawString(5, 35, "Blocks: " + world.getNumberOfBlocks());
		fontRenderer.drawString(5, 50, "Chunks: " + world.getNumberOfChunks());

		// top-right
		fontRenderer.drawString(Window.WINDOW_WIDTH - 135, 5, "Resolution: " + Window.WINDOW_WIDTH + "x" + Window.WINDOW_HEIGHT);				
	}


and the new code:
private void drawDebugInformation()
	{
		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();		
		glOrtho(0, Window.WINDOW_WIDTH, Window.WINDOW_HEIGHT, 0, -1, 1);		
		glMatrixMode(GL_MODELVIEW);
		
		// top-left
		fontRenderer.drawString(5, 5, "FPS: " + FPS);
		fontRenderer.drawString(5, 20, "Vertices: " + totalNumberOfVertices);
		fontRenderer.drawString(5, 35, "Blocks: " + world.getNumberOfBlocks());
		fontRenderer.drawString(5, 50, "Chunks: " + world.getNumberOfChunks());

		// top-right
		fontRenderer.drawString(Window.WINDOW_WIDTH - 135, 5, "Resolution: " + Window.WINDOW_WIDTH + "x" + Window.WINDOW_HEIGHT);			
		
		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();		
		glOrtho(camera.getX(), camera.getX() + Window.WINDOW_WIDTH, camera.getY() + Window.WINDOW_HEIGHT, camera.getY(), -1, 1);		
		glMatrixMode(GL_MODELVIEW);	
	}


It now seems to work fine but this doesn't look like a 'clean' method to do this? I feel like this is actually pretty bad or is it?

Cornix

Thats exactly the same as calling glOrtho and gluLookAt.
This is what you do in OpenGL, you manipulate the matrices depending on what you want to do.