From e4c14b275f86e11dec079e637234b2d59b638615 Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Tue, 4 Jun 2024 13:05:03 +0200 Subject: [PATCH] La zona de juego se dibuja en su propia textura --- source/background.cpp | 3 ++- source/const.h | 10 +++++----- source/game.cpp | 32 +++++++++++++++++++++++++------- source/game.h | 7 ++++--- 4 files changed, 36 insertions(+), 16 deletions(-) diff --git a/source/background.cpp b/source/background.cpp index 30435d2..40161b0 100644 --- a/source/background.cpp +++ b/source/background.cpp @@ -16,7 +16,8 @@ Background::Background(SDL_Renderer *renderer, Screen *screen, Asset *asset) counter = 0; rect = {0, 0, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT}; srcRect = {0, rect.h - playArea.h, playArea.w, playArea.h}; - dstRect = playArea; + srcRect = {playArea.x, rect.h - playArea.h, playArea.w, playArea.h}; + dstRect = {0, 0, playArea.w, playArea.h}; base = rect.h; gradientRect[0] = {0, 0, rect.w, rect.h}; diff --git a/source/const.h b/source/const.h index e02183b..da5e79f 100644 --- a/source/const.h +++ b/source/const.h @@ -21,11 +21,11 @@ const int SCOREBOARD_X = 0; const int SCOREBOARD_Y = GAMECANVAS_HEIGHT - SCOREBOARD_HEIGHT; // Zona de juego -const SDL_Rect playArea = {0, 0, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT - SCOREBOARD_HEIGHT}; -const int PLAY_AREA_TOP = playArea.y; -const int PLAY_AREA_BOTTOM = playArea.y + playArea.h; -const int PLAY_AREA_LEFT = playArea.x; -const int PLAY_AREA_RIGHT = playArea.x + playArea.w; +const SDL_Rect playArea = {10, 10, GAMECANVAS_WIDTH-20, GAMECANVAS_HEIGHT - SCOREBOARD_HEIGHT-20}; +const int PLAY_AREA_TOP = 0; +const int PLAY_AREA_BOTTOM = playArea.h; +const int PLAY_AREA_LEFT = 0; +const int PLAY_AREA_RIGHT = playArea.w; const int PLAY_AREA_WIDTH = playArea.w; const int PLAY_AREA_HEIGHT = playArea.h; const int PLAY_AREA_CENTER_X = PLAY_AREA_LEFT + (PLAY_AREA_WIDTH / 2); diff --git a/source/game.cpp b/source/game.cpp index 3654a35..b23b378 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -46,6 +46,9 @@ Game::Game(int numPlayers, int currentStage, SDL_Renderer *renderer, Screen *scr gameOverSprite = new Sprite(16, 80, 128, 96, gameOverTexture, renderer); gameOverEndSprite = new Sprite(PLAY_AREA_CENTER_X - gameOverEndTexture->getWidth() / 2, 80, 128, 96, gameOverEndTexture, renderer); + canvas = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, playArea.w, playArea.h); + SDL_SetTextureBlendMode(canvas, SDL_BLENDMODE_BLEND); + // Inicializa las variables necesarias para la sección 'Game' init(); } @@ -141,6 +144,8 @@ Game::~Game() delete gameOverSprite; delete gameOverEndSprite; + SDL_DestroyTexture(canvas); + JA_DeleteSound(balloonSound); JA_DeleteSound(bulletSound); JA_DeleteSound(playerCollisionSound); @@ -2684,17 +2689,18 @@ void Game::renderSeparator() { // Dibuja la linea que separa el marcador de la zona de juego SDL_SetRenderDrawColor(renderer, separator.r, separator.g, separator.b, 255); - SDL_RenderDrawLine(renderer, PLAY_AREA_LEFT, PLAY_AREA_BOTTOM, PLAY_AREA_RIGHT, PLAY_AREA_BOTTOM); + SDL_RenderDrawLine(renderer, SCOREBOARD_X, SCOREBOARD_Y, SCOREBOARD_X + SCOREBOARD_WIDTH, SCOREBOARD_Y); } // Dibuja el juego void Game::render() { - // Prepara para empezar a dibujar en la textura de juego - screen->start(); + //Dibujamos el contenido de la zona de juego en su textura + SDL_Texture *temp = SDL_GetRenderTarget(renderer); + SDL_SetRenderTarget(renderer, canvas); - // Limpia la pantalla - screen->clean(bgColor); + SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); + SDL_RenderClear(renderer); // Dibuja los objetos background->render(); @@ -2703,10 +2709,22 @@ void Game::render() renderBalloons(); renderBullets(); renderMessages(); - scoreboard->render(); - renderSeparator(); renderPlayers(); + // Deja el renderizador apuntando donde estaba + SDL_SetRenderTarget(renderer, temp); + + // Prepara para empezar a dibujar en la textura de juego + screen->start(); + + // Limpia la pantalla + screen->clean(bgColor); + + // Dibuja la zona de juego y el marcador + SDL_RenderCopy(renderer, canvas, nullptr, &playArea); + scoreboard->render(); + //renderSeparator(); + if ((deathCounter <= 150) && !players[0]->isAlive()) { renderDeathFade(150 - deathCounter); diff --git a/source/game.h b/source/game.h index e8478eb..17e034e 100644 --- a/source/game.h +++ b/source/game.h @@ -126,6 +126,7 @@ private: section_t *section; // Seccion actual dentro del juego Scoreboard *scoreboard; // Objeto para dibujar el marcador Background *background; // Objetio para dibujar el fondo del juego + SDL_Texture *canvas; // Textura para dibujar la zona de juego std::vector players; // Vector con los jugadores std::vector balloons; // Vector con los globos @@ -140,9 +141,9 @@ private: std::vector player2Textures; // Vector con las texturas del jugador std::vector> playerTextures; // Vector con todas las texturas de los jugadores; - Texture *gameTextTexture; // Textura para los sprites con textos - Texture *gameOverTexture; // Textura para la pantalla de game over - Texture *gameOverEndTexture; // Textura para la pantalla de game over de acabar el juego + Texture *gameTextTexture; // Textura para los sprites con textos + Texture *gameOverTexture; // Textura para la pantalla de game over + Texture *gameOverEndTexture; // Textura para la pantalla de game over de acabar el juego std::vector *> itemAnimations; // Vector con las animaciones de los items std::vector *> playerAnimations; // Vector con las animaciones del jugador