Building a simple 2D graphics library in SDL

jab630
Unregistered
 
Post: #1
I just started this post and accidentally submitted it before I was done... Sorry!

Anyway...

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:

Code:
CIRCLE(String ID, int x, int y, int radius, int R, int G, int B);
RECTANGLE(String ID, int x, int y, int width, int height, int R, int G, int B);
IMAGE(String ID, String filename, int x, int y, int width, int height);
HIDE(String ID);
SHOW(String ID);
MOVE(String ID, int x, int y);
RESIZE(String ID, int width, int height);
ROTATE(String ID, int degrees);
UPDATE();
QUIT();

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.

Thanks!

Josh
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
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...
Quote this message in a reply
jab630
Unregistered
 
Post: #3
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?
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #4
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.
Quote this message in a reply
Moderator
Posts: 508
Joined: 2002.09
Post: #5
OpenGL slow??? Something may be really wrong with your implementation. Using SDL won't make it any faster...

"When you dream, there are no rules..."
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #6
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.
Quote this message in a reply
jab630
Unregistered
 
Post: #7
Thanks to everyone for the replies.
OneSadCookie:
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?

Thanks,
Josh
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #8
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.
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #9
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.
Quote this message in a reply
Sage
Posts: 1,066
Joined: 2004.07
Post: #10
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:

http://lazyfooproductions.com/SDL_tutorials/index.php
http://cone3d.gamedev.net/
http://mavdisk.mnsu.edu/longaj/cornerstone.htm
Quote this message in a reply
Moderator
Posts: 3,572
Joined: 2003.06
Post: #11
> 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.
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #12
I was thinking of trying to draw shapes like circles with polygons. With simple quads, I agree it's a waste of time.
Quote this message in a reply
Moderator
Posts: 3,572
Joined: 2003.06
Post: #13
> 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.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #14
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.
Quote this message in a reply
jab630
Unregistered
 
Post: #15
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.

Thanks,
Josh
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Free, Portable Graphics loading library/Header Jones 5 3,044 Feb 7, 2006 12:15 PM
Last Post: Zekaric
  Building SDL Framework & miniaturize fix PowerMacX 3 3,676 Oct 22, 2004 06:31 PM
Last Post: PowerMacX
  SDL Simple Graphics? IBethune 1 3,174 Jun 29, 2003 04:51 AM
Last Post: OneSadCookie