0
\$\begingroup\$

I've been working on a 2D game using SDL. I've noticed the objects look blurry and glitchy when they're moving, so I was thinking about using OpenGL to render the graphics. Would this help? What are the pros and cons of using OpenGL for a 2D game?

\$\endgroup\$

2 Answers 2

8
\$\begingroup\$

If you're having slowdowns and glitches with SDL software rendering in a 2D game, chances are very slim that OpenGL is going to do anything for you. SDL can run 2D games quickly on a Pentium II or even lower spec machine. In other words, there are problems inherent to how you are making your draw calls. I've had no problems with speed in SDL rendering, and I've written a pure scanline renderer, which is far more intensive than working with sprites.

Two approaches I suggest:

  • Pull out bits of your rendering code, methodically, until you see a speed increase. If you pull out one section and see a drastic speed increase, then pull out smaller pieces within that code section (where possible). If you pull out all of your rendering code (i.e. it's rendering nothing), and it's still slow, then you have problems with your game loop timing. In that case, see Fix Your Timestep.

  • Profile your code. If you are running under Linux, you can happily take advantage of the Valgrind suite of profiling and debugging tools. If you're under Windoze with MinGW or Cygwin, and you really want to profile, you can run your app with gdb and interrupt it, to manually take stack samples (leading you to see in which functions most of your time is being spent) -- see this for more info. This is because gprof, the profiler that comes with gcc/g++, is not very good... at least for real-time games it's next to useless.

\$\endgroup\$
6
  • \$\begingroup\$ In that case, it's probably an issue with my timing since I haven't done much work on the timing code aside from a simple framerate cap...I'm also working on an older laptop with a 1.6ghz processor and 512mb of RAM, so maybe it's just my computer. I haven't really done much research on the debugging tools available for Linux aside from GDB...So I'll definitely look into the Valgrind suite. \$\endgroup\$
    – Adam
    Jan 21, 2012 at 23:01
  • \$\begingroup\$ @Adam Don't worry about your system specs; again, I can guarantee you that won't impact the renderer performance - SDL was written more than a decade ago when many games still used software rendering. It has comparable performance to Allegro, which was written for the 16-bit Atari ST of the late 80's/early 90's. See this list of games written using SDL's renderer. Also, an upvote would be nice if you found this answer helpful. \$\endgroup\$
    – Engineer
    Jan 21, 2012 at 23:05
  • \$\begingroup\$ I did attempt to upvote, and it told me that I needed to be logged in...Which is odd considering that I'm pretty sure I'm logged in. \$\endgroup\$
    – Adam
    Jan 21, 2012 at 23:12
  • \$\begingroup\$ No problem...Thanks for the quick help and insight. \$\endgroup\$
    – Adam
    Jan 21, 2012 at 23:16
  • \$\begingroup\$ It's worth pointing out that the default SDL rendering is quite a lot slower than using OpenGL. Most of the games that use SDL use it as a wrapper for OpenGL. \$\endgroup\$
    – thedaian
    Jan 21, 2012 at 23:45
0
\$\begingroup\$

SDL uses OpenGL for its rendering, they just wrap there code in methods / free functions, so using "OpenGL" vs SDL will give you ultimately the same result, the only difference being that using OpenGL directly will give you more control over your rendering, sometimes this can be helpful as many older libraries use older OpenGL functions for rendering instead of newer standards e.g. ( a older library might use glBegin and glEnd instead of glDrawArrys or glDrawElements ), so in theory i would say yes, using OpenGL directly might improve performance, but in reality i highly doubt its the SDL rendering pipeline getting too much overhead and more possibly the way you are iterating over object in your scene, especially if you aren't optimizing your use of matrices depending on how many object you have in a scene at a time,

I hope this helps see, sorry this answer is so late (=

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .