From a5c983df9d57a93581f0ddbd4d5c3bf986ce90b0 Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Tue, 30 Aug 2022 17:59:10 +0200 Subject: [PATCH] =?UTF-8?q?Actualizada=20la=20clase=20screen=20con=20nuevo?= =?UTF-8?q?s=20procedimientos.=20Inclu=C3=ADdo=20el=20fichero=20const.h.?= =?UTF-8?q?=20Cambio=20de=20tama=C3=B1o=20de=20ventana=20y=20pantalla=20co?= =?UTF-8?q?mpleta?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/const.h | 41 ++++++++++++++++++++++---- source/game.cpp | 75 +++++++++++++++++++++++++++++++---------------- source/logo.cpp | 6 ++-- source/logo.h | 1 + source/prog.cpp | 7 +++-- source/prog.h | 5 +--- source/screen.cpp | 46 +++++++++++++++++++++++++---- source/screen.h | 32 ++++++++++++++++---- source/title.cpp | 3 +- 9 files changed, 162 insertions(+), 54 deletions(-) diff --git a/source/const.h b/source/const.h index aea615f..b7c8ad8 100644 --- a/source/const.h +++ b/source/const.h @@ -1,18 +1,47 @@ #pragma once +#include "utils.h" + #ifndef CONST_H #define CONST_H -// Tamaño de bloque -#define BLOCK 16 -#define HALF_BLOCK 8 +// Textos +#define WINDOW_CAPTION "Volcano" +#define TEXT_COPYRIGHT "2016,2022 JAILDESIGNER & JAILBROTHER" +#define VERSION "0.5" -// Tamaño de la pantalla real -#define SCREEN_WIDTH 320 -#define SCREEN_HEIGHT 240 +// Tamaño de bloque +#define BLOCK 8 +#define HALF_BLOCK 4 // Tamaño de la pantalla virtual #define GAMECANVAS_WIDTH 320 #define GAMECANVAS_HEIGHT 240 +// Zona de juego +const int PLAY_AREA_TOP = (0 * BLOCK); +const int PLAY_AREA_BOTTOM = (16 * BLOCK); +const int PLAY_AREA_LEFT = (0 * BLOCK); +const int PLAY_AREA_RIGHT = (32 * 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); +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; +const int PLAY_AREA_CENTER_Y = PLAY_AREA_TOP + (PLAY_AREA_HEIGHT / 2); +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 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; + +// Colores +const color_t borderColor = {0x27, 0x27, 0x36}; +const color_t black = {0xFF, 0xFF, 0xFF}; + #endif \ No newline at end of file diff --git a/source/game.cpp b/source/game.cpp index 443ee36..7e2e9fe 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -83,6 +83,55 @@ void Game::update() section.name = SECTION_PROG_QUIT; break; } + else if ((eventHandler->type == SDL_KEYDOWN) and (eventHandler->key.repeat == 0)) + { + switch (eventHandler->key.keysym.scancode) + { + case SDL_SCANCODE_ESCAPE: + section.name = SECTION_PROG_QUIT; + break; + + case SDL_SCANCODE_D: + debug = !debug; + musicEnabled = !debug; + if (musicEnabled) + { + JA_PlayMusic(music); + } + else + { + JA_StopMusic(); + } + break; + + case SDL_SCANCODE_R: + delete map; + map = new Map(asset->get("01.map"), renderer, asset, itemTracker); + delete player; + player = new Player(renderer, asset, input, map); + break; + + case SDL_SCANCODE_F: + screen->switchVideoMode(); + break; + + case SDL_SCANCODE_F1: + screen->setWindowSize(1); + break; + + case SDL_SCANCODE_F2: + screen->setWindowSize(2); + break; + + case SDL_SCANCODE_F3: + screen->setWindowSize(3); + break; + + case SDL_SCANCODE_F4: + screen->setWindowSize(4); + break; + } + } } map->update(); @@ -113,32 +162,6 @@ void Game::render() // Comprueba la entrada void Game::checkInput() { - if (input->checkInput(INPUT_BUTTON_2, REPEAT_FALSE)) - { - debug = !debug; - musicEnabled = !debug; - if (musicEnabled) - { - JA_PlayMusic(music); - } - else - { - JA_StopMusic(); - } - } - - if (input->checkInput(INPUT_BUTTON_3, REPEAT_FALSE)) - { - delete map; - map = new Map(asset->get("01.map"), renderer, asset, itemTracker); - delete player; - player = new Player(renderer, asset, input, map); - } - - if (input->checkInput(INPUT_BUTTON_ESCAPE, REPEAT_FALSE)) - { - section.name = SECTION_PROG_QUIT; - } } // Muestra información de depuración diff --git a/source/logo.cpp b/source/logo.cpp index 92999ed..661ca2d 100644 --- a/source/logo.cpp +++ b/source/logo.cpp @@ -12,10 +12,10 @@ Logo::Logo(SDL_Renderer *renderer, Screen *screen, Asset *asset) eventHandler = new SDL_Event(); texture = new LTexture(); loadTextureFromFile(texture, asset->get("logo.png"), renderer); - sprite = new Sprite(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, texture, renderer); + sprite = new Sprite(0, 0, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT, texture, renderer); // Crea un backbuffer para el renderizador - backbuffer = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, SCREEN_WIDTH, SCREEN_HEIGHT); + backbuffer = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT); if (backbuffer == NULL) printf("Backbuffer could not be created!\nSDL Error: %s\n", SDL_GetError()); @@ -105,7 +105,7 @@ void Logo::render() // Dibuja el fade if (counter >= initFade) { - const SDL_Rect rect = {0, 0, SCREEN_WIDTH, SCREEN_HEIGHT}; + const SDL_Rect rect = {0, 0, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT}; const color_t color = {0xFF, 0xFF, 0xFF}; const int fadeLenght = endLogo - initFade; diff --git a/source/logo.h b/source/logo.h index cee7c7c..372f5ee 100644 --- a/source/logo.h +++ b/source/logo.h @@ -7,6 +7,7 @@ #include "screen.h" #include "asset.h" #include "jail_audio.h" +#include "const.h" #ifndef LOGO_H #define LOGO_H diff --git a/source/prog.cpp b/source/prog.cpp index 0b0d60e..4a57dc2 100644 --- a/source/prog.cpp +++ b/source/prog.cpp @@ -9,8 +9,8 @@ Prog::Prog(std::string executablePath) options->windowSize = 2; options->filter = FILTER_NEAREST; options->vSync = true; - options->screenWidth = GAME_WIDTH * options->windowSize; - options->screenHeight = GAME_HEIGHT * options->windowSize; + options->screenWidth = GAMECANVAS_WIDTH * options->windowSize; + options->screenHeight = GAMECANVAS_HEIGHT * options->windowSize; options->integerScale = true; options->keepAspect = true; @@ -29,7 +29,8 @@ Prog::Prog(std::string executablePath) section.name = SECTION_PROG_GAME; } input = new Input(asset->get("gamecontrollerdb.txt")); - screen = new Screen(window, renderer, options); + screen = new Screen(window, renderer, options, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT); + screen->setBorderColor(borderColor); // Controles input->bindKey(INPUT_UP, SDL_SCANCODE_UP); diff --git a/source/prog.h b/source/prog.h index 1a857e1..0e50060 100644 --- a/source/prog.h +++ b/source/prog.h @@ -12,14 +12,11 @@ #include "intro.h" #include "title.h" #include "prog.h" +#include "const.h" #ifndef PROG_H #define PROG_H -#define WINDOW_CAPTION "Volcano" -#define GAME_WIDTH 320 -#define GAME_HEIGHT 240 - class Prog { private: diff --git a/source/screen.cpp b/source/screen.cpp index 4f71934..1c8aee7 100644 --- a/source/screen.cpp +++ b/source/screen.cpp @@ -1,18 +1,17 @@ #include "screen.h" -#include "const.h" #include #include // Constructor -Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, options_t *options) +Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, options_t *options, int gameInternalResX, int gameInternalResY) { // Inicializa variables this->window = window; this->renderer = renderer; this->options = options; - gameCanvasWidth = SCREEN_WIDTH; - gameCanvasHeight = SCREEN_HEIGHT; + gameCanvasWidth = gameInternalResX; + gameCanvasHeight = gameInternalResY; // Establece el modo de video setVideoMode(options->fullScreenMode); @@ -25,6 +24,14 @@ Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, options_t *options) gameCanvas = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, gameCanvasWidth, gameCanvasHeight); if (gameCanvas == NULL) printf("TitleSurface could not be created!\nSDL Error: %s\n", SDL_GetError()); + + // Calcula los anclajes + anchor.left = 0; + anchor.right = gameCanvasWidth; + anchor.center = gameCanvasWidth / 2; + anchor.top = 0; + anchor.bottom = gameCanvasHeight; + anchor.middle = gameCanvasHeight / 2; } // Destructor @@ -82,7 +89,7 @@ void Screen::setVideoMode(int fullScreenMode) } // Si está activo el modo de pantalla completa añade el borde - if (fullScreenMode == SDL_WINDOW_FULLSCREEN_DESKTOP) + else if (fullScreenMode == SDL_WINDOW_FULLSCREEN_DESKTOP) { // Obten el alto y el ancho de la ventana SDL_GetWindowSize(window, &screenWidth, &screenHeight); @@ -130,4 +137,33 @@ void Screen::setVideoMode(int fullScreenMode) // Modifica el tamaño del renderizador SDL_RenderSetLogicalSize(renderer, screenWidth, screenHeight); } + + // Actualiza el valor de la variable + options->fullScreenMode = fullScreenMode; +} + +// Camibia entre pantalla completa y ventana +void Screen::switchVideoMode() +{ + if (options->fullScreenMode == 0) + { + setVideoMode(SDL_WINDOW_FULLSCREEN_DESKTOP); + } + else + { + setVideoMode(0); + } +} + +// Cambia el tamaño de la ventana +void Screen::setWindowSize(int size) +{ + options->windowSize = size; + setVideoMode(0); +} + +// Cambia el color del borde +void Screen::setBorderColor(color_t color) +{ + borderColor = color; } \ No newline at end of file diff --git a/source/screen.h b/source/screen.h index 04f416f..5c15402 100644 --- a/source/screen.h +++ b/source/screen.h @@ -6,6 +6,16 @@ #ifndef SCREEN_H #define SCREEN_H +struct anchor_t +{ + int left; // Parte izquierda de la pantalla de juego + int right; // Parte drecha de la pantalla de juego + int center; // Parte central horizontal de la pantalla de juego + int top; // Parte superior de la pantalla de juego + int bottom; // Parte infoerior de la pantalla de juego + int middle; // Parte central vertical de la pantalla de juego +}; + // Clase Screen class Screen { @@ -15,16 +25,17 @@ private: SDL_Texture *gameCanvas; // Textura para completar la ventana de juego hasta la pantalla completa options_t *options; // Variable con todas las opciones del programa - int screenWidth; // Ancho de la pantalla - int screenHeight; // Alto de la pantalla - int gameCanvasWidth; // Ancho de la textura donde se dibuja el juego - int gameCanvasHeight; // Alto de la textura donde se dibuja el juego - SDL_Rect dest; // Coordenadas donde se va a dibujar la textura del juego + int screenWidth; // Ancho de la pantalla o ventana + int screenHeight; // Alto de la pantalla o ventana + int gameCanvasWidth; // Resolución interna del juego. Es el ancho de la textura donde se dibuja el juego + int gameCanvasHeight; // Resolución interna del juego. Es el alto de la textura donde se dibuja el juego + anchor_t anchor; // Variable con los anclajes de la pantalla + SDL_Rect dest; // Coordenadas donde se va a dibujar la textura del juego sobre la pantalla o ventana color_t borderColor; // Color del borde añadido a la textura de juego para rellenar la pantalla public: // Constructor - Screen(SDL_Window *window, SDL_Renderer *renderer, options_t *options); + Screen(SDL_Window *window, SDL_Renderer *renderer, options_t *options, int gameInternalResX, int gameInternalResY); // Destructor ~Screen(); @@ -40,6 +51,15 @@ public: // Establece el modo de video void setVideoMode(int fullScreenMode); + + // Camibia entre pantalla completa y ventana + void switchVideoMode(); + + // Cambia el tamaño de la ventana + void setWindowSize(int size); + + // Cambia el color del borde + void setBorderColor(color_t color); }; #endif diff --git a/source/title.cpp b/source/title.cpp index bd8961f..70b1d9a 100644 --- a/source/title.cpp +++ b/source/title.cpp @@ -94,7 +94,8 @@ void Title::render() // Dibuja los objetos sprite->render(); - text->writeDX(TXT_CENTER | TXT_COLOR, 160, 205, "@2016,2022 JAILDESIGNER & JAILBROTHER (v0.4)", 0, {255, 93, 4}); + //const std::string text = TEXT_COPYRIGHT + " (" + VERSION + ")"; + this->text->writeDX(TXT_CENTER | TXT_COLOR, GAMECANVAS_CENTER_X, 205, TEXT_COPYRIGHT, 0, {255, 93, 4}); menu->render(); // Vuelca el contenido del renderizador en pantalla