From 02312e1342f4f1556db9aefa5eae2ca42240b3db Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Wed, 5 Oct 2022 20:48:55 +0200 Subject: [PATCH] Ya precarga todos los recursos del juego --- source/balloon.cpp | 4 +- source/balloon.h | 2 +- source/game.cpp | 169 +++++++++++++++++---------------------------- source/game.h | 29 +++----- source/item.cpp | 44 +----------- source/item.h | 3 +- source/player.cpp | 33 +++------ source/player.h | 16 ++--- 8 files changed, 94 insertions(+), 206 deletions(-) diff --git a/source/balloon.cpp b/source/balloon.cpp index c5941ea..fafcab2 100644 --- a/source/balloon.cpp +++ b/source/balloon.cpp @@ -2,9 +2,9 @@ #include "balloon.h" // Constructor -Balloon::Balloon(float x, float y, Uint8 kind, float velx, float speed, Uint16 creationtimer, LTexture *texture, std::vector *buffer, SDL_Renderer *renderer) +Balloon::Balloon(float x, float y, Uint8 kind, float velx, float speed, Uint16 creationtimer, LTexture *texture, std::vector *animation, SDL_Renderer *renderer) { - sprite = new AnimatedSprite(texture, renderer, "", buffer); + sprite = new AnimatedSprite(texture, renderer, "", animation); disable(); enabled = true; diff --git a/source/balloon.h b/source/balloon.h index f093918..c4a264e 100644 --- a/source/balloon.h +++ b/source/balloon.h @@ -143,7 +143,7 @@ private: public: // Constructor - Balloon(float x, float y, Uint8 kind, float velx, float speed, Uint16 creationtimer, LTexture *texture, std::vector *buffer, SDL_Renderer *renderer); + Balloon(float x, float y, Uint8 kind, float velx, float speed, Uint16 creationtimer, LTexture *texture, std::vector *animation, SDL_Renderer *renderer); // Destructor ~Balloon(); diff --git a/source/game.cpp b/source/game.cpp index fb01c39..174c903 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -14,7 +14,7 @@ Game::Game(int numPlayers, int currentStage, SDL_Renderer *renderer, Screen *scr // Pasa variables this->demo.enabled = demo; this->numPlayers = numPlayers; - this->currentStage = 5; // currentStage; + this->currentStage = currentStage; lastStageReached = currentStage; if (numPlayers == 1) { // Si solo juega un jugador, permite jugar tanto con teclado como con mando @@ -27,9 +27,9 @@ Game::Game(int numPlayers, int currentStage, SDL_Renderer *renderer, Screen *scr fade = new Fade(renderer); eventHandler = new SDL_Event(); - std::vector *> *itemAnimations; - std::vector *> *playerAnimations; - std::vector *> *balloonAnimations; + // std::vector *> *itemAnimations = new std::vector *>; + // std::vector *> *playerAnimations = new std::vector *>; + // std::vector *> *balloonAnimations = new std::vector *>; // Carga los recursos loadMedia(); @@ -67,18 +67,6 @@ Game::~Game() // Elimina todos los objetos contenidos en vectores deleteAllVectorObjects(); - balloon1Texture->unload(); - delete balloon1Texture; - - balloon2Texture->unload(); - delete balloon2Texture; - - balloon3Texture->unload(); - delete balloon3Texture; - - balloon4Texture->unload(); - delete balloon4Texture; - bulletTexture->unload(); delete bulletTexture; @@ -103,13 +91,7 @@ Game::~Game() itemTexture->unload(); delete itemTexture; - for (auto texture : itemsTexture) - { - texture->unload(); - delete texture; - } - itemsTexture.clear(); - + // Animaciones for (auto animation : playerAnimations) { delete animation; @@ -128,19 +110,34 @@ Game::~Game() } itemAnimations.clear(); - for (auto texture : player1Texture) + // Texturas + for (auto texture : player1Textures) { texture->unload(); delete texture; } - player1Texture.clear(); + player1Textures.clear(); - for (auto texture : player2Texture) + for (auto texture : player2Textures) { texture->unload(); delete texture; } - player2Texture.clear(); + player2Textures.clear(); + + for (auto texture : itemTextures) + { + texture->unload(); + delete texture; + } + itemTextures.clear(); + + for (auto texture : balloonTextures) + { + texture->unload(); + delete texture; + } + balloonTextures.clear(); delete text; delete textBig; @@ -198,14 +195,14 @@ void Game::init() if (numPlayers == 1) { - Player *player = new Player(renderer, asset, PLAY_AREA_CENTER_X - 11, PLAY_AREA_BOTTOM - 24); + Player *player = new Player(PLAY_AREA_CENTER_X - 11, PLAY_AREA_BOTTOM - 24, renderer, player2Textures, playerAnimations); players.push_back(player); } else if (numPlayers == 2) { - Player *player1 = new Player(renderer, asset, (PLAY_AREA_CENTER_FIRST_QUARTER_X * ((0 * 2) + 1)) - 11, PLAY_AREA_BOTTOM - 24); - Player *player2 = new Player(renderer, asset, (PLAY_AREA_CENTER_FIRST_QUARTER_X * ((1 * 2) + 1)) - 11, PLAY_AREA_BOTTOM - 24); + Player *player1 = new Player((PLAY_AREA_CENTER_FIRST_QUARTER_X * ((0 * 2) + 1)) - 11, PLAY_AREA_BOTTOM - 24, renderer, player1Textures, playerAnimations); + Player *player2 = new Player((PLAY_AREA_CENTER_FIRST_QUARTER_X * ((1 * 2) + 1)) - 11, PLAY_AREA_BOTTOM - 24, renderer, player2Textures, playerAnimations); players.push_back(player1); players.push_back(player2); } @@ -372,10 +369,6 @@ void Game::loadMedia() loadDemoFile(); // Texturas - balloon1Texture = new LTexture(renderer, asset->get("balloon1.png")); - balloon2Texture = new LTexture(renderer, asset->get("balloon2.png")); - balloon3Texture = new LTexture(renderer, asset->get("balloon3.png")); - balloon4Texture = new LTexture(renderer, asset->get("balloon4.png")); bulletTexture = new LTexture(renderer, asset->get("bullet.png")); gameBuildingsTexture = new LTexture(renderer, asset->get("game_buildings.png")); gameCloudsTexture = new LTexture(renderer, asset->get("game_clouds.png")); @@ -385,47 +378,63 @@ void Game::loadMedia() gameTextTexture = new LTexture(renderer, asset->get("game_text.png")); itemTexture = new LTexture(renderer, asset->get("items.png")); + // Texturas - Globos + LTexture *balloon1Texture = new LTexture(renderer, asset->get("balloon1.png")); + balloonTextures.push_back(balloon1Texture); + + LTexture *balloon2Texture = new LTexture(renderer, asset->get("balloon2.png")); + balloonTextures.push_back(balloon2Texture); + + LTexture *balloon3Texture = new LTexture(renderer, asset->get("balloon3.png")); + balloonTextures.push_back(balloon3Texture); + + LTexture *balloon4Texture = new LTexture(renderer, asset->get("balloon4.png")); + balloonTextures.push_back(balloon4Texture); + + // Texturas - Items LTexture *item1 = new LTexture(renderer, asset->get("item_points1_disk.png")); - itemsTexture.push_back(item1); + itemTextures.push_back(item1); LTexture *item2 = new LTexture(renderer, asset->get("item_points2_gavina.png")); - itemsTexture.push_back(item2); + itemTextures.push_back(item2); LTexture *item3 = new LTexture(renderer, asset->get("item_points3_pacmar.png")); - itemsTexture.push_back(item3); + itemTextures.push_back(item3); LTexture *item4 = new LTexture(renderer, asset->get("item_clock.png")); - itemsTexture.push_back(item4); + itemTextures.push_back(item4); LTexture *item5 = new LTexture(renderer, asset->get("item_coffee.png")); - itemsTexture.push_back(item5); + itemTextures.push_back(item5); LTexture *item6 = new LTexture(renderer, asset->get("item_coffee_machine.png")); - itemsTexture.push_back(item6); + itemTextures.push_back(item6); + // Texturas - Player1 LTexture *player1Head = new LTexture(renderer, asset->get("player_bal1_head.png")); - player1Texture.push_back(player1Head); + player1Textures.push_back(player1Head); LTexture *player1Body = new LTexture(renderer, asset->get("player_bal1_body.png")); - player1Texture.push_back(player1Body); + player1Textures.push_back(player1Body); LTexture *player1Legs = new LTexture(renderer, asset->get("player_bal1_legs.png")); - player1Texture.push_back(player1Legs); + player1Textures.push_back(player1Legs); LTexture *player1Death = new LTexture(renderer, asset->get("player_bal1_death.png")); - player1Texture.push_back(player1Death); + player1Textures.push_back(player1Death); + // Texturas - Player2 LTexture *player2Head = new LTexture(renderer, asset->get("player_arounder_head.png")); - player2Texture.push_back(player2Head); + player2Textures.push_back(player2Head); LTexture *player2Body = new LTexture(renderer, asset->get("player_arounder_body.png")); - player2Texture.push_back(player2Body); + player2Textures.push_back(player2Body); LTexture *player2Legs = new LTexture(renderer, asset->get("player_arounder_legs.png")); - player2Texture.push_back(player2Legs); + player2Textures.push_back(player2Legs); LTexture *player2Death = new LTexture(renderer, asset->get("player_arounder_death.png")); - player2Texture.push_back(player2Death); + player2Textures.push_back(player2Death); // Animaciones -- Jugador std::vector *playerHeadAnimation = new std::vector; @@ -1828,7 +1837,8 @@ void Game::renderBalloons() // Crea un globo nuevo en el vector de globos Uint8 Game::createBalloon(float x, int y, Uint8 kind, float velx, float speed, Uint16 creationtimer) { - Balloon *b = new Balloon(x, y, kind, velx, speed, creationtimer, balloonTexture(kind), balloonStreamAnimation(kind), renderer); + const int index = (kind - 1) % 4; + Balloon *b = new Balloon(x, y, kind, velx, speed, creationtimer, balloonTextures.at(index), balloonAnimations.at(index), renderer); balloons.push_back(b); return (Uint8)(balloons.size() - 1); } @@ -1846,7 +1856,7 @@ void Game::createPowerBall() const int x[3] = {left, center, right}; const float vx[3] = {BALLOON_VELX_POSITIVE, BALLOON_VELX_POSITIVE, BALLOON_VELX_NEGATIVE}; - Balloon *b = new Balloon(x[luck], posY, POWER_BALL, vx[luck], enemySpeed, 100, balloon4Texture, balloon4Animation, renderer); + Balloon *b = new Balloon(x[luck], posY, POWER_BALL, vx[luck], enemySpeed, 100, balloonTextures.at(3), balloonAnimations.at(3), renderer); balloons.push_back(b); powerBallEnabled = true; @@ -1857,8 +1867,6 @@ void Game::createPowerBall() void Game::setBalloonSpeed(float speed) { for (auto balloon : balloons) - - // for (int i = 0; i < MAX_BALLOONS; i++) { if (balloon->isEnabled()) { @@ -2130,58 +2138,6 @@ Uint8 Game::countBalloons() return num; } -// Obtiene la textura correspondiente en funcion del tipo -LTexture *Game::balloonTexture(int kind) -{ - if (kind == 1 || kind == 5) - { - return balloon1Texture; - } - - else if (kind == 2 || kind == 6) - { - return balloon2Texture; - } - - else if (kind == 3 || kind == 7) - { - return balloon3Texture; - } - - else if (kind == 4 || kind == 8 || kind == 9) - { - return balloon4Texture; - } - - return balloon1Texture; -} - -// Obtiene la animacion correspondiente en funcion del tipo -std::vector *Game::balloonStreamAnimation(int kind) -{ - if (kind == 1 || kind == 5) - { - return balloon1Animation; - } - - else if (kind == 2 || kind == 6) - { - return balloon2Animation; - } - - else if (kind == 3 || kind == 7) - { - return balloon3Animation; - } - - else if (kind == 4 || kind == 8 || kind == 9) - { - return balloon4Animation; - } - - return balloon1Animation; -} - // Vacia el vector de globos void Game::freeBalloons() { @@ -2489,9 +2445,10 @@ Uint8 Game::dropItem() } // Crea un objeto item -void Game::createItem(Uint8 type, float x, float y) +void Game::createItem(Uint8 kind, float x, float y) { - Item *item = new Item(renderer, asset, type, x, y); + const int index = kind - 1; + Item *item = new Item(kind, x, y, itemTextures.at(index), itemAnimations.at(index), renderer); items.push_back(item); } diff --git a/source/game.h b/source/game.h index 22d7e83..9acfdaf 100644 --- a/source/game.h +++ b/source/game.h @@ -125,14 +125,11 @@ private: std::vector items; // Vector con los items std::vector smartSprites; // Vector con los smartsprites - LTexture *balloon1Texture; // Textura para los globos - LTexture *balloon2Texture; // Textura para los globos - LTexture *balloon3Texture; // Textura para los globos - LTexture *balloon4Texture; // Textura para los globos - LTexture *bulletTexture; // Textura para las balas - std::vector itemsTexture; // Vector con las texturas de los items - std::vector player1Texture; // Vector con las texturas del jugador - std::vector player2Texture; // Vector con las texturas del jugador + LTexture *bulletTexture; // Textura para las balas + std::vector itemTextures; // Vector con las texturas de los items + std::vector balloonTextures; // Vector con las texturas de los globos + std::vector player1Textures; // Vector con las texturas del jugador + std::vector player2Textures; // Vector con las texturas del jugador LTexture *gameBuildingsTexture; // Textura con los edificios de fondo LTexture *gameCloudsTexture; // Textura con las nubes de fondo @@ -143,14 +140,14 @@ private: LTexture *itemTexture; // Textura para los items - std::vector *> *itemAnimations; // Vector con las animaciones de los items - std::vector *> *playerAnimations; // Vector con las animaciones del jugador - std::vector *> *balloonAnimations; // Vector con las animaciones de los globos + std::vector *> itemAnimations; // Vector con las animaciones de los items + std::vector *> playerAnimations; // Vector con las animaciones del jugador + std::vector *> balloonAnimations; // Vector con las animaciones de los globos Text *text; // Fuente para los textos del juego Text *textBig; // Fuente de texto grande Text *textScoreBoard; // Fuente para el marcador del juego - Text *textNokia2; // Otra fuente de texto para mesajes + Text *textNokia2; // Otra fuente de texto para mensajes Text *textNokiaBig2; // Y la versión en grande Menu *gameOverMenu; // Menú de la pantalla de game over @@ -348,12 +345,6 @@ private: // Obtiene el numero de globos activos Uint8 countBalloons(); - // Obtiene la textura correspondiente en funcion del tipo - LTexture *balloonTexture(int kind); - - // Obtiene la animacion correspondiente en funcion del tipo - std::vector *balloonStreamAnimation(int kind); - // Vacia el vector de globos void freeBalloons(); @@ -388,7 +379,7 @@ private: Uint8 dropItem(); // Crea un objeto item - void createItem(Uint8 type, float x, float y); + void createItem(Uint8 kind, float x, float y); // Vacia el vector de items void freeItems(); diff --git a/source/item.cpp b/source/item.cpp index 634704f..a6a468c 100644 --- a/source/item.cpp +++ b/source/item.cpp @@ -2,48 +2,9 @@ #include "item.h" // Constructor -Item::Item(SDL_Renderer *renderer, Asset *asset, Uint8 kind, float x, float y) +Item::Item(Uint8 kind, float x, float y, LTexture *texture, std::vector *animation, SDL_Renderer *renderer) { - std::string filePNG; - std::string fileANI; - switch (kind) - { - case ITEM_POINTS_1_DISK: - filePNG = asset->get("item_points1_disk.png"); - fileANI = asset->get("item_points1_disk.ani"); - break; - - case ITEM_POINTS_2_GAVINA: - filePNG = asset->get("item_points2_gavina.png"); - fileANI = asset->get("item_points2_gavina.ani"); - break; - - case ITEM_POINTS_3_PACMAR: - filePNG = asset->get("item_points3_pacmar.png"); - fileANI = asset->get("item_points3_pacmar.ani"); - break; - - case ITEM_CLOCK: - filePNG = asset->get("item_clock.png"); - fileANI = asset->get("item_clock.ani"); - break; - - case ITEM_COFFEE: - filePNG = asset->get("item_coffee.png"); - fileANI = asset->get("item_coffee.ani"); - break; - - case ITEM_COFFEE_MACHINE: - filePNG = asset->get("item_coffee_machine.png"); - fileANI = asset->get("item_coffee_machine.ani"); - break; - - default: - break; - } - - texture = new LTexture(renderer, filePNG); - sprite = new AnimatedSprite(texture, renderer, fileANI); + sprite = new AnimatedSprite(texture, renderer, "", animation); this->kind = kind; enabled = true; @@ -82,7 +43,6 @@ Item::Item(SDL_Renderer *renderer, Asset *asset, Uint8 kind, float x, float y) // Destructor Item::~Item() { - delete texture; delete sprite; } diff --git a/source/item.h b/source/item.h index 1609f23..daefd32 100644 --- a/source/item.h +++ b/source/item.h @@ -22,7 +22,6 @@ class Item private: // Objetos AnimatedSprite *sprite; // Sprite con los graficos del objeto - LTexture *texture; // Objeto con los graficos del item // Variables float posX; // Posición X del objeto @@ -47,7 +46,7 @@ public: Uint16 timeToLive; // Temporizador con el tiempo que el objeto está presente // Constructor - Item(SDL_Renderer *renderer, Asset *asset, Uint8 kind, float x, float y); + Item(Uint8 kind, float x, float y, LTexture *texture, std::vector *animation, SDL_Renderer *renderer); // Destructor ~Item(); diff --git a/source/player.cpp b/source/player.cpp index 9597fbf..268a8c5 100644 --- a/source/player.cpp +++ b/source/player.cpp @@ -2,20 +2,16 @@ #include "player.h" // Constructor -Player::Player(float x, int y, SDL_Renderer *renderer, std::vector texture) +Player::Player(float x, int y, SDL_Renderer *renderer, std::vector texture, std::vector*> animations) { // Copia los punteros this->renderer = renderer; - this->asset = asset; // Reserva memoria para los objetos - legsTexture = new LTexture(renderer, asset->get("new_player1_legs.png")); - bodyTexture = new LTexture(renderer, asset->get("new_player1_body.png")); - headTexture = new LTexture(renderer, asset->get("new_player1_head.png")); - deathTexture = new LTexture(renderer, asset->get("new_player1_death.png")); - legsSprite = new AnimatedSprite(legsTexture, renderer, asset->get("new_player1_legs.ani")); - bodySprite = new AnimatedSprite(bodyTexture, renderer, asset->get("new_player1_body.ani")); - headSprite = new AnimatedSprite(headTexture, renderer, asset->get("new_player1_head.ani")); + headSprite = new AnimatedSprite(texture.at(0), renderer, "", animations.at(0)); + bodySprite = new AnimatedSprite(texture.at(1), renderer, "", animations.at(1)); + legsSprite = new AnimatedSprite(texture.at(2), renderer, "", animations.at(2)); + deathSprite = new AnimatedSprite(texture.at(3), renderer, "", animations.at(3)); // Establece la posición inicial del jugador posX = x; @@ -27,21 +23,10 @@ Player::Player(float x, int y, SDL_Renderer *renderer, std::vector t // Destructor Player::~Player() { - legsTexture->unload(); - delete legsTexture; - - bodyTexture->unload(); - delete bodyTexture; - - headTexture->unload(); - delete headTexture; - - deathTexture->unload(); - delete deathTexture; - - delete legsSprite; - delete bodySprite; delete headSprite; + delete bodySprite; + delete legsSprite; + delete deathSprite; } // Iniciador @@ -549,7 +534,7 @@ void Player::shiftColliders() // Obtiene el puntero a la textura con los gráficos de la animación de morir LTexture *Player::getDeadTexture() { - return deathTexture; + //return deathTexture; } // Obtiene el valor de la variable diff --git a/source/player.h b/source/player.h index 32c7cdd..ebccf74 100644 --- a/source/player.h +++ b/source/player.h @@ -50,15 +50,11 @@ class Player { private: // Objetos - SDL_Renderer *renderer; // El renderizador de la ventana - Asset *asset; // Objeto que gestiona todos los ficheros de recursos - LTexture *headTexture; // Textura para la cabeza del jugador - LTexture *bodyTexture; // Textura para el cuerpo del jugador - LTexture *deathTexture; // Textura para la animación de muerte del jugador - LTexture *legsTexture; // Textura para las piernas del jugador - AnimatedSprite *legsSprite; // Sprite para dibujar las piernas - AnimatedSprite *bodySprite; // Sprite para dibujar el cuerpo - AnimatedSprite *headSprite; // Sprite para dibujar la cabeza + SDL_Renderer *renderer; // El renderizador de la ventana + AnimatedSprite *headSprite; // Sprite para dibujar la cabeza + AnimatedSprite *bodySprite; // Sprite para dibujar el cuerpo + AnimatedSprite *legsSprite; // Sprite para dibujar las piernas + AnimatedSprite *deathSprite; // Sprite para dibujar el jugador derrotado // Variables float posX; // Posicion en el eje X @@ -106,7 +102,7 @@ public: Uint16 powerUpCounter; // Temporizador para el modo PowerUp // Constructor - Player(float x, int y, SDL_Renderer *renderer, std::vector texture, std::vector*> *animations); + Player(float x, int y, SDL_Renderer *renderer, std::vector texture, std::vector *> animations); // Destructor ~Player();