Trabajando en la precarga de habitaciones

This commit is contained in:
2022-10-30 13:27:30 +01:00
parent d1c27c4639
commit c11f5d2622
74 changed files with 1028 additions and 754 deletions

View File

@@ -25,7 +25,7 @@ animatedSprite_t loadAnimationFromFile(Texture *texture, std::string filePath)
// Si la linea contiene el texto [animation] se realiza el proceso de carga de una animación
if (line == "[animation]")
{
t_animation buffer;
animation_t buffer;
buffer.counter = 0;
buffer.currentFrame = 0;
buffer.completed = false;
@@ -361,7 +361,7 @@ animatedSprite_t AnimatedSprite::loadFromFile(std::string filePath)
// Si la linea contiene el texto [animation] se realiza el proceso de carga de una animación
if (line == "[animation]")
{
t_animation buffer;
animation_t buffer;
buffer.counter = 0;
buffer.currentFrame = 0;
buffer.completed = false;
@@ -505,7 +505,7 @@ bool AnimatedSprite::loadFromVector(std::vector<std::string> *source)
// Si la linea contiene el texto [animation] se realiza el proceso de carga de una animación
if (line == "[animation]")
{
t_animation buffer;
animation_t buffer;
buffer.counter = 0;
buffer.currentFrame = 0;
buffer.completed = false;

View File

@@ -11,7 +11,7 @@
#ifndef ANIMATEDSPRITE_H
#define ANIMATEDSPRITE_H
struct t_animation
struct animation_t
{
std::string name; // Nombre de la animacion
std::vector<SDL_Rect> frames; // Cada uno de los frames que componen la animación
@@ -24,7 +24,7 @@ struct t_animation
struct animatedSprite_t
{
std::vector<t_animation> animations; // Vector con las diferentes animaciones
std::vector<animation_t> animations; // Vector con las diferentes animaciones
Texture *texture; // Textura con los graficos para el sprite
};
@@ -35,7 +35,7 @@ class AnimatedSprite : public MovingSprite
{
private:
// Variables
std::vector<t_animation> animation; // Vector con las diferentes animaciones
std::vector<animation_t> animation; // Vector con las diferentes animaciones
int currentAnimation; // Animacion activa
public:

View File

@@ -14,7 +14,7 @@ void Resource::loadTextures(std::vector<std::string> list)
{
for (auto l : list)
{
texture_t t;
res_texture_t t;
t.name = l;
t.texture = new Texture(renderer, asset->get(l));
textures.push_back(t);
@@ -26,7 +26,7 @@ void Resource::loadAnimations(std::vector<std::string> list)
{
for (auto l : list)
{
animation_t as;
res_animation_t as;
as.name = l + ".ani";
as.animation = new animatedSprite_t(loadAnimationFromFile(getTexture(l + ".png"), asset->get(l + ".ani")));
animations.push_back(as);
@@ -38,13 +38,48 @@ void Resource::loadOffsets(std::vector<std::string> list)
{
for (auto l : list)
{
textOffset_t to;
res_textOffset_t to;
to.name = l;
to.textFile = new textFile_t(LoadTextFile(asset->get(l)));
offsets.push_back(to);
}
}
// Carga los mapas de tiles desde una lista
void Resource::loadTileMaps(std::vector<std::string> list)
{
for (auto l : list)
{
res_tileMap_t tm;
tm.name = l;
tm.tileMap = new std::vector<int>(loadRoomTileFile(asset->get(l)));
tileMaps.push_back(tm);
}
}
// Carga las habitaciones desde una lista
void Resource::loadRooms(std::vector<std::string> list)
{
for (auto l : list)
{
res_room_t r;
r.name = l;
r.room = new room_t(loadRoomFile(asset->get(l)));
r.room->tileMap = getTileMap(r.room->tileMapFile);
for (auto &e:r.room->enemies)
{
e.animation = getAnimation(e.animationString);
}
for (auto &i:r.room->items)
{
i.texture = getTexture(i.tileSetFile);
}
r.room->textureA = getTexture("standard.png");
r.room->textureB = getTexture("standard_zxarne.png");
rooms.push_back(r);
}
}
// Recarga las texturas
void Resource::reLoadTextures()
{
@@ -84,12 +119,34 @@ void Resource::freeOffsets()
offsets.clear();
}
// Libera los mapas de tiles
void Resource::freeTileMaps()
{
for (auto t : tileMaps)
{
delete t.tileMap;
}
tileMaps.clear();
}
// Libera las habitaciones
void Resource::freeRooms()
{
for (auto r : rooms)
{
delete r.room;
}
rooms.clear();
}
// Libera todos los recursos
void Resource::free()
{
freeTextures();
freeAnimations();
freeOffsets();
freeTileMaps();
freeRooms();
}
// Obtiene una textura
@@ -99,12 +156,11 @@ Texture *Resource::getTexture(std::string name)
{
if (texture.name.find(name) != std::string::npos)
{
std::cout << "CACHE: " << name << std::endl;
return texture.texture;
}
}
std::cout << "NOT FOUND: " << name << std::endl;
std::cout << "NOT FOUND ON CACHE: " << name << std::endl;
return nullptr;
}
@@ -115,12 +171,11 @@ animatedSprite_t *Resource::getAnimation(std::string name)
{
if (animation.name.find(name) != std::string::npos)
{
std::cout << "CACHE: " << name << std::endl;
return animation.animation;
}
}
std::cout << "NOT FOUND: " << name << std::endl;
std::cout << "NOT FOUND ON CACHE: " << name << std::endl;
return nullptr;
}
@@ -131,11 +186,40 @@ textFile_t *Resource::getOffset(std::string name)
{
if (offset.name.find(name) != std::string::npos)
{
std::cout << "CACHE: " << name << std::endl;
return offset.textFile;
}
}
std::cout << "NOT FOUND: " << name << std::endl;
std::cout << "NOT FOUND ON CACHE: " << name << std::endl;
return nullptr;
}
// Obtiene un mapa de tiles
std::vector<int> *Resource::getTileMap(std::string name)
{
for (auto tileMap : tileMaps)
{
if (tileMap.name.find(name) != std::string::npos)
{
return tileMap.tileMap;
}
}
std::cout << "NOT FOUND ON CACHE: " << name << std::endl;
return nullptr;
}
// Obtiene una habitacion
room_t *Resource::getRoom(std::string name)
{
for (auto room : rooms)
{
if (room.name.find(name) != std::string::npos)
{
return room.room;
}
}
std::cout << "NOT FOUND ON CACHE: " << name << std::endl;
return nullptr;
}

View File

@@ -3,6 +3,7 @@
#include <SDL2/SDL.h>
#include "animatedsprite.h"
#include "asset.h"
#include "../room.h"
#include "text.h"
#include "texture.h"
#include "utils.h"
@@ -12,24 +13,36 @@
#ifndef RESOURCE_H
#define RESOURCE_H
struct texture_t
struct res_texture_t
{
std::string name; // Nombre de la textura
Texture *texture; // La textura
};
struct animation_t
struct res_animation_t
{
std::string name; // Nombre de la textura
animatedSprite_t *animation; // La animación
};
struct textOffset_t
struct res_textOffset_t
{
std::string name; // Nombre del offeset
std::string name; // Nombre del offset
textFile_t *textFile; // Los offsets de la fuente
};
struct res_tileMap_t
{
std::string name; // Nombre del mapa de tiles
std::vector<int> *tileMap; // Vecor con los indices del mapa de tiles
};
struct res_room_t
{
std::string name; // Nombre de la habitación
room_t *room; // Vecor con las habitaciones
};
// Clase Resource. Almacena recursos de disco en memoria
class Resource
{
@@ -40,9 +53,11 @@ private:
options_t *options; // Puntero a las opciones del juego
// Variables
std::vector<texture_t> textures;
std::vector<animation_t> animations;
std::vector<textOffset_t> offsets;
std::vector<res_texture_t> textures;
std::vector<res_animation_t> animations;
std::vector<res_textOffset_t> offsets;
std::vector<res_tileMap_t> tileMaps;
std::vector<res_room_t> rooms;
public:
// Constructor
@@ -57,6 +72,12 @@ public:
// Carga los offsets desde una lista
void loadOffsets(std::vector<std::string> list);
// Carga los mapas de tiles desde una lista
void loadTileMaps(std::vector<std::string> list);
// Carga las habitaciones desde una lista
void loadRooms(std::vector<std::string> list);
// Recarga las texturas
void reLoadTextures();
@@ -69,6 +90,12 @@ public:
// Libera los offsets
void freeOffsets();
// Libera los mapas de tiles
void freeTileMaps();
// Libera las habitaciones
void freeRooms();
// Libera todos los recursos
void free();
@@ -80,6 +107,12 @@ public:
// Obtiene un offset
textFile_t *getOffset(std::string name);
// Obtiene un mapa de tiles
std::vector<int> *getTileMap(std::string name);
// Obtiene una habitacion
room_t *getRoom(std::string name);
};
#endif