From baa2f3effd0f46416381bc337119bed6f71ec817 Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Sat, 1 Jun 2024 18:17:03 +0200 Subject: [PATCH] Definidas areas para cada zona del juego --- source/background.cpp | 81 ++++++++++++++++++++++++++++++------------ source/background.h | 13 ++++++- source/const.h | 26 +++++++++----- source/game.cpp | 35 ++++++++---------- source/game.h | 1 + source/hiscore_table.h | 1 + source/instructions.h | 1 + source/intro.h | 1 + source/scoreboard.cpp | 4 +-- source/scoreboard.h | 1 + source/title.h | 1 + todo.txt | 9 +++-- 12 files changed, 116 insertions(+), 58 deletions(-) diff --git a/source/background.cpp b/source/background.cpp index d9472bc..30435d2 100644 --- a/source/background.cpp +++ b/source/background.cpp @@ -8,6 +8,22 @@ Background::Background(SDL_Renderer *renderer, Screen *screen, Asset *asset) this->screen = screen; this->asset = asset; + // Inicializa variables + gradientNumber = 0; + alpha = 0; + cloudsSpeed = 0; + transition = 0; + counter = 0; + rect = {0, 0, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT}; + srcRect = {0, rect.h - playArea.h, playArea.w, playArea.h}; + dstRect = playArea; + base = rect.h; + + gradientRect[0] = {0, 0, rect.w, rect.h}; + gradientRect[1] = {256, 0, rect.w, rect.h}; + gradientRect[2] = {0, 192, rect.w, rect.h}; + gradientRect[3] = {256, 192, rect.w, rect.h}; + // Carga las texturas buildingsTexture = new Texture(renderer, asset->get("game_buildings.png")); cloudsTexture = new Texture(renderer, asset->get("game_clouds.png")); @@ -15,32 +31,26 @@ Background::Background(SDL_Renderer *renderer, Screen *screen, Asset *asset) gradientsTexture = new Texture(renderer, asset->get("game_sky_colors.png")); // Crea los sprites - clouds1A = new MovingSprite(0, 0, 256, 52, -0.4f, 0.0f, 0.0f, 0.0f, cloudsTexture, renderer); - clouds1B = new MovingSprite(256, 0, 256, 52, -0.4f, 0.0f, 0.0f, 0.0f, cloudsTexture, renderer); - clouds2A = new MovingSprite(0, 52, 256, 32, -0.2f, 0.0f, 0.0f, 0.0f, cloudsTexture, renderer); - clouds2B = new MovingSprite(256, 52, 256, 32, -0.2f, 0.0f, 0.0f, 0.0f, cloudsTexture, renderer); + clouds1A = new MovingSprite(0, base - 155, 256, 52, -0.4f, 0.0f, 0.0f, 0.0f, cloudsTexture, renderer); + clouds1B = new MovingSprite(256, base - 155, 256, 52, -0.4f, 0.0f, 0.0f, 0.0f, cloudsTexture, renderer); + clouds2A = new MovingSprite(0, base - 155 + 57, 256, 32, -0.2f, 0.0f, 0.0f, 0.0f, cloudsTexture, renderer); + clouds2B = new MovingSprite(256, base - 155 + 57, 256, 32, -0.2f, 0.0f, 0.0f, 0.0f, cloudsTexture, renderer); buildingsSprite = new Sprite(0, 0, 256, 160, buildingsTexture, renderer); - gradientSprite = new Sprite(0, 0, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT, gradientsTexture, renderer); + gradientSprite = new Sprite(0, 0, 256, 192, gradientsTexture, renderer); grassSprite = new Sprite(0, 0, 256, 6, grassTexture, renderer); - // Inicializa variables - clouds1A->setSpriteClip(0, 0, 256, 52); - clouds1B->setSpriteClip(0, 0, 256, 52); - clouds2A->setSpriteClip(0, 52, 256, 32); - clouds2B->setSpriteClip(0, 52, 256, 32); - grassSprite->setPosY(154); + // Inicializa objetos + clouds1A->setSpriteClip(0, 0, 256, 52); + clouds1B->setSpriteClip(0, 0, 256, 52); + clouds2A->setSpriteClip(0, 52, 256, 32); + clouds2B->setSpriteClip(0, 52, 256, 32); + buildingsSprite->setPosY(base - buildingsSprite->getHeight()); + grassSprite->setPosY(base - grassSprite->getHeight()); - gradientRect[0] = {0, 0, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT}; - gradientRect[1] = {256, 0, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT}; - gradientRect[2] = {0, 192, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT}; - gradientRect[3] = {256, 192, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT}; - - gradientNumber = 0; - alpha = 0; - cloudsSpeed = 0; - transition = 0; - counter = 0; + // Crea la textura para componer el fondo + canvas = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, rect.w, rect.h); + SDL_SetTextureBlendMode(canvas, SDL_BLENDMODE_BLEND); } // Destructor @@ -65,6 +75,7 @@ Background::~Background() delete buildingsSprite; delete gradientSprite; delete grassSprite; + SDL_DestroyTexture(canvas); } // Actualiza la lógica del objeto @@ -113,9 +124,13 @@ void Background::update() counter++; } -// Dibuja el objeto -void Background::render() +// Compone todos los elementos del fondo en la textura +void Background::fillCanvas() { + // Cambia el destino del renderizador + SDL_Texture *temp = SDL_GetRenderTarget(renderer); + SDL_SetRenderTarget(renderer, canvas); + // Dibuja el gradiente 2 gradientSprite->setSpriteClip(gradientRect[(gradientNumber + 1) % 4]); gradientsTexture->setAlpha(255); @@ -137,6 +152,16 @@ void Background::render() // Dibuja la hierba grassSprite->render(); + + // Deja el renderizador apuntando donde estaba + SDL_SetRenderTarget(renderer, temp); +} + +// Dibuja el objeto +void Background::render() +{ + fillCanvas(); + SDL_RenderCopy(renderer, canvas, &srcRect, &dstRect); } // Vuelve a cargar las texturas @@ -164,4 +189,14 @@ void Background::setGradientNumber(int value) void Background::setTransition(float value) { transition = value; +} + +// Establece la posición del objeto +void Background::setPos(SDL_Rect rect) +{ + dstRect = rect; + + // Si cambian las medidas del destino, hay que cambiar las del origen para evitar deformar la imagen + srcRect.w = rect.w; + srcRect.h = rect.h; } \ No newline at end of file diff --git a/source/background.h b/source/background.h index 204b45c..569ccbb 100644 --- a/source/background.h +++ b/source/background.h @@ -21,9 +21,14 @@ private: SDL_Rect gradientRect[4]; // Vector con las coordenadas de los 4 degradados para el cielo int gradientNumber; // Indica el número de degradado de fondo que se va a dibujar int alpha; // Transparencia entre los dos degradados - float cloudsSpeed; // Velocidad a la que se desplazan las nubes + float cloudsSpeed; // Velocidad a la que se desplazan las nubes float transition; // Nivel de transición del fondo 0..1 int counter; // Contador interno + SDL_Rect rect; // Tamaño del objeto fondo + SDL_Rect srcRect; // Parte del objeto fondo que se va a dibujará en pantalla + SDL_Rect dstRect; // Posición donde dibujar la parte del objeto fondo que se dibujará en pantalla + SDL_Texture *canvas; // Textura para componer el fondo + int base; // Linea de fondo coincidente con el area inferior de la zona de juego Texture *buildingsTexture; // Textura con los edificios de fondo Texture *cloudsTexture; // Textura con las nubes de fondo @@ -39,6 +44,9 @@ private: Sprite *gradientSprite; // Sprite con los graficos del degradado de color de fondo Sprite *grassSprite; // Sprite para la hierba + // Compone todos los elementos del fondo en la textura + void fillCanvas(); + public: // Constructor Background(SDL_Renderer *renderer, Screen *screen, Asset *asset); @@ -52,6 +60,9 @@ public: // Dibuja el objeto void render(); + // Establece la posición del objeto + void setPos(SDL_Rect); + // Vuelve a cargar las texturas void reloadTextures(); diff --git a/source/const.h b/source/const.h index 35972e6..e02183b 100644 --- a/source/const.h +++ b/source/const.h @@ -2,7 +2,6 @@ #include #include "common/utils.h" -#include "lang.h" #ifndef CONST_H #define CONST_H @@ -11,20 +10,24 @@ #define BLOCK 8 #define HALF_BLOCK BLOCK / 2 -// Tamaño de la pantalla virtual +// Resolución nativa del juego #define GAMECANVAS_WIDTH 256 #define GAMECANVAS_HEIGHT 192 // Marcador -const int SCOREBOARD_HEIGHT = (4 * BLOCK); +const int SCOREBOARD_WIDTH = GAMECANVAS_WIDTH; +const int SCOREBOARD_HEIGHT = 32; +const int SCOREBOARD_X = 0; +const int SCOREBOARD_Y = GAMECANVAS_HEIGHT - SCOREBOARD_HEIGHT; // Zona de juego -const int PLAY_AREA_TOP = (0 * BLOCK); -const int PLAY_AREA_BOTTOM = GAMECANVAS_HEIGHT - SCOREBOARD_HEIGHT; -const int PLAY_AREA_LEFT = (0 * BLOCK); -const int PLAY_AREA_RIGHT = GAMECANVAS_WIDTH - (0 * BLOCK); -const int PLAY_AREA_WIDTH = PLAY_AREA_RIGHT - PLAY_AREA_LEFT; -const int PLAY_AREA_HEIGHT = PLAY_AREA_BOTTOM - PLAY_AREA_TOP; +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 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); const int PLAY_AREA_CENTER_FIRST_QUARTER_X = (PLAY_AREA_WIDTH / 4); const int PLAY_AREA_CENTER_THIRD_QUARTER_X = (PLAY_AREA_WIDTH / 4) * 3; @@ -64,5 +67,10 @@ const int GAMECANVAS_THIRD_QUARTER_Y = (GAMECANVAS_HEIGHT / 4) * 3; const color_t bgColor = {0x27, 0x27, 0x36}; const color_t noColor = {0xFF, 0xFF, 0xFF}; const color_t shdwTxtColor = {0x43, 0x43, 0x4F}; +const color_t separator = {0x0D, 0x1A, 0x2B}; +const color_t scoreboardColor = {46, 63, 71}; +const color_t difficultyEasyColor = {75, 105, 47}; +const color_t difficultyNormalColor = {255, 122, 0}; +const color_t difficultyHardColor = {118, 66, 138}; #endif \ No newline at end of file diff --git a/source/game.cpp b/source/game.cpp index a0326bc..3654a35 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -194,41 +194,34 @@ void Game::init() case DIFFICULTY_EASY: defaultEnemySpeed = BALLOON_SPEED_1; difficultyScoreMultiplier = 0.5f; - difficultyColor = {75, 105, 47}; - pauseMenu->setSelectorColor(difficultyColor, 255); - gameOverMenu->setSelectorColor(difficultyColor, 255); + difficultyColor = difficultyEasyColor; + scoreboard->setColor(difficultyColor); break; case DIFFICULTY_NORMAL: defaultEnemySpeed = BALLOON_SPEED_1; difficultyScoreMultiplier = 1.0f; - difficultyColor = {255, 122, 0}; - pauseMenu->setSelectorColor(difficultyColor, 255); - gameOverMenu->setSelectorColor(difficultyColor, 255); + difficultyColor = difficultyNormalColor; + scoreboard->setColor(scoreboardColor); break; case DIFFICULTY_HARD: defaultEnemySpeed = BALLOON_SPEED_5; difficultyScoreMultiplier = 1.5f; - difficultyColor = {118, 66, 138}; - pauseMenu->setSelectorColor(difficultyColor, 255); - gameOverMenu->setSelectorColor(difficultyColor, 255); + difficultyColor = difficultyHardColor; + scoreboard->setColor(difficultyColor); break; default: break; } + // Colores + pauseMenu->setSelectorColor(difficultyColor, 255); + gameOverMenu->setSelectorColor(difficultyColor, 255); + // Variables para el marcador - scoreboard->setPos({PLAY_AREA_LEFT, PLAY_AREA_BOTTOM, PLAY_AREA_WIDTH, SCOREBOARD_HEIGHT}); - if (difficulty == DIFFICULTY_NORMAL) - { - scoreboard->setColor({46, 63, 71}); - } - else - { - scoreboard->setColor(difficultyColor); - } + scoreboard->setPos({SCOREBOARD_X, SCOREBOARD_Y, SCOREBOARD_WIDTH, SCOREBOARD_HEIGHT}); // Resto de variables gameCompleted = false; @@ -2690,7 +2683,7 @@ void Game::updateBackground() void Game::renderSeparator() { // Dibuja la linea que separa el marcador de la zona de juego - SDL_SetRenderDrawColor(renderer, 13, 26, 43, 255); + 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); } @@ -2705,11 +2698,11 @@ void Game::render() // Dibuja los objetos background->render(); + renderItems(); + renderSmartSprites(); renderBalloons(); renderBullets(); renderMessages(); - renderItems(); - renderSmartSprites(); scoreboard->render(); renderSeparator(); renderPlayers(); diff --git a/source/game.h b/source/game.h index 2af2ba8..e8478eb 100644 --- a/source/game.h +++ b/source/game.h @@ -20,6 +20,7 @@ #include "player.h" #include "scoreboard.h" #include "background.h" +#include "lang.h" #include #ifndef GAME_H diff --git a/source/hiscore_table.h b/source/hiscore_table.h index 39594b1..5e1a4e0 100644 --- a/source/hiscore_table.h +++ b/source/hiscore_table.h @@ -9,6 +9,7 @@ #include "common/text.h" #include "common/utils.h" #include "const.h" +#include "lang.h" #ifndef HISCORE_TABLE_H #define HISCORE_TABLE_H diff --git a/source/instructions.h b/source/instructions.h index f62aaa0..7179ce5 100644 --- a/source/instructions.h +++ b/source/instructions.h @@ -9,6 +9,7 @@ #include "common/text.h" #include "common/utils.h" #include "const.h" +#include "lang.h" #ifndef INSTRUCTIONS_H #define INSTRUCTIONS_H diff --git a/source/intro.h b/source/intro.h index 5c28524..6520cc1 100644 --- a/source/intro.h +++ b/source/intro.h @@ -9,6 +9,7 @@ #include "common/utils.h" #include "common/writer.h" #include "const.h" +#include "lang.h" #include #ifndef INTRO_H diff --git a/source/scoreboard.cpp b/source/scoreboard.cpp index 79ea9c1..e245838 100644 --- a/source/scoreboard.cpp +++ b/source/scoreboard.cpp @@ -52,7 +52,7 @@ Scoreboard::Scoreboard(SDL_Renderer *renderer, Screen *screen, Asset *asset, Lan powerMeterSprite = new Sprite(offsetPowerMeter.x - 20, offsetPowerMeter.y, 40, 7, gamePowerMeterTexture, renderer); textScoreBoard = new Text(asset->get("8bithud.png"), asset->get("8bithud.txt"), renderer); - // Crea la textura dibujar el marcador + // Crea la textura para dibujar el marcador background = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, rect.w, rect.h); SDL_SetTextureBlendMode(background, SDL_BLENDMODE_BLEND); @@ -245,6 +245,6 @@ void Scoreboard::fillBackgroundTexture() textScoreBoard->writeCentered(offsetHiScoreLabel.x, offsetHiScoreLabel.y, lang->getText(56)); textScoreBoard->writeCentered(offsetHiScore.x, offsetHiScore.y, hiScoreName + updateScoreText(hiScore)); - // Deja el renderizador apuntando a la pantalla + // Deja el renderizador apuntando donde estaba SDL_SetRenderTarget(renderer, temp); } \ No newline at end of file diff --git a/source/scoreboard.h b/source/scoreboard.h index 6f723c2..b5e5941 100644 --- a/source/scoreboard.h +++ b/source/scoreboard.h @@ -13,6 +13,7 @@ #include "common/utils.h" #include "common/writer.h" #include "const.h" +#include "lang.h" #ifndef SCOREBOARD_H #define SCOREBOARD_H diff --git a/source/title.h b/source/title.h index e5c28d7..1c0329a 100644 --- a/source/title.h +++ b/source/title.h @@ -17,6 +17,7 @@ #include "hiscore_table.h" #include "instructions.h" #include "item.h" +#include "lang.h" #ifndef TITLE_H #define TITLE_H diff --git a/todo.txt b/todo.txt index b988fee..5472410 100644 --- a/todo.txt +++ b/todo.txt @@ -1,4 +1,9 @@ [] Cargar todos los graficos al inicio -[] Independizar el pintado del fondo del juego +[x] Independizar el pintado del fondo del juego [] Cambiar la resolución a 320x240 -[] Permitir entrar al segundo jugador en caliente \ No newline at end of file +[] Permitir entrar al segundo jugador en caliente +[] Arreglar la pantalla de titulo +[] Hacer el marcador transparente +[] Valorar la opcion de que write devuelva texturas con el texto en lugar de pintar letra a letra +[] Arreglar los anclajes en la pantalla de game over +[] Al poner pausa, que se sigan moviendo las nubes \ No newline at end of file