forked from jaildesigner-jailgames/jaildoctors_dilemma
386 lines
11 KiB
C++
386 lines
11 KiB
C++
#include "const.h"
|
|
#include "utils.h"
|
|
#include "director.h"
|
|
#include <iostream>
|
|
#include <string>
|
|
|
|
// Constructor
|
|
Director::Director(std::string path)
|
|
{
|
|
// Inicializa la ruta
|
|
setExecutablePath(path);
|
|
|
|
// Crea el objeto que controla los ficheros de recursos
|
|
mAsset = new Asset(mExecutablePath);
|
|
|
|
// Establece la lista de ficheros
|
|
setFileList();
|
|
|
|
// Si falta algún fichero no inicia el programa
|
|
Uint8 section = SECTION_PROG_GAME;
|
|
if (!mAsset->check())
|
|
section = SECTION_PROG_QUIT;
|
|
|
|
// Inicializa el objeto de idioma
|
|
mLang = new Lang(mAsset);
|
|
|
|
// Crea el puntero a la estructura y carga el fichero de configuración
|
|
mOptions = new options_t;
|
|
if (!loadConfigFile())
|
|
{
|
|
mOptions->fullScreenMode = 0;
|
|
mOptions->windowSize = 3;
|
|
mOptions->language = en_UK;
|
|
mOptions->difficulty = DIFFICULTY_NORMAL;
|
|
mOptions->input[0].deviceType = INPUT_USE_KEYBOARD;
|
|
mOptions->input[1].deviceType = INPUT_USE_GAMECONTROLLER;
|
|
mOptions->filter = FILTER_NEAREST;
|
|
mOptions->vSync = true;
|
|
}
|
|
|
|
// Crea los objetos
|
|
mInput = new Input(mAsset->get("gamecontrollerdb.txt"));
|
|
|
|
// Inicializa SDL
|
|
initSDL();
|
|
|
|
// Inicializa JailAudio
|
|
initJailAudio();
|
|
|
|
// Aplica las opciones
|
|
SDL_SetWindowFullscreen(mWindow, mOptions->fullScreenMode);
|
|
SDL_SetWindowSize(mWindow, SCREEN_WIDTH * mOptions->windowSize, SCREEN_HEIGHT * mOptions->windowSize);
|
|
mLang->setLang(mOptions->language);
|
|
|
|
// Inicializa el resto de variables
|
|
init(section);
|
|
}
|
|
|
|
Director::~Director()
|
|
{
|
|
saveConfigFile();
|
|
|
|
delete mInput;
|
|
mInput = nullptr;
|
|
|
|
delete mLang;
|
|
mLang = nullptr;
|
|
|
|
delete mOptions;
|
|
mOptions = nullptr;
|
|
|
|
delete mAsset;
|
|
mAsset = nullptr;
|
|
|
|
SDL_DestroyRenderer(mRenderer);
|
|
SDL_DestroyWindow(mWindow);
|
|
mRenderer = nullptr;
|
|
mWindow = nullptr;
|
|
|
|
SDL_Quit();
|
|
}
|
|
|
|
// Inicia las variables necesarias para arrancar el programa
|
|
void Director::init(Uint8 name)
|
|
{
|
|
// Sección
|
|
mSection.name = name;
|
|
mSection.subsection = 0;
|
|
|
|
// Textos
|
|
mLang->setLang(mOptions->language);
|
|
|
|
// Controles
|
|
mInput->bindKey(INPUT_UP, SDL_SCANCODE_UP);
|
|
mInput->bindKey(INPUT_DOWN, SDL_SCANCODE_DOWN);
|
|
mInput->bindKey(INPUT_LEFT, SDL_SCANCODE_LEFT);
|
|
mInput->bindKey(INPUT_RIGHT, SDL_SCANCODE_RIGHT);
|
|
mInput->bindKey(INPUT_ACCEPT, SDL_SCANCODE_RETURN);
|
|
mInput->bindKey(INPUT_CANCEL, SDL_SCANCODE_ESCAPE);
|
|
mInput->bindKey(INPUT_BUTTON_1, SDL_SCANCODE_SPACE);
|
|
mInput->bindKey(INPUT_BUTTON_2, SDL_SCANCODE_D);
|
|
mInput->bindKey(INPUT_BUTTON_PAUSE, SDL_SCANCODE_ESCAPE);
|
|
mInput->bindKey(INPUT_BUTTON_ESCAPE, SDL_SCANCODE_ESCAPE);
|
|
|
|
mInput->bindGameControllerButton(INPUT_UP, SDL_CONTROLLER_BUTTON_DPAD_UP);
|
|
mInput->bindGameControllerButton(INPUT_DOWN, SDL_CONTROLLER_BUTTON_DPAD_DOWN);
|
|
mInput->bindGameControllerButton(INPUT_LEFT, SDL_CONTROLLER_BUTTON_DPAD_LEFT);
|
|
mInput->bindGameControllerButton(INPUT_RIGHT, SDL_CONTROLLER_BUTTON_DPAD_RIGHT);
|
|
mInput->bindGameControllerButton(INPUT_ACCEPT, SDL_CONTROLLER_BUTTON_B);
|
|
mInput->bindGameControllerButton(INPUT_CANCEL, SDL_CONTROLLER_BUTTON_A);
|
|
mInput->bindGameControllerButton(INPUT_BUTTON_1, SDL_CONTROLLER_BUTTON_B);
|
|
mInput->bindGameControllerButton(INPUT_BUTTON_PAUSE, SDL_CONTROLLER_BUTTON_GUIDE);
|
|
mInput->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)
|
|
//if (SDL_Init(SDL_INIT_EVERYTHING) < 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(mOptions->filter).c_str()))
|
|
{
|
|
printf("Warning: Nearest texture filtering not enabled!\n");
|
|
}
|
|
|
|
// Crea la ventana
|
|
mWindow = SDL_CreateWindow(WINDOW_CAPTION, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, VIEW_WIDTH, VIEW_HEIGHT, SDL_WINDOW_SHOWN | SDL_WINDOW_ALLOW_HIGHDPI);
|
|
if (mWindow == NULL)
|
|
{
|
|
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 (mOptions->vSync)
|
|
mRenderer = SDL_CreateRenderer(mWindow, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
|
|
else
|
|
mRenderer = SDL_CreateRenderer(mWindow, -1, SDL_RENDERER_ACCELERATED);
|
|
|
|
if (mRenderer == NULL)
|
|
{
|
|
printf("Renderer could not be created!\nSDL Error: %s\n", SDL_GetError());
|
|
success = false;
|
|
}
|
|
else
|
|
{
|
|
// Inicializa el color de renderizado
|
|
SDL_SetRenderDrawColor(mRenderer, 0x00, 0x00, 0x00, 0xFF);
|
|
|
|
// Establece el tamaño del buffer de renderizado
|
|
SDL_RenderSetLogicalSize(mRenderer, SCREEN_WIDTH, SCREEN_HEIGHT);
|
|
|
|
// Establece el modo de mezcla
|
|
SDL_SetRenderDrawBlendMode(mRenderer, SDL_BLENDMODE_BLEND);
|
|
}
|
|
}
|
|
}
|
|
|
|
printf("\n");
|
|
return success;
|
|
}
|
|
|
|
// Crea el indice de ficheros
|
|
void Director::setFileList()
|
|
{
|
|
mAsset->add("/media/font/8bithud.png", font);
|
|
mAsset->add("/media/font/8bithud.txt", font);
|
|
mAsset->add("/media/font/nokia.png", font);
|
|
mAsset->add("/media/font/nokia.txt", font);
|
|
mAsset->add("/media/font/nokia2.png", font);
|
|
mAsset->add("/media/font/nokia2.txt", font);
|
|
mAsset->add("/media/font/smb2.png", font);
|
|
mAsset->add("/media/font/smb2.txt", font);
|
|
mAsset->add("/media/lang/es_ES.txt", lang);
|
|
mAsset->add("/media/lang/en_UK.txt", lang);
|
|
mAsset->add("/media/lang/ba_BA.txt", lang);
|
|
mAsset->add("/data/gamecontrollerdb.txt", data);
|
|
mAsset->add("/data/config.bin", data, false);
|
|
mAsset->add("/data/room/01.room", room);
|
|
mAsset->add("/data/room/02.room", room);
|
|
mAsset->add("/data/room/room1.tmx", room);
|
|
mAsset->add("/data/room/room2.tmx", room);
|
|
mAsset->add("/media/tilesets/room1.png", bitmap);
|
|
mAsset->add("/media/enemies/enemy01.png", bitmap);
|
|
mAsset->add("/media/player/player01.png", bitmap);
|
|
mAsset->add("/media/items/items.png", bitmap);
|
|
}
|
|
|
|
// Carga el fichero de configuración
|
|
bool Director::loadConfigFile()
|
|
{
|
|
// Pone unos valores por defecto
|
|
mOptions->fullScreenMode = 0;
|
|
mOptions->windowSize = 3;
|
|
mOptions->language = en_UK;
|
|
mOptions->difficulty = DIFFICULTY_NORMAL;
|
|
mOptions->input[0].deviceType = INPUT_USE_KEYBOARD;
|
|
mOptions->input[1].deviceType = INPUT_USE_GAMECONTROLLER;
|
|
mOptions->filter = FILTER_NEAREST;
|
|
mOptions->vSync = true;
|
|
|
|
// Indicador de éxito en la carga
|
|
bool success = true;
|
|
|
|
const std::string p = mAsset->get("config.bin");
|
|
std::string filename = p.substr(p.find_last_of("\\/") + 1);
|
|
SDL_RWops *file = SDL_RWFromFile(p.c_str(), "r+b");
|
|
|
|
// El fichero no existe
|
|
if (file == NULL)
|
|
{
|
|
printf("Warning: Unable to open %s file\n", filename.c_str());
|
|
|
|
// Crea el fichero para escritura
|
|
file = SDL_RWFromFile(p.c_str(), "w+b");
|
|
if (file != NULL)
|
|
{
|
|
printf("New file (%s) created!\n", filename.c_str());
|
|
|
|
// Escribe los datos
|
|
SDL_RWwrite(file, &mOptions->fullScreenMode, sizeof(mOptions->fullScreenMode), 1);
|
|
SDL_RWwrite(file, &mOptions->windowSize, sizeof(mOptions->windowSize), 1);
|
|
SDL_RWwrite(file, &mOptions->language, sizeof(mOptions->language), 1);
|
|
SDL_RWwrite(file, &mOptions->difficulty, sizeof(mOptions->difficulty), 1);
|
|
SDL_RWwrite(file, &mOptions->input[0].deviceType, sizeof(mOptions->input[0].deviceType), 1);
|
|
SDL_RWwrite(file, &mOptions->input[1].deviceType, sizeof(mOptions->input[1].deviceType), 1);
|
|
SDL_RWwrite(file, &mOptions->filter, sizeof(mOptions->filter), 1);
|
|
SDL_RWwrite(file, &mOptions->vSync, sizeof(mOptions->vSync), 1);
|
|
|
|
// Cierra el fichero
|
|
SDL_RWclose(file);
|
|
}
|
|
else
|
|
{
|
|
printf("Error: Unable to create file %s\n", filename.c_str());
|
|
success = false;
|
|
}
|
|
}
|
|
// El fichero existe
|
|
else
|
|
{
|
|
// Carga los datos
|
|
printf("Reading file %s\n", filename.c_str());
|
|
SDL_RWread(file, &mOptions->fullScreenMode, sizeof(mOptions->fullScreenMode), 1);
|
|
SDL_RWread(file, &mOptions->windowSize, sizeof(mOptions->windowSize), 1);
|
|
SDL_RWread(file, &mOptions->language, sizeof(mOptions->language), 1);
|
|
SDL_RWread(file, &mOptions->difficulty, sizeof(mOptions->difficulty), 1);
|
|
SDL_RWread(file, &mOptions->input[0].deviceType, sizeof(mOptions->input[0].deviceType), 1);
|
|
SDL_RWread(file, &mOptions->input[1].deviceType, sizeof(mOptions->input[1].deviceType), 1);
|
|
SDL_RWread(file, &mOptions->filter, sizeof(mOptions->filter), 1);
|
|
SDL_RWread(file, &mOptions->vSync, sizeof(mOptions->vSync), 1);
|
|
|
|
// Normaliza los valores
|
|
if (!((mOptions->fullScreenMode == 0) ||
|
|
(mOptions->fullScreenMode == SDL_WINDOW_FULLSCREEN) ||
|
|
(mOptions->fullScreenMode == SDL_WINDOW_FULLSCREEN_DESKTOP)))
|
|
mOptions->fullScreenMode = 0;
|
|
if ((mOptions->windowSize < 1) || (mOptions->windowSize > 4))
|
|
mOptions->windowSize = 3;
|
|
if ((mOptions->language < 0) || (mOptions->language > MAX_LANGUAGES))
|
|
mOptions->language = en_UK;
|
|
|
|
// Cierra el fichero
|
|
SDL_RWclose(file);
|
|
}
|
|
|
|
return success;
|
|
}
|
|
|
|
// Guarda el fichero de configuración
|
|
bool Director::saveConfigFile()
|
|
{
|
|
bool success = true;
|
|
const std::string p = mAsset->get("config.bin");
|
|
std::string filename = p.substr(p.find_last_of("\\/") + 1);
|
|
SDL_RWops *file = SDL_RWFromFile(p.c_str(), "w+b");
|
|
if (file != NULL)
|
|
{
|
|
// Guarda los datos
|
|
SDL_RWwrite(file, &mOptions->fullScreenMode, sizeof(mOptions->fullScreenMode), 1);
|
|
SDL_RWwrite(file, &mOptions->windowSize, sizeof(mOptions->windowSize), 1);
|
|
SDL_RWwrite(file, &mOptions->language, sizeof(mOptions->language), 1);
|
|
SDL_RWwrite(file, &mOptions->difficulty, sizeof(mOptions->difficulty), 1);
|
|
SDL_RWwrite(file, &mOptions->input[0].deviceType, sizeof(mOptions->input[0].deviceType), 1);
|
|
SDL_RWwrite(file, &mOptions->input[1].deviceType, sizeof(mOptions->input[1].deviceType), 1);
|
|
SDL_RWwrite(file, &mOptions->filter, sizeof(mOptions->filter), 1);
|
|
SDL_RWwrite(file, &mOptions->vSync, sizeof(mOptions->vSync), 1);
|
|
|
|
printf("Writing file %s\n", filename.c_str());
|
|
|
|
// Cierra el fichero
|
|
SDL_RWclose(file);
|
|
}
|
|
else
|
|
{
|
|
printf("Error: Unable to save %s file! %s\n", filename.c_str(), SDL_GetError());
|
|
}
|
|
return success;
|
|
}
|
|
|
|
// Establece el valor de la variable
|
|
void Director::setExecutablePath(std::string path)
|
|
{
|
|
mExecutablePath = path.substr(0, path.find_last_of("\\/"));
|
|
}
|
|
|
|
// Obtiene el valor de la variable
|
|
Uint8 Director::getSubsection()
|
|
{
|
|
return mSection.subsection;
|
|
}
|
|
|
|
// Obtiene el valor de la variable
|
|
Uint8 Director::getSection()
|
|
{
|
|
return mSection.name;
|
|
}
|
|
|
|
// Establece el valor de la variable
|
|
void Director::setSection(section_t section)
|
|
{
|
|
mSection = section;
|
|
}
|
|
|
|
void Director::runLogo()
|
|
{
|
|
}
|
|
|
|
void Director::runIntro()
|
|
{
|
|
}
|
|
|
|
void Director::runTitle()
|
|
{
|
|
}
|
|
|
|
void Director::runGame()
|
|
{
|
|
mGame = new Game(mWindow, mRenderer, mAsset, mLang, mInput);
|
|
setSection(mGame->run());
|
|
delete mGame;
|
|
}
|
|
|
|
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_GAME:
|
|
runGame();
|
|
break;
|
|
}
|
|
}
|
|
} |