/* INCLUDES */

//standard
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>

//deps handled by meson
#include "SDL.h"

//quick declaration for logic.c and render.c to function properly :p
SDL_Window *window; //create pointers
SDL_Renderer *renderer;
SDL_Surface *surface;
SDL_Event event;

#define VER 0

static const int bl_resolution[] = {800,800};
static const int bl_targetfps = 60;
static const int bl_framedelay = 1000 / bl_targetfps;
/* MAIN */
int init(void) {
	if (SDL_Init(SDL_INIT_VIDEO) < 0) {
		SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "could not initialize sdl: %s", SDL_GetError());
		return 1;
	}
	
	if (SDL_CreateWindowAndRenderer(bl_resolution[0], bl_resolution[1], 0, &window, &renderer)) {
		SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "could not create window and renderer: %s", SDL_GetError());
		return 1;
	}
	return 0;
}

static void cleanup(void) {
	SDL_DestroyRenderer(renderer);
	SDL_DestroyWindow(window);
	SDL_Quit();
}

static void bl_set2bg(void) {
	SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xff);
}

static void bl_set2fg(void) {
	SDL_SetRenderDrawColor(renderer, 0x00, 0x99, 0x00, 0xff);
}

static void bl_clear(void) {
	bl_set2bg();
	SDL_RenderClear(renderer);
}

static void bl_point(int x, int y) {
	int s = 20;
	SDL_Rect foo = { x - s/2 , y - s/2, s, s };
	bl_set2fg();
	SDL_RenderFillRect(renderer, &foo);
}

int bl_screen_x (float x) {
	return (x+1)/2*bl_resolution[0];
}
int bl_screen_y (float y) {
	return (1 - (y+1)/2 ) * bl_resolution[1];
}

float bl_project (float a, float z) {
	return a/z;
}

static void bl_3dpt (float x, float y, float z) {
	bl_point (
		bl_screen_x(bl_project(x,z)),
		bl_screen_y(bl_project(y,z))
	);
}

int main() {
	
	if (init()) {
		return 3;
	}
	
	SDL_SetWindowResizable(window, SDL_FALSE); 
	float x=0;
	int z;

	uint32_t bl_framestart;
	int bl_ftime;

	bool reverse=false;

	int cycles=0;

	while (1) {
		bl_framestart = SDL_GetTicks();

		SDL_PollEvent(&event);
		if (event.type == SDL_QUIT) {
			break;
		}
		
		bl_clear();
		
		//origin
		//bl_3dpt(0,0,z);

		//x
		for (z=1; z<5 ;z++) {

			bl_3dpt(x-1,	-1,	z);
			bl_3dpt(x-0.5,	-1,	z);
			bl_3dpt(x,	-1,	z);
			bl_3dpt(x+0.5,	-1,	z);
			bl_3dpt(x+1,	-1,	z);
			
			bl_3dpt(x-1,	-0.5,	z);
			bl_3dpt(x-0.5,	-0.5,	z);
			bl_3dpt(x,	-0.5,	z);
			bl_3dpt(x+0.5,	-0.5,	z);
			bl_3dpt(x+1,	-0.5,	z);
	
			bl_3dpt(x-1,	0,	z);
			bl_3dpt(x-0.5,	0,	z);
			bl_3dpt(x,	0,	z);
			bl_3dpt(x+0.5,	0,	z);
			bl_3dpt(x+1,	0,	z);
	
			bl_3dpt(x-1,	0.5,	z);
			bl_3dpt(x-0.5,	0.5,	z);
			bl_3dpt(x,	0.5,	z);
			bl_3dpt(x+0.5,	0.5,	z);
			bl_3dpt(x+1,	0.5,	z);
	
			bl_3dpt(x-1,	1,	z);
			bl_3dpt(x-0.5,	1,	z);
			bl_3dpt(x,	1,	z);
			bl_3dpt(x+0.5,	1,	z);
			bl_3dpt(x+1,	1,	z);
		}

		SDL_RenderPresent(renderer);

		cycles++;
		
		if (reverse) {
			if (x  1.5){}
			} else {
				x -= 0.1f;
				cycle = false;
			}

		} else {
			if (x >= 1.5) {
				reverse = !reverse;
				cycle = true;
			} else {
				x += 0.1f;
			}
		}
		
		bl_ftime = SDL_GetTicks() - bl_framestart;
		if (bl_ftime < bl_framedelay) {
			SDL_Delay(bl_framedelay - bl_ftime);
		}
	}
	
	cleanup();
	return 0;
}
