/*
** EPITECH PROJECT, 2018
** task01
** File description:
** I do task
*/
#include "lib/my_graphics.h"
#include "game/draw.h"
#include "game/core.h"

static const uint8_t wall_pik[] = {128, 192, 128, 0, 128, 192, 128, 0};
static const uint8_t player_run_a_r[] = {0, 126, 66, 86, 66, 66, 126, 40};
static const uint8_t player_run_b_r[] = {0, 126, 66, 86, 66, 66, 126, 34};
static const uint8_t player_run_c_r[] = {126, 66, 86, 66, 66, 127, 128, 0};
static const uint8_t player_run_d_r[] = {126, 66, 86, 66, 66, 254, 1, 0};
static const uint8_t player_idle_r[] = {0, 126, 66, 86, 66, 66, 126, 66};
static const uint8_t player_jump_r[] = {126, 66, 86, 66, 66, 255, 0, 0};
static const uint8_t player_run_a_l[] = {0, 126, 66, 106, 66, 66, 126, 20};
static const uint8_t player_run_b_l[] = {0, 126, 66, 106, 66, 66, 126, 68};
static const uint8_t player_run_c_l[] = {126, 66, 106, 66, 66, 254, 1, 0};
static const uint8_t player_run_d_l[] = {126, 66, 106, 66, 66, 127, 128, 0};
static const uint8_t player_idle_l[] = {0, 126, 66, 106, 66, 66, 126, 66};
static const uint8_t player_jump_l[] = {126, 66, 106, 66, 66, 255, 0, 0};
static const uint8_t explosion_a[] = {0, 44, 82, 74, 129, 110, 48, 0};
static const uint8_t explosion_b[] = {6, 105, 66, 136, 2, 33, 132, 99};
static const uint8_t explosion_c[] = {8, 65, 16, 132, 0, 0, 17, 130};
static const uint8_t* player[]={
	player_run_a_l, player_run_b_l, player_run_c_l, player_run_d_l,
	player_run_a_r, player_run_b_r, player_run_c_r, player_run_d_r,
	player_jump_l, player_jump_r,
	player_idle_l, player_idle_r,
	explosion_a, explosion_b, explosion_c
};

static void draw_line(sfml_t *sfml, const uint8_t *bmp, int draw[4], int y)
{
	int end;
	int x;

	x = (draw[0] < 0) ? -draw[0] - 1 : -1;
	end = (x + (8 << draw[2]) > sfml->width) ?
sfml->width - x : (8 << draw[2]);
	while (++x < end){
		if (bmp[y >> draw[2]] & 0x80 >> (x >> draw[2]))
			my_pixel(sfml, draw[0] + x, draw[1] + y, draw[3]);
	}
}

void draw_monochrome(sfml_t *sfml, const uint8_t *bmp, int draw[4])
{
	int end;
	int y;

	y = (draw[1] < 0) ? -draw[1] - 1 : -1;
	end = (y + (8 << draw[2]) > sfml->height) ?
sfml->height - y : (8 << draw[2]);
	while (++y < end)
		if (bmp[y >> draw[2]])
			draw_line(sfml, bmp, draw, y);
}

void player_rendering(sfml_t *sfml)
{
	int draw[4];
	int locate;

	locate = 0;
	if (sfml->scene->player->action & PLAYER_RUN)
		locate = sfml->scene->player->frame_counter / 5
		+ ((sfml->scene->player->action & PLAYER_LEFT) ? 4 : 0);
	if (sfml->scene->player->action & PLAYER_JUMP)
		locate = (sfml->scene->player->action & PLAYER_LEFT) ? 9 : 8;
	if (sfml->scene->player->action & PLAYER_IDLE)
		locate = (sfml->scene->player->action & PLAYER_LEFT) ? 11 : 10;
	if (sfml->scene->player->action & PLAYER_DEAD)
		locate = sfml->scene->player->dead_counter /
		PLAYER_DEAD_TIME + 12;
	draw[0] = sfml->scene->player->x - sfml->scene->camera->x;
	draw[1] = sfml->scene->player->y - sfml->scene->camera->y;
	draw[2] = 3;
	draw[3] = 0x000000ff;
	draw_monochrome(sfml, player[locate], draw);
}

void wall_rendering(sfml_t *sfml)
{
	int offset_y = sfml->scene->camera->y & 0x1f;
	int sprite_load_y;
	int rectangle[4];
	int draw[4];
	int i = -1;

	rectangle[0] = sfml->scene->wall->x - sfml->scene->camera->x;
	rectangle[1] = 0;
	rectangle[2] = sfml->scene->wall->x + WALL_SIZE -
	sfml->scene->camera->x;
	rectangle[3] = sfml->height;
	my_filled_rectangle(sfml, rectangle, 0x000000ff);
	sprite_load_y = (offset_y) ? BLOCK_LOAD_Y : BLOCK_LOAD_Y + 1;
	while (++i < sprite_load_y){
		draw[0] = sfml->scene->wall->x + WALL_SIZE -
		sfml->scene->camera->x;
		draw[1] = (i << 6) - offset_y;
		draw[2] = 3;
		draw[3] = 0x000000ff;
		draw_monochrome(sfml, wall_pik, draw);
	}
}
