Archive for: January, 2009

2D Graphics

I was searching for a nice 2D graphics library today, again, because I wanted to piece something together very quickly. My requirements were:

  • Should be available for the Mac so that I could code with my MacBook Pro on my lap. Currently I really prefer this way of coding. Screw dual monitors!
  • Crossplatform would be a plus so that I could continue coding on the PC if need-be.
  • Should have a somewhat modern C++ API, i.e. SDL was out of the question.
  • Should provide basic shape drawing functionality (e.g. circles, lines,…) and font support (TTF or at least bitmap fonts) innately. SDL disqualified again.
  • Either mature enough to be almost bug-free or still under active development.

I came up with something that was described as a “C++ version of SDL” somewhere. I must admit, this didn’t sound very promising to me at first, but when I digged a bit deeper, I became confident that this was what I was searching for.

http://www.sfml-dev.org/

SFML has everything I need plus lots of other cool features. And the best is, it even comes with pre-compiled OSX frameworks and XCode project templates to get you started directly.

The standard main.cpp looks somewhat like that. It’s pretty straightforward and you can jump right in and take care for the more important things:

#include <SFML/Graphics.hpp>

using namespace sf;

int main( int argc, char* argv[] ) {
    RenderWindow app( VideoMode( 640, 480 ), "SFML Graphics" );
    while( app.IsOpened() ) {
        Event event;
        while( app.GetEvent( event ) ) {
            if( event.Type == Event::Closed ) {
                app.Close();
            }
        }

        app.Clear();
        app.Draw( Shape::Circle( 200, 200, 100, Color::Yellow, 10, Color::Blue ) );
        app.Display();
    }

    return EXIT_SUCCESS;
}

The only thing I don’t like so far is the PascalCasing for method names, but hell, you can’t have everything, can you? So I’ll be using SFML for some debug graphics for another project I’m working on, DVW-related.