#include "director.h"
#include "common/utils.h"
#include <iostream>
#include <string>

// Constructor
Director::Director(std::string path)
{
	section.name = SECTION_PROG_LOGO;
	section.subsection = SUBSECTION_LOGO_TO_INTRO;

	section.name = SECTION_PROG_GAME;

	// Crea el objeto que controla los ficheros de recursos
	asset = new Asset(path.substr(0, path.find_last_of("\\/")));

	// Si falta algún fichero no inicia el programa
	if (!setFileList())
	{
		section.name = SECTION_PROG_QUIT;
	}

	// Inicializa variables desde el fichero de configuración
	loadConfig();

	// Inicializa SDL
	initSDL();

	// Inicializa JailAudio
	initJailAudio();

	// Crea los objetos
	resource = new Resource(renderer, asset, options);
	input = new Input(asset->get("gamecontrollerdb.txt"));
	initInput();
	screen = new Screen(window, renderer, options, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT);
	screen->setBorderColor(borderColor);
	screen->setVideoMode(options->fullScreenMode);
	debug = new Debug(renderer, screen, asset);
	music = JA_LoadMusic(asset->get("title.ogg").c_str());
}

Director::~Director()
{
	// Guarda las opciones de configuración
	saveConfig();

	// Libera la memoria
	delete options;
	delete asset;
	delete input;
	delete screen;
	delete debug;
	delete resource;
	JA_DeleteMusic(music);

	SDL_DestroyRenderer(renderer);
	SDL_DestroyWindow(window);
	SDL_Quit();
}

// Carga el fichero de configuración
bool Director::loadConfig()
{
	// Crea el puntero a la estructura de opciones e inicializa valores
	options = new options_t;
	options->fullScreenMode = 0;
	options->windowSize = 3;
	options->filter = FILTER_NEAREST;
	options->vSync = true;
	options->integerScale = true;
	options->keepAspect = true;
	options->borderEnabled = true;
	options->borderSize = 0.1f;
	options->palette = p_zxspectrum;

	// Indicador de éxito en la carga
	bool success = true;

	// Variables para manejar el fichero
	std::string line;
	std::ifstream file(asset->get("config.txt"));

	// Si el fichero se puede abrir
	if (file.good())
	{
		// Procesa el fichero linea a linea
		printf("Reading file config.txt\n");
		while (std::getline(file, line))
		{
			// Comprueba que la linea no sea un comentario
			if (line.substr(0, 1) != "#")
			{
				// Encuentra la posición del caracter '='
				int pos = line.find("=");
				// Procesa las dos subcadenas
				if (!setOptions(options, line.substr(0, pos), line.substr(pos + 1, line.length())))
				{
					printf("Warning: file %s\n, unknown parameter \"%s\"\n", "config.txt", line.substr(0, pos).c_str());
					success = false;
				}
			}
		}

		// Cierra el fichero
		printf("Closing file config.txt\n\n");
		file.close();
	}

	// El fichero no existe
	else
	{ // Crea el fichero con los valores por defecto
		saveConfig();
	}

	// Aplica opciones
	if (options->borderEnabled)
	{
		const int incWidth = GAMECANVAS_WIDTH * options->borderSize;
		const int incHeight = GAMECANVAS_HEIGHT * options->borderSize;
		options->screenWidth = (GAMECANVAS_WIDTH + incWidth) * options->windowSize;
		options->screenHeight = (GAMECANVAS_HEIGHT + incHeight) * options->windowSize;
	}
	else
	{
		options->screenWidth = GAMECANVAS_WIDTH * options->windowSize;
		options->screenHeight = GAMECANVAS_HEIGHT * options->windowSize;
	}

	return success;
}

// Guarda el fichero de configuración
bool Director::saveConfig()
{
	bool success = true;

	// Crea y abre el fichero de texto
	std::ofstream file(asset->get("config.txt"));

	// Escribe en el fichero
	if (options->fullScreenMode == 0)
	{
		file << "fullScreenMode=0\n";
	}

	else if (options->fullScreenMode == SDL_WINDOW_FULLSCREEN)
	{
		file << "fullScreenMode=SDL_WINDOW_FULLSCREEN\n";
	}

	else if (options->fullScreenMode == SDL_WINDOW_FULLSCREEN_DESKTOP)
	{
		file << "fullScreenMode=SDL_WINDOW_FULLSCREEN_DESKTOP\n";
	}

	file << "windowSize=" + std::to_string(options->windowSize) + "\n";

	if (options->filter == FILTER_NEAREST)
	{
		file << "filter=FILTER_NEAREST\n";
	}
	else
	{
		file << "filter=FILTER_LINEAL\n";
	}

	file << "vSync=" + boolToString(options->vSync) + "\n";
	file << "integerScale=" + boolToString(options->integerScale) + "\n";
	file << "keepAspect=" + boolToString(options->keepAspect) + "\n";
	file << "borderEnabled=" + boolToString(options->borderEnabled) + "\n";
	file << "borderSize=" + std::to_string(options->borderSize) + "\n";
	file << "palette=" + std::to_string(options->palette) + "\n";

	// Cierra el fichero
	file.close();

	return success;
}

// Carga los recursos
void Director::loadResources(section_t section)
{
	std::cout << "** LOAD RESOURCES" << std::endl;

	if (section.name == SECTION_PROG_LOGO)
	{
		std::vector<std::string> textureList;
		textureList.push_back("jailgames.png");
		textureList.push_back("since_1998.png");

		resource->loadTextures(textureList);
	}

	else if (section.name == SECTION_PROG_INTRO)
	{
		std::vector<std::string> textureList;
		textureList.push_back("loading_screen_bn.png");
		textureList.push_back("loading_screen_color.png");
		textureList.push_back("loading_screen_bn_zxarne.png");
		textureList.push_back("loading_screen_color_zxarne.png");

		resource->loadTextures(textureList);
	}

	else if (section.name == SECTION_PROG_TITLE)
	{
		std::vector<std::string> textureList;
		textureList.push_back("loading_screen_color.png");
		textureList.push_back("loading_screen_color_zxarne.png");
		textureList.push_back("smb2.png");

		resource->loadTextures(textureList);

		// Offsets
		std::vector<std::string> offsetsList;
		offsetsList.push_back("smb2.txt");

		resource->loadOffsets(offsetsList);
	}

	else if (section.name == SECTION_PROG_CREDITS)
	{
		// Texturas
		std::vector<std::string> textureList;
		textureList.push_back("shine.png");
		textureList.push_back("smb2.png");

		resource->loadTextures(textureList);

		// Animaciones
		std::vector<std::string> animationList;
		animationList.push_back("shine");

		resource->loadAnimations(animationList);

		// Offsets
		std::vector<std::string> offsetsList;
		offsetsList.push_back("smb2.txt");

		resource->loadOffsets(offsetsList);
	}

	else if (section.name == SECTION_PROG_GAME || section.name == SECTION_PROG_DEMO)
	{
		// Texturas
		std::vector<std::string> textureList;

		// Jugador
		textureList.push_back("player.png");

		// Tilesets
		textureList.push_back("standard.png");
		textureList.push_back("standard_zxarne.png");

		// Enemigos
		textureList.push_back("paco.png");
		textureList.push_back("chip.png");
		textureList.push_back("wave.png");
		textureList.push_back("wave_v.png");
		textureList.push_back("sigmasua.png");
		textureList.push_back("diskette.png");
		textureList.push_back("bird.png");
		textureList.push_back("bin.png");
		textureList.push_back("qvoid.png");
		textureList.push_back("batman.png");
		textureList.push_back("tuno.png");
		textureList.push_back("matatunos.png");
		textureList.push_back("abad.png");
		textureList.push_back("jailbattle_human.png");
		textureList.push_back("jailbattle_alien.png");
		textureList.push_back("jailer.png");
		textureList.push_back("jailer2.png");
		textureList.push_back("jailer3.png");
		textureList.push_back("printer.png");
		textureList.push_back("code.png");
		textureList.push_back("demon.png");
		textureList.push_back("dimallas.png");
		textureList.push_back("dimallas_v.png");
		textureList.push_back("heavy.png");
		textureList.push_back("spider.png");
		textureList.push_back("macaronni_ted.png");
		textureList.push_back("mummy.png");
		textureList.push_back("sam.png");
		textureList.push_back("amstrad_character_set.png");
		textureList.push_back("breakout.png");
		textureList.push_back("lamp.png");
		textureList.push_back("bry.png");
		textureList.push_back("tv.png");
		textureList.push_back("tv_panel.png");
		textureList.push_back("arounders_door.png");
		textureList.push_back("arounders_machine.png");
		textureList.push_back("arounder_walk.png");
		textureList.push_back("arounder_stop.png");
		textureList.push_back("arounder_fly.png");
		textureList.push_back("bat.png");

		// Items
		textureList.push_back("items.png");

		// Texto
		textureList.push_back("smb2.png");
		textureList.push_back("debug.png");

		resource->loadTextures(textureList);

		// Animaciones
		std::vector<std::string> animationList;

		// Jugador
		animationList.push_back("player");

		// Enemigos
		animationList.push_back("paco");
		animationList.push_back("chip");
		animationList.push_back("wave");
		animationList.push_back("wave_v");
		animationList.push_back("sigmasua");
		animationList.push_back("diskette");
		animationList.push_back("bird");
		animationList.push_back("bin");
		animationList.push_back("qvoid");
		animationList.push_back("batman");
		animationList.push_back("tuno");
		animationList.push_back("matatunos");
		animationList.push_back("abad");
		animationList.push_back("jailbattle_human");
		animationList.push_back("jailbattle_alien");
		animationList.push_back("jailer");
		animationList.push_back("jailer2");
		animationList.push_back("jailer3");
		animationList.push_back("printer");
		animationList.push_back("code");
		animationList.push_back("demon");
		animationList.push_back("dimallas");
		animationList.push_back("dimallas_v");
		animationList.push_back("heavy");
		animationList.push_back("spider");
		animationList.push_back("macaronni_ted");
		animationList.push_back("mummy");
		animationList.push_back("sam");
		animationList.push_back("amstrad_character_set");
		animationList.push_back("breakout");
		animationList.push_back("lamp");
		animationList.push_back("bry");
		animationList.push_back("tv");
		animationList.push_back("tv_panel");
		animationList.push_back("arounders_door");
		animationList.push_back("arounders_machine");
		animationList.push_back("arounder_walk");
		animationList.push_back("arounder_stop");
		animationList.push_back("arounder_fly");
		animationList.push_back("bat");

		resource->loadAnimations(animationList);

		// Offsets
		std::vector<std::string> offsetsList;
		offsetsList.push_back("smb2.txt");
		offsetsList.push_back("debug.txt");

		resource->loadOffsets(offsetsList);

		// TileMaps
		std::vector<std::string> tileMapList;
		tileMapList.push_back("01.tmx");
		tileMapList.push_back("02.tmx");
		tileMapList.push_back("03.tmx");
		tileMapList.push_back("04.tmx");
		tileMapList.push_back("05.tmx");
		tileMapList.push_back("06.tmx");
		tileMapList.push_back("07.tmx");
		tileMapList.push_back("08.tmx");
		tileMapList.push_back("09.tmx");
		tileMapList.push_back("10.tmx");
		tileMapList.push_back("11.tmx");
		tileMapList.push_back("12.tmx");
		tileMapList.push_back("13.tmx");
		tileMapList.push_back("14.tmx");
		tileMapList.push_back("15.tmx");
		tileMapList.push_back("16.tmx");
		tileMapList.push_back("17.tmx");
		tileMapList.push_back("18.tmx");
		tileMapList.push_back("19.tmx");
		tileMapList.push_back("20.tmx");
		tileMapList.push_back("21.tmx");
		tileMapList.push_back("22.tmx");
		tileMapList.push_back("23.tmx");
		tileMapList.push_back("24.tmx");
		tileMapList.push_back("25.tmx");
		tileMapList.push_back("26.tmx");
		tileMapList.push_back("27.tmx");
		tileMapList.push_back("28.tmx");
		tileMapList.push_back("29.tmx");
		tileMapList.push_back("30.tmx");
		tileMapList.push_back("31.tmx");
		tileMapList.push_back("32.tmx");
		tileMapList.push_back("33.tmx");
		tileMapList.push_back("34.tmx");
		tileMapList.push_back("35.tmx");
		tileMapList.push_back("36.tmx");
		tileMapList.push_back("37.tmx");
		tileMapList.push_back("38.tmx");
		tileMapList.push_back("39.tmx");
		tileMapList.push_back("40.tmx");
		tileMapList.push_back("41.tmx");
		tileMapList.push_back("42.tmx");
		tileMapList.push_back("43.tmx");
		tileMapList.push_back("44.tmx");
		tileMapList.push_back("45.tmx");
		tileMapList.push_back("46.tmx");
		tileMapList.push_back("47.tmx");
		tileMapList.push_back("48.tmx");
		tileMapList.push_back("49.tmx");
		tileMapList.push_back("50.tmx");
		tileMapList.push_back("51.tmx");
		tileMapList.push_back("52.tmx");
		tileMapList.push_back("53.tmx");
		tileMapList.push_back("54.tmx");
		tileMapList.push_back("55.tmx");
		tileMapList.push_back("56.tmx");
		tileMapList.push_back("57.tmx");
		tileMapList.push_back("58.tmx");
		tileMapList.push_back("59.tmx");
		tileMapList.push_back("60.tmx");

		resource->loadTileMaps(tileMapList);

		// Habitaciones
		std::vector<std::string> roomList;
		roomList.push_back("01.room");
		roomList.push_back("02.room");
		roomList.push_back("03.room");
		roomList.push_back("04.room");
		roomList.push_back("05.room");
		roomList.push_back("06.room");
		roomList.push_back("07.room");
		roomList.push_back("08.room");
		roomList.push_back("09.room");
		roomList.push_back("10.room");
		roomList.push_back("11.room");
		roomList.push_back("12.room");
		roomList.push_back("13.room");
		roomList.push_back("14.room");
		roomList.push_back("15.room");
		roomList.push_back("16.room");
		roomList.push_back("17.room");
		roomList.push_back("18.room");
		roomList.push_back("19.room");
		roomList.push_back("20.room");
		roomList.push_back("21.room");
		roomList.push_back("22.room");
		roomList.push_back("23.room");
		roomList.push_back("24.room");
		roomList.push_back("25.room");
		roomList.push_back("26.room");
		roomList.push_back("27.room");
		roomList.push_back("28.room");
		roomList.push_back("29.room");
		roomList.push_back("30.room");
		roomList.push_back("31.room");
		roomList.push_back("32.room");
		roomList.push_back("33.room");
		roomList.push_back("34.room");
		roomList.push_back("35.room");
		roomList.push_back("36.room");
		roomList.push_back("37.room");
		roomList.push_back("38.room");
		roomList.push_back("39.room");
		roomList.push_back("40.room");
		roomList.push_back("41.room");
		roomList.push_back("42.room");
		roomList.push_back("43.room");
		roomList.push_back("44.room");
		roomList.push_back("45.room");
		roomList.push_back("46.room");
		roomList.push_back("47.room");
		roomList.push_back("48.room");
		roomList.push_back("49.room");
		roomList.push_back("50.room");
		roomList.push_back("51.room");
		roomList.push_back("52.room");
		roomList.push_back("53.room");
		roomList.push_back("54.room");
		roomList.push_back("55.room");
		roomList.push_back("56.room");
		roomList.push_back("57.room");
		roomList.push_back("58.room");
		roomList.push_back("59.room");
		roomList.push_back("60.room");

		resource->loadRooms(roomList);
	}

	std::cout << "** RESOURCES LOADED" << std::endl;
}

// Asigna variables a partir de dos cadenas
bool Director::setOptions(options_t *options, std::string var, std::string value)
{
	// Indicador de éxito en la asignación
	bool success = true;

	if (var == "fullScreenMode")
	{
		if (value == "SDL_WINDOW_FULLSCREEN_DESKTOP")
		{
			options->fullScreenMode = SDL_WINDOW_FULLSCREEN_DESKTOP;
		}
		else if (value == "SDL_WINDOW_FULLSCREEN")
		{
			options->fullScreenMode = SDL_WINDOW_FULLSCREEN;
		}
		else
		{
			options->fullScreenMode = 0;
		}
	}

	else if (var == "windowSize")
	{
		options->windowSize = std::stoi(value);
		if ((options->windowSize < 1) || (options->windowSize > 4))
		{
			options->windowSize = 3;
		}
	}

	else if (var == "filter")
	{
		if (value == "FILTER_LINEAL")
		{
			options->filter = FILTER_LINEAL;
		}
		else
		{
			options->filter = FILTER_NEAREST;
		}
	}

	else if (var == "vSync")
	{
		options->vSync = stringToBool(value);
	}

	else if (var == "integerScale")
	{
		options->integerScale = stringToBool(value);
	}

	else if (var == "keepAspect")
	{
		options->keepAspect = stringToBool(value);
	}

	else if (var == "borderEnabled")
	{
		options->borderEnabled = stringToBool(value);
	}

	else if (var == "borderSize")
	{
		options->borderSize = std::stof(value);
		if (options->borderSize < 0.0f || options->borderSize > 0.5f)
		{
			options->borderSize = 0.1f;
		}
	}

	else if (var == "palette")
	{
		const int pal = std::stoi(value);

		if (pal == 0)
		{
			options->palette = p_zxspectrum;
		}

		else if (pal == 1)
		{
			options->palette = p_zxarne;
		}
	}

	else if (var == "")
	{
	}

	else
	{
		success = false;
	}

	return success;
}

// Inicia las variables necesarias para arrancar el programa
void Director::initInput()
{
	input->bindKey(INPUT_UP, SDL_SCANCODE_UP);
	input->bindKey(INPUT_DOWN, SDL_SCANCODE_DOWN);
	input->bindKey(INPUT_LEFT, SDL_SCANCODE_LEFT);
	input->bindKey(INPUT_RIGHT, SDL_SCANCODE_RIGHT);
	input->bindKey(INPUT_ACCEPT, SDL_SCANCODE_RETURN);
	input->bindKey(INPUT_CANCEL, SDL_SCANCODE_ESCAPE);
	input->bindKey(INPUT_BUTTON_1, SDL_SCANCODE_SPACE);
	input->bindKey(INPUT_BUTTON_2, SDL_SCANCODE_D);
	input->bindKey(INPUT_BUTTON_PAUSE, SDL_SCANCODE_ESCAPE);
	input->bindKey(INPUT_BUTTON_ESCAPE, SDL_SCANCODE_ESCAPE);

	input->bindGameControllerButton(INPUT_UP, SDL_CONTROLLER_BUTTON_DPAD_UP);
	input->bindGameControllerButton(INPUT_DOWN, SDL_CONTROLLER_BUTTON_DPAD_DOWN);
	input->bindGameControllerButton(INPUT_LEFT, SDL_CONTROLLER_BUTTON_DPAD_LEFT);
	input->bindGameControllerButton(INPUT_RIGHT, SDL_CONTROLLER_BUTTON_DPAD_RIGHT);
	input->bindGameControllerButton(INPUT_ACCEPT, SDL_CONTROLLER_BUTTON_B);
	input->bindGameControllerButton(INPUT_CANCEL, SDL_CONTROLLER_BUTTON_A);
	input->bindGameControllerButton(INPUT_BUTTON_1, SDL_CONTROLLER_BUTTON_B);
	input->bindGameControllerButton(INPUT_BUTTON_PAUSE, SDL_CONTROLLER_BUTTON_GUIDE);
	input->bindGameControllerButton(INPUT_BUTTON_ESCAPE, SDL_CONTROLLER_BUTTON_GUIDE);
}

// Inicializa JailAudio
void Director::initJailAudio()
{
	JA_Init(48000, AUDIO_S16, 2);
}

// Arranca SDL y crea la ventana
bool Director::initSDL()
{
	// Indicador de éxito
	bool success = true;

	// Inicializa SDL
	if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER | SDL_INIT_AUDIO) < 0)
	{
		printf("SDL could not initialize!\nSDL Error: %s\n", SDL_GetError());
		success = false;
	}
	else
	{
		// Inicia el generador de numeros aleatorios
		std::srand(static_cast<unsigned int>(SDL_GetTicks()));

		// Establece el filtro de la textura a nearest
		if (!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, std::to_string(options->filter).c_str()))
		{
			printf("Warning: Nearest texture filtering not enabled!\n");
		}

		// Crea la ventana
		window = SDL_CreateWindow(WINDOW_CAPTION, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, options->screenWidth, options->screenHeight, SDL_WINDOW_SHOWN | SDL_WINDOW_ALLOW_HIGHDPI);
		if (window == nullptr)
		{
			printf("Window could not be created!\nSDL Error: %s\n", SDL_GetError());
			success = false;
		}
		else
		{
			// Crea un renderizador para la ventana. El vsync se activa en funcion de las opciones
			if (options->vSync)
				renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
			else
				renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);

			if (renderer == nullptr)
			{
				printf("Renderer could not be created!\nSDL Error: %s\n", SDL_GetError());
				success = false;
			}
			else
			{
				// Inicializa el color de renderizado
				SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xFF);

				// Establece el tamaño del buffer de renderizado
				SDL_RenderSetLogicalSize(renderer, options->screenWidth, options->screenHeight);

				// Establece el modo de mezcla
				SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
			}
		}
	}

	printf("\n");
	return success;
}

// Crea el indice de ficheros
bool Director::setFileList()
{
	// Texto
	asset->add("/data/font/smb2.png", t_font);
	asset->add("/data/font/smb2.txt", t_font);
	asset->add("/data/font/debug.png", t_font);
	asset->add("/data/font/debug.txt", t_font);

	// Configuración
	asset->add("/data/input/gamecontrollerdb.txt", t_data);
	asset->add("/data/config/config.txt", t_data, false);

	// Habitaciones
	asset->add("/data/room/01.room", t_room);
	asset->add("/data/room/02.room", t_room);
	asset->add("/data/room/03.room", t_room);
	asset->add("/data/room/04.room", t_room);
	asset->add("/data/room/05.room", t_room);
	asset->add("/data/room/06.room", t_room);
	asset->add("/data/room/07.room", t_room);
	asset->add("/data/room/08.room", t_room);
	asset->add("/data/room/09.room", t_room);
	asset->add("/data/room/10.room", t_room);
	asset->add("/data/room/11.room", t_room);
	asset->add("/data/room/12.room", t_room);
	asset->add("/data/room/13.room", t_room);
	asset->add("/data/room/14.room", t_room);
	asset->add("/data/room/15.room", t_room);
	asset->add("/data/room/16.room", t_room);
	asset->add("/data/room/17.room", t_room);
	asset->add("/data/room/18.room", t_room);
	asset->add("/data/room/19.room", t_room);
	asset->add("/data/room/20.room", t_room);
	asset->add("/data/room/21.room", t_room);
	asset->add("/data/room/22.room", t_room);
	asset->add("/data/room/23.room", t_room);
	asset->add("/data/room/24.room", t_room);
	asset->add("/data/room/25.room", t_room);
	asset->add("/data/room/26.room", t_room);
	asset->add("/data/room/27.room", t_room);
	asset->add("/data/room/28.room", t_room);
	asset->add("/data/room/29.room", t_room);
	asset->add("/data/room/30.room", t_room);
	asset->add("/data/room/31.room", t_room);
	asset->add("/data/room/32.room", t_room);
	asset->add("/data/room/33.room", t_room);
	asset->add("/data/room/34.room", t_room);
	asset->add("/data/room/35.room", t_room);
	asset->add("/data/room/36.room", t_room);
	asset->add("/data/room/37.room", t_room);
	asset->add("/data/room/38.room", t_room);
	asset->add("/data/room/39.room", t_room);
	asset->add("/data/room/40.room", t_room);
	asset->add("/data/room/41.room", t_room);
	asset->add("/data/room/42.room", t_room);
	asset->add("/data/room/43.room", t_room);
	asset->add("/data/room/44.room", t_room);
	asset->add("/data/room/45.room", t_room);
	asset->add("/data/room/46.room", t_room);
	asset->add("/data/room/47.room", t_room);
	asset->add("/data/room/48.room", t_room);
	asset->add("/data/room/49.room", t_room);
	asset->add("/data/room/50.room", t_room);
	asset->add("/data/room/51.room", t_room);
	asset->add("/data/room/52.room", t_room);
	asset->add("/data/room/53.room", t_room);
	asset->add("/data/room/54.room", t_room);
	asset->add("/data/room/55.room", t_room);
	asset->add("/data/room/56.room", t_room);
	asset->add("/data/room/57.room", t_room);
	asset->add("/data/room/58.room", t_room);
	asset->add("/data/room/59.room", t_room);
	asset->add("/data/room/60.room", t_room);

	// Tilemaps
	asset->add("/data/room/01.tmx", t_room);
	asset->add("/data/room/02.tmx", t_room);
	asset->add("/data/room/03.tmx", t_room);
	asset->add("/data/room/04.tmx", t_room);
	asset->add("/data/room/05.tmx", t_room);
	asset->add("/data/room/06.tmx", t_room);
	asset->add("/data/room/07.tmx", t_room);
	asset->add("/data/room/08.tmx", t_room);
	asset->add("/data/room/09.tmx", t_room);
	asset->add("/data/room/10.tmx", t_room);
	asset->add("/data/room/11.tmx", t_room);
	asset->add("/data/room/12.tmx", t_room);
	asset->add("/data/room/13.tmx", t_room);
	asset->add("/data/room/14.tmx", t_room);
	asset->add("/data/room/15.tmx", t_room);
	asset->add("/data/room/16.tmx", t_room);
	asset->add("/data/room/17.tmx", t_room);
	asset->add("/data/room/18.tmx", t_room);
	asset->add("/data/room/19.tmx", t_room);
	asset->add("/data/room/20.tmx", t_room);
	asset->add("/data/room/21.tmx", t_room);
	asset->add("/data/room/22.tmx", t_room);
	asset->add("/data/room/23.tmx", t_room);
	asset->add("/data/room/24.tmx", t_room);
	asset->add("/data/room/25.tmx", t_room);
	asset->add("/data/room/26.tmx", t_room);
	asset->add("/data/room/27.tmx", t_room);
	asset->add("/data/room/28.tmx", t_room);
	asset->add("/data/room/29.tmx", t_room);
	asset->add("/data/room/30.tmx", t_room);
	asset->add("/data/room/31.tmx", t_room);
	asset->add("/data/room/32.tmx", t_room);
	asset->add("/data/room/33.tmx", t_room);
	asset->add("/data/room/34.tmx", t_room);
	asset->add("/data/room/35.tmx", t_room);
	asset->add("/data/room/36.tmx", t_room);
	asset->add("/data/room/37.tmx", t_room);
	asset->add("/data/room/38.tmx", t_room);
	asset->add("/data/room/39.tmx", t_room);
	asset->add("/data/room/40.tmx", t_room);
	asset->add("/data/room/41.tmx", t_room);
	asset->add("/data/room/42.tmx", t_room);
	asset->add("/data/room/43.tmx", t_room);
	asset->add("/data/room/44.tmx", t_room);
	asset->add("/data/room/45.tmx", t_room);
	asset->add("/data/room/46.tmx", t_room);
	asset->add("/data/room/47.tmx", t_room);
	asset->add("/data/room/48.tmx", t_room);
	asset->add("/data/room/49.tmx", t_room);
	asset->add("/data/room/50.tmx", t_room);
	asset->add("/data/room/51.tmx", t_room);
	asset->add("/data/room/52.tmx", t_room);
	asset->add("/data/room/53.tmx", t_room);
	asset->add("/data/room/54.tmx", t_room);
	asset->add("/data/room/55.tmx", t_room);
	asset->add("/data/room/56.tmx", t_room);
	asset->add("/data/room/57.tmx", t_room);
	asset->add("/data/room/58.tmx", t_room);
	asset->add("/data/room/59.tmx", t_room);
	asset->add("/data/room/60.tmx", t_room);

	// Tilesets
	asset->add("/data/tilesets/standard.png", t_bitmap);
	asset->add("/data/tilesets/standard_zxarne.png", t_bitmap);

	// Enemigos
	asset->add("/data/enemies/paco.png", t_bitmap);
	asset->add("/data/enemies/paco.ani", t_data);
	asset->add("/data/enemies/chip.png", t_bitmap);
	asset->add("/data/enemies/chip.ani", t_data);
	asset->add("/data/enemies/wave.png", t_bitmap);
	asset->add("/data/enemies/wave.ani", t_data);
	asset->add("/data/enemies/wave_v.png", t_bitmap);
	asset->add("/data/enemies/wave_v.ani", t_data);
	asset->add("/data/enemies/sigmasua.png", t_bitmap);
	asset->add("/data/enemies/sigmasua.ani", t_data);
	asset->add("/data/enemies/diskette.png", t_bitmap);
	asset->add("/data/enemies/diskette.ani", t_data);
	asset->add("/data/enemies/bird.png", t_bitmap);
	asset->add("/data/enemies/bird.ani", t_data);
	asset->add("/data/enemies/bin.png", t_bitmap);
	asset->add("/data/enemies/bin.ani", t_data);
	asset->add("/data/enemies/qvoid.png", t_bitmap);
	asset->add("/data/enemies/qvoid.ani", t_data);
	asset->add("/data/enemies/batman.png", t_bitmap);
	asset->add("/data/enemies/batman.ani", t_data);
	asset->add("/data/enemies/tuno.png", t_bitmap);
	asset->add("/data/enemies/tuno.ani", t_data);
	asset->add("/data/enemies/matatunos.png", t_bitmap);
	asset->add("/data/enemies/matatunos.ani", t_data);
	asset->add("/data/enemies/abad.png", t_bitmap);
	asset->add("/data/enemies/abad.ani", t_data);
	asset->add("/data/enemies/jailbattle_human.png", t_bitmap);
	asset->add("/data/enemies/jailbattle_human.ani", t_data);
	asset->add("/data/enemies/jailbattle_alien.png", t_bitmap);
	asset->add("/data/enemies/jailbattle_alien.ani", t_data);
	asset->add("/data/enemies/jailer.png", t_bitmap);
	asset->add("/data/enemies/jailer.ani", t_data);
	asset->add("/data/enemies/jailer2.png", t_bitmap);
	asset->add("/data/enemies/jailer2.ani", t_data);
	asset->add("/data/enemies/jailer3.png", t_bitmap);
	asset->add("/data/enemies/jailer3.ani", t_data);
	asset->add("/data/enemies/printer.png", t_bitmap);
	asset->add("/data/enemies/printer.ani", t_data);
	asset->add("/data/enemies/code.png", t_bitmap);
	asset->add("/data/enemies/code.ani", t_data);
	asset->add("/data/enemies/demon.png", t_bitmap);
	asset->add("/data/enemies/demon.ani", t_data);
	asset->add("/data/enemies/dimallas.png", t_bitmap);
	asset->add("/data/enemies/dimallas.ani", t_data);
	asset->add("/data/enemies/dimallas_v.png", t_bitmap);
	asset->add("/data/enemies/dimallas_v.ani", t_data);
	asset->add("/data/enemies/heavy.png", t_bitmap);
	asset->add("/data/enemies/heavy.ani", t_data);
	asset->add("/data/enemies/spider.png", t_bitmap);
	asset->add("/data/enemies/spider.ani", t_data);
	asset->add("/data/enemies/macaronni_ted.png", t_bitmap);
	asset->add("/data/enemies/macaronni_ted.ani", t_data);
	asset->add("/data/enemies/mummy.png", t_bitmap);
	asset->add("/data/enemies/mummy.ani", t_data);
	asset->add("/data/enemies/sam.png", t_bitmap);
	asset->add("/data/enemies/sam.ani", t_data);
	asset->add("/data/enemies/amstrad_character_set.png", t_bitmap);
	asset->add("/data/enemies/amstrad_character_set.ani", t_data);
	asset->add("/data/enemies/breakout.png", t_bitmap);
	asset->add("/data/enemies/breakout.ani", t_data);
	asset->add("/data/enemies/lamp.png", t_bitmap);
	asset->add("/data/enemies/lamp.ani", t_data);
	asset->add("/data/enemies/bry.png", t_bitmap);
	asset->add("/data/enemies/bry.ani", t_data);
	asset->add("/data/enemies/tv.png", t_bitmap);
	asset->add("/data/enemies/tv.ani", t_data);
	asset->add("/data/enemies/tv_panel.png", t_bitmap);
	asset->add("/data/enemies/tv_panel.ani", t_data);
	asset->add("/data/enemies/arounders_door.png", t_bitmap);
	asset->add("/data/enemies/arounders_door.ani", t_data);
	asset->add("/data/enemies/arounders_machine.png", t_bitmap);
	asset->add("/data/enemies/arounders_machine.ani", t_data);
	asset->add("/data/enemies/arounder_walk.png", t_bitmap);
	asset->add("/data/enemies/arounder_walk.ani", t_data);
	asset->add("/data/enemies/arounder_stop.png", t_bitmap);
	asset->add("/data/enemies/arounder_stop.ani", t_data);
	asset->add("/data/enemies/arounder_fly.png", t_bitmap);
	asset->add("/data/enemies/arounder_fly.ani", t_data);
	asset->add("/data/enemies/bat.png", t_bitmap);
	asset->add("/data/enemies/bat.ani", t_data);

	// Jugador
	asset->add("/data/player/player.png", t_bitmap);
	asset->add("/data/player/player.ani", t_data);

	// Items
	asset->add("/data/items/items.png", t_bitmap);

	// Musicas
	asset->add("/data/music/title.ogg", t_music);
	asset->add("/data/music/game.ogg", t_music);
	asset->add("/data/music/loading_sound1.ogg", t_music);
	asset->add("/data/music/loading_sound2.ogg", t_music);
	asset->add("/data/music/loading_sound3.ogg", t_music);

	// Efectos de sonido
	asset->add("/data/sound/item.wav", t_sound);
	asset->add("/data/sound/death.wav", t_sound);
	asset->add("/data/sound/jump1.wav", t_sound);
	asset->add("/data/sound/jump2.wav", t_sound);
	asset->add("/data/sound/jump3.wav", t_sound);
	asset->add("/data/sound/jump4.wav", t_sound);
	asset->add("/data/sound/jump5.wav", t_sound);
	asset->add("/data/sound/jump6.wav", t_sound);
	asset->add("/data/sound/jump7.wav", t_sound);
	asset->add("/data/sound/jump8.wav", t_sound);
	asset->add("/data/sound/jump9.wav", t_sound);
	asset->add("/data/sound/jump10.wav", t_sound);
	asset->add("/data/sound/jump11.wav", t_sound);
	asset->add("/data/sound/jump12.wav", t_sound);
	asset->add("/data/sound/jump13.wav", t_sound);
	asset->add("/data/sound/jump14.wav", t_sound);
	asset->add("/data/sound/jump15.wav", t_sound);
	asset->add("/data/sound/jump16.wav", t_sound);
	asset->add("/data/sound/jump17.wav", t_sound);
	asset->add("/data/sound/jump18.wav", t_sound);
	asset->add("/data/sound/jump19.wav", t_sound);
	asset->add("/data/sound/jump20.wav", t_sound);
	asset->add("/data/sound/jump21.wav", t_sound);
	asset->add("/data/sound/jump22.wav", t_sound);
	asset->add("/data/sound/jump23.wav", t_sound);
	asset->add("/data/sound/jump24.wav", t_sound);

	// Logo
	asset->add("/data/logo/jailgames.png", t_bitmap);
	asset->add("/data/logo/since_1998.png", t_bitmap);

	// Intro
	asset->add("/data/title/loading_screen_bn.png", t_bitmap);
	asset->add("/data/title/loading_screen_color.png", t_bitmap);
	asset->add("/data/title/loading_screen_bn_zxarne.png", t_bitmap);
	asset->add("/data/title/loading_screen_color_zxarne.png", t_bitmap);

	// Credits
	asset->add("/data/credits/shine.png", t_bitmap);
	asset->add("/data/credits/shine.ani", t_bitmap);

	return asset->check();
}

// Obtiene el valor de la variable
Uint8 Director::getSubsection()
{
	return section.subsection;
}

// Obtiene el valor de la variable
Uint8 Director::getSection()
{
	return section.name;
}

// Establece el valor de la variable
void Director::setSection(section_t section)
{
	this->section = section;
}

// Ejecuta la seccion de juego con el logo
void Director::runLogo()
{
	std::cout << std::endl
			  << "* SECTION: LOGO" << std::endl;
	loadResources(section);
	logo = new Logo(renderer, screen, resource, asset, options, section.subsection);
	setSection(logo->run());
	delete logo;
	resource->free();
}

// Ejecuta la seccion de juego de la introducción
void Director::runIntro()
{
	std::cout << std::endl
			  << "* SECTION: INTRO" << std::endl;
	loadResources(section);
	intro = new Intro(renderer, screen, resource, asset, options);
	setSection(intro->run());
	delete intro;
	resource->free();
}

// Ejecuta la seccion de juego con el titulo y los menus
void Director::runTitle()
{
	std::cout << std::endl
			  << "* SECTION: TITLE" << std::endl;
	if ((JA_GetMusicState() == JA_MUSIC_INVALID) || (JA_GetMusicState() == JA_MUSIC_STOPPED))
	{
		JA_PlayMusic(music);
	}
	loadResources(section);
	title = new Title(renderer, screen, resource, asset, options);
	setSection(title->run());
	delete title;
	resource->free();
}

// Ejecuta la seccion de los creditos del juego
void Director::runCredits()
{
	std::cout << std::endl
			  << "* SECTION: CREDITS" << std::endl;
	loadResources(section);
	credits = new Credits(renderer, screen, resource, asset, options);
	setSection(credits->run());
	delete credits;
	resource->free();
}

// Ejecuta la seccion de la demo, donde se ven pantallas del juego
void Director::runDemo()
{
	std::cout << std::endl
			  << "* SECTION: DEMO" << std::endl;
	loadResources(section);
	demo = new Demo(renderer, screen, resource, asset, options, debug);
	setSection(demo->run());
	delete demo;
	resource->free();
}

// Ejecuta la seccion de juego donde se juega
void Director::runGame()
{
	std::cout << std::endl
			  << "* SECTION: GAME" << std::endl;
	JA_StopMusic();
	loadResources(section);
	game = new Game(renderer, screen, resource, asset, options, input, debug);
	setSection(game->run());
	delete game;
	resource->free();
}

void Director::run()
{
	// Bucle principal
	while (getSection() != SECTION_PROG_QUIT)
	{
		switch (getSection())
		{
		case SECTION_PROG_LOGO:
			runLogo();
			break;

		case SECTION_PROG_INTRO:
			runIntro();
			break;

		case SECTION_PROG_TITLE:
			runTitle();
			break;

		case SECTION_PROG_CREDITS:
			runCredits();
			break;

		case SECTION_PROG_DEMO:
			runDemo();
			break;

		case SECTION_PROG_GAME:
			runGame();
			break;
		}
	}
}