hi,
here is a very simple program that does nearly nothing :
-
we initialize SDL and we use SDL_SetVideoMode(1024, 768, 0,
SDL_SWSURFACE) -
and we fall in this loop :
while (!end)
{
modifs = SDL_GetModState();
{
SDL_Event event;while (SDL_PollEvent(&event)) { if (event.type == SDL_QUIT) { end = 1; } }
}
}
Note that this loop does not update the screen.
- we exit when we receive the SDL_QUIT event.
Well, my problem is that this program (even it’s a big word) uses 100% of
my CPU (well, top says it).
So I had a look at aliens 1.0.2 and saw the WaitFrame() function :
#define FRAMES_PER_SEC 50
void WaitFrame(void)
{
static Uint32 next_tick = 0;
Uint32 this_tick;
/* Wait for the next frame */
this_tick = SDL_GetTicks();
if ( this_tick < next_tick ) {
SDL_Delay(next_tick-this_tick);
}
next_tick = this_tick + (1000/FRAMES_PER_SEC);
}
Now if I add a call to this function within the loop :
while(!end)
{
…
WaitFrame();
}
the CPU usage falls to 1% or 2%.
Can you explain me this behavior ? It would help me a lot.
Thanks in advance,
Cl?ment Bourdarias (phneutre).