// Leif by jonelf at gmail.com

#ifdef __cplusplus
    #include <cstdlib>
#else
    #include <stdlib.h>
#endif
#ifdef __APPLE__
#include <SDL/SDL.h>
#else
#include <SDL.h>
#endif

#include <time.h>

// 25 fps
#define TICK_INTERVAL 40

Uint32 TimeLeft(void)
{
    static Uint32 next_time = 0;
    Uint32 now;

    now = SDL_GetTicks();
    if ( next_time <= now ) {
        next_time = now+TICK_INTERVAL;
        return(0);
    }
    return(next_time-now);
}

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

    // load an image
    SDL_Surface* surface = SDL_LoadBMP("kafka.bmp");

    Uint32 width=surface->w;
    Uint32 height=surface->h;

    // create a new window
    SDL_Surface* screen = SDL_SetVideoMode(width, height, 32,
                                           SDL_HWSURFACE|SDL_DOUBLEBUF);

    if ( !screen )
    {
        printf("Unable to set 640x480 video: %s\n", SDL_GetError());
        return 1;
    }

    SDL_Surface* bmp = SDL_DisplayFormat(surface);
    SDL_FreeSurface(surface);

    if (!bmp)
    {
        printf("Unable to load bitmap: %s\n", SDL_GetError());
        return 1;
    }

    Uint16 pitch=(bmp->pitch);
    Uint16 pitch32 = pitch/4;
    Uint32 numberOfPixels=pitch32*height;

    SDL_WM_SetCaption("Leif","Leif");

    SDL_Rect dstrect;
    dstrect.x=0;
    dstrect.y=0;

    // Show picture for a while before messing it up
    SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
    SDL_BlitSurface(bmp, 0, screen, &dstrect);
    SDL_Flip(screen);
    SDL_Delay(2000);

    srand ( time(NULL) );

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

            case SDL_MOUSEBUTTONDOWN:
            {
                if(event.button.button == SDL_BUTTON_LEFT){
                    Uint32 *pixels = (Uint32 *)bmp->pixels;
                    Uint32 colors[] = {0xFFFF0000,0xFF00FF00,0xFF0000FF,0x00000000};
                    int cn;
                    for (Uint32 p=0; p<numberOfPixels; p++)
                    {
                        cn = rand() % 4;
                        pixels[p]=colors[cn];
                    }
                }
            }
                // 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

        for (int l=0; l<10; l++)
        {
            SDL_Delay(TimeLeft());

            SDL_LockSurface(bmp);

            Uint32 *pixels = (Uint32 *)bmp->pixels;

            Uint32 pos;
            Uint32 pixel;
            pos = ((Uint32)rand()+pitch32) % (numberOfPixels-pitch32-1);

            for(int i=0; i<32000; i++)
            {
                pos = (pos + (Uint32)rand()) % (numberOfPixels-pitch32-1) + pitch32;
                pixel = pixels[pos];

                pixels[pos-1]=pixel;
                pixels[pos+1]=pixel;
                pixels[pos-pitch32]=pixel;
                pixels[pos+pitch32]=pixel;
            }

            SDL_UnlockSurface(bmp);

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