#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <stdio.h>

const int SCR_W = 320;
const int SCR_H = 200;
SDL_Window * sdlWindow;
SDL_Renderer * sdlRenderer;
SDL_Surface * sdlScreenSurface;
SDL_Event sdlEvent;

static char * sprsht[] = {
/* 8x8 spr on a 8x5 sheet */
	"80 8 4 1",
	". c #000000",
	"# c #ff0000",
	"| c #000000",
	"_ c #000000",
/*   <---------------------------------------------hexadecimal-------------------------------------------------------------->     */
/*   <                         ascii 48-57                                   >   |   <               ascii 97-102 & 65-70          */
 "..###..|...#...|..###..|..###..|.....#.|######.|...##..|#######|.#####.|.#####.|...#...|######.|.#####.|######.|#######|#######|",
 ".#...#.|..##...|.#...#.|.#...#.|....##.|#......|..#....|......#|#.....#|#.....#|..#.#..|#.....#|#.....#|#.....#|#......|#......|",
 "#.....#|.###...|#.....#|#.....#|...#.#.|#......|.#.....|......#|#.....#|#.....#|.#...#.|#.....#|#......|#.....#|#......|#......|",
 "#.....#|...#...|......#|....##.|..#..#.|#####..|#.####.|.....#.|#.....#|.....#.|#.... #|#.....#|#......|#.....#|#......|#......|",
 "#.....#|...#...|.....#.|......#|.#...#.|.....#.|##....#|....#..|#.....#|.....#.|#.... #|#.....#|#......|#.....#|#......|#......|",
 "#.....#|...#...|...#...|#.....#|#######|.....#.|#.....#|...#...|#.....#|....#..|#.....#|#.....#|#.....#|#.....#|#......|#......|",
 ".#...#.|...#...|.#.....|.#...#.|.....#.|#...#..|.#...#.|...#...|.#####.|...#...|#.....#|######.|.#####.|######.|#######|#......|",
 "..###..|.#####.|#######|..###..|.....#.|.###...|..###..|...#...|_______|_______|_______|_______|_______|_______|_______|_______|",
/*  ...____________________________________________________ascii 103-118 && 71-86___________________________________________...   */
 ".......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|",
 ".......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|",
 ".......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|",
 ".......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|",
 ".......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|",
 ".......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|",
 ".......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|",
 "_______|_______|_______|_______|_______|_______|_______|_______|.......|.......|.......|.......|.......|.......|.......|.......|",
/* ... ascii 119-122 & 87-90 >   |  */
 ".......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.#.#.#.|",
 ".......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|#.#.#.#|",
 ".......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.#.#.#.|",
 ".......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|#.#.#.#|",
 ".......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.#.#.#.|",
 ".......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|#.#.#.#|",
 ".......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.......|.#.#.#.|",
 "_______|_______|_______|_______|_______|_______|_______|_______|_______|_______|_______|_______|_______|_______|_______|_______|",


};
SDL_Surface * shtSurface;

void SDLBegin(){
	if (SDL_Init(SDL_INIT_VIDEO)<0){ printf("FUCK! ; SDL %s\n", SDL_GetError()); exit(0);}
	sdlWindow = SDL_CreateWindow("Sentinel Omega", 
				SDL_WINDOWPOS_UNDEFINED,
				SDL_WINDOWPOS_UNDEFINED,
				SCR_W,SCR_H,
				SDL_WINDOW_SHOWN);
	if(sdlWindow==NULL){printf("fuck %s\n",SDL_GetError());exit(0);}

	sdlScreenSurface = SDL_GetWindowSurface(sdlWindow);
	if(sdlScreenSurface==NULL){printf("fuck window %s\n",SDL_GetError());exit(0);}

	SDL_Surface * tmp;
	tmp = IMG_ReadXPMFromArray(sprsht);
	if (tmp == NULL){printf("FUCK XPM %s .\n",IMG_GetError());exit(0); }

	shtSurface = SDL_ConvertSurface(tmp,sdlScreenSurface->format,0);
	if (shtSurface == NULL){printf("FUCK convert image %s \n",SDL_GetError());exit(0);  }
}

void SDLEnd(){
	SDL_DestroyWindow(sdlWindow);
	SDL_Quit();
}

void Spr(char col, char row){
	SDL_Rect src = {0,0,8,8};
	SDL_Rect rect = { col*8, row*8, 8, 8  };
	SDL_BlitSurface(shtSurface,&src,sdlScreenSurface,&rect);
}

void Plt(char col, char row){
	SDL_Rect rect = { col, row, 8, 8  }; 
	SDL_FillRect(sdlScreenSurface,&rect,SDL_MapRGB(sdlScreenSurface->format, 255,0,0));
}

void Clr(){
	SDL_UpdateWindowSurface(sdlWindow);
	//SDL_FillRect(sdlScreenSurface,NULL, SDL_MapRGB(sdlScreenSurface->format, 0,0,0));
}

int Btn(){
	SDL_PollEvent(&sdlEvent);
	switch(sdlEvent.type){
		case SDL_QUIT:
			return 1;
			break;
		case SDL_KEYDOWN:
			return 1;
			break;
		case SDL_MOUSEBUTTONDOWN:
			return 1;
			break;
		default:
			break;
	}
	return 0;
}

void Tck(){
	SDL_Delay(300);
	//printf("happening\n");
}

int main(){
	SDLBegin();
	
	while(!Btn()){
		Clr();
		Plt(100,100);
		Spr(0,0);
		Tck();
	}

	SDLEnd();
}

