high cpu usage problem

Started by
14 comments, last by WWakerFAN 18 years, 9 months ago
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();
}
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.
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.
stick a SDL_Wait(5) in somewhere
"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?
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.
-Scott
the current framerate i'm getting is around 28-30 fps if that helps
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.
ok, so if the 100% thing is normal, is there anything i can do to get a higher frame rate?
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.

This topic is closed to new replies.

Advertisement