Jump to content
  • Advertisement
Sign in to follow this  
Show

help: 100% CPU usage..

This topic is 4453 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

Whenever I launch my (opengl) game.. the cpu usage goes up to 100%. Is this normal? my game doesnt even do anything.. it just draws a tile map on the screen and that's it. Here is the code that's causing high cpu usage.
bool DR::DrawScene()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    glTranslatef(-11.0f, 9.0f,-35.0f);

    char tile(' ');
    int xpos(0);
    int ypos(0);  
    int counter(0);
 
    do
    {
        tile = map.CurrentMap()[counter];
        
        if ( xpos > 21 ) { //goes to next line
            xpos=0;
            ypos++;
        }
              
        switch(tile)
        {
            case '0':
                 glColor3f(0.6f,0.3f,0.1f); //wall=dark brown
                 break;
            case '1':
                 glColor3f(0.8f,0.3f,0.1f); //path=light brown
                 break;
            case '2':
                 glColor3f(0.0f,0.0f,1.0f); //player=blue
                 break;
            case '3':
                 glColor3f(1.0f,1.0f,0.0f); //gold=yellow
                 break;
            case '4':
                 glColor3f(0.8f,0.3f,0.8f); //treasure box=orange
                 break;
            case '5':
                 glColor3f(1.0f,0.6f,0.6f); //heart= pink
                 break;
            case '6':
                 glColor3f(1.0f,0.0f,0.0f); //mine=red
                 break;
            case '7':
                 glColor3f(0.0f,1.0f,0.0f); //store=teal
                 break;
            case 'X':
                 glColor3f(0.0f,0.0f,0.0f); //game border
        }
        
        glBegin(GL_QUADS);
			glVertex3f(float(xpos), float(-ypos), 0.0f);
			glVertex3f(float(xpos), float(-ypos-1), 0.0f);
			glVertex3f(float(xpos+1), float(-ypos-1), 0.0f);
			glVertex3f(float(xpos+1), float(-ypos), 0.0f);
		glEnd();
		
		xpos++;
		counter++;
        
    } while(counter < map.CurrentMap().size());
    
    return true;
}


map.CurrentMap() returns a vector that's size 400. winmain:
int WINAPI WinMain(	HINSTANCE	hInstance,			// Instance
					HINSTANCE	hPrevInstance,		// Previous Instance
					LPSTR		lpCmdLine,			// Command Line Parameters
					int			nCmdShow)			// Window Show State
{
	MSG		msg;									// Windows Message Structure
	BOOL	done = false;								// Bool Variable To Exit Loop
    DR game;                                // Create our game
	
	// Create OpenGL window
	if (!CreateGLWindow("Dark Raider",600,600,16,fullscreen))
	{
		return 0;									// Quit If Window Was Not Created
	}

	while(!done)									// loop until done == true
	{
		if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))	// Is There A Message Waiting?
		{
			if (msg.message==WM_QUIT)				// if received quit msg
			{
				done = true;						// then set done = true
			}
			else									// If Not, Deal With Window Messages
			{
				TranslateMessage(&msg);				// Translate The Message
				DispatchMessage(&msg);				// Dispatch The Message
			}
		}
		else										// If There Are No Messages
		{
			// Draw The Scene.  Watch For ESC Key And Quit Messages From DrawGLScene()
			if ((active && !game.DrawScene()) || keys[VK_ESCAPE])	// Active?  Was There A Quit Received?
			{
				done=true;							// ESC or DrawGLScene Signalled A Quit
			}
			else									// Not Time To Quit, Update Screen
			{
				SwapBuffers(hDC);					// Swap Buffers (Double Buffering)
			}
		}
	}

	// Shutdown
	KillGLWindow();									// Kill The Window
	return (msg.wParam);							// Exit The Program
}


Any help would be appreciated.. Thanks.

Share this post


Link to post
Share on other sites
Advertisement
I couldn't see that you let the process sleep anywhere. If nothing very much interesting is happening, you could add Sleep(1); in the standard-loop where not much happens.

Share this post


Link to post
Share on other sites
You're main loop is just looping constantly. As soon as it fiunishes one iteration, it goes on to the next. Obviously you are not giving anything back to the OS. You can use a sleep call or something to give a bit back. However, a full screen game probably should be using as much resources as possible, especially if it will be run on slower hardware.

Share this post


Link to post
Share on other sites
From what I hear it normal but what happens if you set the frame rate at max 32 ms?

...
DWORD start_time, curr_time;
start_time = curr_time = GetTickCount();
...
curr_time = GetTickCount();
if(curr_time - start_time > 32)
{
if ((active && !game.DrawScene()) || keys[VK_ESCAPE])
{
done=true;
}
else
{
SwapBuffers(hDC);
}
}
...

I have a similar project that runs with 50% cpu with this setup

Share this post


Link to post
Share on other sites
Quote:
Original post by _0D_0A
I couldn't see that you let the process sleep anywhere. If nothing very much interesting is happening, you could add Sleep(1); in the standard-loop where not much happens.


Well, if you're in the foreground, and full screen, you'll want to have 100% of the CPU, or as much of it as possible. Sleep can work a little, but really isn't what I prefer.

As I see it though, your program is always active unless it quits, so its always going to draw, even if you move it into the background.

If you minimize the window, or move it into the background, you should wait for a message

Share this post


Link to post
Share on other sites
Quote:
Original post by pulpfist
From what I hear it normal but what happens if you set the frame rate at max 32 ms?

...
DWORD start_time, curr_time;
start_time = curr_time = GetTickCount();
...
curr_time = GetTickCount();
if(curr_time - start_time > 32)
{
if ((active && !game.DrawScene()) || keys[VK_ESCAPE])
{
done=true;
}
else
{
SwapBuffers(hDC);
}
}
...


I have a similar project that runs with 50% cpu with this setup


All that will accomplish is slowing down the framerate. The CPU usage will still be 100% since the loop ois just ging as fast as it can. If there is any reason he CAN'T have 100% CPU (maybe to be nice to laptop users, for instance), then a sleep call is the only way.

The reason your CPU is at 50% means you havea Pentium 4 with Hyperthreading enabled, a dual core processor, or some other CPU intensive program is running in the background.

Sleep is a kinda unreliable call. Sleep(1) basically says "Tell the OS to do something else for AT LEAST 1 millisecond." It could be 1 ms, it could be 10. I would not base anything to important off of this time.


Edit: A quick note: If your game is taking 100% of the CPU, that means there is nothing else using the CPU. If something else actually needed the CPU constantly, your game would use only 50%. The windows scheduler is decent. When you tell it to sleep and there is nothing else to do, the OS is just going to chill for a while and do nothing. You won't be helping anything except possibly the processor temperature.

Share this post


Link to post
Share on other sites
Quote:
Original post by pulpfist
I guess your right.
How about a yield call in Windows API? Is there such a thing?


It is called "Sleep()". Just use that function and pass the number of milliseconds. I have no idea if it is windows only or anything (I'll look it up in a second).

Edit: It does appear to be windows only...

Share this post


Link to post
Share on other sites
Quote:
Original post by Sr_Guapo
Quote:
Original post by pulpfist
I guess your right.
How about a yield call in Windows API? Is there such a thing?


It is called "Sleep()". Just use that function and pass the number of milliseconds. I have no idea if it is windows only or anything (I'll look it up in a second).


It's Windows only and you shouldn't just pass it a number of MS. Sleep(0) is about the same as a yield. You shouldn't pass anything greater unless you wait for a very long time. Sleep(1) often results in more than 10ms waits, which is completely unacceptable.

EDIT: Also you shouldn't use yield unless you can see you are rendering way too much (>150 FPS, at least). imagine slowing your game down 3% when the user already have low-end hardware.

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!