- Trabajando en la creación y destrucción de la ventana

- FIX: la clase screen no liberaba la textura gameCanvas al finalizar
This commit is contained in:
2023-02-10 22:55:36 +01:00
parent 68ebff722e
commit d1143b9dfe
4 changed files with 90 additions and 9 deletions

View File

@@ -44,10 +44,76 @@ Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, options
notifyActive = false; 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 // Destructor
Screen::~Screen() Screen::~Screen()
{ {
delete notify; delete notify;
SDL_DestroyTexture(gameCanvas);
} }
// Limpia la pantalla // Limpia la pantalla
@@ -86,11 +152,8 @@ void Screen::blit()
// Establece el modo de video // Establece el modo de video
void Screen::setVideoMode(int videoMode) void Screen::setVideoMode(int videoMode)
{ {
// Muestra el puntero // Destruye la ventana
SDL_ShowCursor(SDL_ENABLE); destroyDisplay();
// Aplica el modo de video
SDL_SetWindowFullscreen(window, videoMode);
// Si está activo el modo ventana quita el borde // Si está activo el modo ventana quita el borde
if (videoMode == 0) if (videoMode == 0)
@@ -109,9 +172,19 @@ void Screen::setVideoMode(int videoMode)
dest = {0, 0, gameCanvasWidth, gameCanvasHeight}; dest = {0, 0, gameCanvasWidth, gameCanvasHeight};
} }
// Modifica el tamaño del renderizador y de la ventana createDisplay();
SDL_RenderSetLogicalSize(renderer, windowWidth, windowHeight);
SDL_SetWindowSize(window, windowWidth * options->windowSize, windowHeight * options->windowSize); /*
// 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 // Si está activo el modo de pantalla completa añade el borde

View File

@@ -4,6 +4,7 @@
#include "asset.h" #include "asset.h"
#include "notify.h" #include "notify.h"
#include "utils.h" #include "utils.h"
#include "../const.h"
#include <vector> #include <vector>
#ifndef SCREEN_H #ifndef SCREEN_H
@@ -45,6 +46,12 @@ private:
int spectrumFadeLenght; // Duración del fade spectrum int spectrumFadeLenght; // Duración del fade spectrum
std::vector<color_t> spectrumColor; // Colores para el fade spectrum std::vector<color_t> spectrumColor; // Colores para el fade spectrum
// Crea la ventana
void createDisplay();
// Destruye la ventana
void destroyDisplay();
// Inicializa las variables para el fade // Inicializa las variables para el fade
void iniFade(); void iniFade();

View File

@@ -19,7 +19,7 @@ Director::Director(int argc, char *argv[])
section->subsection = SUBSECTION_LOGO_TO_INTRO; section->subsection = SUBSECTION_LOGO_TO_INTRO;
#ifdef DEBUG #ifdef DEBUG
section->name = SECTION_PROG_LOGO; section->name = SECTION_PROG_GAME;
#endif #endif
// Crea e inicializa las opciones del programa // Crea e inicializa las opciones del programa

View File

@@ -21,6 +21,7 @@ Game::Game(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset *as
const int x = 25; const int x = 25;
const int y = 13; const int y = 13;
spawnPoint = {x * 8, y * 8, 0, 0, 0, s_standing, SDL_FLIP_HORIZONTAL}; spawnPoint = {x * 8, y * 8, 0, 0, 0, s_standing, SDL_FLIP_HORIZONTAL};
debug->setEnabled(true);
#else #else
currentRoom = "03.room"; currentRoom = "03.room";
const int x = 25; const int x = 25;