Primera implementación de la clase asset
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
// Constructor
|
||||
Asset::Asset(std::string path)
|
||||
{
|
||||
mExecutablePath = path;
|
||||
}
|
||||
|
||||
// Destructor
|
||||
@@ -11,10 +12,56 @@ Asset::~Asset()
|
||||
}
|
||||
|
||||
// Añade un elemento a la lista
|
||||
void Asset::add(std::string file, enum assetType type)
|
||||
void Asset::add(std::string file, enum assetType type, bool required)
|
||||
{
|
||||
item_t temp;
|
||||
temp.file = file;
|
||||
temp.file = mExecutablePath + "/.." + file;
|
||||
temp.type = type;
|
||||
temp.required = required;
|
||||
mFileList.push_back(temp);
|
||||
}
|
||||
|
||||
// Devuelve un elemento de la lista a partir de una cadena
|
||||
std::string Asset::get(std::string text)
|
||||
{
|
||||
for (int i = 0; i < mFileList.size(); i++)
|
||||
if (mFileList[i].file.find(text) != std::string::npos)
|
||||
return mFileList[i].file;
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
// Comprueba que existen todos los elementos
|
||||
bool Asset::check()
|
||||
{
|
||||
bool success = true;
|
||||
|
||||
for (int i = 0; i < mFileList.size(); i++)
|
||||
if (mFileList[i].required)
|
||||
success &= checkFile(mFileList[i].file);
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
// Comprueba que existe un fichero
|
||||
bool Asset::checkFile(std::string path)
|
||||
{
|
||||
bool success = true;
|
||||
|
||||
// Comprueba si existe el fichero
|
||||
const std::string filename = path.substr(path.find_last_of("\\/") + 1);
|
||||
SDL_RWops *file = SDL_RWFromFile(path.c_str(), "r+b");
|
||||
|
||||
if (file != NULL)
|
||||
{
|
||||
printf("Checking file %-20s [OK]\n", filename.c_str());
|
||||
SDL_RWclose(file);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Checking file %-20s [ERROR]\n", filename.c_str());
|
||||
success = false;
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
@@ -6,14 +6,15 @@
|
||||
#ifndef ASSET_H
|
||||
#define ASSET_H
|
||||
|
||||
enum assetType {bitmap, music, sound, data, font, lang};
|
||||
|
||||
#define ASSET_TYPE_BITMAP 0
|
||||
#define ASSET_TYPE_MUSIC 1
|
||||
#define ASSET_TYPE_SOUND 2
|
||||
#define ASSET_TYPE_DATA 3
|
||||
#define ASSET_TYPE_FONT 4
|
||||
#define ASSET_TYPE_LANG 5
|
||||
enum assetType
|
||||
{
|
||||
bitmap,
|
||||
music,
|
||||
sound,
|
||||
data,
|
||||
font,
|
||||
lang
|
||||
};
|
||||
|
||||
// Clase Asset
|
||||
class Asset
|
||||
@@ -22,11 +23,16 @@ private:
|
||||
// Estructura para definir un item
|
||||
struct item_t
|
||||
{
|
||||
std::string file;
|
||||
enum assetType type;
|
||||
std::string file; // Ruta del fichero desde la raiz del directorio
|
||||
enum assetType type; // Indica el tipo de recurso
|
||||
bool required; // Indica si es un fichero que debe de existir
|
||||
};
|
||||
|
||||
std::vector<item_t> mFileList;
|
||||
std::string mExecutablePath;
|
||||
|
||||
// Comprueba que existe un fichero
|
||||
bool checkFile(std::string path);
|
||||
|
||||
public:
|
||||
// Constructor
|
||||
@@ -36,7 +42,13 @@ public:
|
||||
~Asset();
|
||||
|
||||
// Añade un elemento a la lista
|
||||
void add(std::string file, enum assetType type);
|
||||
void add(std::string file, enum assetType type, bool required = true);
|
||||
|
||||
// Devuelve un elemento de la lista a partir de una cadena
|
||||
std::string get(std::string text);
|
||||
|
||||
// Comprueba que existen todos los elementos
|
||||
bool check();
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -10,19 +10,19 @@ Director::Director(std::string path)
|
||||
// Inicializa la ruta
|
||||
setExecutablePath(path);
|
||||
|
||||
// Establece la lista de ficheros
|
||||
setFileList();
|
||||
|
||||
// Crea el objeto que controla los ficheros de recursos
|
||||
mAsset = new Asset(mExecutablePath);
|
||||
|
||||
// Si falta algún fichero no inicies el programa
|
||||
// Establece la lista de ficheros
|
||||
setFileList();
|
||||
|
||||
// Si falta algún fichero no inicia el programa
|
||||
Uint8 section = PROG_SECTION_GAME;
|
||||
if (!checkFileList())
|
||||
if (!mAsset->check())
|
||||
section = PROG_SECTION_QUIT;
|
||||
|
||||
// Inicializa el objeto de idioma
|
||||
mLang = new Lang(mFileList);
|
||||
mLang = new Lang(mAsset);
|
||||
|
||||
// Crea el puntero a la estructura y carga el fichero de configuración
|
||||
mOptions = new options_t;
|
||||
@@ -39,7 +39,7 @@ Director::Director(std::string path)
|
||||
}
|
||||
|
||||
// Crea los objetos
|
||||
mInput = new Input(mFileList[11]);
|
||||
mInput = new Input(mAsset->get("gamecontrollerdb.txt"));
|
||||
|
||||
// Inicializa SDL
|
||||
initSDL();
|
||||
@@ -187,91 +187,19 @@ bool Director::initSDL()
|
||||
// Crea el indice de ficheros
|
||||
void Director::setFileList()
|
||||
{
|
||||
// Inicializa el vector
|
||||
for (int i = 0; i < MAX_FILE_LIST; i++)
|
||||
mFileList[i] = "";
|
||||
|
||||
// Fuentes
|
||||
mFileList[0] = mExecutablePath + "/" + "../media/font/8bithud.png";
|
||||
mFileList[1] = mExecutablePath + "/" + "../media/font/8bithud.txt";
|
||||
mFileList[2] = mExecutablePath + "/" + "../media/font/nokia.png";
|
||||
mFileList[3] = mExecutablePath + "/" + "../media/font/nokia.txt";
|
||||
mFileList[4] = mExecutablePath + "/" + "../media/font/nokia2.png";
|
||||
mFileList[5] = mExecutablePath + "/" + "../media/font/nokia2.txt";
|
||||
mFileList[6] = mExecutablePath + "/" + "../media/font/smb2.png";
|
||||
mFileList[7] = mExecutablePath + "/" + "../media/font/smb2.txt";
|
||||
|
||||
// Textos
|
||||
mFileList[8] = mExecutablePath + "/" + "../media/lang/es_ES.txt";
|
||||
mFileList[9] = mExecutablePath + "/" + "../media/lang/en_UK.txt";
|
||||
mFileList[10] = mExecutablePath + "/" + "../media/lang/ba_BA.txt";
|
||||
|
||||
// DATA
|
||||
mFileList[11] = mExecutablePath + "/" + "../data/gamecontrollerdb.txt";
|
||||
mFileList[12] = mExecutablePath + "/" + "../data/config.bin";
|
||||
}
|
||||
|
||||
// Comprueba los ficheros del vector de ficheros que coinciden con una ruta dada
|
||||
bool Director::checkFolder(std::string name, std::string path)
|
||||
{
|
||||
bool success = true;
|
||||
std::string p;
|
||||
std::string filename;
|
||||
SDL_RWops *file;
|
||||
|
||||
// Comprueba los ficheros de la carpeta
|
||||
printf("\n>> %s FILES\n", name.c_str());
|
||||
for (int i = 3; i < MAX_FILE_LIST; i++)
|
||||
{
|
||||
if (mFileList[i].find(path.c_str()) != std::string::npos)
|
||||
{
|
||||
p = mFileList[i].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);
|
||||
}
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
// Comprueba que todos los ficheros existen
|
||||
bool Director::checkFileList()
|
||||
{
|
||||
bool success = true;
|
||||
printf("Checking files...\n");
|
||||
|
||||
if (success)
|
||||
success &= checkFolder("MUSIC", "/media/music/");
|
||||
|
||||
if (success)
|
||||
success &= checkFolder("SOUND", "/media/sound/");
|
||||
|
||||
if (success)
|
||||
success &= checkFolder("BITMAP", "/media/gfx/");
|
||||
|
||||
if (success)
|
||||
success &= checkFolder("FONT", "/media/font/");
|
||||
|
||||
if (success)
|
||||
success &= checkFolder("LANG", "/media/lang/");
|
||||
|
||||
// Resultado
|
||||
if (success)
|
||||
printf("\n** All files OK.\n\n");
|
||||
else
|
||||
printf("\n** A file is missing. Exiting.\n\n");
|
||||
|
||||
return success;
|
||||
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);
|
||||
}
|
||||
|
||||
// Carga el fichero de configuración
|
||||
@@ -290,7 +218,7 @@ bool Director::loadConfigFile()
|
||||
// Indicador de éxito en la carga
|
||||
bool success = true;
|
||||
|
||||
const std::string p = mFileList[12];
|
||||
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");
|
||||
|
||||
@@ -359,7 +287,7 @@ bool Director::loadConfigFile()
|
||||
bool Director::saveConfigFile()
|
||||
{
|
||||
bool success = true;
|
||||
const std::string p = mFileList[12];
|
||||
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)
|
||||
@@ -424,7 +352,7 @@ void Director::runTitle()
|
||||
|
||||
void Director::runGame()
|
||||
{
|
||||
mGame = new Game(mRenderer, mFileList, mLang, mInput);
|
||||
mGame = new Game(mRenderer, mAsset, mLang, mInput);
|
||||
setSection(mGame->run());
|
||||
delete mGame;
|
||||
}
|
||||
|
||||
@@ -1,25 +1,25 @@
|
||||
#include "game.h"
|
||||
|
||||
// Constructor
|
||||
Game::Game(SDL_Renderer *renderer, std::string *filelist, Lang *lang, Input *input)
|
||||
Game::Game(SDL_Renderer *renderer, Asset *asset, Lang *lang, Input *input)
|
||||
{
|
||||
// Copia los punteros
|
||||
mRenderer = renderer;
|
||||
mFileList = filelist;
|
||||
mAsset = asset;
|
||||
mLang = lang;
|
||||
mInput = input;
|
||||
|
||||
mScreen = new Screen(renderer);
|
||||
mEventHandler = new SDL_Event();
|
||||
mTextureText = new LTexture();
|
||||
mText = new Text(mFileList[5], mTextureText, renderer);
|
||||
mText = new Text(mAsset->get("nokia2.txt"), mTextureText, renderer);
|
||||
mFade = new Fade(renderer);
|
||||
}
|
||||
|
||||
Game::~Game()
|
||||
{
|
||||
mRenderer = nullptr;
|
||||
mFileList = nullptr;
|
||||
mAsset = nullptr;
|
||||
mLang = nullptr;
|
||||
mInput = nullptr;
|
||||
|
||||
@@ -59,7 +59,7 @@ bool Game::loadMedia()
|
||||
bool success = true;
|
||||
|
||||
// Texturas
|
||||
success &= loadTextureFromFile(mTextureText, mFileList[4], mRenderer);
|
||||
success &= loadTextureFromFile(mTextureText, mAsset->get("nokia2.png"), mRenderer);
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
#include "fade.h"
|
||||
#include "lang.h"
|
||||
#include "screen.h"
|
||||
#include "asset.h"
|
||||
#include "jail_audio.h"
|
||||
|
||||
#ifndef GAME_H
|
||||
@@ -24,8 +25,9 @@ class Game
|
||||
{
|
||||
private:
|
||||
SDL_Renderer *mRenderer; // El renderizador de la ventana
|
||||
Screen *mScreen; // El objeto encargado de manejar el renderizador
|
||||
SDL_Event *mEventHandler; // Manejador de eventos
|
||||
Screen *mScreen; // El objeto encargado de manejar el renderizador
|
||||
Asset *mAsset; // Objeto con la ruta a todos los ficheros de recursos
|
||||
Lang *mLang; // Objeto para gestionar los textos en diferentes idiomas
|
||||
Input *mInput; // Manejador de entrada
|
||||
Text *mText; // Fuente para los textos del juego
|
||||
@@ -43,7 +45,7 @@ private:
|
||||
|
||||
public:
|
||||
// Constructor
|
||||
Game(SDL_Renderer *renderer, std::string *filelist, Lang *lang, Input *input);
|
||||
Game(SDL_Renderer *renderer, Asset *asset, Lang *lang, Input *input);
|
||||
|
||||
// Destructor
|
||||
~Game();
|
||||
|
||||
@@ -3,14 +3,15 @@
|
||||
#include <fstream>
|
||||
|
||||
// Constructor
|
||||
Lang::Lang(std::string *fileList)
|
||||
Lang::Lang(Asset *asset)
|
||||
{
|
||||
mFileList = fileList;
|
||||
mAsset = asset;
|
||||
}
|
||||
|
||||
// Destructor
|
||||
Lang::~Lang()
|
||||
{
|
||||
mAsset = nullptr;
|
||||
}
|
||||
|
||||
// Inicializa los textos del juego en el idioma seleccionado
|
||||
@@ -21,19 +22,19 @@ bool Lang::setLang(Uint8 lang)
|
||||
switch (lang)
|
||||
{
|
||||
case es_ES:
|
||||
file = mFileList[49];
|
||||
file = mAsset->get("es_ES.txt");
|
||||
break;
|
||||
|
||||
case en_UK:
|
||||
file = mFileList[50];
|
||||
file = mAsset->get("en_UK.txt");
|
||||
break;
|
||||
|
||||
case ba_BA:
|
||||
file = mFileList[51];
|
||||
file = mAsset->get("ba_BA.txt");
|
||||
break;
|
||||
|
||||
default:
|
||||
file = mFileList[50];
|
||||
file = mAsset->get("en_UK.txt");
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
#include "ifdefs.h"
|
||||
#include "asset.h"
|
||||
#include <string>
|
||||
|
||||
#ifndef LANG_H
|
||||
@@ -18,12 +19,12 @@
|
||||
class Lang
|
||||
{
|
||||
private:
|
||||
std::string *mFileList; // Lista de ficheros con los recursos
|
||||
Asset *mAsset;
|
||||
std::string mTextStrings[MAX_TEXT_STRINGS];
|
||||
|
||||
public:
|
||||
// Constructor
|
||||
Lang(std::string *fileList);
|
||||
Lang(Asset *asset);
|
||||
|
||||
// Destructor
|
||||
~Lang();
|
||||
|
||||
Reference in New Issue
Block a user