- La ventana ya no se destruye al cambiar de tamaño de ventana

- La ventana aparece centrada al cambiar de tamaño
This commit is contained in:
2023-02-12 17:05:16 +01:00
parent d1143b9dfe
commit 8b3d257baf
3 changed files with 12 additions and 90 deletions

View File

@@ -44,71 +44,6 @@ 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()
{ {
@@ -152,12 +87,15 @@ void Screen::blit()
// Establece el modo de video // Establece el modo de video
void Screen::setVideoMode(int videoMode) void Screen::setVideoMode(int videoMode)
{ {
// Destruye la ventana // Aplica el modo de video
destroyDisplay(); 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)
{ {
// Muestra el puntero
SDL_ShowCursor(SDL_ENABLE);
if (options->borderEnabled) if (options->borderEnabled)
{ {
windowWidth = gameCanvasWidth + borderWidth; windowWidth = gameCanvasWidth + borderWidth;
@@ -172,19 +110,9 @@ void Screen::setVideoMode(int videoMode)
dest = {0, 0, gameCanvasWidth, gameCanvasHeight}; dest = {0, 0, gameCanvasWidth, gameCanvasHeight};
} }
createDisplay(); // Modifica el tamaño de la ventana
SDL_SetWindowSize(window, windowWidth * options->windowSize, windowHeight * options->windowSize);
/* SDL_SetWindowPosition(window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
// 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
@@ -235,10 +163,10 @@ void Screen::setVideoMode(int videoMode)
dest.h = windowHeight; dest.h = windowHeight;
dest.x = dest.y = 0; dest.x = dest.y = 0;
} }
}
// Modifica el tamaño del renderizador // Modifica el tamaño del renderizador
SDL_RenderSetLogicalSize(renderer, windowWidth, windowHeight); SDL_RenderSetLogicalSize(renderer, windowWidth, windowHeight);
}
// Actualiza las opciones // Actualiza las opciones
options->videoMode = videoMode; options->videoMode = videoMode;

View File

@@ -46,12 +46,6 @@ 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

@@ -21,7 +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); debug->setEnabled(false);
#else #else
currentRoom = "03.room"; currentRoom = "03.room";
const int x = 25; const int x = 25;