Programming FAQ

This differs from the main SDL FAQ, which is about SDL itself and getting it working, as this is about the actual programming and problems you may come up with.

I made a game loop, but it uses 100% CPU

And so it should! As soon as it finishes one run of the loop it'll start again straight away, so it's bound to use all the CPU. You usually don't need the loop to go as fast as it possibly can, which is where the SDL_Delay function comes in. This function basically tells the Operating System that the program is going to go to sleep for a specified amount of milliseconds, so the OS knows it can use the time for other things, or nothing, depending on what you've got going on.

Check out the time-based loop in Time Examples. You'll notice it doesn't just delay the same time every time, it uses SDL_GetTicks to work out how much time has passed and then decides how much longer to sleep for. This is to try and maintain a constant rate, because the work your program does in the loop could change depending on what your program is doing.

I used SDL_INIT_EVENTTHREAD on Win32 and SDL crashes.

Well don't ask me why but initializing the event thread leads to SDL to crash on windows. The problem is that when you leave it away on any X11 system it may, depending on the version, not produce any events. The only real solution is to use conditional compiling. At least there is the WIN32 macro defined.

#ifdef WIN32

On the other hand you could hope that the system running your program has a newer SDL version, which works fine without the SDL_INIT_EVENTTHREAD flag, but can you rely on this?

SDL-1.3/Programming_FAQ (last edited 2009-09-09 20:05:16 by Scribe)