Simple Directmedia Layer – Start to build your own Graphics application using SDL with C/C++

Simple DirectMedia Layer is a cross-platform multimedia library designed to provide low-level access to audio, keyboard, mouse, joystick, 3D hardware via OpenGL, and 2D video framebuffer. It is used by MPEG playback software, emulators, and many popular games, including the award-winning Linux port of “Civilization: Call To Power.”
SDL supports Linux, Windows, Windows CE, BeOS, MacOS, Mac OS X, FreeBSD, NetBSD, OpenBSD, BSD/OS, Solaris, IRIX, and QNX. The code contains support for AmigaOS, Dreamcast, Atari, AIX, OSF/Tru64, RISC OS, SymbianOS, and OS/2, but these are not officially supported.
SDL is written in C, but works with C++ natively, and has bindings to several other languages, including Ada, C#, D, Eiffel, Erlang, Euphoria, Go, Guile, Haskell, Java, Lisp, Lua, ML, Objective C, Pascal, Perl, PHP, Pike, Pliant, Python, Ruby, Smalltalk, and Tcl.
SDL is distributed under GNU LGPL version 2. This license allows you to use SDL freely in commercial programs as long as you link with the dynamic library. – Source: http://libsdl.org
A simple example of SDL project and its output is given below
#ifdef __cplusplus

    #include <cstdlib>

#else

    #include <stdlib.h>

#endif

#ifdef __APPLE__

#include <SDL/SDL.h>

#else

#include <SDL.h>

#endif


int main ( int argc, char** argv )

{

    // initialize SDL video

    if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )

    {

        printf( "Unable to init SDL: %s\n", SDL_GetError() );

        return 1;

    }


    // make sure SDL cleans up before exit

    atexit(SDL_Quit);


    // create a new window

    SDL_Surface* screen = SDL_SetVideoMode(640, 480, 16,

                                           SDL_HWSURFACE|SDL_DOUBLEBUF);

    if ( !screen )

    {

        printf("Unable to set 640x480 video: %s\n", SDL_GetError());

        return 1;

    }


    // load an image

    SDL_Surface* bmp = SDL_LoadBMP("cb.bmp");

    if (!bmp)

    {

        printf("Unable to load bitmap: %s\n", SDL_GetError());

        return 1;

    }

    

    // centre the bitmap on screen

    SDL_Rect dstrect;

    dstrect.x = (screen->w - bmp->w) / 2;

    dstrect.y = (screen->h - bmp->h) / 2;


    // program main loop

    bool done = false;

    while (!done)

    {

        // message processing loop

        SDL_Event event;

        while (SDL_PollEvent(&event))

        {

            // check for messages

            switch (event.type)

            {

                // exit if the window is closed

            case SDL_QUIT:

                done = true;

                break;


                // check for keypresses

            case SDL_KEYDOWN:

                {

                    // exit if ESCAPE is pressed

                    if (event.key.keysym.sym == SDLK_ESCAPE)

                        done = true;

                    break;

                }

            } // end switch

        } // end of message processing


        // DRAWING STARTS HERE

        

        // clear screen

        SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));


        // draw bitmap

        SDL_BlitSurface(bmp, 0, screen, &dstrect);


        // DRAWING ENDS HERE


        // finally, update the screen :)

        SDL_Flip(screen);

    } // end main loop


    // free loaded bitmap

    SDL_FreeSurface(bmp);


    // all is well ;)

    printf("Exited cleanly\n");

    return 0;

}

The output is 

SDLOutput

SHARE Simple Directmedia Layer – Start to build your own Graphics application using SDL with C/C++

You may also like...

Leave a Reply

Your email address will not be published.

Share