How to keep the CPU usage down while running an SDL program?

Posted by budwiser on Stack Overflow See other posts from Stack Overflow or by budwiser
Published on 2012-10-07T15:24:26Z Indexed on 2012/10/07 15:37 UTC
Read the original article Hit count: 233

Filed under:
|

I've done a very basic window with SDL and want to keep it running until I press the X on window.

#include "SDL.h"
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;

int main(int argc, char **argv)
{
    SDL_Init( SDL_INIT_VIDEO );
    SDL_Surface* screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, 0, 
                                            SDL_HWSURFACE | SDL_DOUBLEBUF );
    SDL_WM_SetCaption( "SDL Test", 0 ); 
    SDL_Event event;
    bool quit = false;
    while (quit != false)
    {
        if (SDL_PollEvent(&event)) {
            if (event.type == SDL_QUIT) {
                quit = true;
            }
        }
        SDL_Delay(80);
    }
    SDL_Quit();
    return 0;
}

I tried adding SDL_Delay() at the end of the while-clause and it worked quite well.

However, 80 ms seemed to be the highest value I could use to keep the program running smoothly and even then the CPU usage is about 15-20%.

Is this the best way to do this and do I have to just live with the fact that it eats this much CPU already on this point?

© Stack Overflow or respective owner

Related posts about c++

Related posts about sdl