/* SDL */
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include "co.h"

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

/*
0 to 9 is numericly the same  (ascii [48-57] dec)
a to z is numbered from 10 to 36 (ascii [97-122] dec)
A to Z is the same number, all uppercase (ascii [65-90] dec)
and then there is (.) period (ascii 46)
@ ascii 64
/ ascii 47
\ ascii 92
? ascii 63
! ascii 33

space and newline are not mapped

but there is a special caracter, dither. its the last

first 16 number map to hexadecimal
*/
#define ANUMBER(c) ( c >= '0' && c <= '9' )
#define AUPPER(c) ( c >= 'A' && c <= 'Z' ) 
#define ALOWER(c) ( c >= 'a' && c <= 'z' )
#define A2SHTI(c) ( ANUMBER(c) ? c - '0' : AUPPER(c) ? c - 'A' : ALOWER(c) ? c - 'a' : 47 )
#define SHTI2X(i) ( ( i % 16 ) * 8 )
#define SHTI2Y(i) ( ( i / 16 ) * 8 )
static const char * sprsht[] = {
/* 8x8 spr on a 8x5 sheet */
	"80 8 4 1",
	". c #000000",
	"# c #ffffff",
	"| 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 TBegin(CO * ctx){
	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);

	SDL_Surface * tmp;
	tmp = IMG_ReadXPMFromArray(sprsht);
	if (tmp == NULL){
		printf("FUCK XPM $s\n",SDL_GetError());
	}
	shtSurface = SDL_ConvertSurface(tmp,sdlScreenSurface->format,NULL);
	if (shtSurface == NULL){
		printf("FUCK convert image %s \n",SDL_GetError());
	}
	//printf("image loaded w%d h%d \n",shtSurface->w,textXpm3Surface->h);
}

void TEnd(CO * ctx){
	SDL_DestroyWindow(sdlWindow);
	SDL_Quit();
}

void Prnt(char * c, char col, char row, enum COLOR color){
	if (*c==0) return;
	char cnum = A2SHTI(*c);
	char shtcol = SHTI2X(cnum);
	char shtrow = SHTI2Y(cnum);
	SDL_Rect src = {shtcol,shtrow,8,8};
	SDL_Rect rect = { col*8, row*8, 8, 8  };
	SDL_BlitSurface(shtSurface,&src,sdlScreenSurface,&rect);
	Prnt(c+sizeof(char),col+1,row,color);
}
void Plt(char col, char row, enum COLOR color){
	SDL_Rect rect = { col*8, row*8, 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(enum BTN btn){
	SDL_WaitEvent(&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(66);
}


void ABegin(){}
void AEnd(){}
void Bt(){
	
}
