<gh_stars>0
#include <exception>
#include <string>
#include <iostream>
#include <SDL.h>
#include <SDL_image.h>

class InitError : public std::exception
{
    std::string msg;
public:
    InitError();
    InitError( const std::string & );
    virtual ~InitError() throw();
    virtual const char * what() const throw();
};

InitError::InitError() :
    exception(),
    msg( SDL_GetError() )
{
}

InitError::InitError( const std::string & m ) :
    exception(),
    msg( m )
{
}

InitError::~InitError() throw()
{
}

const char * InitError::what() const throw()
{
    return msg.c_str();
}

class SDL
{
    SDL_Window * m_window;
    SDL_Renderer * m_renderer;
public:
    SDL( Uint32 flags = 0 );
    virtual ~SDL();
    void draw();
};

SDL::SDL( Uint32 flags )
{
    if ( SDL_Init( flags ) != 0 )
        throw InitError();

    if ( SDL_CreateWindowAndRenderer( 640, 480, SDL_WINDOW_SHOWN,
                                      &m_window, &m_renderer ) != 0 )
        throw InitError();
}

SDL::~SDL()
{
    SDL_DestroyWindow( m_window );
    SDL_DestroyRenderer( m_renderer );
    SDL_Quit();
}

void SDL::draw()
{
    // Clear the window with a black background
    SDL_SetRenderDrawColor( m_renderer, 0, 0, 0, 255 );
    SDL_RenderClear( m_renderer );

    // Show the window
    SDL_RenderPresent( m_renderer );

    int rgb[] = { 203, 203, 203, // Gray
                  254, 254,  31, // Yellow
                    0, 255, 255, // Cyan
                    0, 254,  30, // Green
                  255,  16, 253, // Magenta
                  253,   3,   2, // Red
                   18,  14, 252, // Blue
                    0,   0,   0  // Black
                };

    SDL_Rect colorBar;
    colorBar.x = 0; colorBar.y = 0; colorBar.w = 90; colorBar.h = 480;

    // Render a new color bar every 0.5 seconds
    for ( int i = 0; i != sizeof rgb / sizeof *rgb; i += 3, colorBar.x += 90 )
    {
        SDL_SetRenderDrawColor( m_renderer, rgb[i], rgb[i + 1], rgb[i + 2], 255 );
        SDL_RenderFillRect( m_renderer, &colorBar );
        SDL_RenderPresent( m_renderer );
        SDL_Delay( 500 );
    }
}

int main( int argc, char * argv[] )
{
    try
    {
        /* test la fonctionnalite de la bibliotheque :

        SDL sdl( SDL_INIT_VIDEO | SDL_INIT_TIMER );
        sdl.draw();

        */

        SDL_Window *affichage = NULL;
        SDL_Renderer *renderer = NULL;
        SDL_Surface *image = NULL;

        if(SDL_Init(SDL_INIT_VIDEO) != 0)
        {
            SDL_Log("error : init SDL > %s\n", SDL_GetError());
            exit(EXIT_FAILURE);
        }

        // Affichage de l'image
        affichage = SDL_CreateWindow("MiniProjet_2", 0, 0, 287, 450, SDL_WINDOW_BORDERLESS);
        if(affichage == NULL)
        {
            SDL_Log("error : create window SDL > %s\n", SDL_GetError());
            exit(EXIT_FAILURE);
        }

        renderer = SDL_CreateRenderer(affichage, -1, SDL_RENDERER_SOFTWARE);
        if(renderer == NULL)
        {
            SDL_Log("error : create renderer SDL > %s\n", SDL_GetError());
            exit(EXIT_FAILURE);
        }

        image = IMG_Load("pinguin.jpg");
        if(image == NULL)
        {
            SDL_Log("error : create image SDL > %s\n", SDL_GetError());
            exit(EXIT_FAILURE);
        }

        SDL_Delay(5000);

        SDL_DestroyWindow(affichage);
        SDL_Quit();

        return 0;
    }
    catch ( const InitError & err )
    {
        std::cerr << "Error while initializing SDL:  "
                  << err.what()
                  << std::endl;
    }

    return 1;
}
