From 90ebea48073c423042fbeaa7013681a832d4d8fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Valor=20Mart=C3=ADnez?= Date: Sun, 7 May 2023 14:22:14 +0200 Subject: [PATCH] =?UTF-8?q?Demo:=20implementado=20el=20cambio=20de=20tama?= =?UTF-8?q?=C3=B1o=20de=20la=20ventana?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.cpp | 36 +++++++++++++++++++++++--------- units/notify.cpp | 6 +++--- units/screen.cpp | 54 +++++++++++++++++++++++++----------------------- units/utils.cpp | 27 ++++++++++++------------ units/utils.h | 25 +++++++++++----------- 5 files changed, 82 insertions(+), 66 deletions(-) diff --git a/main.cpp b/main.cpp index 7ecffdb..10247fe 100644 --- a/main.cpp +++ b/main.cpp @@ -32,8 +32,10 @@ int main(int argc, char *argv[]) // Inicializa las opciones struct options_t *options = new options_t; initOptions(options); - options->gameWidth = 640; - options->gameHeight = 480; + options->screen.nativeWidth = 320; + options->screen.nativeHeight = 240; + options->screen.nativeZoom = 1; + options->screen.windowZoom = 2; options->console = true; // Inicializa la lista de recursos @@ -53,7 +55,7 @@ int main(int argc, char *argv[]) // Inicializa SDL y la ventana SDL_Init(SDL_INIT_EVERYTHING); - window = SDL_CreateWindow("jail_engine_demo", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, options->gameWidth, options->gameHeight, SDL_WINDOW_SHOWN); + window = SDL_CreateWindow("jail_engine_demo", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, options->screen.nativeWidth * options->screen.nativeZoom * options->screen.windowZoom, options->screen.nativeHeight * options->screen.nativeZoom * options->screen.windowZoom, SDL_WINDOW_SHOWN); if (window != nullptr) { renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); @@ -116,6 +118,19 @@ int main(int argc, char *argv[]) { switch (event->key.keysym.scancode) { + + case SDL_SCANCODE_ESCAPE: + should_exit = true; + break; + + case SDL_SCANCODE_1: + screen->decWindowSize(); + break; + + case SDL_SCANCODE_2: + screen->incWindowSize(); + break; + case SDL_SCANCODE_N: screen->showNotification("Ejemplo de notificacion", "con 2 lineas de texto", 0); break; @@ -139,7 +154,7 @@ int main(int argc, char *argv[]) screen->update(); // Actualiza el sprite - if (sprite->getPosX() + sprite->getWidth() > options->gameWidth or sprite->getPosX() < 0) + if (sprite->getPosX() + sprite->getWidth() > options->screen.nativeWidth or sprite->getPosX() < 0) { sprite->undoMoveX(); int spr_direction = 1; @@ -155,7 +170,7 @@ int main(int argc, char *argv[]) sprite->setVelX(spr_force * spr_direction); JA_PlaySound(sound); } - if (sprite->getPosY() + sprite->getHeight() > options->gameHeight or sprite->getPosY() < 0) + if (sprite->getPosY() + sprite->getHeight() > options->screen.nativeHeight or sprite->getPosY() < 0) { sprite->undoMoveY(); int spr_direction = 1; @@ -193,8 +208,8 @@ int main(int argc, char *argv[]) screen->clean(); // Dibuja un degradado de fondo - const int gradFirstLine = options->gameHeight / 3; - const int gradLastLine = options->gameHeight; + const int gradFirstLine = options->screen.nativeHeight / 3; + const int gradLastLine = options->screen.nativeHeight; const int gradNumLines = gradLastLine - gradFirstLine; const int gradColorFrom = 0; @@ -203,14 +218,15 @@ int main(int argc, char *argv[]) float step = ((float)(i - gradFirstLine) / gradNumLines); int color = gradColorFrom + ((gradCurrentColor - gradColorFrom) * step); SDL_SetRenderDrawColor(renderer, color, 0x00, 0x00, 0xFF); - SDL_RenderDrawLine(renderer, 0, i, options->gameWidth, i); + SDL_RenderDrawLine(renderer, 0, i, options->screen.nativeWidth, i); } // Escribe el texto text->setZoom(2); - text->writeCentered(options->gameWidth / 2, text->getCharacterSize(), "Jail Engine DEMO"); + text->writeCentered(options->screen.nativeWidth / 2, text->getCharacterSize(), "Jail Engine DEMO"); text->disableZoom(); - text->writeCentered(options->gameWidth / 2, text->getCharacterSize() * 6, "Pulsa 'N' para mostrar una notificacion"); + text->writeCentered(options->screen.nativeWidth / 2, text->getCharacterSize() * 7, "Pulsa 'N' para mostrar"); + text->writeCentered(options->screen.nativeWidth / 2, text->getCharacterSize() * 8, "una notificacion"); // Dibuja el sprite sprite->render(); diff --git a/units/notify.cpp b/units/notify.cpp index 9050928..ad6a9fc 100644 --- a/units/notify.cpp +++ b/units/notify.cpp @@ -142,11 +142,11 @@ void Notify::showText(string text1, string text2, int icon) } else if (options->notifications.posH == pos_middle) { - despH = ((options->screen.windowWidth * options->windowSize) / 2 - (width / 2)); + despH = ((options->screen.nativeWidth * options->screen.windowZoom) / 2 - (width / 2)); } else { - despH = (options->screen.windowWidth * options->windowSize) - width - padding; + despH = (options->screen.nativeWidth * options->screen.windowZoom) - width - padding; } // Posición vertical @@ -157,7 +157,7 @@ void Notify::showText(string text1, string text2, int icon) } else { - despV = (options->screen.windowHeight * options->windowSize) - height - padding; + despV = (options->screen.nativeHeight * options->screen.windowZoom) - height - padding; } const int travelDist = height + padding; diff --git a/units/screen.cpp b/units/screen.cpp index 0cbfa8d..bdc1fad 100644 --- a/units/screen.cpp +++ b/units/screen.cpp @@ -10,10 +10,10 @@ Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, options_t *options) this->renderer = renderer; this->options = options; - gameCanvasWidth = options->gameWidth; - gameCanvasHeight = options->gameHeight; - borderWidth = options->borderWidth * 2; - borderHeight = options->borderHeight * 2; + gameCanvasWidth = options->screen.nativeWidth; + gameCanvasHeight = options->screen.nativeHeight; + borderWidth = options->screen.borderWidth * 2; + borderHeight = options->screen.borderHeight * 2; iniFade(); iniSpectrumFade(); @@ -32,7 +32,7 @@ Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, options_t *options) } // Establece el modo de video - setVideoMode(options->videoMode); + setVideoMode(options->screen.mode); } // Destructor @@ -90,7 +90,7 @@ void Screen::setVideoMode(int videoMode) // Muestra el puntero SDL_ShowCursor(SDL_ENABLE); - if (options->borderEnabled) + if (options->screen.borderEnabled) { windowWidth = gameCanvasWidth + borderWidth; windowHeight = gameCanvasHeight + borderHeight; @@ -99,13 +99,17 @@ void Screen::setVideoMode(int videoMode) else { - windowWidth = gameCanvasWidth; - windowHeight = gameCanvasHeight; - dest = {0, 0, gameCanvasWidth, gameCanvasHeight}; + //windowWidth = gameCanvasWidth; + windowWidth = gameCanvasWidth * options->screen.nativeZoom * options->screen.windowZoom; + //windowHeight = gameCanvasHeight; + windowHeight = gameCanvasHeight * options->screen.nativeZoom * options->screen.windowZoom; + //dest = {0, 0, gameCanvasWidth, gameCanvasHeight}; + dest = {0, 0, windowWidth, windowHeight}; } // Modifica el tamaño de la ventana - SDL_SetWindowSize(window, windowWidth * options->windowSize, windowHeight * options->windowSize); + //SDL_SetWindowSize(window, windowWidth * options->screen.windowZoom, windowHeight * options->screen.windowZoom); + SDL_SetWindowSize(window, windowWidth, windowHeight); SDL_SetWindowPosition(window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED); } @@ -119,7 +123,7 @@ void Screen::setVideoMode(int videoMode) SDL_GetWindowSize(window, &windowWidth, &windowHeight); // Aplica el escalado al rectangulo donde se pinta la textura del juego - if (options->integerScale) + if (options->screen.integerScale) { // Calcula el tamaño de la escala máxima int scale = 0; @@ -133,7 +137,7 @@ void Screen::setVideoMode(int videoMode) dest.x = (windowWidth - dest.w) / 2; dest.y = (windowHeight - dest.h) / 2; } - else if (options->keepAspect) + else if (options->screen.keepAspect) { float ratio = (float)gameCanvasWidth / (float)gameCanvasHeight; if ((windowWidth - gameCanvasWidth) >= (windowHeight - gameCanvasHeight)) @@ -163,38 +167,36 @@ void Screen::setVideoMode(int videoMode) SDL_RenderSetLogicalSize(renderer, windowWidth, windowHeight); // Actualiza las opciones - options->videoMode = videoMode; - options->screen.windowWidth = windowWidth; - options->screen.windowHeight = windowHeight; + options->screen.mode = videoMode; } // Camibia entre pantalla completa y ventana void Screen::switchVideoMode() { - options->videoMode = (options->videoMode == 0) ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0; - setVideoMode(options->videoMode); + options->screen.mode = (options->screen.mode == 0) ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0; + setVideoMode(options->screen.mode); } // Cambia el tamaño de la ventana void Screen::setWindowSize(int size) { - options->windowSize = size; + options->screen.windowZoom = size; setVideoMode(0); } // Reduce el tamaño de la ventana void Screen::decWindowSize() { - --options->windowSize; - options->windowSize = std::max(options->windowSize, 1); + --options->screen.windowZoom; + options->screen.windowZoom = std::max(options->screen.windowZoom, 1); setVideoMode(0); } // Aumenta el tamaño de la ventana void Screen::incWindowSize() { - ++options->windowSize; - options->windowSize = std::min(options->windowSize, 4); + ++options->screen.windowZoom; + options->screen.windowZoom = std::min(options->screen.windowZoom, 4); setVideoMode(0); } @@ -213,25 +215,25 @@ void Screen::setBlendMode(SDL_BlendMode blendMode) // Establece el tamaño del borde void Screen::setBorderWidth(int s) { - options->borderWidth = s; + options->screen.borderWidth = s; } // Establece el tamaño del borde void Screen::setBorderHeight(int s) { - options->borderHeight = s; + options->screen.borderHeight = s; } // Establece si se ha de ver el borde en el modo ventana void Screen::setBorderEnabled(bool value) { - options->borderEnabled = value; + options->screen.borderEnabled = value; } // Cambia entre borde visible y no visible void Screen::switchBorder() { - options->borderEnabled = !options->borderEnabled; + options->screen.borderEnabled = !options->screen.borderEnabled; setVideoMode(0); } diff --git a/units/utils.cpp b/units/utils.cpp index f7d84a5..0ec44e6 100644 --- a/units/utils.cpp +++ b/units/utils.cpp @@ -559,25 +559,24 @@ bool colorAreEqual(color_t color1, color_t color2) void initOptions(options_t *options) { options->configVersion = ""; - options->videoMode = 0; - options->windowSize = 1; - options->filter = 0; - options->vSync = true; - options->gameWidth = 320; - options->gameHeight = 240; - options->integerScale = true; - options->keepAspect = true; - options->borderEnabled = false; - options->borderWidth = 0; - options->borderHeight = 0; options->palette = p_zxspectrum; options->console = false; + options->screen.mode = 0; + options->screen.windowZoom = 1; + options->screen.filter = 0; + options->screen.vsync = true; + options->screen.nativeWidth = 320; + options->screen.nativeHeight = 240; + options->screen.nativeZoom = 1; + options->screen.integerScale = true; + options->screen.keepAspect = true; + options->screen.borderEnabled = false; + options->screen.borderWidth = 0; + options->screen.borderHeight = 0; + options->notifications.posV = pos_top; options->notifications.posH = pos_left; options->notifications.sound = true; options->notifications.color = {48, 48, 48}; - - options->screen.windowWidth = options->gameWidth * options->windowSize; - options->screen.windowHeight = options->gameHeight * options->windowSize; } \ No newline at end of file diff --git a/units/utils.h b/units/utils.h index 77c456b..3e80288 100644 --- a/units/utils.h +++ b/units/utils.h @@ -122,25 +122,24 @@ struct op_stats_t // Estructura con opciones de la pantalla struct op_screen_t { - int windowWidth; // Ancho de la ventana - int windowHeight; // Alto de la ventana + Uint32 mode; // Contiene el valor del modo de pantalla completa + Uint32 filter; // Filtro usado para el escalado de la imagen + bool vsync; // Indica si se quiere usar vsync o no + bool integerScale; // Indica si el escalado de la imagen ha de ser entero en el modo a pantalla completa + bool keepAspect; // Indica si se ha de mantener la relación de aspecto al poner el modo a pantalla completa + bool borderEnabled; // Indica si ha de mostrar el borde en el modo de ventana + int borderWidth; // Cantidad de pixels que se añade en el borde de la ventana + int borderHeight; // Cantidad de pixels que se añade en el borde de la ventana + int nativeWidth; // Ancho de la textura original de renderizado del juego + int nativeHeight; // Alto de la textura original de renderizado del juego + int nativeZoom; // Zoom que se aplica a la textura original de renderizado del juego + int windowZoom; // Zoom que se aplica a la ventana de juegoF }; // Estructura con todas las opciones de configuración del programa struct options_t { string configVersion; // Versión del programa. Sirve para saber si las opciones son compatibles - Uint32 videoMode; // Contiene el valor del modo de pantalla completa - int windowSize; // Contiene el valor por el que se multiplica el tamaño de la ventana - Uint32 filter; // Filtro usado para el escalado de la imagen - bool vSync; // Indica si se quiere usar vsync o no - int gameWidth; // Ancho de la resolucion nativa del juego - int gameHeight; // Alto de la resolucion nativa del juego - bool integerScale; // Indica si el escalado de la imagen ha de ser entero en el modo a pantalla completa - bool keepAspect; // Indica si se ha de mantener la relación de aspecto al poner el modo a pantalla completa - bool borderEnabled; // Indica si ha de mostrar el borde en el modo de ventana - int borderWidth; // Cantidad de pixels que se añade en el borde de la ventana - int borderHeight; // Cantidad de pixels que se añade en el borde de la ventana palette_e palette; // Paleta de colores a usar en el juego bool console; // Indica si ha de mostrar información por la consola de texto cheat_t cheat; // Contiene trucos y ventajas para el juego