Jump to content
  • Advertisement
Sign in to follow this  
WWakerFAN

OpenGL high cpu usage problem

This topic is 4860 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i'm try to create a tiling system for a 2d game using a combination of SDL and openGL, but it seems it likes to use 100% cpu usage and i can't figure out why this is the function where all the drawing takes place
void render() {
	SDL_LockSurface(screen);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glEnable(GL_TEXTURE_2D);
		for(GLint j=-1;j<15;j++) {
			glLoadIdentity();
			glTranslatef(-16.0f,j*16,0.0f);
			for(GLint k=-1;k<17;k++) {
				glBindTexture(GL_TEXTURE_2D, image[test_map[j+1][k+1]]);
				glCallList(base_map);
				glTranslatef(16.0f,0.0f,0.0f);
			}
		}
	glDisable(GL_TEXTURE_2D);
	glFlush();
	SDL_UnlockSurface(screen);
	SDL_GL_SwapBuffers();
}

Share this post


Link to post
Share on other sites
Advertisement
2 quick observations:

Measure your framerate. If your game is not VBL synched, it will redraw unnecessarily quickly, consuming all available CPU.

SDL_LockSurface and SDL_UnlockSurface are unnecessary when drawing with OpenGL.

Share this post


Link to post
Share on other sites
Normally a game loop will consume all available processor time, unless you do as the previous poster mentioned. I suppose it depends on the nature of the game if this behavior is a problem. Certainly we wouldn't want solitaire running at 99% cpu utilization. However for a real-time game such as quake3, it may be acceptable to hog the cpu. Regardless, it wouldn't be a bad idea to give the user the option to cap the frame rate. People who use laptops will appreciated it, as running at 99% utilization will drain the battery in a hurry.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
"Realtime" games are supposed to use 100%.

What does your main loop look like? If there is no wait/sleep function and your app isn't event based, what do you expect CPU usage to be?

Share this post


Link to post
Share on other sites
When your application is running feel free to run at max speed and use 100% of the CPU, just don't notch up your threads priority w/o good reason.

When your application is NOT active, you should tell windows to wait around and every 2 or so seconds check the messages.


int WindowsEntryPoint::Run()
{
MSG msg;
memset(&msg,0,sizeof(MSG));

while (msg.message != WM_QUIT)
{
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

EntryPoint::Run(); //calls the parent function implicility

if (m_bAlive == false)
PostQuitMessage(0);

if (m_bActive == false)
{
Sleep(1500); //check every 1.5 seconds to see if we need to come back
}


}

return (int)msg.wParam;
}




Note, EntryPoint::Run will return imediatly if m_bActive == false.
m_bActive means if the application is focused.

Share this post


Link to post
Share on other sites
Well, how much CPU do you expect it to use? It renders one frame (which takes 100% cpu for a short bit of time). What happens after that? Do you tell it to wait? Does it start on the next frame? Most likely you do the latter. In that case, it immediately starts on another frame, which again consumes 100% cpu until it's done, and so on.

Of course a loop takes 100% cpu if there's nothing in there to halt it occasionally. That's why practically *every* game on the market uses 100% cpu.

As someone said above, that's fine, just make sure you don't do it if the game isn't active. If they user alt-tabs out, for example.

Share this post


Link to post
Share on other sites
hi, well without changing much your rendering loop you could remove the enable/disable calls for GL_TEXTURE_2D and place only one enable in your init function.
Also texture binding is quite costly so if you have a small number of images you could sort the tiles based on image index, bind the texture and render all tiles with that texture.
You could also combine all your tiles in one texture and play with texture coordinates (this would work only if you have a small tile image size).

Hope this helps somehow.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!