From d1143b9dfe92687a46ebccc6582457104491bc1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Valor=20Mart=C3=ADnez?= Date: Fri, 10 Feb 2023 22:55:36 +0100 Subject: [PATCH] =?UTF-8?q?-=20Trabajando=20en=20la=20creaci=C3=B3n=20y=20?= =?UTF-8?q?destrucci=C3=B3n=20de=20la=20ventana=20-=20FIX:=20la=20clase=20?= =?UTF-8?q?screen=20no=20liberaba=20la=20textura=20gameCanvas=20al=20final?= =?UTF-8?q?izar?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/common/screen.cpp | 89 ++++++++++++++++++++++++++++++++++++---- source/common/screen.h | 7 ++++ source/director.cpp | 2 +- source/game.cpp | 1 + 4 files changed, 90 insertions(+), 9 deletions(-) diff --git a/source/common/screen.cpp b/source/common/screen.cpp index 6d48927..e6454a5 100644 --- a/source/common/screen.cpp +++ b/source/common/screen.cpp @@ -44,10 +44,76 @@ Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, options notifyActive = false; } +// Crea la ventana +void Screen::createDisplay() +{ + window = SDL_CreateWindow(WINDOW_CAPTION, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, windowWidth * options->windowSize, windowHeight * options->windowSize, SDL_WINDOW_SHOWN | SDL_WINDOW_ALLOW_HIGHDPI); + if (window == nullptr) + { + if (options->console) + { + std::cout << "Window could not be created!\nSDL Error: " << SDL_GetError() << std::endl; + } + } + else + { + // Crea un renderizador para la ventana. El vsync se activa en funcion de las opciones + if (options->vSync) + { + renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); + } + else + { + renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); + } + + if (renderer == nullptr) + { + if (options->console) + { + std::cout << "Renderer could not be created!\nSDL Error: " << SDL_GetError() << std::endl; + } + } + else + { + // Inicializa el color de renderizado + SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xFF); + + // Establece el tamaño del buffer de renderizado + SDL_RenderSetLogicalSize(renderer, windowWidth, windowHeight); + + // Establece el modo de mezcla + SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND); + + // Muestra el puntero + SDL_ShowCursor(SDL_ENABLE); + + // Crea la textura donde se dibujan los graficos del juego + gameCanvas = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, gameCanvasWidth, gameCanvasHeight); + if (gameCanvas == nullptr) + { + if (options->console) + { + std::cout << "TitleSurface could not be created!\nSDL Error: " << SDL_GetError() << std::endl; + } + } + } + } +} + +// Destruye la ventana +void Screen::destroyDisplay() +{ + SDL_DestroyTexture(gameCanvas); + SDL_DestroyRenderer(renderer); + SDL_DestroyWindow(window); +} + // Destructor Screen::~Screen() { delete notify; + SDL_DestroyTexture(gameCanvas); } // Limpia la pantalla @@ -86,11 +152,8 @@ void Screen::blit() // Establece el modo de video void Screen::setVideoMode(int videoMode) { - // Muestra el puntero - SDL_ShowCursor(SDL_ENABLE); - - // Aplica el modo de video - SDL_SetWindowFullscreen(window, videoMode); + // Destruye la ventana + destroyDisplay(); // Si está activo el modo ventana quita el borde if (videoMode == 0) @@ -109,9 +172,19 @@ void Screen::setVideoMode(int videoMode) dest = {0, 0, gameCanvasWidth, gameCanvasHeight}; } - // Modifica el tamaño del renderizador y de la ventana - SDL_RenderSetLogicalSize(renderer, windowWidth, windowHeight); - SDL_SetWindowSize(window, windowWidth * options->windowSize, windowHeight * options->windowSize); + createDisplay(); + + /* + // Modifica el tamaño del renderizador y de la ventana + SDL_RenderSetLogicalSize(renderer, windowWidth, windowHeight); + // SDL_SetWindowSize(window, windowWidth * options->windowSize, windowHeight * options->windowSize); + + // Muestra el puntero + SDL_ShowCursor(SDL_ENABLE); + */ + + // Aplica el modo de video + SDL_SetWindowFullscreen(window, videoMode); } // Si está activo el modo de pantalla completa añade el borde diff --git a/source/common/screen.h b/source/common/screen.h index a657292..c0b8bc8 100644 --- a/source/common/screen.h +++ b/source/common/screen.h @@ -4,6 +4,7 @@ #include "asset.h" #include "notify.h" #include "utils.h" +#include "../const.h" #include #ifndef SCREEN_H @@ -45,6 +46,12 @@ private: int spectrumFadeLenght; // Duración del fade spectrum std::vector spectrumColor; // Colores para el fade spectrum + // Crea la ventana + void createDisplay(); + + // Destruye la ventana + void destroyDisplay(); + // Inicializa las variables para el fade void iniFade(); diff --git a/source/director.cpp b/source/director.cpp index d8dbe69..69a154b 100644 --- a/source/director.cpp +++ b/source/director.cpp @@ -19,7 +19,7 @@ Director::Director(int argc, char *argv[]) section->subsection = SUBSECTION_LOGO_TO_INTRO; #ifdef DEBUG - section->name = SECTION_PROG_LOGO; + section->name = SECTION_PROG_GAME; #endif // Crea e inicializa las opciones del programa diff --git a/source/game.cpp b/source/game.cpp index e88f15e..0a1c94d 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -21,6 +21,7 @@ Game::Game(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset *as const int x = 25; const int y = 13; spawnPoint = {x * 8, y * 8, 0, 0, 0, s_standing, SDL_FLIP_HORIZONTAL}; + debug->setEnabled(true); #else currentRoom = "03.room"; const int x = 25;