Sign in to follow this  
tobi_fake_jr

sdl_delay, directx and opengl.

Recommended Posts

tobi_fake_jr    100
I've seen a lot of documentation in SDL tutorials and examples about SDL_Delay and its needs when working with the framework. It's a good practice to use it to avoid CPU hogging and give cpu a breath and run other proccess. It makes sense.

Is this something to worry about when programming in Directx and Opengl too? I don't remind seeing something like that when working with directx. Neither in the official Directx examples. Does directx/opengl handles this automagically? Does it need to do that?

Share this post


Link to post
Share on other sites
mhagain    13430
This is not good practice - SDL_Delay is nothing more than a Sleep call, and Sleep suffers from multiple issues when used in a game, not least the fact that the time to sleep for is only a guaranteed minimum and your program may actually sleep for any arbitrarily longer time. The only justifiable cause in which it would be used is to reduce power consumption for mobile devices; otherwise - think about it this way. You're running a high-performance real-time interactive simulation. [i]Of course[/i] you want it to be running as fast as possible. Using SDL_Delay in a game to reduce CPU usage is like putting a speed-limiter in a Ferrari to reduce fuel consumption.

Share this post


Link to post
Share on other sites
tobi_fake_jr    100
Well, I guess it is a good practice at least when programming in SDL, I've seen a lot of awesome guys using it. But I have to say that I never saw something like that in pure directx applications.

Share this post


Link to post
Share on other sites
SimonForsman    7642
It depends on the game and the situation really, you only want to sleep to preserve the battery on mobile devices or for enviromental reasons, if your game needs very little cpu time then it is good practice to sleep frequently, if it needs alot of cpu time then sleeping may have a very negative impact on the user experience and should be avoided unless the game is paused or minimized.

These days i would recommend that you put in a power saving mode(that the user can activate/deactivate as they see fit) that gives control of this to the user (Sleeping for 1ms at the end of each update is usually enough to greatly reduce the power consumption of a modern cpu but any time you sleep you will also allow other processes to hog the cpu for atleast a full timeslice (which can be far longer than the time you specified in the sleep call).

Share this post


Link to post
Share on other sites
rip-off    10976
Pure SDL programs are more likely to be running on a wide range of platforms, including mobile devices. In addition, SDL games tend to be simpler affairs, and tend not to benefit from pegging the CPU. Most OpenGL and DirectX applications use VSync, which is another form of sleeping.

That said, this is a design decision. You can provide or disable this functionality in any of these APIs. The key point is to understand the reason, and choose to include or omit it, or to make it a runtime decision based on some property of the device you are running on (e.g. using the new SDL_GetPowerInfo() call that appears in SDL 1.3) or as SimonForsman suggested making it a user option.

Share this post


Link to post
Share on other sites
mhagain    13430
Generally what I do is run at full tilt, but if the game goes idle (e.g. no user input for more than a certain period of time - although you would want to tune this depending on what your game does) then I start sleeping.

A modern OS should have a good enough scheduler that running at full speed would normally not be a problem for peaceful coexistence with other apps, so long as you don't do anything silly like assign high or realtime priority to the program. Using sleep for that purpose is really trying to second-guess the OS, which will likely only end in tears. (It is something you may have considered in the old co-operative multitasking days, however.)

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