debuging memory leaks
This commit is contained in:
453
source/director.cpp
Normal file
453
source/director.cpp
Normal file
@@ -0,0 +1,453 @@
|
||||
#include "const.h"
|
||||
#include "utils.h"
|
||||
#include "director.h"
|
||||
#include <iostream>
|
||||
#ifdef __MIPSEL__
|
||||
#include <sys/stat.h>
|
||||
#include <dirent.h>
|
||||
#endif
|
||||
|
||||
// Constructor
|
||||
Director::Director(std::string path)
|
||||
{
|
||||
// Crea los objetos
|
||||
mInput = new Input();
|
||||
mOptions = new options_t;
|
||||
|
||||
// Inicializa variables
|
||||
setExecutablePath(path);
|
||||
setFileList();
|
||||
checkFileList();
|
||||
|
||||
// Inicializa SDL
|
||||
initSDL();
|
||||
|
||||
// Inicializa JailAudio
|
||||
initJailAudio();
|
||||
|
||||
#ifdef __MIPSEL__
|
||||
DIR *dir = opendir("/media/data/local/home/.coffee_crisis");
|
||||
if (dir)
|
||||
{
|
||||
closedir(dir);
|
||||
}
|
||||
else if (ENOENT == errno)
|
||||
{
|
||||
int status = mkdir("/media/data/local/home/.coffee_crisis", 755);
|
||||
}
|
||||
#endif
|
||||
|
||||
// Carga recursos
|
||||
loadConfigFile();
|
||||
|
||||
// Inicializa el resto de variables
|
||||
init();
|
||||
}
|
||||
|
||||
Director::~Director()
|
||||
{
|
||||
saveConfigFile();
|
||||
|
||||
delete mInput;
|
||||
mInput = nullptr;
|
||||
|
||||
delete mOptions;
|
||||
mOptions = nullptr;
|
||||
|
||||
SDL_DestroyRenderer(mRenderer);
|
||||
SDL_DestroyWindow(mWindow);
|
||||
mRenderer = nullptr;
|
||||
mWindow = nullptr;
|
||||
|
||||
SDL_Quit();
|
||||
}
|
||||
|
||||
// Inicia las variables necesarias para arrancar el programa
|
||||
void Director::init()
|
||||
{
|
||||
mSection.name = PROG_SECTION_LOGO;
|
||||
mSection.subsection = 0;
|
||||
|
||||
// Teclado
|
||||
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);
|
||||
#ifdef __MIPSEL__
|
||||
mInput->bindKey(INPUT_BUTTON_1, SDL_SCANCODE_LSHIFT);
|
||||
mInput->bindKey(INPUT_BUTTON_2, SDL_SCANCODE_SPACE);
|
||||
mInput->bindKey(INPUT_BUTTON_3, SDL_SCANCODE_LCTRL);
|
||||
#else
|
||||
mInput->bindKey(INPUT_BUTTON_1, SDL_SCANCODE_Q);
|
||||
mInput->bindKey(INPUT_BUTTON_2, SDL_SCANCODE_W);
|
||||
mInput->bindKey(INPUT_BUTTON_3, SDL_SCANCODE_E);
|
||||
#endif
|
||||
mInput->bindKey(INPUT_BUTTON_4, SDL_SCANCODE_ESCAPE); // PAUSE
|
||||
mInput->bindKey(INPUT_BUTTON_5, SDL_SCANCODE_ESCAPE); // ESCAPE
|
||||
}
|
||||
|
||||
// Inicializa JailAudio
|
||||
void Director::initJailAudio()
|
||||
{
|
||||
JA_Init(48000, AUDIO_S16, 2);
|
||||
}
|
||||
|
||||
// Arranca SDL y crea la ventana
|
||||
bool Director::initSDL()
|
||||
{
|
||||
// Indicador de inicialización
|
||||
bool success = true;
|
||||
|
||||
// Inicializa SDL
|
||||
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_AUDIO | SDL_INIT_HAPTIC) < 0)
|
||||
{
|
||||
printf("SDL could not initialize!\nSDL Error: %s\n", SDL_GetError());
|
||||
success = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Establece el filtro de la textura a nearest
|
||||
if (!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "0"))
|
||||
{
|
||||
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 con vsync
|
||||
mRenderer = SDL_CreateRenderer(mWindow, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
|
||||
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()
|
||||
{
|
||||
// Ficheros binarios
|
||||
#ifdef __MIPSEL__
|
||||
mFileList[0] = "/media/data/local/home/.coffee_crisis/score.bin";
|
||||
mFileList[1] = "/media/data/local/home/.coffee_crisis/demo.bin";
|
||||
mFileList[2] = "/media/data/local/home/.coffee_crisis/config.bin";
|
||||
#else
|
||||
mFileList[0] = mExecutablePath + "/" + "../data/score.bin";
|
||||
mFileList[1] = mExecutablePath + "/" + "../data/demo.bin";
|
||||
mFileList[2] = mExecutablePath + "/" + "../data/config.bin";
|
||||
#endif
|
||||
|
||||
// Musicas
|
||||
mFileList[3] = mExecutablePath + "/" + "../media/music/intro.ogg";
|
||||
mFileList[4] = mExecutablePath + "/" + "../media/music/playing.ogg";
|
||||
mFileList[5] = mExecutablePath + "/" + "../media/music/title.ogg";
|
||||
|
||||
// Sonidos
|
||||
mFileList[6] = mExecutablePath + "/" + "../media/sound/balloon.wav";
|
||||
mFileList[7] = mExecutablePath + "/" + "../media/sound/bubble1.wav";
|
||||
mFileList[8] = mExecutablePath + "/" + "../media/sound/bubble2.wav";
|
||||
mFileList[9] = mExecutablePath + "/" + "../media/sound/bubble3.wav";
|
||||
mFileList[10] = mExecutablePath + "/" + "../media/sound/bubble4.wav";
|
||||
mFileList[11] = mExecutablePath + "/" + "../media/sound/bullet.wav";
|
||||
mFileList[12] = mExecutablePath + "/" + "../media/sound/coffeeout.wav";
|
||||
mFileList[13] = mExecutablePath + "/" + "../media/sound/hiscore.wav";
|
||||
mFileList[14] = mExecutablePath + "/" + "../media/sound/itemdrop.wav";
|
||||
mFileList[15] = mExecutablePath + "/" + "../media/sound/itempickup.wav";
|
||||
mFileList[16] = mExecutablePath + "/" + "../media/sound/menu_cancel.wav";
|
||||
mFileList[17] = mExecutablePath + "/" + "../media/sound/menu_move.wav";
|
||||
mFileList[18] = mExecutablePath + "/" + "../media/sound/menu_select.wav";
|
||||
mFileList[19] = mExecutablePath + "/" + "../media/sound/player_collision.wav";
|
||||
mFileList[20] = mExecutablePath + "/" + "../media/sound/stage_change.wav";
|
||||
mFileList[21] = mExecutablePath + "/" + "../media/sound/title.wav";
|
||||
mFileList[22] = mExecutablePath + "/" + "../media/sound/clock.wav";
|
||||
mFileList[23] = mExecutablePath + "/" + "../media/sound/powerball.wav";
|
||||
|
||||
// Texturas
|
||||
mFileList[24] = mExecutablePath + "/" + "../media/gfx/balloon.png";
|
||||
mFileList[25] = mExecutablePath + "/" + "../media/gfx/bullet.png";
|
||||
mFileList[26] = mExecutablePath + "/" + "../media/gfx/font_black_x2.png";
|
||||
mFileList[27] = mExecutablePath + "/" + "../media/gfx/font_black.png";
|
||||
mFileList[28] = mExecutablePath + "/" + "../media/gfx/font_nokia.png";
|
||||
mFileList[29] = mExecutablePath + "/" + "../media/gfx/font_white_x2.png";
|
||||
mFileList[30] = mExecutablePath + "/" + "../media/gfx/font_white.png";
|
||||
mFileList[31] = mExecutablePath + "/" + "../media/gfx/game_bg.png";
|
||||
mFileList[32] = mExecutablePath + "/" + "../media/gfx/game_text.png";
|
||||
mFileList[33] = mExecutablePath + "/" + "../media/gfx/intro.png";
|
||||
mFileList[34] = mExecutablePath + "/" + "../media/gfx/items.png";
|
||||
mFileList[35] = mExecutablePath + "/" + "../media/gfx/logo.png";
|
||||
mFileList[36] = mExecutablePath + "/" + "../media/gfx/menu.png";
|
||||
mFileList[37] = mExecutablePath + "/" + "../media/gfx/player_body.png";
|
||||
mFileList[38] = mExecutablePath + "/" + "../media/gfx/player_death.png";
|
||||
mFileList[39] = mExecutablePath + "/" + "../media/gfx/player_legs.png";
|
||||
mFileList[40] = mExecutablePath + "/" + "../media/gfx/title.png";
|
||||
}
|
||||
|
||||
// Comprueba que todos los ficheros existen
|
||||
bool Director::checkFileList()
|
||||
{
|
||||
bool success = true;
|
||||
/*std::string p;
|
||||
std::string filename;
|
||||
SDL_RWops *file;
|
||||
|
||||
// Comprueba los ficheros de musica
|
||||
printf("\n>> MUSIC FILES\n");
|
||||
if (success)
|
||||
for (int i = 0; i < TOTAL_MUSIC; i++)
|
||||
{
|
||||
p = mMusic[i].file.c_str();
|
||||
filename = p.substr(p.find_last_of("\\/") + 1);
|
||||
file = SDL_RWFromFile(p.c_str(), "r+b");
|
||||
if (file != NULL)
|
||||
{
|
||||
printf("Checking file %-20s [OK]\n", filename.c_str());
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Checking file %-20s [ERROR]\n", filename.c_str());
|
||||
success = false;
|
||||
break;
|
||||
}
|
||||
SDL_RWclose(file);
|
||||
}
|
||||
|
||||
// Comprueba los ficheros de sonidos
|
||||
printf("\n>> SOUND FILES\n");
|
||||
if (success)
|
||||
for (int i = 0; i < TOTAL_SOUND; i++)
|
||||
{
|
||||
p = mSound[i].file.c_str();
|
||||
filename = p.substr(p.find_last_of("\\/") + 1);
|
||||
file = SDL_RWFromFile(p.c_str(), "r+b");
|
||||
if (file != NULL)
|
||||
{
|
||||
printf("Checking file %-20s [OK]\n", filename.c_str());
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Checking file %-20s [ERROR]\n", filename.c_str());
|
||||
success = false;
|
||||
break;
|
||||
}
|
||||
SDL_RWclose(file);
|
||||
}
|
||||
|
||||
// Comprueba los ficheros con texturas
|
||||
printf("\n>> TEXTURE FILES\n");
|
||||
if (success)
|
||||
for (int i = 0; i < TOTAL_TEXTURE; i++)
|
||||
{
|
||||
p = mTexture[i].file.c_str();
|
||||
filename = p.substr(p.find_last_of("\\/") + 1);
|
||||
file = SDL_RWFromFile(p.c_str(), "r+b");
|
||||
if (file != NULL)
|
||||
{
|
||||
printf("Checking file %-20s [OK]\n", filename.c_str());
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Checking file %-20s [ERROR]\n", filename.c_str());
|
||||
success = false;
|
||||
break;
|
||||
}
|
||||
SDL_RWclose(file);
|
||||
}
|
||||
|
||||
// Resultado
|
||||
if (success)
|
||||
printf("\n** All files OK.\n\n");
|
||||
else
|
||||
printf("\n** A file is missing. Exiting.\n\n");
|
||||
|
||||
*/
|
||||
return success;
|
||||
}
|
||||
|
||||
// Carga el fichero de configuración
|
||||
bool Director::loadConfigFile()
|
||||
{
|
||||
// Indicador de éxito en la carga
|
||||
bool success = true;
|
||||
|
||||
const std::string p = mFileList[2];
|
||||
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());
|
||||
|
||||
// Creamos el fichero para escritura
|
||||
file = SDL_RWFromFile(p.c_str(), "w+b");
|
||||
if (file != NULL)
|
||||
{
|
||||
printf("New file (%s) created!\n", filename.c_str());
|
||||
|
||||
// Inicializamos los datos
|
||||
mOptions->fullScreenMode = 0;
|
||||
SDL_RWwrite(file, &mOptions->fullScreenMode, sizeof(mOptions->fullScreenMode), 1);
|
||||
|
||||
mOptions->windowSize = 3;
|
||||
SDL_RWwrite(file, &mOptions->windowSize, sizeof(mOptions->windowSize), 1);
|
||||
|
||||
// Cerramos 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);
|
||||
|
||||
// Aplica las opciones
|
||||
SDL_SetWindowFullscreen(mWindow, mOptions->fullScreenMode);
|
||||
SDL_SetWindowSize(mWindow, SCREEN_WIDTH * mOptions->windowSize, SCREEN_HEIGHT * mOptions->windowSize);
|
||||
|
||||
// Cierra el fichero
|
||||
SDL_RWclose(file);
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
// Guarda el fichero de configuración
|
||||
bool Director::saveConfigFile()
|
||||
{
|
||||
bool success = true;
|
||||
const std::string p = mFileList[2];
|
||||
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(Uint32), 1);
|
||||
SDL_RWwrite(file, &mOptions->windowSize, sizeof(Uint8), 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()
|
||||
{
|
||||
mLogo = new Logo(mRenderer, mFileList);
|
||||
setSection(mLogo->run());
|
||||
delete mLogo;
|
||||
}
|
||||
|
||||
void Director::runIntro()
|
||||
{
|
||||
mIntro = new Intro(mRenderer, mFileList);
|
||||
setSection(mIntro->run());
|
||||
delete mIntro;
|
||||
}
|
||||
|
||||
void Director::runTitle()
|
||||
{
|
||||
mTitle = new Title(mWindow, mRenderer, mInput, mFileList, mOptions);
|
||||
setSection(mTitle->run(mSection.subsection));
|
||||
delete mTitle;
|
||||
}
|
||||
|
||||
void Director::runGame()
|
||||
{
|
||||
mGame = new Game(mRenderer, mFileList, mInput, false);
|
||||
setSection(mGame->run());
|
||||
delete mGame;
|
||||
}
|
||||
|
||||
void Director::run()
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
Game *test = new Game(mRenderer, mFileList, mInput, true);
|
||||
delete test;
|
||||
printf("%i\n", i);
|
||||
}
|
||||
mSection.name = PROG_SECTION_QUIT;
|
||||
|
||||
// Bucle principal
|
||||
while (!(getSection() == PROG_SECTION_QUIT))
|
||||
{
|
||||
switch (getSection())
|
||||
{
|
||||
case PROG_SECTION_LOGO:
|
||||
runLogo();
|
||||
break;
|
||||
case PROG_SECTION_INTRO:
|
||||
runIntro();
|
||||
break;
|
||||
case PROG_SECTION_TITLE:
|
||||
runTitle();
|
||||
break;
|
||||
case PROG_SECTION_GAME:
|
||||
runGame();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user