Sign in to follow this  
AndreOsti

[SDL]Is SDL fast enough for a commercial game

Recommended Posts

Hi everyone, I've done a simple spaceship game for a university work, and I noticed the game was taking more than 50% of CPU, is there any way to optimize SDL to make it fast enough, and less CPU consumer?


Thanks!

Share this post


Link to post
Share on other sites
[url="http://en.wikipedia.org/wiki/List_of_games_using_SDL"]Of course it is fast enough for a commercial game[/url]. I'll bet $20 that the %50 CPU consumption you're seeing isn't SDL, but rather your own code. And if it is SDL, chances are you aren't using it efficiently. Though it's impossible to tell any of this (and hence how to help you) from only one sentence.

Share this post


Link to post
Share on other sites
One common mistake often made is to not use SDL_DisplayFormat (or SDL_DisplayFormatAlpha) to make sure that all surfaces have the correct format.

If only small portions of the screen is updated each frame you can use SDL_UpdateRects to specify the parts that needs to be updated instead of calling SDL_Flip. This is not suitable for all kind of games.

Using OpenGL can give you a big performance boost. SDL only helps with setting up the OpenGL context so you will have write most of the code yourself.

Use a profiler to see where the program spend most of it's time. Focus on optimizing these parts.

Share this post


Link to post
Share on other sites
Similarly to what Wooh said you want to check your own code. You want to make sure that you are formatting the images when loading them, not when drawing them. When drawing you would have to constantly reformat. Also I would look for potential leaks because SDL is most likely fast enough.

Share this post


Link to post
Share on other sites
Unless your game makes a blocking call, it will generally consume all available time on any CPUs it can access. I'm assuming the computer is dual core and your program is single threaded. In that case, your your SDL game will naturally saturate one core, if t can. If it did not, that means that you have a CPU that is capable of doing more processing, but something is preventing you (e.g. other processes, some OS limitation).

I would refer you to [url="http://www.gamedev.net/topic/607646-should-i-use-multithreading-in-my-game/page__view__findpost__p__4843976"]this post[/url] for my opinion and advice on this matter.

Share this post


Link to post
Share on other sites
Thanks for answering me.
Wooh: I use the functions from lazyfoo site, I'm almost sure it uses SDL_DisplayFormat, also they are formatted when I load them.
Rip-off: The actualization is limited to 30 fps, executing in a pentium dual core. I will try to use some profiler.
Bloodisblue: I exhibit more than 50 objects on the screen, but I just load 4 or 5 images. I'll post the code later.

Share this post


Link to post
Share on other sites
As rip-off had suggested I used a profiler, the gprof in my code, it seems my code spends lots of time manipulating stl vectors. I use vectors to hold the shots, enemies, and players. Is required to use vectors for holding multiple objects or there are any other ways to manipulate all game objects in an efficient way?

Share this post


Link to post
Share on other sites
[quote]
The actualization is limited to 30 fps
[/quote]
How is this achieved? Some ways of limiting frames per second are more processor intensive than others. The others often trade off timing reliability. For example, using a naive sleep() call can put your simulation under pressure to hit 30 FPS on devices operating near their capacity, even though the device would have enough capacity to process your simulation at 30 FPS if you weren't trying to limit the frame rate.

Share this post


Link to post
Share on other sites
[quote name='Lehrling' timestamp='1317849111' post='4869554']
As rip-off had suggested I used a profiler, the gprof in my code, it seems my code spends lots of time manipulating stl vectors. I use vectors to hold the shots, enemies, and players. Is required to use vectors for holding multiple objects or there are any other ways to manipulate all game objects in an efficient way?
[/quote]
I refer you back to my [url="http://www.gamedev.net/topic/607646-should-i-use-multithreading-in-my-game/page__view__findpost__p__4843976"]earlier link[/url]. You were originally concerned about [i][b]Problem #2:[/b] Using apparently excessive CPU[/i]. Your above post is trying to solve [i][b]Problem #1:[/b] You don't have enough CPU time to do what you need[/i]. Solving this won't help your original goal. For example, if you were to optimise this, your game loop will run more often, and you'll still be using 100% of a single core.

To actually solve Problem #2, you have essentially two choices. One is to use a blocking call somewhere in your game. Examples of blocking calls are vertically synchronised page flipping, another is using input routines like SDL_WaitEvent() instead of SDL_PollEvent(). If you are running an active simulation, you need to be careful with blocking input.

Another option is to call a "sleeping" function like SDL_Delay() when you know there are a good few milliseconds in which you have no work to do. SDL_Delay() asks the O.S. not to run your process for [u]at least[/u] the specified number of milliseconds. The "at least" is a bit of a problem, because if you ask to sleep for 1 millisecond, and you actually sleep for 15, this can result in your missing frames.

An inconsistent frame rate is more notable and annoying than a low one. Users will feel frame rate fluctuations.

Aiming for 30FPS, you have ~33.3 milliseconds to update and draw any given frame (note: if you are using a fixed timestep, you might render more frequently than you update). SDL_Delay(), on most platforms, has a granularity of about 10 milliseconds. This means that you can easily end up sleeping for the specified number of milliseconds, rounded up to 10. If you have less than the SDL delay granularity, you might decide not to sleep, but instead use a busy loop to limit the frame rate.

A more complex solution would be to set up a SDL_Timer to push SDL_USEREVENTs onto the event queue, with a frequency set by your FPS. Then, in main(), you would use SDL_WaitEvent(), and process the game update / render based on the arrival of these USEREVENTS.

[hr]

For completeness, if you did actually need to optimise this, the solution would probably involve a higher level data structure/algorithm, such as spatial partitioning.

Share this post


Link to post
Share on other sites
I tried to use SDL_WaitEvent but game style requires actualization don't matter player moves or not. To limit frames I used SDL_Delay. Game doesn't see to have any delay problems, as I calculate in every frame the required time to generate the 30 fps.

[quote name='rip-off' timestamp='1317851056' post='4869565']
[quote name='Lehrling' timestamp='1317849111' post='4869554']
As rip-off had suggested I used a profiler, the gprof in my code, it seems my code spends lots of time manipulating stl vectors. I use vectors to hold the shots, enemies, and players. Is required to use vectors for holding multiple objects or there are any other ways to manipulate all game objects in an efficient way?
[/quote]
I refer you back to my [url="http://www.gamedev.net/topic/607646-should-i-use-multithreading-in-my-game/page__view__findpost__p__4843976"]earlier link[/url]. You were originally concerned about [i][b]Problem #2:[/b] Using apparently excessive CPU[/i]. Your above post is trying to solve [i][b]Problem #1:[/b] You don't have enough CPU time to do what you need[/i]. Solving this won't help your original goal. For example, if you were to optimise this, your game loop will run more often, and you'll still be using 100% of a single core.

To actually solve Problem #2, you have essentially two choices. One is to use a blocking call somewhere in your game. Examples of blocking calls are vertically synchronised page flipping, another is using input routines like SDL_WaitEvent() instead of SDL_PollEvent(). If you are running an active simulation, you need to be careful with blocking input.

Another option is to call a "sleeping" function like SDL_Delay() when you know there are a good few milliseconds in which you have no work to do. SDL_Delay() asks the O.S. not to run your process for [u]at least[/u] the specified number of milliseconds. The "at least" is a bit of a problem, because if you ask to sleep for 1 millisecond, and you actually sleep for 15, this can result in your missing frames.

An inconsistent frame rate is more notable and annoying than a low one. Users will feel frame rate fluctuations.

Aiming for 30FPS, you have ~33.3 milliseconds to update and draw any given frame (note: if you are using a fixed timestep, you might render more frequently than you update). SDL_Delay(), on most platforms, has a granularity of about 10 milliseconds. This means that you can easily end up sleeping for the specified number of milliseconds, rounded up to 10. If you have less than the SDL delay granularity, you might decide not to sleep, but instead use a busy loop to limit the frame rate.

A more complex solution would be to set up a SDL_Timer to push SDL_USEREVENTs onto the event queue, with a frequency set by your FPS. Then, in main(), you would use SDL_WaitEvent(), and process the game update / render based on the arrival of these USEREVENTS.

[hr]

For completeness, if you did actually need to optimise this, the solution would probably involve a higher level data structure/algorithm, such as spatial partitioning.
[/quote]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this