#include "raylib.h"
#include "images.h"

Texture2D background_texture;
Texture2D car_textures[2];
Texture2D player_textures[6];
Texture2D number_textures[10];

RenderTexture2D renderTexture;

void load_images(void)
{

    renderTexture = LoadRenderTexture(1280, 720);
	Image image_buffer;


	Image sprite_sheet = LoadImageFromMemory(".png", (const unsigned char *)sprite_sheet_png, sprite_sheet_png_len); 
	ImageResizeNN(&sprite_sheet, 1280,760);
	Texture2D sprite_texture = LoadTextureFromImage(sprite_sheet);

	// -------------------------------------------------------------- Background
	image_buffer = ImageFromImage(sprite_sheet, (Rectangle){0,40,1280,720});
	background_texture = LoadTextureFromImage(image_buffer);

	// ------------------------------------------------------------ Car textures
	image_buffer = ImageFromImage(sprite_sheet, (Rectangle){340,0, 64,40});
	car_textures[0] = LoadTextureFromImage(image_buffer);
	ImageFlipHorizontal(&image_buffer);
	car_textures[1] = LoadTextureFromImage(image_buffer);

	// --------------------------------------------------------- Player textures
	for (int i = 0; i < 6; i++)
	{
		image_buffer = ImageFromImage(sprite_sheet, (Rectangle){0+(56*i), 0, 56,40});
		player_textures[i] = LoadTextureFromImage(image_buffer);	
	}

	// --------------------------------------------------------- Number textures
	for (int i = 0; i < 10; i++)
	{
		image_buffer = ImageFromImage(sprite_sheet, (Rectangle){800+(48*i), 0, 48,40});
		number_textures[i] = LoadTextureFromImage(image_buffer);	
	}

	UnloadImage(image_buffer);
	UnloadImage(sprite_sheet);
	UnloadTexture(sprite_texture);
}

void unload_images(void)
{
	UnloadTexture(car_textures[0]);
	UnloadTexture(car_textures[1]);

	for (int i = 0; i < 6; i++)
	{
		UnloadTexture(player_textures[i]);
	}

	for (int i = 0; i < 10; i++)
	{
		UnloadTexture(number_textures[i]);
	}	

	UnloadRenderTexture(renderTexture);
}
