2024-10-18 16:53:53 +02:00
12 changed files with 240 additions and 144 deletions

View File

@@ -16,18 +16,16 @@ AnimatedSprite::AnimatedSprite(std::shared_ptr<Texture> texture, const std::stri
{
animations_ = loadFromFile(file_path);
}
// setSpriteClip(animations_[current_animation_].frames[animations_[current_animation_].current_frame]);
}
AnimatedSprite::AnimatedSprite(std::shared_ptr<Texture> texture, std::vector<std::string> *animations)
AnimatedSprite::AnimatedSprite(std::shared_ptr<Texture> texture, const std::vector<std::string> &animations)
: MovingSprite(texture),
current_animation_(0)
{
if (animations)
if (!animations.empty())
{
loadFromVector(animations);
}
// setSpriteClip(animations_[current_animation_].frames[animations_[current_animation_].current_frame]);
}
// Constructor
@@ -178,6 +176,149 @@ SDL_Rect AnimatedSprite::getAnimationClip(int indexA, Uint8 indexF)
return animations_[indexA].frames[indexF];
}
// Carga la animación desde un vector
bool AnimatedSprite::loadFromVector(const std::vector<std::string> &source)
{
// Inicializa variables
auto frames_per_row = 0;
auto frame_width = 0;
auto frame_height = 0;
auto max_tiles = 0;
// Indicador de éxito en el proceso
auto success = true;
std::string line;
// Recorre todo el vector
auto index = 0;
while (index < (int)source.size())
{
// Lee desde el vector
line = source.at(index);
// Si la linea contiene el texto [animation] se realiza el proceso de carga de una animación
if (line == "[animation]")
{
Animation animation;
animation.counter = 0;
animation.current_frame = 0;
animation.completed = false;
animation.name.clear();
animation.speed = 5;
animation.loop = 0;
animation.frames.clear();
do
{
// Aumenta el indice para leer la siguiente linea
index++;
line = source.at(index);
// Encuentra la posición del caracter '='
int pos = line.find("=");
// Procesa las dos subcadenas
if (pos != static_cast<int>(line.npos))
{
if (line.substr(0, pos) == "name")
{
animation.name = line.substr(pos + 1, line.length());
}
else if (line.substr(0, pos) == "speed")
{
animation.speed = std::stoi(line.substr(pos + 1, line.length()));
}
else if (line.substr(0, pos) == "loop")
{
animation.loop = std::stoi(line.substr(pos + 1, line.length()));
}
else if (line.substr(0, pos) == "frames")
{
// Se introducen los valores separados por comas en un vector
std::stringstream ss(line.substr(pos + 1, line.length()));
std::string tmp;
SDL_Rect rect = {0, 0, frame_width, frame_height};
while (getline(ss, tmp, ','))
{
// Comprueba que el tile no sea mayor que el maximo indice permitido
const int num_tile = std::stoi(tmp) > max_tiles ? 0 : std::stoi(tmp);
rect.x = (num_tile % frames_per_row) * frame_width;
rect.y = (num_tile / frames_per_row) * frame_height;
animation.frames.push_back(rect);
}
}
else
{
std::cout << "Warning: unknown parameter " << line.substr(0, pos).c_str() << std::endl;
success = false;
}
}
} while (line != "[/animation]");
// Añade la animación al vector de animaciones
animations_.push_back(animation);
}
// En caso contrario se parsea el fichero para buscar las variables y los valores
else
{
// Encuentra la posición del caracter '='
int pos = line.find("=");
// Procesa las dos subcadenas
if (pos != (int)line.npos)
{
if (line.substr(0, pos) == "frames_per_row")
{
frames_per_row = std::stoi(line.substr(pos + 1, line.length()));
}
else if (line.substr(0, pos) == "frame_width")
{
frame_width = std::stoi(line.substr(pos + 1, line.length()));
}
else if (line.substr(0, pos) == "frame_height")
{
frame_height = std::stoi(line.substr(pos + 1, line.length()));
}
else
{
std::cout << "Warning: unknown parameter " << line.substr(0, pos).c_str() << std::endl;
success = false;
}
// Normaliza valores
if (frames_per_row == 0 && frame_width > 0)
{
frames_per_row = texture_->getWidth() / frame_width;
}
if (max_tiles == 0 && frame_width > 0 && frame_height > 0)
{
const int w = texture_->getWidth() / frame_width;
const int h = texture_->getHeight() / frame_height;
max_tiles = w * h;
}
}
}
// Una vez procesada la linea, aumenta el indice para pasar a la siguiente
index++;
}
// Pone un valor por defecto
setWidth(frame_width);
setHeight(frame_height);
return success;
}
// Establece la animacion actual
void AnimatedSprite::setCurrentAnimation(const std::string &name)
{
@@ -235,7 +376,7 @@ void AnimatedSprite::resetAnimation()
}
// Carga la animación desde un fichero
std::vector<Animation> AnimatedSprite::loadFromFile(std::string file_path)
std::vector<Animation> AnimatedSprite::loadFromFile(const std::string &file_path)
{
// Inicializa variables
std::vector<Animation> animations;

View File

@@ -32,15 +32,15 @@ protected:
void animate();
// Carga la animación desde un fichero
std::vector<Animation> loadFromFile(std::string filePath);
std::vector<Animation> loadFromFile(const std::string &file_path);
// Carga la animación desde un vector
bool loadFromVector(std::vector<std::string> *source);
bool loadFromVector(const std::vector<std::string> &source);
public:
// Constructor
AnimatedSprite(std::shared_ptr<Texture> texture, const std::string &file_path);
AnimatedSprite(std::shared_ptr<Texture> texture, std::vector<std::string> *animations);
AnimatedSprite(std::shared_ptr<Texture> texture, const std::vector<std::string> &animations);
explicit AnimatedSprite(std::shared_ptr<Texture> texture);
// Destructor

View File

@@ -7,7 +7,7 @@
#include "texture.h" // for Texture
// Constructor
Balloon::Balloon(float x, float y, Uint8 kind, float vel_x, float speed, Uint16 creation_timer, std::shared_ptr<Texture> texture, std::vector<std::string> *animation)
Balloon::Balloon(float x, float y, Uint8 kind, float vel_x, float speed, Uint16 creation_timer, std::shared_ptr<Texture> texture, const std::vector<std::string> &animation)
: sprite_(std::make_unique<AnimatedSprite>(texture, animation)),
pos_x_(x),
pos_y_(y),

View File

@@ -141,7 +141,7 @@ private:
public:
// Constructor
Balloon(float x, float y, Uint8 kind, float vel_x, float speed, Uint16 creation_timer, std::shared_ptr<Texture> texture, std::vector<std::string> *animation);
Balloon(float x, float y, Uint8 kind, float vel_x, float speed, Uint16 creation_timer, std::shared_ptr<Texture> texture, const std::vector<std::string> &animation);
// Destructor
~Balloon() = default;

View File

@@ -38,22 +38,17 @@ void Explosions::render()
}
// Añade texturas al objeto
void Explosions::addTexture(int size, std::shared_ptr<Texture> texture, std::vector<std::string> *animation)
void Explosions::addTexture(int size, std::shared_ptr<Texture> texture, std::vector<std::string> &animation)
{
ExplosionTexture temp;
temp.size = size;
temp.texture = texture;
temp.animation = animation;
textures_.push_back(temp);
textures_.emplace_back(ExplosionTexture(size, texture, animation));
}
// Añade una explosión
void Explosions::add(int x, int y, int size)
{
const int index = getIndexBySize(size);
auto sprite = std::make_unique<AnimatedSprite>(textures_[index].texture, textures_[index].animation);
sprite->setPos(x, y);
explosions_.push_back(std::move(sprite));
const auto index = getIndexBySize(size);
explosions_.emplace_back(std::make_unique<AnimatedSprite>(textures_[index].texture, textures_[index].animation));
explosions_.back()->setPos(x, y);
}
// Vacia el vector de elementos finalizados

View File

@@ -8,9 +8,13 @@ class Texture;
struct ExplosionTexture
{
std::shared_ptr<Texture> texture; // Textura para la explosión
std::vector<std::string> *animation; // Animación para la textura
int size; // Tamaño de la explosión
std::shared_ptr<Texture> texture; // Textura para la explosión
std::vector<std::string> animation; // Animación para la textura
// Constructor
ExplosionTexture(int sz, std::shared_ptr<Texture> tex, std::vector<std::string> anim)
: size(sz), texture(tex), animation(anim) {}
};
// Clase explosions
@@ -41,7 +45,7 @@ public:
void render();
// Añade texturas al objeto
void addTexture(int size, std::shared_ptr<Texture> texture, std::vector<std::string> *animation);
void addTexture(int size, std::shared_ptr<Texture> texture, std::vector<std::string> &animation);
// Añade una explosión
void add(int x, int y, int size);

View File

@@ -304,15 +304,18 @@ void Game::loadMedia()
// Limpia
{
player_animations_.clear();
balloon_animations_.clear();
item_animations_.clear();
player1_textures_.clear();
player2_textures_.clear();
item_textures_.clear();
// Texturas
game_text_textures_.clear();
balloon_textures_.clear();
explosions_textures_.clear();
game_text_textures_.clear();
item_textures_.clear();
player_textures_.clear();
// Animaciones
player_animations_.clear();
balloon_animations_.clear();
explosions_animations_.clear();
item_animations_.clear();
}
// Texturas
@@ -358,109 +361,64 @@ void Game::loadMedia()
// Texturas - Player1
{
player1_textures_.emplace_back(std::make_shared<Texture>(renderer_, asset_->get("player1.gif")));
player1_textures_.back()->addPalette(asset_->get("player1_pal1.gif"));
player1_textures_.back()->addPalette(asset_->get("player1_pal2.gif"));
player1_textures_.back()->addPalette(asset_->get("player1_pal3.gif"));
std::vector<std::shared_ptr<Texture>> player_texture;
player_texture.emplace_back(std::make_shared<Texture>(renderer_, asset_->get("player1.gif")));
player_texture.back()->addPalette(asset_->get("player1_pal1.gif"));
player_texture.back()->addPalette(asset_->get("player1_pal2.gif"));
player_texture.back()->addPalette(asset_->get("player1_pal3.gif"));
player1_textures_.emplace_back(std::make_shared<Texture>(renderer_, asset_->get("player_power.gif")));
player1_textures_.back()->addPalette(asset_->get("player_power_pal.gif"));
player_texture.emplace_back(std::make_shared<Texture>(renderer_, asset_->get("player_power.gif")));
player_texture.back()->addPalette(asset_->get("player_power_pal.gif"));
player_textures_.push_back(player1_textures_);
player_textures_.push_back(player_texture);
}
// Texturas - Player2
{
player2_textures_.emplace_back(std::make_shared<Texture>(renderer_, asset_->get("player2.gif")));
player2_textures_.back()->addPalette(asset_->get("player2_pal1.gif"));
player2_textures_.back()->addPalette(asset_->get("player2_pal2.gif"));
player2_textures_.back()->addPalette(asset_->get("player2_pal3.gif"));
std::vector<std::shared_ptr<Texture>> player_texture;
player_texture.emplace_back(std::make_shared<Texture>(renderer_, asset_->get("player2.gif")));
player_texture.back()->addPalette(asset_->get("player2_pal1.gif"));
player_texture.back()->addPalette(asset_->get("player2_pal2.gif"));
player_texture.back()->addPalette(asset_->get("player2_pal3.gif"));
player2_textures_.emplace_back(std::make_shared<Texture>(renderer_, asset_->get("player_power.gif")));
player2_textures_.back()->addPalette(asset_->get("player_power_pal.gif"));
player2_textures_.back()->setPalette(1);
player_texture.emplace_back(std::make_shared<Texture>(renderer_, asset_->get("player_power.gif")));
player_texture.back()->addPalette(asset_->get("player_power_pal.gif"));
player_texture.back()->setPalette(1);
player_textures_.push_back(player2_textures_);
player_textures_.push_back(player_texture);
}
// Animaciones -- Jugador
{
std::vector<std::string> *player_animations = new std::vector<std::string>;
loadAnimations(asset_->get("player.ani"), player_animations);
player_animations_.push_back(player_animations);
std::vector<std::string> *player_power_animations = new std::vector<std::string>;
loadAnimations(asset_->get("player_power.ani"), player_power_animations);
player_animations_.push_back(player_power_animations);
player_animations_.emplace_back(loadAnimations(asset_->get("player.ani")));
player_animations_.emplace_back(loadAnimations(asset_->get("player_power.ani")));
}
// Animaciones -- Globos
{
std::vector<std::string> *balloon1_animations = new std::vector<std::string>;
loadAnimations(asset_->get("balloon1.ani"), balloon1_animations);
balloon_animations_.push_back(balloon1_animations);
std::vector<std::string> *balloon2_animations = new std::vector<std::string>;
loadAnimations(asset_->get("balloon2.ani"), balloon2_animations);
balloon_animations_.push_back(balloon2_animations);
std::vector<std::string> *balloon3_animations = new std::vector<std::string>;
loadAnimations(asset_->get("balloon3.ani"), balloon3_animations);
balloon_animations_.push_back(balloon3_animations);
std::vector<std::string> *balloon4_animations = new std::vector<std::string>;
loadAnimations(asset_->get("balloon4.ani"), balloon4_animations);
balloon_animations_.push_back(balloon4_animations);
std::vector<std::string> *balloon5_animations = new std::vector<std::string>;
loadAnimations(asset_->get("powerball.ani"), balloon5_animations);
balloon_animations_.push_back(balloon5_animations);
balloon_animations_.emplace_back(loadAnimations(asset_->get("balloon1.ani")));
balloon_animations_.emplace_back(loadAnimations(asset_->get("balloon2.ani")));
balloon_animations_.emplace_back(loadAnimations(asset_->get("balloon3.ani")));
balloon_animations_.emplace_back(loadAnimations(asset_->get("balloon4.ani")));
balloon_animations_.emplace_back(loadAnimations(asset_->get("powerball.ani")));
}
// Animaciones -- Explosiones
{
std::vector<std::string> *explosions1_animations = new std::vector<std::string>;
loadAnimations(asset_->get("explosion1.ani"), explosions1_animations);
explosions_animations_.push_back(explosions1_animations);
std::vector<std::string> *explosions2_animations = new std::vector<std::string>;
loadAnimations(asset_->get("explosion2.ani"), explosions2_animations);
explosions_animations_.push_back(explosions2_animations);
std::vector<std::string> *explosions3_animations = new std::vector<std::string>;
loadAnimations(asset_->get("explosion3.ani"), explosions3_animations);
explosions_animations_.push_back(explosions3_animations);
std::vector<std::string> *explosions4_animations = new std::vector<std::string>;
loadAnimations(asset_->get("explosion4.ani"), explosions4_animations);
explosions_animations_.push_back(explosions4_animations);
explosions_animations_.emplace_back(loadAnimations(asset_->get("explosion1.ani")));
explosions_animations_.emplace_back(loadAnimations(asset_->get("explosion2.ani")));
explosions_animations_.emplace_back(loadAnimations(asset_->get("explosion3.ani")));
explosions_animations_.emplace_back(loadAnimations(asset_->get("explosion4.ani")));
}
// Animaciones -- Items
{
std::vector<std::string> *item1_animations = new std::vector<std::string>;
loadAnimations(asset_->get("item_points1_disk.ani"), item1_animations);
item_animations_.push_back(item1_animations);
std::vector<std::string> *item2_animations = new std::vector<std::string>;
loadAnimations(asset_->get("item_points2_gavina.ani"), item2_animations);
item_animations_.push_back(item2_animations);
std::vector<std::string> *item3_animations = new std::vector<std::string>;
loadAnimations(asset_->get("item_points3_pacmar.ani"), item3_animations);
item_animations_.push_back(item3_animations);
std::vector<std::string> *item4_animations = new std::vector<std::string>;
loadAnimations(asset_->get("item_clock.ani"), item4_animations);
item_animations_.push_back(item4_animations);
std::vector<std::string> *item5_animations = new std::vector<std::string>;
loadAnimations(asset_->get("item_coffee.ani"), item5_animations);
item_animations_.push_back(item5_animations);
std::vector<std::string> *item6_animations = new std::vector<std::string>;
loadAnimations(asset_->get("item_coffee_machine.ani"), item6_animations);
item_animations_.push_back(item6_animations);
item_animations_.emplace_back(loadAnimations(asset_->get("item_points1_disk.ani")));
item_animations_.emplace_back(loadAnimations(asset_->get("item_points2_gavina.ani")));
item_animations_.emplace_back(loadAnimations(asset_->get("item_points3_pacmar.ani")));
item_animations_.emplace_back(loadAnimations(asset_->get("item_clock.ani")));
item_animations_.emplace_back(loadAnimations(asset_->get("item_coffee.ani")));
item_animations_.emplace_back(loadAnimations(asset_->get("item_coffee_machine.ani")));
}
// Texto
@@ -498,11 +456,11 @@ void Game::loadMedia()
void Game::unloadMedia()
{
// Texturas
player1_textures_.clear();
player2_textures_.clear();
item_textures_.clear();
game_text_textures_.clear();
balloon_textures_.clear();
explosions_textures_.clear();
item_textures_.clear();
player_textures_.clear();
// Animaciones
player_animations_.clear();
@@ -2158,21 +2116,24 @@ void Game::checkEvents()
}
// Carga las animaciones
void Game::loadAnimations(std::string filePath, std::vector<std::string> *buffer)
std::vector<std::string> Game::loadAnimations(const std::string &file_path)
{
std::ifstream file(filePath);
std::vector<std::string> buffer;
std::ifstream file(file_path);
if (!file)
{
std::cerr << "Error: no se pudo abrir el archivo " << file_path << std::endl;
return buffer;
}
std::string line;
if (file)
{
std::cout << "Animation loaded: " << filePath.substr(filePath.find_last_of("\\/") + 1).c_str() << std::endl;
std::cout << "Animation loaded: " << file_path.substr(file_path.find_last_of("\\/") + 1) << std::endl;
while (std::getline(file, line))
{
buffer->push_back(line);
}
file.close();
buffer.push_back(line);
}
return buffer;
}
// Elimina todos los objetos contenidos en vectores
@@ -2198,14 +2159,12 @@ void Game::reloadTextures()
texture->reLoad();
}
for (auto &texture : player1_textures_)
for (auto &textures : player_textures_)
{
for (auto &texture : textures)
{
texture->reLoad();
}
for (auto &texture : player2_textures_)
{
texture->reLoad();
}
for (auto &texture : game_text_textures_)

View File

@@ -129,17 +129,14 @@ private:
std::vector<std::shared_ptr<Texture>> item_textures_; // Vector con las texturas de los items
std::vector<std::shared_ptr<Texture>> balloon_textures_; // Vector con las texturas de los globos
std::vector<std::shared_ptr<Texture>> explosions_textures_; // Vector con las texturas de las explosiones
std::vector<std::shared_ptr<Texture>> player1_textures_; // Vector con las texturas del jugador
std::vector<std::shared_ptr<Texture>> player2_textures_; // Vector con las texturas del jugador
std::vector<std::vector<std::shared_ptr<Texture>>> player_textures_; // Vector con todas las texturas de los jugadores;
std::vector<std::shared_ptr<Texture>> game_text_textures_; // Vector con las texturas para los sprites con textos
// std::vector<std::shared_ptr<SpriteSmart>> game_text_sprites_; // Sprite con el textos que aparecen al coger items
std::vector<std::vector<std::string> *> item_animations_; // Vector con las animaciones de los items
std::vector<std::vector<std::string> *> player_animations_; // Vector con las animaciones del jugador
std::vector<std::vector<std::string> *> balloon_animations_; // Vector con las animaciones de los globos
std::vector<std::vector<std::string> *> explosions_animations_; // Vector con las animaciones de las explosiones
std::vector<std::vector<std::string>> item_animations_; // Vector con las animaciones de los items
std::vector<std::vector<std::string>> player_animations_; // Vector con las animaciones del jugador
std::vector<std::vector<std::string>> balloon_animations_; // Vector con las animaciones de los globos
std::vector<std::vector<std::string>> explosions_animations_; // Vector con las animaciones de las explosiones
std::unique_ptr<Text> text_; // Fuente para los textos del juego
std::unique_ptr<Text> text_big_; // Fuente de texto grande
@@ -403,7 +400,7 @@ private:
bool allPlayersAreNotPlaying();
// Carga las animaciones
void loadAnimations(std::string file_path, std::vector<std::string> *buffer);
std::vector<std::string> loadAnimations(const std::string &file_path);
// Elimina todos los objetos contenidos en vectores
void deleteAllVectorObjects();

View File

@@ -5,7 +5,7 @@
class Texture;
// Constructor
Item::Item(ItemType type, float x, float y, SDL_Rect *play_area, std::shared_ptr<Texture> texture, std::vector<std::string> *animation)
Item::Item(ItemType type, float x, float y, SDL_Rect *play_area, std::shared_ptr<Texture> texture, const std::vector<std::string> &animation)
: sprite_(std::make_unique<AnimatedSprite>(texture, animation)),
accel_x_(0.0f),
floor_collision_(false),

View File

@@ -58,7 +58,7 @@ private:
public:
// Constructor
Item(ItemType type, float x, float y, SDL_Rect *play_area, std::shared_ptr<Texture> texture, std::vector<std::string> *animation);
Item(ItemType type, float x, float y, SDL_Rect *play_area, std::shared_ptr<Texture> texture, const std::vector<std::string> &animation);
// Destructor
~Item() = default;

View File

@@ -11,7 +11,7 @@
#include "options.h"
// Constructor
Player::Player(int id, float x, int y, bool demo, SDL_Rect *play_area, std::vector<std::shared_ptr<Texture>> texture, std::vector<std::vector<std::string> *> animations)
Player::Player(int id, float x, int y, bool demo, SDL_Rect *play_area, std::vector<std::shared_ptr<Texture>> texture, const std::vector<std::vector<std::string>> &animations)
: player_sprite_(std::make_unique<AnimatedSprite>(texture[0], animations[0])),
power_sprite_(std::make_unique<AnimatedSprite>(texture[1], animations[1])),
enter_name_(std::make_unique<EnterName>()),

View File

@@ -113,7 +113,7 @@ private:
public:
// Constructor
Player(int id, float x, int y, bool demo, SDL_Rect *play_area, std::vector<std::shared_ptr<Texture>> texture, std::vector<std::vector<std::string> *> animations);
Player(int id, float x, int y, bool demo, SDL_Rect *play_area, std::vector<std::shared_ptr<Texture>> texture, const std::vector<std::vector<std::string>> &animations);
// Destructor
~Player() = default;