Slashdot Mirror


Graphics for Beginners (Using SDL)

Jari Komppa writes "Looking at how learning programming these days is much harder than when I was starting, I decided to write a tutorial on how to make graphics at pixel level. The aim for the tutorial is to show that programming can actually be fun."

51 comments

  1. SDL Rocks!! by trompete · · Score: 4, Interesting

    I used it for a game I wrote. It makes Threading, CDRoms, Joysticks, Keyboards, and graphics sooooo easy.

    If you're using SDL for games, you should also look into:
    NET2 - Fast Networking for SDLNet for multiplayer game programming. It basically has a separate thread that polls for Network traffic on your sockets and pushes events to your event queue. He even extends SDL events to be thread safe and FASTER.
    SDL rocks.

    1. Re:SDL Rocks!! by duncangough · · Score: 1

      Agreed - I've written simple arcade games quickly and I've fleshed out puzzle games in no time.

      PyGame is where the Python and SDL integration really got me interested though - I was concerned about performance but I never ran into a problem with that, PyGame is a fast as you'll need.

      Playaholics - Free online games, scoretables and a whole host player stats

  2. Hi Sol by Intruger · · Score: 0, Troll

    I bet this tutorial will be on the next Hugi. ;)

  3. Programming... harder?! by Anonymous Coward · · Score: 0

    Programming is easier than ever. Back in the day we had punch cards and assembly language.

    You're just lazy is all it is.

    1. Re:Programming... harder?! by Anonymous Coward · · Score: 2, Funny

      I once wrote a program to "draw" pictures on punchcards by punching out holes.
      I wrote it in FORTRAN, though, not assembly.
      A friend of mine enhanced it to do flip-card animation, but the animation was pretty bad, due to the fact that (IBM) punch-card holes are pretty spaced out.

  4. Also... by eviltypeguy · · Score: 4, Informative

    Another great tutorial is the book called:

    http://www.nostarch.com/frameset.php?startat=plg

    One of the few programming books I've purchased that I didn't feel like I got ripped off in purchasing.

    Written by an Ex-Loki associated person John R. Hall

    1. Re:Also... by Anonymous Coward · · Score: 0

      There's a free download of the book as well. I don't have the link handy. :|

    2. Re:Also... by Krunch · · Score: 4, Informative

      Here it is. However it seems down for now but you can still grab it from archive.org.

      --
      No GNU has been Hurd during the making of this comment.
    3. Re:Also... by duckie13 · · Score: 3, Informative

      If you're looking for the actual text, I found it here:

      http://www.tux.org/pub/tux/plg/

      --
      "My days are less enjoyable because of people." ~ Johnny the Homicidal Maniac
    4. Re:Also... by Anonymous Coward · · Score: 0

      Another great tutorial is the book called:

      http://www.nostarch.com/frameset.php?startat=plg


      Yes, I've been wanting to get http://www.nostarch.com/frameset.php?startat=plg for quite some time now. I even added http://www.nostarch.com/frameset.php?startat=plg to my christmas wish list.
  5. Back in my day... by Phatboy · · Score: 4, Funny
    We had to walk 500 miles uphill to get water than another 500 uphill back. We had to grind the flour with our hands, while it snowed. And it was proper snow then - so thick that you could hide cars in it most days, and that was in Summer. Now take programming, we had to write 500 lines of code to add two numbers together. And we liked it.

    And now you tell me programming was easier in my day? You, with your fancy SDL and OpenGL? I'd like to see you code once you've ploughed 300 fields in one day with your bare hands.

    1. Re:Back in my day... by Anonymous Coward · · Score: 0

      Now take programming, we had to write 500 lines of code to add two numbers together. And we liked it.

      Have you been using Visual Studio's "wizards" again?

  6. There are a couple of Ruby/SDL games... by tcopeland · · Score: 3, Informative

    ...on RubyForge, including Magic Maze (which was inspired by Gauntlet) and Tadpoles.

    1. Re:There are a couple of Ruby/SDL games... by kiatoa · · Score: 1

      Hey! Thanks for the pointers, my 11 yr old son is curious about programming games and these are an interesting example of an easy way to start.

      --
      90% of the wealth is in 2% of the pockets. Bummer to be in the majority.
    2. Re:There are a couple of Ruby/SDL games... by denthijs · · Score: 2
      WOW, there are????
      thats, like, SOO great

      I never would've thought anyone would actually use SDL...
      let alone from RUBY.....

      Don't tell me the perlmongers, pythons and/or javastics did it as well?
      WOW, they did????
      thats, like, SOO great

      I never wou.....
      Ad infinitum, ad nauseam

      </SARCASM>

  7. SDL == DirectX without the MS millstone by leonbrooks · · Score: 4, Informative
    OpenGL == Direct3D, SDL fills in the rest.

    It's an approximation, and there's some things which each set of software does markedly better than the other, but SDL+OpenGL is considerably easier to extend (e.g. the NET2 library mentioned above) so I'd expect to see it get better faster (cheaper :-).

    The big advantages are:
    • you're not tied to one platform, one manufacturer or a handful of languages; and
    • You can find out exactly what each function and parameter does; and
    • You can easily modify or extend anything (and no DMCA worries); and
    • You don't have to give away any rights before you start using it.

    These advantages are usually enough to handsomely compensate for any peripheral shortcomings (e.g. no force-feedback API, slightly smaller texturing envelope).

    --
    Got time? Spend some of it coding or testing
  8. Mod me down, eh? by Ayanami+Rei · · Score: 1

    I'm 100% serious. All this Copeland guy does is plug his rubyforge site or InfoEther.

    Do you actually do any work on Ruby or do you just spend your time trolling message boards and mailing lists?

    PS, I know where you live and work. Ever been to the theatre at Worldgate? I bet you have. You don't want some albino storming into your life and fucking up your shit.

    --
    THIS THING CAN TURN ON A DIME, MACROSSZERO STYLE ALSO FUCK BETA, ~NYORON
    1. Re:Mod me down, eh? by deadlinegrunt · · Score: 1

      " I'm 100% serious. All this Copeland guy does is plug his rubyforge site or InfoEther.

      Do you actually do any work on Ruby or do you just spend your time trolling message boards and mailing lists?"


      The sad part is that I know you are serious as well as telling the truth.

      I think the same thing virtually everytime I see him post.

      --
      BSD is designed. Linux is grown. C++ libs
    2. Re:Mod me down, eh? by tcopeland · · Score: 1

      > you actually do any work on Ruby

      Yup, I use it at work for various things, and I wrote a little SQL analysis thingy in it.

      > I know where you live and work

      Hm, yup, not hard to find...

      > Ever been to the theatre at Worldgate?

      Nope. Most of the time I work down in Arlington on a client site.

      > You don't want some albino storming
      > into your life and fucking up your shit.

      Hm. Why would you do that?

    3. Re:Mod me down, eh? by Spoing · · Score: 1
      1. I'm 100% serious. All this Copeland guy does is plug his rubyforge site or InfoEther.

        Do you actually do any work on Ruby or do you just spend your time trolling message boards and mailing lists?"

        The sad part is that I know you are serious as well as telling the truth.

        I think the same thing virtually everytime I see him post.

      I call BS. I checked his post history...and he mentions Ruby infrequently.

      --
      A firewall can not protect you from yourself. Turn off what you do not need. Do not use the firewall to do your work.
  9. Looks awesome! by breon.halling · · Score: 3, Interesting

    I've taken a quick glance at the tutorial, and it looks pretty good! As a Flash programmer, I've long been meaning to branch off into something more, um, sophisticated, and maybe this is the simple introduction that I've been waiting for!

    Many thanks, Jari Komppa!

    --
    "Yeah, well, Dracula called and he's coming over tonight for you and I said okay."
  10. SDL is overrated by Junks+Jerzey · · Score: 4, Insightful

    SDL has a couple of things going in its favor:

    1. It works.
    2. It's cross-platform.
    3. It's not trying to do too much.

    That said, SDL isn't particularly well-designed otherwise. It's awkward. There's too much underlying info exposed to the user. The "surface" abstraction is muddy, in that you can accidentally run without hardware acceleration too easily, and you do easily do other things that hurt performance simply because you didn't pay attention to barely documented flags.

    1. Re:SDL is overrated by ggambett · · Score: 2, Informative

      Well, what you say is true, but SDL is supposed to be a low level API. Things tend to get messy if you read the documentation, especially in a low level API.

      Other than that, I doubt anyone uses SDL directly. I wrote a high level framework on top of SDL, at a level of abstraction similar to Flash. It's quite good for doing that, precisely for being so low level and not forcing you to do things in a certain way. For the results, see my sig.

    2. Re:SDL is overrated by Junks+Jerzey · · Score: 1

      Well, what you say is true, but SDL is supposed to be a low level API.

      I wouldn't call it a low-level API, just that it has tight focus on what it's supposed to do. As such, it doesn't need to be messy; that's just poor design.

      For a dead-simple graphics API, check out TinyPTC, which is unfortunately Windows-only. But the design is absolutely beautiful.

    3. Re:SDL is overrated by voodoo1man · · Score: 2, Interesting
      For a dead-simple graphics API, check out TinyPTC, which is unfortunately Windows-only. But the design is absolutely beautiful.
      Are you referring to the GPU shader-accelerated version of TinyPTC? The webpage itself states that the current version uses software blitting (and runs on a bunch of platforms) with optional MMX optimizations, which would make TinyPTC as fast as worst-case blitting in SDL. If you want to do hardware accelerated blitting with masking/alpha, I really can't see a better interface than what SDL provides - if you care about performance, call SDL_GetVideoInfo and use whatever mode the video card has acceleration for, and if not just use whatever you want and SDL will handle it in software with MMX optimizations like TinyPTC does in all cases. Sure, there are a few functions in SDL that I never use, but I wouldn't exactly call that poor design. I really am wondering why you think that graphics in SDL are so bad. If you said that about sound, I'd be inclined to agree with you, but when it comes to graphics I think SDL is really well designed, especially considering the alternatives.
      --

      In the great CONS chain of life, you can either be the CAR or be in the CDR.

    4. Re:SDL is overrated by Anonymous Coward · · Score: 0

      SDL lacks proper documentation or tutorials on how to find suitable hardware accelerated video modes and still maximize compatibility.

      Or do you have any pointers to detailed info on that?

    5. Re:SDL is overrated by Junks+Jerzey · · Score: 1

      Are you referring to the GPU shader-accelerated version of TinyPTC?

      No. I can't find any information on real code for such a thing.

      The webpage itself states that the current version uses software blitting (and runs on a bunch of platforms) with optional MMX optimizations, which would make TinyPTC as fast as worst-case blitting in SDL.

      TinyPTC gives you a fixed format frame buffer and a call to convert and copy it to the screen. That's all. So if you want to do software blitting (which is what the original linked article was about), then TinyPTC is perfect--for Windows.

      SDL is fine if you want to be sloppy and don't care about frame buffer formats and let things get converted internally behind your back in undocumented ways. If you want to write something in a rock solid way to work across all video cards, then TinyPTC is a better choice.

    6. Re:SDL is overrated by ClippyHater · · Score: 1

      Before you hype SDL too much for hardware acceleration, you should check out this thread.

    7. Re:SDL is overrated by voodoo1man · · Score: 1

      All that thread points out is that you can't get direct access to the video card under X11 without DGA extensions, and then (obviously) only in full-screen mode. SDL isn't going to do magic for you - if whatever it's running on top of doesn't support direct access to the hardware, then SDL won't give you hardware acceleration.

      --

      In the great CONS chain of life, you can either be the CAR or be in the CDR.

    8. Re:SDL is overrated by Taladar · · Score: 1

      If you want to abstract the OS out of the code (like SDL) you have to "let things get converted internally behind your back in undocumented ways". Basically you describe abstraction as bad here which makes me wonder what your point was.

  11. Re:What the fuck? by Anonymous Coward · · Score: 0

    Maybe he should talk to a psychiatrist.
    I wonder if anyone has ported LIZA to Ruby?

  12. HTML, anyone? by magefile · · Score: 3, Interesting

    I had a friend who did 2-player "TANKS" in HTML, Javascript and CSS, simply by having tons of 1 pixel divs. Quite impressive ...

    1. Re:HTML, anyone? by magefile · · Score: 1

      This is modded funny, but I was serious. It's not that hard to do with a stylesheet, some basic HTML, and a bit of Javascript. And javascript is at least a better first language than BASIC!

    2. Re:HTML, anyone? by twistedcubic · · Score: 1


      And javascript is at least a better first language than BASIC!

      Oh, I see this is the funny part.

    3. Re:HTML, anyone? by Scorchio · · Score: 1

      Reminds me of Excel Pacman... another fine example of bashing nails with a screwdriver. Brilliant, nevertheless.

    4. Re:HTML, anyone? by Mr.+Slippery · · Score: 1
      another fine example of bashing nails with a screwdriver.

      Heh. Off-topic, but this phrase caught my eye, because just minutes ago I was pounding (well, more like tapping) nails with a pair of needlenose pliers. Why? Because it was a tight spot, a repair job of re-sinking nails that had been pulled loose, in a space where no hammer would fit.

      For initial construction, there's usually a specific right tool designed for the job; but things can break in so many different and interesting ways that often creative tool is required.

      Still, though, Pacman for Excel...that's just sick.

      --
      Tom Swiss | the infamous tms | my blog
      You cannot wash away blood with blood
  13. Re:What the fuck? by tcopeland · · Score: 1

    > Why [...] plug Ruby?

    Don't know... just seemed appropriate in this case...

  14. Sir, I think maybe you missed the joke by Anonymous Coward · · Score: 0

    Put "Ayanami Rei albino" in google.

  15. the 5k compo by Z-MaxX · · Score: 2, Informative
    Sounds like a common technique used in the 5k competition, a contest to make the coolest web page in 5 kilobytes.

    Check out some of the winners from the last 5k compo. Really cool stuff.

    --
    Dr Superlove 300ml. I use my powers for awesome
  16. SDL is far too low level by aj444 · · Score: 0, Flamebait

    why would you use a library that doesn't even have a decent putpixel() ? try using allegro.

  17. What sig? by Anonymous Coward · · Score: 0

    What sig?

  18. Wrong by Anonymous Coward · · Score: 0

    You don't have to give away any rights before you start using it.

    Wrong. It is under the LGPL, so you lose some rights. Like for example you have to make all the object files used to compile your code available. That is not really a big deal, but it is an inconvenience.

    There are some alternate libraries that do the same thing as SDL, like Allegro, that are under BSD or freer licenses.

  19. What ever happened to 2-D graphics? by Latent+Heat · · Score: 1
    I work with sound spectrogram displays ("voice prints") using the Windows 2-D graphics API. The calls that tie me down to Windows are IDirectDraw::WaitForVerticalBlank(), ScrollWindowEx(), and CreateDIBSection(). This API is far from simple, but these are the Windows-specific calls for which it is hard to find equivalents in other systems.

    CreateDIBSection() allows blitting a raster to the screen. I call a raster a memory array of pixels where you can control all of the pixel values. OK, CreateDIBSection() creates a bitmap object along with a corresponding raster memory allocation, you have to create a memory graphics context (Windows HDC) and load the bitmap object into that graphics context, and you have to blit from the memory graphics context into the display graphics context, but apart from the complicated dance step, you are essentially blitting a raster into the display -- something that TinyPTC does with a simple, clean interface.

    What Windows allows and TinyPTC along with many other systems does not support is blitting a subrectangle of the raster into the display. Why this is important to me is that I scroll the display with ScrollWindowEx() (which takes advantage of hardware acceleration, and I don't find that call outside of Windows), and I scroll my in-memory raster representation of the display by shifting a circular-buffer pointer, and once I scroll the raster in that way, I need to blit in piecemeal into the display in response to paint messages.

    Finally, IDirectDraw::WaitForVerticalBlank() allows doing the scrolls in step with the display refresh to get absolutely smooth scrolls without tear or flicker. Is there anything like this call under other "platforms"?

    That these three calls exist under Windows is an outgrowth of the WinG and later DirectDraw/DirectX initiatives. There was a vast supply of largely 2-D DOS games and His Billness decided they needed to move to Windows, and Windows got a lot of game-specific support for the 2-D games of that generation.

    It seems that the game world has moved on from 2-D games and on to 3-D games and the 2-D game API's are deprecated, even in the Windows world. If I understand the 3-D graphics world, this business of background scrolls with sprites on top has all gone out the window as a tool for getting a high-performance pseudo-depth game display. You are supposed to recreate each frame in its entirety with each frame refresh, although you describe the scene in terms of those shader triangles and get hardware assist to draw them and sort out the depth.

    Maybe I am living in the past and unnecessarily tied to the kinds of 2-D performance optimizations just described. Maybe I should switch from 8-bit palette-mapped rasters to 32-bit RGB rasters, memory usage be damned, I should scroll those rasters by copying the pixels in memory, and I should blit the entire raster wholesale using the TinyPTC-style API with each frame update. Its just that what I have is high performance across a broad range of hardware speeds and works quite well.

    1. Re:What ever happened to 2-D graphics? by mandrake*rpgdx · · Score: 1

      Allegro ( here ) does allow you to do this (blitting sub-rectangles, as well as hardware sub-rectangle blitting, I do it for my triple buffering system right now) and is cross-platform as well.

  20. SOL! by YakumoFuji · · Score: 1

    sol.. ltns.. mm i have been away from irc too long. i bet nothing has changed on #coders in 5+ years...

    --

    no sig for you
  21. audio delay on openbsd by Anonymous Coward · · Score: 0

    I noticed there's a slight delay (1 second or two) in the audio on SDL/OpenBSD. It's weird, and only SDL seeems to have this problem. For example, if I compile a straight X11 (non-SDL) version of Xmame, the sound is normal.
    This problem happens on every app I've compile with SDL. It's pretty disconcerting. Anybody know what's up, and if there's a fix or workaround?

  22. well as a matter of fact... ;) by Anonymous Coward · · Score: 0

    ... there is a Perl SDL game called Frozen Bubble. It's pretty fun, you should try it sometime. Admitedtly, it's not 100% Perl, there's also some C in some places too. Y'know for optimization, kinda like back in the day you'd optimize your C or Turbo Pascal code with some inline ASM. Well at least I used to do that, for my putpixel algorithm and other low-level gfx functions. ;)
    Anyway check it out: http://www.frozen-bubble.org/

  23. SDL is good, but not the holy grail by rasmusneckelmann · · Score: 1

    One problem IMHO, is that SDL tries to be kind-of-good at too many things, but isn't superbly good at anything. Of course it's good at being Open Source, cross-platform, easy-to-learn, yada yada, but fails in so many other areas. A general problem is that, in many cases, it abstracts the various hardware capabilities at a way too high level. For instance when it comes to the audio part: relying on the application programmer to do all mixing, etc, is probably nice seen from the "simplicity" point of view, but it hides 99% of the features of modern sound hardware entirely; don't you think the reason why people buys nifty Audigy-sound cards, is because they want to listen to cool environmental effects, 3D sounds, etc? The same could be said about the SDL_net project (I know it isn't officially (or is it?) part of SDL)... the lack of multicasting capabilities might not be a general problem, but I really don't like too much functionality to be hidden for me -- don't ask why ;) From my point of view, SDL is simply a nifty way of initializing OpenGL, managing household threading needs, and retreiving simple user input in a cross-platform way. What about CD-ROM audio? Sure, it's easy, but has anyone used it since the nineties? If you want to do 2D pixel graphics it's cool too, way easier than plain DirectDraw, and just as powerful. But again, almost no (serious) games today use such a thing as non-3D graphics (even for 2D). DirectX might be bloated, but at least you can access all the features of your hardware. I'd really like to see something similar but cross-platform - then we would probably also see some more serious games able of running on linux. Okay, enough ranting :) As the title of the thread somehow states, SDL is ideal for people who want an easy way to write some cool graphical stuff that runs on many platforms.

  24. Allegro by slapout · · Score: 1

    I just wanted to mention that there is also Allegro. It's a multi-platform gaming library:
    http://www.talula.demon.co.uk/allegro/
    also see http://www.allegro.cc/

    --
    Coder's Stone: The programming language quick ref for iPad