Jump to content
  • Advertisement
Sign in to follow this  
kingpinzs

issue with my game loop taking up to much cpu

This topic is 4828 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 am making a realy simple directdraw program that runs in windowed mode and all it does is blt a image to the screen. the issue is that it takes for ever to let me exit out of it. If you need more detailes or code just let me know. does any one know why just blting to the front buffer would take up so much time in the cycle? The inage is really small.

Share this post


Link to post
Share on other sites
Advertisement
Maybe you should only draw it when it needs to be updated, or if you are always updating it (like moving it around) then you might want to add a Sleep(0), so that it will let other processes use the CPU.

Share this post


Link to post
Share on other sites
windows main loop


//message pump
for( ; ; )
{
//look for a message
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
//there is a message
//check that we arent quitting
if(msg.message==WM_QUIT) break;
//translate message
TranslateMessage(&msg);
//dispatch message
DispatchMessage(&msg);
}
//run main game loop
Main_Loop();
}
//clean up program data
//return the wparam from the WM_QUIT message
return(msg.wParam);




inlize directdraw

void ddrawinlize()
{
HRESULT hr;

hRet = DirectDrawCreateEx(NULL, (void**)&g_pdd,IID_IDirectDraw7, NULL);

if (hRet != DD_OK)
MessageBox(hwnd,"DirectDrawCreateEX Failed","Error",0);


hRet = g_pdd->SetCooperativeLevel(hwnd, DDSCL_NORMAL);

if (hRet != DD_OK)
MessageBox(hwnd,"SetCooperativelevel failed","Error",0);

GetClientRect( hwnd, &g_rcViewport ) ;
GetClientRect( hwnd, &g_rcScreen ) ;
ClientToScreen(hwnd,(POINT*)&g_rcScreen.left);
ClientToScreen(hwnd,(POINT*)&g_rcScreen.right);
//////////////////////////////////////////////////////////////////////////
//create the primary surface
DDSURFACEDESC2 ddsd;

ZeroMemory(&ddsd,sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS;
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE ;

hRet = g_pdd->CreateSurface(&ddsd, &g_pddsprimary, NULL);
if (hRet != DD_OK)
MessageBox(hwnd,"CreateSurface failed","Error",0);

//-- Create the back buffer
ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS;
// Make our off-screen surface 320x240
ddsd.dwWidth = 800;
ddsd.dwHeight = 640;
// Create an offscreen surface
ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN |DDSCAPS_3DDEVICE;

hRet = g_pdd->CreateSurface(&ddsd, &g_pddsback, NULL);
if (hRet != DD_OK)
MessageBox(hwnd,"CreateSurface failed","Error",0);

// Create the clipper using the DirectDraw object
hRet = g_pdd->CreateClipper(0, &g_pClipper, NULL);
if (hRet != DD_OK)
MessageBox(hwnd,"CreateClipper failed","Error",0);

hRet = g_pClipper->SetHWnd(0, hwnd);
if (hRet != DD_OK)
MessageBox(hwnd,"CreateClipper failed","Error",0);


g_pddsprimary->SetClipper(g_pClipper);


g_pClipper->Release();
g_pClipper = NULL;

}





int Main_Loop()
{


FlipBlt();


}




blt

int FlipBlt()
{
// If we are in windowed mode, perform a blt.
g_pddsprimary->Blt( &g_rcScreen, g_pddsback,&g_rcViewport,DDBLT_WAIT, NULL );

}




Right now I am just drawing a blank screen

Share this post


Link to post
Share on other sites
You're running MainLoop for EACH windows message. That'll make it very slow, as each Windows operation is a ton of messages.

You should instead loop over the get/translate/dispatch loop, without doing your MainLoop() call, as long as there actually are messages -- change the if() to a while().

Share this post


Link to post
Share on other sites
Not only change the if to a while, also consider using PM_NOREMOVE and add GetMessage inside the while:


while ( PeekMessage( &msg, NULL, 0, 0, PM_NOREMOVE ) )
{
GetMessage( &msg, NULL, 0, 0 );
TranslateMessage( &msg );
DispatchMessage( &msg );
}
Main_Loop();

Share this post


Link to post
Share on other sites
for( ; ; )
{
//look for a message
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
//there is a message
//check that we arent quitting
if(msg.message==WM_QUIT) break;
//translate message
TranslateMessage(&msg);
//dispatch message
DispatchMessage(&msg);
}
else
//run main game loop
Main_Loop();
}

Try this
Luck!

Share this post


Link to post
Share on other sites
You also might want to add your own control over that message loop, like this:
bool meActive=true;
bool game_done=false;

void game_end() {
//Simply end the game
game_done=true;
}

//Window Process:
LRESULT CALLBACK WindowProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {
switch( Message ) {
case WM_ACTIVATEAPP:
meActive = wParam;
break;
case WM_SETCURSOR:
break;
case WM_CREATE:
break;
case WM_DESTROY:
game_end();
break;
}
return DefWindowProc(hwnd, Message, wParam, lParam);
}

//In the message loop:
while (!game_done) {
if (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)) {
if (!GetMessage(&msg, NULL, 0, 0)) return msg.wParam;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else if (meActive) {
//Update the game
GameUpdate();
}
else
{
//Sleep if there's nothing else to do
WaitMessage();
}
}

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!