/* -- Include the precompiled libraries -- */
#ifdef WIN32
#pragma comment(lib, "SDL.lib")
#pragma comment(lib, "SDLmain.lib")
#pragma comment(lib, "SDL_image.lib")
#endif

#include <stdlib.h>
#include "SDL.h"
#include "SDL_Image.h"

#define TRUE 1
#define FALSE 0


int main(int argc, char *argv[])
{
	SDL_Surface *screen;
	SDL_Surface *picture;
	SDL_Event event;
	SDL_Rect pictureLocation;
	int leftPressed, rightPressed, upPressed, downPressed;
	const SDL_VideoInfo* videoinfo;

	atexit(SDL_Quit);

	/* Initialize the SDL library */
	if( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
		fprintf(stderr,
			"Couldn't initialize SDL: %s\n", SDL_GetError());
		exit(1);
	}

	screen = SDL_SetVideoMode(640, 480, 32, SDL_DOUBLEBUF | SDL_HWSURFACE);
	if ( screen == NULL ) {
		fprintf(stderr, "Couldn't set 640x480x8 video mode: %s\n",
			SDL_GetError());
		exit(1);
	}

	videoinfo = SDL_GetVideoInfo();

	printf("%i", videoinfo->blit_hw);

	// Load Picture
	picture = IMG_Load("SDL_now.bmp");

	if (picture == NULL) {
		fprintf(stderr, "Couldn't load %s: %s\n", "SDL_now.bmp", SDL_GetError());
		return 0;
	}

	pictureLocation.x = 0;
	pictureLocation.y = 0;
	leftPressed = FALSE;
	rightPressed = FALSE;
	upPressed = FALSE;
	downPressed = FALSE;
	while(1) {
		if (leftPressed) {
			if (pictureLocation.x > 0)
				pictureLocation.x--;
		}
		if (rightPressed) {
			if (pictureLocation.x < 640-picture->w)
				pictureLocation.x++;
		}
		if (upPressed) {
			if (pictureLocation.y > 0) {
				pictureLocation.y--;
			}
		}
		if (downPressed) {
			if (pictureLocation.y < 480-picture->h) {
				pictureLocation.y++;
			}
		}
		SDL_FillRect(screen, NULL, 1000);
		SDL_BlitSurface(picture, NULL, screen, &pictureLocation);
		SDL_Flip(screen);

		if( SDL_PollEvent( &event ) ){
			/* We are only worried about SDL_KEYDOWN and SDL_KEYUP events */
			switch( event.type ){
	  case SDL_KEYDOWN:
		  switch(event.key.keysym.sym) {
	  case SDLK_LEFT:
		  leftPressed = TRUE;
		  break;
	  case SDLK_RIGHT:
		  rightPressed = TRUE;
		  break;
	  case SDLK_DOWN:
		  downPressed = TRUE;
		  break;
	  case SDLK_UP:
		  upPressed = TRUE;
		  break;
	  case SDLK_ESCAPE:
		  exit(0);
	  default:
		  break;
		  }
		  break;

	  case SDL_KEYUP:
		  switch(event.key.keysym.sym) {
	  case SDLK_LEFT:
		  leftPressed = FALSE;
		  break;
	  case SDLK_RIGHT:
		  rightPressed = FALSE;
		  break;
	  case SDLK_DOWN:
		  downPressed = FALSE;
		  break;
	  case SDLK_UP:
		  upPressed = FALSE;
		  break;
	  default:
		  break;
		  }
		  break;

	  default:
		  break;
			}
		}
	}

	return 0;
}