From 37325a2ec328e1c6eb9b817da4af7ceb1c6d7edd Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Thu, 9 Sep 2021 17:18:34 +0200 Subject: [PATCH] =?UTF-8?q?Finalizada=20la=20primera=20implementaci=C3=B3n?= =?UTF-8?q?=20funcional=20de=20la=20clase=20screen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/const.h | 28 ++++++++++++++-------------- source/director.cpp | 4 ++-- source/game.cpp | 14 ++++++++------ source/screen.cpp | 39 ++++++++++++++++++++++++++------------- source/screen.h | 26 ++++++++++++++++---------- 5 files changed, 66 insertions(+), 45 deletions(-) diff --git a/source/const.h b/source/const.h index c2674b9..e36770e 100644 --- a/source/const.h +++ b/source/const.h @@ -15,22 +15,22 @@ #define HALF_BLOCK 4 // Tamaño de la pantalla real -#define REAL_SCREEN_WIDTH 320 -#define REAL_SCREEN_HEIGHT 240 +#define SCREEN_WIDTH 320 +#define SCREEN_HEIGHT 240 // Tamaño de la pantalla virtual -#define SCREEN_WIDTH 256 -#define SCREEN_HEIGHT 192 +#define GAMECANVAS_WIDTH 256 +#define GAMECANVAS_HEIGHT 192 // Tamaño de la pantalla que se muestra -const int VIEW_WIDTH = REAL_SCREEN_WIDTH * 3; -const int VIEW_HEIGHT = REAL_SCREEN_HEIGHT * 3; +const int VIEW_WIDTH = SCREEN_WIDTH * 3; +const int VIEW_HEIGHT = SCREEN_HEIGHT * 3; // Zona de juego const int PLAY_AREA_TOP = (0 * BLOCK); -const int PLAY_AREA_BOTTOM = SCREEN_HEIGHT - (4 * BLOCK); +const int PLAY_AREA_BOTTOM = GAMECANVAS_HEIGHT - (4 * BLOCK); const int PLAY_AREA_LEFT = (0 * BLOCK); -const int PLAY_AREA_RIGHT = SCREEN_WIDTH - (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 int PLAY_AREA_CENTER_X = PLAY_AREA_LEFT + (PLAY_AREA_WIDTH / 2); @@ -41,12 +41,12 @@ const int PLAY_AREA_FIRST_QUARTER_Y = PLAY_AREA_HEIGHT / 4; const int PLAY_AREA_THIRD_QUARTER_Y = (PLAY_AREA_HEIGHT / 4) * 3; // Anclajes de pantalla -const int SCREEN_CENTER_X = SCREEN_WIDTH / 2; -const int SCREEN_FIRST_QUARTER_X = SCREEN_WIDTH / 4; -const int SCREEN_THIRD_QUARTER_X = (SCREEN_WIDTH / 4) * 3; -const int SCREEN_CENTER_Y = SCREEN_HEIGHT / 2; -const int SCREEN_FIRST_QUARTER_Y = SCREEN_HEIGHT / 4; -const int SCREEN_THIRD_QUARTER_Y = (SCREEN_HEIGHT / 4) * 3; +const int GAMECANVAS_CENTER_X = GAMECANVAS_WIDTH / 2; +const int GAMECANVAS_FIRST_QUARTER_X = GAMECANVAS_WIDTH / 4; +const int GAMECANVAS_THIRD_QUARTER_X = (GAMECANVAS_WIDTH / 4) * 3; +const int GAMECANVAS_CENTER_Y = GAMECANVAS_HEIGHT / 2; +const int GAMECANVAS_FIRST_QUARTER_Y = GAMECANVAS_HEIGHT / 4; +const int GAMECANVAS_THIRD_QUARTER_Y = (GAMECANVAS_HEIGHT / 4) * 3; // Secciones del programa #define PROG_SECTION_LOGO 0 diff --git a/source/director.cpp b/source/director.cpp index 5834c14..13c3f12 100644 --- a/source/director.cpp +++ b/source/director.cpp @@ -49,7 +49,7 @@ Director::Director(std::string path) // Aplica las opciones SDL_SetWindowFullscreen(mWindow, mOptions->fullScreenMode); - SDL_SetWindowSize(mWindow, REAL_SCREEN_WIDTH * mOptions->windowSize, REAL_SCREEN_HEIGHT * mOptions->windowSize); + SDL_SetWindowSize(mWindow, SCREEN_WIDTH * mOptions->windowSize, SCREEN_HEIGHT * mOptions->windowSize); mLang->setLang(mOptions->language); // Inicializa el resto de variables @@ -172,7 +172,7 @@ bool Director::initSDL() SDL_SetRenderDrawColor(mRenderer, 0x00, 0x00, 0x00, 0xFF); // Establece el tamaño del buffer de renderizado - SDL_RenderSetLogicalSize(mRenderer, REAL_SCREEN_WIDTH, REAL_SCREEN_HEIGHT); + SDL_RenderSetLogicalSize(mRenderer, SCREEN_WIDTH, SCREEN_HEIGHT); // Establece el modo de mezcla SDL_SetRenderDrawBlendMode(mRenderer, SDL_BLENDMODE_BLEND); diff --git a/source/game.cpp b/source/game.cpp index 1ae6f71..ccd2221 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -92,15 +92,17 @@ section_t Game::run() } } - // Limpia la pantalla - mScreen->clean(); + // Prepara para dibujar el frame + const color_t color = {0xAA, 0x55, 0x55}; + mScreen->start(); + mScreen->clean(color); - // Medidas de ancho y alto de la pantalla - mText->writeCentered(SCREEN_CENTER_X, 0, std::to_string(SCREEN_WIDTH), -1); - mText->write(0, SCREEN_CENTER_Y - (mText->getCharacterWidth() / 2), std::to_string(SCREEN_HEIGHT), -1); + // Escribe las medidas de ancho y alto de la pantalla + mText->writeCentered(GAMECANVAS_CENTER_X, 0, std::to_string(GAMECANVAS_WIDTH), -1); + mText->write(0, GAMECANVAS_CENTER_Y - (mText->getCharacterWidth() / 2), std::to_string(GAMECANVAS_HEIGHT), -1); // Texto en el centro de la pantalla - mText->writeCentered(SCREEN_CENTER_X, SCREEN_CENTER_Y - (mText->getCharacterWidth() / 2), "Pepe el Cazavampiros", -1); + mText->writeCentered(GAMECANVAS_CENTER_X, GAMECANVAS_CENTER_Y - (mText->getCharacterWidth() / 2), "Pepe el Cazavampiros", -1); // Actualiza la pantalla mScreen->blit(); diff --git a/source/screen.cpp b/source/screen.cpp index fcc3f62..821f5ff 100644 --- a/source/screen.cpp +++ b/source/screen.cpp @@ -4,17 +4,21 @@ // Constructor Screen::Screen(SDL_Window *window, SDL_Renderer *renderer) { + // Inicializa variables mWindow = window; mRenderer = renderer; - mRealScreenWidth = REAL_SCREEN_WIDTH; - mRealScreenHeight = REAL_SCREEN_HEIGHT; mScreenWidth = SCREEN_WIDTH; mScreenHeight = SCREEN_HEIGHT; - mBorderX = (REAL_SCREEN_WIDTH - SCREEN_WIDTH) / 2; - mBorderY = (REAL_SCREEN_HEIGHT - SCREEN_HEIGHT) / 2; + mGameCanvasWidth = GAMECANVAS_WIDTH; + mGameCanvasHeight = GAMECANVAS_HEIGHT; + mGameCanvasPosX = (SCREEN_WIDTH - GAMECANVAS_WIDTH) / 2; + mGameCanvasPosY = (SCREEN_HEIGHT - GAMECANVAS_HEIGHT) / 2; - mGameCanvas = SDL_CreateTexture(mRenderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, SCREEN_WIDTH, SCREEN_HEIGHT); + mBorderColor = {0x27, 0x27, 0x36}; + + // Crea la textura donde se dibujan los graficos del juego + mGameCanvas = SDL_CreateTexture(mRenderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT); if (mGameCanvas == NULL) printf("TitleSurface could not be created!\nSDL Error: %s\n", SDL_GetError()); } @@ -26,25 +30,34 @@ Screen::~Screen() } // Limpia la pantalla -void Screen::clean() +void Screen::clean(color_t color) { - SDL_SetRenderDrawColor(mRenderer, 0x27, 0x27, 0x36, 0xFF); + SDL_SetRenderDrawColor(mRenderer, color.r, color.g, color.b, 0xFF); SDL_RenderClear(mRenderer); +} +// Prepara para empezar a dibujar en la textura de juego +void Screen::start() +{ SDL_SetRenderTarget(mRenderer, mGameCanvas); - SDL_SetRenderDrawColor(mRenderer, 0xAA, 0x44, 0x44, 0xFF); - SDL_RenderClear(mRenderer); } // Vuelca el contenido del renderizador en pantalla void Screen::blit() { - SDL_Rect src = {0, 0, SCREEN_WIDTH, SCREEN_HEIGHT}; - SDL_Rect dest = {mBorderX, mBorderY, SCREEN_WIDTH, SCREEN_HEIGHT}; - + // Vuelve a dejar el renderizador en modo normal SDL_SetRenderTarget(mRenderer, NULL); - + + // Borra el contenido previo + SDL_SetRenderDrawColor(mRenderer, mBorderColor.r, mBorderColor.g, mBorderColor.b, 0xFF); + SDL_RenderClear(mRenderer); + + // Rectangulo de destino donde se dibujarà la textura con el juego + SDL_Rect dest = {mGameCanvasPosX, mGameCanvasPosY, mGameCanvasWidth, mGameCanvasHeight}; + + // Copia la textura de juego en el renderizador en la posición adecuada SDL_RenderCopy(mRenderer, mGameCanvas, NULL, &dest); + // Muestra por pantalla el renderizador SDL_RenderPresent(mRenderer); } \ No newline at end of file diff --git a/source/screen.h b/source/screen.h index 6785d2a..45bfef1 100644 --- a/source/screen.h +++ b/source/screen.h @@ -1,5 +1,6 @@ #pragma once #include "ifdefs.h" +#include "utils.h" #ifndef SCREEN_H #define SCREEN_H @@ -8,16 +9,18 @@ class Screen { private: - SDL_Window *mWindow; // Ventana de la aplicación - SDL_Renderer *mRenderer; // El renderizador de la ventana - SDL_Texture *mGameCanvas; // Textura para completar la ventana de juego hasta la pantalla completa + SDL_Window *mWindow; // Ventana de la aplicación + SDL_Renderer *mRenderer; // El renderizador de la ventana + SDL_Texture *mGameCanvas; // Textura para completar la ventana de juego hasta la pantalla completa - int mRealScreenWidth; - int mRealScreenHeight; - int mScreenWidth; - int mScreenHeight; - int mBorderX; - int mBorderY; + int mScreenWidth; // Ancho de la pantalla + int mScreenHeight; // Alto de la pantalla + int mGameCanvasWidth; // Ancho de la textura donde se dibuja el juego + int mGameCanvasHeight; // Alto de la textura donde se dibuja el juego + int mGameCanvasPosX; // Posicion en el eje X donde se dibujará la textura del juego dentro de la pantalla + int mGameCanvasPosY; // Posicion en el eje Y donde se dibujará la textura del juego dentro de la pantalla + + color_t mBorderColor; // Color del borde añadido a la textura de juego para rellenar la pantalla public: // Constructor @@ -27,7 +30,10 @@ public: ~Screen(); // Limpia la pantalla - void clean(); + void clean(color_t color); + + // Prepara para empezar a dibujar en la textura de juego + void start(); // Vuelca el contenido del renderizador en pantalla void blit();