Integrada la clase asset. Corregidos algunos fallos en el bucle principal
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
Asset::Asset(std::string path)
|
||||
{
|
||||
mExecutablePath = path;
|
||||
longest_name = 0;
|
||||
}
|
||||
|
||||
// Destructor
|
||||
@@ -19,6 +20,9 @@ void Asset::add(std::string file, enum assetType type, bool required)
|
||||
temp.type = type;
|
||||
temp.required = required;
|
||||
mFileList.push_back(temp);
|
||||
|
||||
const std::string filename = file.substr(file.find_last_of("\\/") + 1);
|
||||
longest_name = SDL_max(longest_name, filename.size());
|
||||
}
|
||||
|
||||
// Devuelve el fichero de un elemento de la lista a partir de una cadena
|
||||
@@ -37,14 +41,27 @@ bool Asset::check()
|
||||
{
|
||||
bool success = true;
|
||||
|
||||
printf("\n** Checking files.\n");
|
||||
|
||||
// Comprueba la lista de ficheros clasificandolos por tipo
|
||||
for (int type = 0; type < maxAssetType; type++)
|
||||
{
|
||||
printf("\n>> %s FILES\n", getTypeName(type).c_str());
|
||||
// Comprueba si hay ficheros de ese tipo
|
||||
bool any = false;
|
||||
|
||||
for (int i = 0; i < mFileList.size(); i++)
|
||||
if ((mFileList[i].required) && (mFileList[i].type == type))
|
||||
success &= checkFile(mFileList[i].file);
|
||||
any = true;
|
||||
|
||||
// Si hay ficheros de ese tipo, comprueba si existen
|
||||
if (any)
|
||||
{
|
||||
printf("\n>> %s FILES\n", getTypeName(type).c_str());
|
||||
|
||||
for (int i = 0; i < mFileList.size(); i++)
|
||||
if ((mFileList[i].required) && (mFileList[i].type == type))
|
||||
success &= checkFile(mFileList[i].file);
|
||||
}
|
||||
}
|
||||
|
||||
// Resultado
|
||||
@@ -59,7 +76,8 @@ bool Asset::check()
|
||||
// Comprueba que existe un fichero
|
||||
bool Asset::checkFile(std::string path)
|
||||
{
|
||||
bool success = true;
|
||||
bool success = false;
|
||||
std::string result = "ERROR";
|
||||
|
||||
// Comprueba si existe el fichero
|
||||
const std::string filename = path.substr(path.find_last_of("\\/") + 1);
|
||||
@@ -67,14 +85,13 @@ bool Asset::checkFile(std::string path)
|
||||
|
||||
if (file != NULL)
|
||||
{
|
||||
printf("Checking file %-20s [OK]\n", filename.c_str());
|
||||
result = "OK";
|
||||
success = true;
|
||||
SDL_RWclose(file);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Checking file %-20s [ERROR]\n", filename.c_str());
|
||||
success = false;
|
||||
}
|
||||
|
||||
const std::string s = "Checking file %-" + std::to_string(longest_name) + "s [" + result + "]\n";
|
||||
printf(s.c_str(), filename.c_str());
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
@@ -32,6 +32,8 @@ private:
|
||||
bool required; // Indica si es un fichero que debe de existir
|
||||
};
|
||||
|
||||
int longest_name; // Contiene la longitud del nombre de fichero mas largo
|
||||
|
||||
std::vector<item_t> mFileList;
|
||||
std::string mExecutablePath;
|
||||
|
||||
|
||||
@@ -5,19 +5,49 @@
|
||||
const Uint8 *keystates;
|
||||
|
||||
// Constructor
|
||||
Director::Director(std::string _path)
|
||||
Director::Director(std::string path)
|
||||
{
|
||||
// Arranca SDL y crea la ventana
|
||||
initSDL();
|
||||
|
||||
// Crea todos los objetos del juego
|
||||
initObjects();
|
||||
eventHandler = new SDL_Event();
|
||||
// text.white = new Text("", nullptr, nullptr);
|
||||
screen = new Screen(window, renderer);
|
||||
asset = new Asset(path.substr(0, path.find_last_of("\\/")));
|
||||
|
||||
setFileList();
|
||||
|
||||
// Inicializa todas las variables
|
||||
init(_path);
|
||||
section = NONE;
|
||||
gameControllerFound = false;
|
||||
for (int i = 0; i < 360; i++)
|
||||
sen[i] = sin(i * 3.14 / 180);
|
||||
for (int i = 0; i < TOTAL_SCORE_DATA; i++)
|
||||
scoreData[i] = 0;
|
||||
|
||||
initOptions();
|
||||
initGame();
|
||||
quit = false;
|
||||
}
|
||||
|
||||
Director::~Director()
|
||||
{
|
||||
|
||||
quitGame();
|
||||
|
||||
// Borra todos los objetos del juego
|
||||
deleteObjects();
|
||||
delete eventHandler;
|
||||
eventHandler = nullptr;
|
||||
|
||||
// delete text.white;
|
||||
// text.white = nullptr;
|
||||
|
||||
delete screen;
|
||||
screen = nullptr;
|
||||
|
||||
delete asset;
|
||||
asset = nullptr;
|
||||
|
||||
// Destruye la ventana
|
||||
SDL_DestroyRenderer(renderer);
|
||||
@@ -31,21 +61,6 @@ Director::~Director()
|
||||
SDL_Quit();
|
||||
}
|
||||
|
||||
// Inicializa todas las variables
|
||||
void Director::init(std::string _path)
|
||||
{
|
||||
setPath(_path);
|
||||
section = NONE;
|
||||
gameControllerFound = false;
|
||||
for (int i = 0; i < 360; i++)
|
||||
sen[i] = sin(i * 3.14 / 180);
|
||||
for (int i = 0; i < TOTAL_SCORE_DATA; i++)
|
||||
scoreData[i] = 0;
|
||||
|
||||
initOptions();
|
||||
quit = false;
|
||||
}
|
||||
|
||||
// Inicializa las variables de las opciones
|
||||
void Director::initOptions()
|
||||
{
|
||||
@@ -70,14 +85,7 @@ void Director::initGame()
|
||||
player = new Player(renderer, asset);
|
||||
|
||||
// Map
|
||||
resource.texture[TEXTURE_ACTORS].texture = new LTexture();
|
||||
loadTextureFromFile(resource.texture[TEXTURE_ACTORS].texture, resource.texture[TEXTURE_ACTORS].file, renderer);
|
||||
resource.texture[TEXTURE_BKG_SURFACE].texture = new LTexture();
|
||||
loadTextureFromFile(resource.texture[TEXTURE_BKG_SURFACE].texture, resource.texture[TEXTURE_BKG_SURFACE].file, renderer);
|
||||
resource.texture[TEXTURE_TILES_VOLCANO].texture = new LTexture();
|
||||
loadTextureFromFile(resource.texture[TEXTURE_TILES_VOLCANO].texture, resource.texture[TEXTURE_TILES_VOLCANO].file, renderer);
|
||||
map = new Map(renderer, resource.texture[TEXTURE_TILES_VOLCANO].texture, resource.texture[TEXTURE_ACTORS].texture,
|
||||
resource.texture[TEXTURE_BKG_SURFACE].texture, resource.file[FILE_MAP_VOLCANO].file);
|
||||
map = new Map(renderer, asset->get("volcano.map"), asset);
|
||||
}
|
||||
|
||||
// Limpia las variables del juego
|
||||
@@ -86,18 +94,10 @@ void Director::quitGame()
|
||||
// Player
|
||||
delete player;
|
||||
player = nullptr;
|
||||
delete resource.texture[TEXTURE_PLAYER].texture;
|
||||
resource.texture[TEXTURE_PLAYER].texture = nullptr;
|
||||
|
||||
// Map
|
||||
delete map;
|
||||
map = nullptr;
|
||||
delete resource.texture[TEXTURE_TILES_VOLCANO].texture;
|
||||
resource.texture[TEXTURE_TILES_VOLCANO].texture = nullptr;
|
||||
delete resource.texture[TEXTURE_ACTORS].texture;
|
||||
resource.texture[TEXTURE_ACTORS].texture = nullptr;
|
||||
delete resource.texture[TEXTURE_BKG_SURFACE].texture;
|
||||
resource.texture[TEXTURE_BKG_SURFACE].texture = nullptr;
|
||||
}
|
||||
|
||||
// Arranca SDL y crea la ventana
|
||||
@@ -206,34 +206,12 @@ bool Director::initSDL()
|
||||
return success;
|
||||
}
|
||||
|
||||
// Crea los objetos del programa
|
||||
void Director::initObjects()
|
||||
{
|
||||
eventHandler = new SDL_Event();
|
||||
text.white = new Text("", nullptr, nullptr);
|
||||
screen = new Screen(window, renderer);
|
||||
asset = new Asset(path);
|
||||
}
|
||||
|
||||
// Borra los objetos del programa
|
||||
void Director::deleteObjects()
|
||||
{
|
||||
delete eventHandler;
|
||||
delete text.white;
|
||||
delete screen;
|
||||
delete asset;
|
||||
eventHandler = nullptr;
|
||||
text.white = nullptr;
|
||||
screen = nullptr;
|
||||
asset = nullptr;
|
||||
}
|
||||
|
||||
// Crea el indice de ficheros de recursos
|
||||
bool Director::setFileList()
|
||||
{
|
||||
// Ficheros binarios
|
||||
asset->add("/data/volcano.map", data);
|
||||
asset->add("/data/config.bin", data);
|
||||
asset->add("/data/config.bin", data, false);
|
||||
|
||||
// Texturas
|
||||
asset->add("/media/gfx/actors.png", bitmap);
|
||||
@@ -274,7 +252,7 @@ bool Director::loadMedia(Uint8 section)
|
||||
{
|
||||
case GAME_SECTION_INIT:
|
||||
{
|
||||
p = resource.file[FILE_CONFIG].file.c_str();
|
||||
p = asset->get("config-bin").c_str();
|
||||
std::string filename = p.substr(p.find_last_of("\\/") + 1);
|
||||
filename = p.substr(p.find_last_of("\\/") + 1);
|
||||
|
||||
@@ -322,12 +300,6 @@ bool Director::loadMedia(Uint8 section)
|
||||
SDL_RWclose(file);
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
// Texturas
|
||||
|
||||
// Sonidos
|
||||
|
||||
// Musicas
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -386,7 +358,7 @@ bool Director::unLoadMedia(Uint8 section)
|
||||
{
|
||||
case GAME_SECTION_INIT:
|
||||
{
|
||||
p = resource.file[FILE_CONFIG].file;
|
||||
p = asset->get("config-bin");
|
||||
std::string filename = p.substr(p.find_last_of("\\/") + 1);
|
||||
|
||||
// Abre el fichero de puntuación para escribir
|
||||
@@ -454,12 +426,6 @@ bool Director::unLoadMedia(Uint8 section)
|
||||
return success;
|
||||
}
|
||||
|
||||
// Establece el valor de la variable
|
||||
void Director::setPath(std::string _path)
|
||||
{
|
||||
path = _path.substr(0, _path.find_last_of("\\/"));
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
Uint8 Director::getGameSection()
|
||||
{
|
||||
@@ -590,33 +556,8 @@ void Director::renderGame()
|
||||
// Bucle principal
|
||||
void Director::run()
|
||||
{
|
||||
bool quit = false;
|
||||
|
||||
// Asigna y comprueba si existen todos los ficheros de recursos
|
||||
if (!setFileList())
|
||||
while (isRunning())
|
||||
{
|
||||
quit = true;
|
||||
runGame();
|
||||
}
|
||||
|
||||
// Arranca SDL y crea la ventana
|
||||
if ((!quit) && (!initSDL()))
|
||||
{
|
||||
quit = true;
|
||||
}
|
||||
|
||||
// Inicializa las variables del juego
|
||||
if (!quit)
|
||||
{
|
||||
initGame();
|
||||
}
|
||||
|
||||
// Bucle del juego
|
||||
if (!quit)
|
||||
{
|
||||
while (isRunning())
|
||||
{
|
||||
runGame();
|
||||
}
|
||||
}
|
||||
quitGame();
|
||||
}
|
||||
@@ -59,7 +59,6 @@ private:
|
||||
Screen *screen; // Objeto para pintar en pantalla
|
||||
Asset *asset; // Objeto con todos los ficheros de recursos
|
||||
Uint32 scoreData[TOTAL_SCORE_DATA]; // Datos del fichero de puntuación
|
||||
std::string path; // Path donde está el ejecutable del juego
|
||||
enum _section // Lista con todas las secciones en las que se divide el programa
|
||||
{
|
||||
GAME,
|
||||
@@ -70,9 +69,6 @@ private:
|
||||
_section section; // Seccion actual del programa
|
||||
bool quit; // Indica si hay que terminar el programa
|
||||
|
||||
// Inicializa todas las variables
|
||||
void init(std::string _path);
|
||||
|
||||
// Inicializa las variables de las opciones
|
||||
void initOptions();
|
||||
|
||||
@@ -85,15 +81,6 @@ private:
|
||||
// Arranca SDL y crea la ventana
|
||||
bool initSDL();
|
||||
|
||||
// Crea los objetos del programa
|
||||
void initObjects();
|
||||
|
||||
// Borra los objetos del programa
|
||||
void deleteObjects();
|
||||
|
||||
// Establece el valor de la variable
|
||||
void setPath(std::string _path);
|
||||
|
||||
// Crea el indice de ficheros de recursos
|
||||
bool setFileList();
|
||||
|
||||
|
||||
@@ -27,21 +27,6 @@ int main(int argc, char *args[])
|
||||
// Crea el objeto director
|
||||
Director *director = new Director(args[0]);
|
||||
|
||||
/* // Comprueba si existen todos los ficheros de recursos
|
||||
if (!director->initResourceList())
|
||||
return -1;
|
||||
|
||||
// Arranca SDL y crea la ventana
|
||||
if (!director->initSDL())
|
||||
return -1;
|
||||
|
||||
director->initGame();
|
||||
while (director->isRunning())
|
||||
{
|
||||
director->runGame();
|
||||
}
|
||||
director->quitGame(); */
|
||||
|
||||
// Bucle principal
|
||||
director->run();
|
||||
|
||||
|
||||
@@ -2,40 +2,23 @@
|
||||
#include "map.h"
|
||||
|
||||
// Constructor
|
||||
Map::Map(SDL_Renderer *renderer, LTexture *texture1, LTexture *texture2, LTexture *texture3, std::string file)
|
||||
Map::Map(SDL_Renderer *renderer, std::string file, Asset *asset)
|
||||
{
|
||||
init(renderer, texture1, texture2, texture3, file);
|
||||
}
|
||||
this->asset = asset;
|
||||
|
||||
// Destructor
|
||||
Map::~Map()
|
||||
{
|
||||
delete sprite_tile;
|
||||
delete sprite_actor;
|
||||
delete background;
|
||||
sprite_tile = nullptr;
|
||||
sprite_actor = nullptr;
|
||||
background = nullptr;
|
||||
texture_tile = new LTexture();
|
||||
texture_actor = new LTexture();
|
||||
texture_bg = new LTexture();
|
||||
|
||||
delete[] tile;
|
||||
delete[] actor;
|
||||
}
|
||||
loadTextureFromFile(texture_tile, asset->get("tiles_volcano.png"), renderer);
|
||||
loadTextureFromFile(texture_actor, asset->get("actors.png"), renderer);
|
||||
loadTextureFromFile(texture_bg, asset->get("bkg_surface.png"), renderer);
|
||||
|
||||
// Inicializa todas las variables
|
||||
void Map::init(SDL_Renderer *renderer, LTexture *texture1, LTexture *texture2, LTexture *texture3, std::string file)
|
||||
{
|
||||
sprite_tile = new AnimatedSprite();
|
||||
sprite_tile->setTexture(texture1);
|
||||
sprite_tile->setRenderer(renderer);
|
||||
sprite_tile = new AnimatedSprite(texture_tile, renderer);
|
||||
sprite_actor = new AnimatedSprite(texture_actor, renderer);
|
||||
|
||||
sprite_actor = new AnimatedSprite();
|
||||
sprite_actor->setTexture(texture2);
|
||||
sprite_actor->setRenderer(renderer);
|
||||
|
||||
background = new Sprite();
|
||||
background->setTexture(texture3);
|
||||
background->setRenderer(renderer);
|
||||
background->setSpriteClip(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
|
||||
const SDL_Rect rect = {0, 0, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT};
|
||||
background = new Sprite(rect, texture_bg, renderer);
|
||||
|
||||
src_rect = {0, 0, 0, 0};
|
||||
dst_rect = {0, 0, 0, 0};
|
||||
@@ -46,6 +29,33 @@ void Map::init(SDL_Renderer *renderer, LTexture *texture1, LTexture *texture2, L
|
||||
loadFromFile(file);
|
||||
}
|
||||
|
||||
// Destructor
|
||||
Map::~Map()
|
||||
{
|
||||
texture_tile->unload();
|
||||
delete texture_tile;
|
||||
texture_tile = nullptr;
|
||||
|
||||
texture_actor->unload();
|
||||
delete texture_actor;
|
||||
texture_actor = nullptr;
|
||||
|
||||
texture_bg->unload();
|
||||
delete texture_bg;
|
||||
texture_bg = nullptr;
|
||||
|
||||
delete sprite_tile;
|
||||
sprite_tile = nullptr;
|
||||
delete sprite_actor;
|
||||
|
||||
sprite_actor = nullptr;
|
||||
delete background;
|
||||
background = nullptr;
|
||||
|
||||
delete[] tile;
|
||||
delete[] actor;
|
||||
}
|
||||
|
||||
// Carga el mapa a partir de un fichero
|
||||
void Map::loadFromFile(std::string path)
|
||||
{
|
||||
@@ -74,16 +84,11 @@ void Map::loadFromFile(std::string path)
|
||||
|
||||
for (long i = 0; i < size; i++)
|
||||
SDL_RWread(file, &actor[i], sizeof(Uint8), 1);
|
||||
|
||||
|
||||
SDL_RWclose(file);
|
||||
}
|
||||
}
|
||||
|
||||
// Resetea ciertas variables
|
||||
void Map::reset()
|
||||
{
|
||||
}
|
||||
|
||||
// Actualiza todas las variables
|
||||
void Map::update()
|
||||
{
|
||||
|
||||
43
source/map.h
43
source/map.h
@@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
#include "animatedsprite.h"
|
||||
#include "asset.h"
|
||||
#include "jail_audio.h"
|
||||
#include "utils.h"
|
||||
|
||||
@@ -9,29 +10,11 @@
|
||||
// The player
|
||||
class Map
|
||||
{
|
||||
public:
|
||||
// Constructor
|
||||
Map(SDL_Renderer *renderer, LTexture *texture1, LTexture *texture2, LTexture *texture3, std::string file);
|
||||
|
||||
// Destructor
|
||||
~Map();
|
||||
|
||||
// Inicializa todas las variables
|
||||
void init(SDL_Renderer *renderer, LTexture *texture1, LTexture *texture2, LTexture *texture3, std::string file);
|
||||
|
||||
// Carga el mapa a partir de un fichero
|
||||
void loadFromFile(std::string file);
|
||||
|
||||
// Resetea ciertas variables
|
||||
void reset();
|
||||
|
||||
// Actualiza todas las variables
|
||||
void update();
|
||||
|
||||
// Dibuja el objeto
|
||||
void render();
|
||||
|
||||
private:
|
||||
LTexture *texture_tile; // Textura con los gráficos de los tiles
|
||||
LTexture *texture_actor; // Textura con los gráficos de los actores
|
||||
LTexture *texture_bg; // Textura con los gráficos de fondo
|
||||
Asset *asset; // Objeto con los ficheros de recursos
|
||||
AnimatedSprite *sprite_tile; // Sprite de los tiles del mapa
|
||||
AnimatedSprite *sprite_actor; // Sprite de los actores
|
||||
Sprite *background; // Fondo de la pantalla
|
||||
@@ -43,6 +26,22 @@ private:
|
||||
Uint8 h; // Altura en habitaciones del mapa
|
||||
Uint8 room; // Habitación actual del mapa
|
||||
std::string mapfile; // Ruta con el fichero del mapa
|
||||
|
||||
public:
|
||||
// Constructor
|
||||
Map(SDL_Renderer *renderer, std::string file, Asset *asset);
|
||||
|
||||
// Destructor
|
||||
~Map();
|
||||
|
||||
// Carga el mapa a partir de un fichero
|
||||
void loadFromFile(std::string file);
|
||||
|
||||
// Actualiza todas las variables
|
||||
void update();
|
||||
|
||||
// Dibuja el objeto
|
||||
void render();
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -18,7 +18,7 @@ Screen::Screen(SDL_Window *window, SDL_Renderer *renderer)
|
||||
mBorderColor = {0x27, 0x27, 0x36};
|
||||
|
||||
// Crea la textura donde se dibujan los graficos del juego
|
||||
mGameCanvas = SDL_CreateTexture(mRenderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT);
|
||||
mGameCanvas = SDL_CreateTexture(mRenderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, mGameCanvasWidth, mGameCanvasHeight);
|
||||
if (mGameCanvas == NULL)
|
||||
printf("TitleSurface could not be created!\nSDL Error: %s\n", SDL_GetError());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user