Why is SDL so slow?

Started by
30 comments, last by Brien Shrimp 20 years, 10 months ago
Yikes. I wanted to try SDL (http://www.libsdl.org/). I was pointed to a set of tutorials at Cone3D: (http://cone3d.gamedev.net/cgi-bin/index.pl?page=tutorials/gfxsdl/index ). And so I wrote a simple frame for SDL projects in VC++ Dot Net. Wow. All it's doing is initalizing SDL and the SDL_mixer library but when I compile and run, it's taking 100% of my cpu usage and 8 friggen megs of RAM (have an AthlonXP 1800+). I've written complete (yet simple) games with GLUT that only take 8% of my cpu time and maybe 6 MB of RAM, max. And this is without optimization. I'm either doing something wrong or SDL is far too inefficent for me to waste my time on. I'm no good at optimization yet. [edited by - Brien Shrimp on June 4, 2003 7:10:32 PM]
Brien Smith-MartinezGarbage In, Games Out
Advertisement
It''s because it keeps redrawing probably. Did you enter any statements to wait?
most games use 100% of the CPU... and most game designers want their games to use 100% CPU, this way, when the game is ready doing one thing, it starts with the next right away! if you want it to only do this and that every frame, you need to tell it to do that.... you can make "a = 1 + 1" use 100% of the CPU if you want...
-Anders-Oredsson-Norway-
Okay, well, I''ve gotten it to be less of a resource hog by using the SDL_Delay() function. So now it pauses for about 10 ms before looping. This is probably an idiotic brute-force method, but I''m having trouble finding any resources to tell me otherwise.

I don''t want this game to run at 100%. This is going to be a 2D sprite-based game. There is no reason this should need more resources than my full 3D tank game I did with GLUT. I never needed any ''wait'' functions with GLUT. It ran render() to display and in ran render() when idle! It always did the same thing, never demanded more than 8% from my CPU.

Brien Smith-MartinezGarbage In, Games Out
I don''t think you know what CPU usage means. Something will use 100% of the CPU, regardless of what it is doing, if it never yields to the operating system. As uncutno said, a=1+1 willl use the entire CPU if you don''t give the OS some time to do it''s own thing. GLUT is doing this for you, internally. You just don''t realize it because it''s all behind an API.
Alright. So how can I get SDL to do the same thing that GLUT was doing?

Brien Smith-MartinezGarbage In, Games Out
you need to read up on how to use the SDL thread functionality.
I''m assuming you entered a loop that does nothing but check for an escape key press and/or a close event. If you are clearing the screen in the loop as well, it might even cause the window to appear unresponsive. This has nothing to do with SDL. Your loop is hogging the cpu and because it is using the full timeslice the OS gives it even though it doesn''t need to. That''s why your call to SDL_Delay helped. It would be even nicer to call SDL_Delay with an argument of 0ms (assuming SDL_Delay is the equivalent of Sleep). This basically yields the unused time remaining in the timeslice back to the OS. Once you have a complete game around the loop though, you''ll want to use as much of that timeslice as you can get of course

If you want to experiment, write up a quick DirectDraw windowed app (if you''re on windows), or even OpenGL (without GLUT if possible). Run the standard windows game loop (if peek message do the message stuff, else do the game loop) which does nothing but clear the screen. Try to interact with the window. You''ll see a delay everytime you try to move it or resize it. Even without the call to clear the screen you''ll see a contimuous 100% cpu usage like you are now.
while(!kbhit());


will take up 100% of your cpu.
-YoshiXGXCX ''99
I''ve got a full Tetris game built on SDL that isn''t using more than 8% CPU while the game is running.

It isn''t placing any calls to SDL_Wait() either - though SDL_PumpEvents() might do it.

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

This topic is closed to new replies.

Advertisement