/**
 * A random procrastionation production, aka "buntvid"
 * gcc -o buntvid buntvid.c `sdl-config --libs --cflags`
 * thp <thpinfo.com/about> 2009-01-11
 **/

#include <stdlib.h>
#include <unistd.h>
#include <math.h>
#include "SDL.h"

#define WIDTH 640
#define HEIGHT 480

#define GET_PIXEL_DATA(surface,x,y) (*((Uint32*)(surface->pixels + x * surface->format->BytesPerPixel + y * surface->pitch)))

#define GET_PIXEL_RGB(surface,x,y,r,g,b) (SDL_GetRGB( GET_PIXEL_DATA(surface,x,y), surface->format, r, g, b))
#define SET_PIXEL_RGB(surface,x,y,r,g,b) (GET_PIXEL_DATA(surface,x,y)=SDL_MapRGB(surface->format, r, g, b))

int main()
{
    int i, x=0, y, notquit=1;
    int moodr, moodg, moodb;
    int moodrd=5, moodgd=-5, moodbd=5;
    SDL_Surface *surface;
    SDL_Event evt;
    SDL_Init(SDL_INIT_VIDEO);

    srand(time(NULL));

    SDL_ShowCursor(0);

    moodr = rand()%210;
    moodg = rand()%210;
    moodb = rand()%210;

    surface = SDL_SetVideoMode(WIDTH, HEIGHT, 0, SDL_FULLSCREEN | SDL_SWSURFACE);
    while (notquit) {
        if (SDL_PollEvent(&evt)) {
            switch (evt.type) {
                case SDL_KEYDOWN:
                case SDL_KEYUP:
                    notquit=0;
                    break;
            }
        }
        if (x%5==0) {
            moodr += moodrd;
            if (moodr == 0 || moodr == 210) {
                moodrd *= -1;
            }
        }
        if ((x+4)%5==0) {
            moodg += moodgd;
            if (moodg == 0 || moodg == 210) {
                moodgd *= -1;
            }
        }
        if ((x+2)%5==0) {
            moodb += moodbd;
            if (moodb == 0 || moodb == 210) {
                moodbd *= -1;
            }
        }
        x++;
        SDL_LockSurface(surface);
        i=(x%2);
        while((i+=2)<WIDTH) {
            y=0;
            while(++y<HEIGHT) {
                int a;
                if(moodbd>0) a = i*y*x;
                else if (moodgd<0) a = x*y;
                else if (moodrd<0) a = y*i;
                SET_PIXEL_RGB(surface, ((x%2==0)?(i):((WIDTH-i)%WIDTH)), y, moodr+((a*i)%50), moodg+((a*i-y)%50), moodb+((a*i-y)%50));
            }
        }
        SDL_UnlockSurface(surface);
        SDL_Flip(surface);
    }
    SDL_Quit();
}
