#ifdef __cplusplus
    #include <cstdlib>
#else
    #include <stdlib.h>
#endif
#include <iostream>
#include <SDL/SDL.h>
#include "Engine.h"

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
    screen = SDL_SetVideoMode(800, 600, 16,
                                           SDL_HWSURFACE|SDL_DOUBLEBUF);
    if ( !screen )
    {
        printf("Unable to set 640x480 video: %s\n", SDL_GetError());
        return 1;
    }

    SDL_WM_SetCaption("Sokoban - Use Arrows to move, and press (R) to restart level", NULL);


    g_Engine.Init();

    // 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;

                    g_Engine.updateKey(event.key.keysym.sym);

                    break;
                }
            } // end switch
        } // end of message processing

        // DRAWING STARTS HERE

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

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

       g_Engine.update();

        // DRAWING ENDS HERE

        // finally, update the screen :)
        SDL_Flip(screen);

    } // end main loop

    // free screen and loaded bitmap
    SDL_FreeSurface(screen);

    // all is well ;)
    printf("Exited cleanly\n");

    return 0;
}
