Preventing SDL from using 100% CPU

Started by
3 comments, last by Spudder 21 years, 2 months ago
I''m making a game using SDL and noticed that when the game is running the CPU usage is around 95%, i''ve read through the SDL docs but havent found anything for this so i''m hoping someone heres knows of a solution or can point me in the direction of one. And before anyone says it i have Googled for an answer but nothin came up.
Advertisement
it''s not SDL most likely, if you''re running the game loop, that''s the reason why it''s using so much CPU power. I don''t know what stage you''re in in development but if it''s early on, your message pump and loop will eat all the processer. It goes down later when you start moving memory and pushing stuff to the vid card.
You could look into process/thread priorities. Perhaps changing the CPU priority of the game could be an option for the user. Be careful with ''realtime'' and ''idle'' though.
Write program as follows

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
while(1){}
return 0;
}

Program will take up 100%, or close enough to, CPU time. Replace the function body with

{
unsigned long SomeData[5000];
while(1){memset(SomeData,0,sizeof(SomeData);}
return 0;
}

Program will take up less processor time.
You could also look into use the multithreaded functionality of SDL. You could then put your input loop into thread that is only called when an event is generated instead of running constantly.

This topic is closed to new replies.

Advertisement