Demo: implementado el cambio de tamaño de la ventana

This commit is contained in:
2023-05-07 14:22:14 +02:00
parent 9b9fd15f4e
commit 90ebea4807
5 changed files with 82 additions and 66 deletions

View File

@@ -32,8 +32,10 @@ int main(int argc, char *argv[])
// Inicializa las opciones // Inicializa las opciones
struct options_t *options = new options_t; struct options_t *options = new options_t;
initOptions(options); initOptions(options);
options->gameWidth = 640; options->screen.nativeWidth = 320;
options->gameHeight = 480; options->screen.nativeHeight = 240;
options->screen.nativeZoom = 1;
options->screen.windowZoom = 2;
options->console = true; options->console = true;
// Inicializa la lista de recursos // Inicializa la lista de recursos
@@ -53,7 +55,7 @@ int main(int argc, char *argv[])
// Inicializa SDL y la ventana // Inicializa SDL y la ventana
SDL_Init(SDL_INIT_EVERYTHING); 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) if (window != nullptr)
{ {
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); 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) 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: case SDL_SCANCODE_N:
screen->showNotification("Ejemplo de notificacion", "con 2 lineas de texto", 0); screen->showNotification("Ejemplo de notificacion", "con 2 lineas de texto", 0);
break; break;
@@ -139,7 +154,7 @@ int main(int argc, char *argv[])
screen->update(); screen->update();
// Actualiza el sprite // 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(); sprite->undoMoveX();
int spr_direction = 1; int spr_direction = 1;
@@ -155,7 +170,7 @@ int main(int argc, char *argv[])
sprite->setVelX(spr_force * spr_direction); sprite->setVelX(spr_force * spr_direction);
JA_PlaySound(sound); 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(); sprite->undoMoveY();
int spr_direction = 1; int spr_direction = 1;
@@ -193,8 +208,8 @@ int main(int argc, char *argv[])
screen->clean(); screen->clean();
// Dibuja un degradado de fondo // Dibuja un degradado de fondo
const int gradFirstLine = options->gameHeight / 3; const int gradFirstLine = options->screen.nativeHeight / 3;
const int gradLastLine = options->gameHeight; const int gradLastLine = options->screen.nativeHeight;
const int gradNumLines = gradLastLine - gradFirstLine; const int gradNumLines = gradLastLine - gradFirstLine;
const int gradColorFrom = 0; const int gradColorFrom = 0;
@@ -203,14 +218,15 @@ int main(int argc, char *argv[])
float step = ((float)(i - gradFirstLine) / gradNumLines); float step = ((float)(i - gradFirstLine) / gradNumLines);
int color = gradColorFrom + ((gradCurrentColor - gradColorFrom) * step); int color = gradColorFrom + ((gradCurrentColor - gradColorFrom) * step);
SDL_SetRenderDrawColor(renderer, color, 0x00, 0x00, 0xFF); 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 // Escribe el texto
text->setZoom(2); 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->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 // Dibuja el sprite
sprite->render(); sprite->render();

View File

@@ -142,11 +142,11 @@ void Notify::showText(string text1, string text2, int icon)
} }
else if (options->notifications.posH == pos_middle) 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 else
{ {
despH = (options->screen.windowWidth * options->windowSize) - width - padding; despH = (options->screen.nativeWidth * options->screen.windowZoom) - width - padding;
} }
// Posición vertical // Posición vertical
@@ -157,7 +157,7 @@ void Notify::showText(string text1, string text2, int icon)
} }
else else
{ {
despV = (options->screen.windowHeight * options->windowSize) - height - padding; despV = (options->screen.nativeHeight * options->screen.windowZoom) - height - padding;
} }
const int travelDist = height + padding; const int travelDist = height + padding;

View File

@@ -10,10 +10,10 @@ Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, options_t *options)
this->renderer = renderer; this->renderer = renderer;
this->options = options; this->options = options;
gameCanvasWidth = options->gameWidth; gameCanvasWidth = options->screen.nativeWidth;
gameCanvasHeight = options->gameHeight; gameCanvasHeight = options->screen.nativeHeight;
borderWidth = options->borderWidth * 2; borderWidth = options->screen.borderWidth * 2;
borderHeight = options->borderHeight * 2; borderHeight = options->screen.borderHeight * 2;
iniFade(); iniFade();
iniSpectrumFade(); iniSpectrumFade();
@@ -32,7 +32,7 @@ Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, options_t *options)
} }
// Establece el modo de video // Establece el modo de video
setVideoMode(options->videoMode); setVideoMode(options->screen.mode);
} }
// Destructor // Destructor
@@ -90,7 +90,7 @@ void Screen::setVideoMode(int videoMode)
// Muestra el puntero // Muestra el puntero
SDL_ShowCursor(SDL_ENABLE); SDL_ShowCursor(SDL_ENABLE);
if (options->borderEnabled) if (options->screen.borderEnabled)
{ {
windowWidth = gameCanvasWidth + borderWidth; windowWidth = gameCanvasWidth + borderWidth;
windowHeight = gameCanvasHeight + borderHeight; windowHeight = gameCanvasHeight + borderHeight;
@@ -99,13 +99,17 @@ void Screen::setVideoMode(int videoMode)
else else
{ {
windowWidth = gameCanvasWidth; //windowWidth = gameCanvasWidth;
windowHeight = gameCanvasHeight; windowWidth = gameCanvasWidth * options->screen.nativeZoom * options->screen.windowZoom;
dest = {0, 0, gameCanvasWidth, gameCanvasHeight}; //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 // 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); SDL_SetWindowPosition(window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
} }
@@ -119,7 +123,7 @@ void Screen::setVideoMode(int videoMode)
SDL_GetWindowSize(window, &windowWidth, &windowHeight); SDL_GetWindowSize(window, &windowWidth, &windowHeight);
// Aplica el escalado al rectangulo donde se pinta la textura del juego // 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 // Calcula el tamaño de la escala máxima
int scale = 0; int scale = 0;
@@ -133,7 +137,7 @@ void Screen::setVideoMode(int videoMode)
dest.x = (windowWidth - dest.w) / 2; dest.x = (windowWidth - dest.w) / 2;
dest.y = (windowHeight - dest.h) / 2; dest.y = (windowHeight - dest.h) / 2;
} }
else if (options->keepAspect) else if (options->screen.keepAspect)
{ {
float ratio = (float)gameCanvasWidth / (float)gameCanvasHeight; float ratio = (float)gameCanvasWidth / (float)gameCanvasHeight;
if ((windowWidth - gameCanvasWidth) >= (windowHeight - gameCanvasHeight)) if ((windowWidth - gameCanvasWidth) >= (windowHeight - gameCanvasHeight))
@@ -163,38 +167,36 @@ void Screen::setVideoMode(int videoMode)
SDL_RenderSetLogicalSize(renderer, windowWidth, windowHeight); SDL_RenderSetLogicalSize(renderer, windowWidth, windowHeight);
// Actualiza las opciones // Actualiza las opciones
options->videoMode = videoMode; options->screen.mode = videoMode;
options->screen.windowWidth = windowWidth;
options->screen.windowHeight = windowHeight;
} }
// Camibia entre pantalla completa y ventana // Camibia entre pantalla completa y ventana
void Screen::switchVideoMode() void Screen::switchVideoMode()
{ {
options->videoMode = (options->videoMode == 0) ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0; options->screen.mode = (options->screen.mode == 0) ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0;
setVideoMode(options->videoMode); setVideoMode(options->screen.mode);
} }
// Cambia el tamaño de la ventana // Cambia el tamaño de la ventana
void Screen::setWindowSize(int size) void Screen::setWindowSize(int size)
{ {
options->windowSize = size; options->screen.windowZoom = size;
setVideoMode(0); setVideoMode(0);
} }
// Reduce el tamaño de la ventana // Reduce el tamaño de la ventana
void Screen::decWindowSize() void Screen::decWindowSize()
{ {
--options->windowSize; --options->screen.windowZoom;
options->windowSize = std::max(options->windowSize, 1); options->screen.windowZoom = std::max(options->screen.windowZoom, 1);
setVideoMode(0); setVideoMode(0);
} }
// Aumenta el tamaño de la ventana // Aumenta el tamaño de la ventana
void Screen::incWindowSize() void Screen::incWindowSize()
{ {
++options->windowSize; ++options->screen.windowZoom;
options->windowSize = std::min(options->windowSize, 4); options->screen.windowZoom = std::min(options->screen.windowZoom, 4);
setVideoMode(0); setVideoMode(0);
} }
@@ -213,25 +215,25 @@ void Screen::setBlendMode(SDL_BlendMode blendMode)
// Establece el tamaño del borde // Establece el tamaño del borde
void Screen::setBorderWidth(int s) void Screen::setBorderWidth(int s)
{ {
options->borderWidth = s; options->screen.borderWidth = s;
} }
// Establece el tamaño del borde // Establece el tamaño del borde
void Screen::setBorderHeight(int s) void Screen::setBorderHeight(int s)
{ {
options->borderHeight = s; options->screen.borderHeight = s;
} }
// Establece si se ha de ver el borde en el modo ventana // Establece si se ha de ver el borde en el modo ventana
void Screen::setBorderEnabled(bool value) void Screen::setBorderEnabled(bool value)
{ {
options->borderEnabled = value; options->screen.borderEnabled = value;
} }
// Cambia entre borde visible y no visible // Cambia entre borde visible y no visible
void Screen::switchBorder() void Screen::switchBorder()
{ {
options->borderEnabled = !options->borderEnabled; options->screen.borderEnabled = !options->screen.borderEnabled;
setVideoMode(0); setVideoMode(0);
} }

View File

@@ -559,25 +559,24 @@ bool colorAreEqual(color_t color1, color_t color2)
void initOptions(options_t *options) void initOptions(options_t *options)
{ {
options->configVersion = ""; 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->palette = p_zxspectrum;
options->console = false; 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.posV = pos_top;
options->notifications.posH = pos_left; options->notifications.posH = pos_left;
options->notifications.sound = true; options->notifications.sound = true;
options->notifications.color = {48, 48, 48}; options->notifications.color = {48, 48, 48};
options->screen.windowWidth = options->gameWidth * options->windowSize;
options->screen.windowHeight = options->gameHeight * options->windowSize;
} }

View File

@@ -122,25 +122,24 @@ struct op_stats_t
// Estructura con opciones de la pantalla // Estructura con opciones de la pantalla
struct op_screen_t struct op_screen_t
{ {
int windowWidth; // Ancho de la ventana Uint32 mode; // Contiene el valor del modo de pantalla completa
int windowHeight; // Alto de la ventana 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 // Estructura con todas las opciones de configuración del programa
struct options_t struct options_t
{ {
string configVersion; // Versión del programa. Sirve para saber si las opciones son compatibles 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 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 bool console; // Indica si ha de mostrar información por la consola de texto
cheat_t cheat; // Contiene trucos y ventajas para el juego cheat_t cheat; // Contiene trucos y ventajas para el juego