Building a simple 2D graphics library in SDL - Printable Version
+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Graphics & Audio Programming (/forum-9.html)
+--- Thread: Building a simple 2D graphics library in SDL (/thread-4323.html)
Building a simple 2D graphics library in SDL - jab630 - Apr 23, 2006 12:13 PM
I just started this post and accidentally submitted it before I was done... Sorry!
I need to build a very simple 2D graphics library. All it needs to do is respond to the following list of very simple commands:
Basically, the CIRCLE, RECTANGLE, and IMAGE commands create new graphical elements. The other commands (HIDE, SHOW, MOVE, RESIZE, ROTATE) are applied to existing graphical elements. Then after a sequence of the previous commands, UPDATE is called to (re)paint all currently-visible graphical elements.
I will be sending the above commands to my program in real time over a network from a client program. I have already written the code that listens for these commands over the network, all I need to do now is build the graphics library. SDL seems like a good choice for this. Unfortunately, I only have experience doing 3D graphics in OpenGL. Can anyone point me toward some good tutorials in SDL that will get me started? I just want to make sure I'm not going to be reinventing the wheel before I go at it.
Building a simple 2D graphics library in SDL - OneSadCookie - Apr 23, 2006 01:48 PM
If you know OpenGL, why wouldn't you use it? It's going to be a lot faster than whatever random "2D" API you choose...
Building a simple 2D graphics library in SDL - jab630 - Apr 23, 2006 01:57 PM
Actually, no. The reason I've been hired to rewrite this program is because the existing OpenGL version is way too slow. Not to mention it's the wrong tool for the job. And by the way, SDL is no "random 2D API" - you should really check out what it is before you bash it: http://libsdl.org.
Does anyone else have experience in SDL?
Building a simple 2D graphics library in SDL - OneSadCookie - Apr 23, 2006 02:16 PM
If the OpenGL version is too slow, it's either programmed wrong or running on a computer without hardware acceleration. There is no way you can beat dedicated graphics hardware for speed.
It's not the wrong tool for the job unless you have very high quality requirements, in which case SDL isn't the right tool for the job either. If you have high quality requirements, Quartz (Mac OS X only) or Cairo (cross-platform, and potentially OpenGL-accelerated) are better.
SDL is just as random a choice as most. It's also shockingly slow on PowerPC Macs, and doesn't get any acceleration even on Intel ones. Like I said: speed => OpenGL, quality => Quartz or Cairo.
Building a simple 2D graphics library in SDL - Taxxodium - Apr 23, 2006 02:23 PM
OpenGL slow??? Something may be really wrong with your implementation. Using SDL won't make it any faster...
Building a simple 2D graphics library in SDL - akb825 - Apr 23, 2006 02:24 PM
OpenGL is going to be a much better solution than anything else. It isn't the "wrong tool for the job" since even though it's used for 3D, you can opt to not enable the options normally used for 3D (such as depth testing, lighting, etc.) and use an orthographic projection to do 2D. If the current version uses OpenGL and is too slow, maybe you should look into optimizing the current OpenGL version. If used correctly, OpenGL should be faster than just about any other API out there. (at least for the Mac, on Windows you could use DirectX, of course, though that wouldn't really be any faster) If you ask for help here, I'm sure we could help you find the design flaws.
As for SDL, it is mainly used as a cross-platform method to get OpenGL contexts running, along with some sound and network support. I believe it does have some blitting abilities, but it is very slow. (I don't think it supports shapes, just pictures, but I could be wrong) It is really not meant to be a graphics API of any kind, it's used to make it easy to use OpenGL.
And maybe you should look up what OneSadCookie knows before you start bashing him. He is pretty much the main guru of these forums. He gives the final solution to nearly every problem that is asked about on these forums. I'm sure he knows perfectly well all the abilities and downfalls of SDL already.
Building a simple 2D graphics library in SDL - jab630 - Apr 23, 2006 02:30 PM
Thanks to everyone for the replies.
Sorry, you definitely know what you're talking about, I didn't mean to imply otherwise. I'm not sure why the OpenGL version is so slow, but I'm guessing it's because it was programmed wrong (I'm certain hardware acceleration is working). Speed is definitely much more important for this program than quality. But I went for SDL on a tip from a friend who said it was more appropriate if you're only doing 2D graphics. I guess he was wrong? Anyway, it sounds like I should just rewrite the OpenGL version from scratch. Unfortunately, my OpenGL experience is limited to only 3D stuff (my final project for a computer graphics class last semester was a 3D modeler). Can anyone point me toward how to do the simple 2D stuff I require in OpenGL?
Building a simple 2D graphics library in SDL - OneSadCookie - Apr 23, 2006 02:41 PM
All you need to do to get OpenGL to draw in 2 dimensions is use gluOrtho2D or glOrtho instead of gluPerspective or glFrustum to set up your projection. After that, you can just ignore the z coordinate.
Building a simple 2D graphics library in SDL - akb825 - Apr 23, 2006 02:57 PM
When you do the actual drawing, you will want to do all the practices you'd usually do in 3D, such as making display lists or vertex arrays. (though in this case I'm sure display lists are more appropriate) Since it's so slow, I wouldn't be surprised if the current program just uses glDrawPixels or something similar to draw it. If you're using sprites, the fastest way to draw them would be to have quads with the sprites texture mapped. For any specific questions, feel free to ask and we'll be happy to help.
Building a simple 2D graphics library in SDL - Nick - Apr 23, 2006 04:04 PM
Well here's a few links in case you really want to use SDL's graphics. I'm agreeing with everyone that using OpenGL correctly will be much faster and more powerful than SDL, but if you're set using SDL:
Building a simple 2D graphics library in SDL - AnotherJake - Apr 23, 2006 04:10 PM
> When you do the actual drawing, you will want to do all the practices you'd usually do in 3D, such as making display lists or vertex arrays. (though in this case I'm sure display lists are more appropriate)
Vertex arrays won't be of much help if you're just pushing four coordinates at a time to the card -- it might even be detrimental. I wouldn't bother with display lists either for most purposes except bitmapped fonts, where it is convenient. From my experience, no special techniques are needed for rendering 2D sprites on textured quads -- I've tested *hundreds* of sprites on-screen at a time, using collision detection on all of them, and on a G3 300 with a Radeon 7k in it, it was still over 20 fps @640x480 windowed.
Building a simple 2D graphics library in SDL - akb825 - Apr 23, 2006 04:17 PM
I was thinking of trying to draw shapes like circles with polygons. With simple quads, I agree it's a waste of time.
Building a simple 2D graphics library in SDL - AnotherJake - Apr 23, 2006 05:53 PM
> I was thinking of trying to draw shapes like circles with polygons.
For a circle you could also use gluDisk(). But indeed, I was not thinking in that direction at all. Textured quads are a natural fit for replacing classic 2D blitted sprite rendering, so my mind was stuck in that frame.
Building a simple 2D graphics library in SDL - OneSadCookie - Apr 23, 2006 06:31 PM
gluDisk uses immediate mode internally... the only way to make it at all fast is to compile it into a display list. Completely OT, of course -- if you were going to draw a circle with polygons, you'd do it yourself, and chances are that you'll do it with a texture map, anyway.
Building a simple 2D graphics library in SDL - jab630 - Apr 24, 2006 08:31 AM
Thank you all for the replies. I was wondering if there is an already-existing library that sits on top of OpenGL capable of doing the simple things I require. Like I said, I don't want to reinvent the wheel, and these seem like such basic things, though the setup to do them in OpenGL is annoying enough that a 2D OpenGL library ought to exist. Note that I don't need windows, buttons, labels, widgets, etc - all the drawing I need to do is fullscreen.