Skip to main content
gamedev.net
🔒 Locked

[SDL] Displaying multiple objects

Started by Hamstah Mar 26, 2009 at 10:50 AM 5 replies 3.9k views
Original Post
Hamstah
Hamstah
Hello again, GameDev community! I've been wondering, what would be the most efficient way to blit multiple objects (i.e. Bullets, Enemies, etc.)? So far I've been using for loops that loop through a vector of Bullets/Enemies and blits each one individually. Every now and then I'm experiencing flickering, which gets pretty annoying after a while. Is there a better way to draw objects on screen, or should I just give up on SDL and go with OpenGL? P.S.: I just noticed, that this question could be more suitable for the Graphics theory section. Maybe if any of the mods is feeling up to it, please move it there. Thanks. [Edited by - Hamstah on March 27, 2009 5:42:39 AM]
signal_
signal_
Quote:
So far I've been using for loops that loop through a vector of Bullets/Enemies and blits each one individually.

I think this is about the best way to do things. In my games, I just have each entity call its "render" method and its corresponding sprite is blitted to the screen.
Quote:
Every now and then I'm experiencing flickering, which gets pretty annoying after a while.

Well, SDL currently uses software rendering so things can get slow.

I think the only thing you can do is either exploit dirty rect frame updates or else you need to go to the hardware acceleration route.

I went from using SDL to using SDL with OpenGL and I am pleased with the results. Ultimately, you have to find out what works for you, but I think going with OpenGL may offer you more flexibility down the road.

The software 2D rendering of SDL inherently dislikes many blits per frame and, of course, redrawing the whole screen each frame (gasp!) is surely taboo with SDL alone. So if you need either many blits per frame or you redraw the whole scene each frame piecewise with the corresponding "puzzle-piece blits" you definitely should look into OpenGL.
Hamstah
Hamstah
Thank you for answering!

I though about delving into OpenGL later down the road, but it seems like I have no real option but to do it now.
TriggerB
TriggerB
The solution depends on you finding out why you're flickering. Is your flickering coming from the overworking of the system in drawing too many objects? Or is your flickering coming from your SDL_Flip call?

On modern systems, you should be able to draw a fairly substantial amount before you experience problems with SDL. I think the problem is more likely the timing of your call to SDL_Flip. Are you locking the frame rate? Take a look at your frames and see if they exceed your refresh, if they do, I would lock the rate and see if you still have any flicker.
Hamstah
Hamstah
I taught SDL was capable of doing more than drawing a few bullets onto the screen! I limited FPS to 60 FPS and voila, works awesome now.

Thanks TriggerB!
evillive2
evillive2
Quote:
Original post by Hamstah
I taught SDL was capable of doing more than drawing a few bullets onto the screen! I limited FPS to 60 FPS and voila, works awesome now.

Thanks TriggerB!

If you were trying to draw faster than the screen refresh rate you may see flicker or tearing (Check your video cards vsync setting).

Also, if you do not use double buffering and draw directly to the screen this will almost always look bad. With the SDL_DOUBLEBUF flag enabled the screen has 2 surfaces - one that is viewable and one you are drawing on. You then use SDL_Flip at the end of your rendering loop to swap which one is viewable. When you draw to the surface returned by SDL_GetVideoSurface you are drawing to the backbuffer (not visible surface).

Your mileage may vary but SDL on modern hardware can do quite a bit so a few bullets on the screen shouldn't slow it down at all unless there is alpha blending and/or rotation going on. If those are absolutely necessary then a 3d API like OpenGL or D3D would be more beneficial.
Evillive2
cobrausn
cobrausn
Don't know if this is relevant, but I thought I would post it.

You mentioned you render by iterating through a vector of sprites. If you are constantly removing and adding new sprites to this vector, you may consider the use of a list. It tends to work a bit faster when the entire list will be iterated and objects removed or added often. Vectors work much better when you need random access and insertions or deletions happen less often.

I would also second signal_ and say to use OpenGL. It is worth learning.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.