From 063016cf5591dae67b58cf3ace7c7e15122d9a61 Mon Sep 17 00:00:00 2001 From: Raimon Zamora Date: Tue, 1 Jul 2025 06:28:54 +0200 Subject: [PATCH] - A que huelen los commits? --- source/jdraw.cpp | 65 ++++++++++++++++++++++++++++++++++++++++++++---- source/jdraw.h | 2 +- source/jgame.cpp | 8 +++++- source/jgame.h | 2 ++ 4 files changed, 70 insertions(+), 7 deletions(-) diff --git a/source/jdraw.cpp b/source/jdraw.cpp index 57ef1e6..a45d508 100644 --- a/source/jdraw.cpp +++ b/source/jdraw.cpp @@ -15,6 +15,15 @@ namespace draw SDL_Texture *sdl_texture {nullptr}; // La textura de SDL a la que pintarem la nostra superficie "screen" i que despres volcarem a pantalla static int screen_zoom = 1; + static bool screen_fullscreen = false; + static float window_ratio = 1; + static int canvas_width; + static int canvas_height; + static int desktop_width; + static int desktop_height; + static int window_width; + static int window_height; + char window_title[256]; surface *screen {nullptr}; // La superficie screen, que representa la pantalla. Se crea i destrueix internament surface *destination {nullptr}; // Punter a la actual superficie de destí @@ -34,26 +43,72 @@ namespace draw bool fading_out = false; bool fading_in = false; + void createDisplay() + { + // Ajustem el zoom i el ratio, per si tenen valors locs + if (screen_zoom <= 0) screen_zoom = 1; + if (window_ratio <= 0) window_ratio = 1; + + // Ajustem el tamany de la finestra, segons el zoom i el ratio + window_width = canvas_width*screen_zoom; + window_height = window_ratio != 1 ? canvas_width*window_ratio*screen_zoom : canvas_height*screen_zoom; + + // Mentres no càpiga en la pantalla, reduïm el zoom + while (window_width > desktop_width || window_height > desktop_height) { + screen_zoom--; + window_width = canvas_width*screen_zoom; + window_height = window_ratio != 1 ? canvas_width*window_ratio*screen_zoom : canvas_height*screen_zoom; + } + + sdl_window = SDL_CreateWindow(window_title, window_width, window_height, SDL_WINDOW_OPENGL|(screen_fullscreen?SDL_WINDOW_FULLSCREEN:0)); + if (!sdl_window) { + SDL_LogCritical(SDL_LOG_CATEGORY_VIDEO, "ERROR (draw::init): Failed to initialize window!\n"); + exit(1); + } + + } + + void destroyDisplay() + { + SDL_DestroyTexture(sdl_texture); + SDL_DestroyRenderer(sdl_renderer); + SDL_DestroyWindow(sdl_window); + } + // Inicialització de tot el que fa falta per a carregar gràfics i pintar en pantalla - void init(const char *titol, const uint16_t width, const uint16_t height, const int zoom) + void init(const char *titol, const uint16_t width, const uint16_t height, const int zoom, const bool fullscreen, const float ratio) { screen_zoom = zoom; + window_ratio = ratio; + canvas_width = width; + canvas_height = height; + strcpy(window_title, titol); - // [TODO] Incloure gestió de pantalla completa + const SDL_DisplayMode *dm = SDL_GetDesktopDisplayMode(SDL_GetPrimaryDisplay()); + if (!dm) + { + SDL_Log("SDL_GetDesktopDisplayMode failed: %s", SDL_GetError()); + exit(1); + } + desktop_width = dm->w; + desktop_height = dm->h; // Inicialització de les estructures de SDL sdl_window = SDL_CreateWindow(titol, width * zoom, height * zoom, 0); if (!sdl_window) { - SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "ERROR (draw::init): Failed to initialize window!\n"); exit(1); + SDL_LogCritical(SDL_LOG_CATEGORY_VIDEO, "ERROR (draw::init): Failed to initialize window!\n"); + exit(1); } sdl_renderer = SDL_CreateRenderer(sdl_window, NULL); if (!sdl_renderer) { - SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "ERROR (draw::init): Failed to initialize renderer!\n"); exit(1); + SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "ERROR (draw::init): Failed to initialize renderer!\n"); + exit(1); } sdl_texture = SDL_CreateTexture(sdl_renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, width, height); SDL_SetTextureScaleMode(sdl_texture, SDL_SCALEMODE_NEAREST); if (!sdl_texture) { - SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "ERROR (draw::init): Failed to initialize texture!\n"); exit(1); + SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "ERROR (draw::init): Failed to initialize texture!\n"); + exit(1); } // Creem la superficie "screen" i la establim com a superficie destinació diff --git a/source/jdraw.h b/source/jdraw.h index 7b93730..126907b 100644 --- a/source/jdraw.h +++ b/source/jdraw.h @@ -25,7 +25,7 @@ namespace draw /// @param width es el ample de la finestra "virtual" /// @param height es el alt de la finestra "virtual" /// @param zoom es com de grans son els pixels. - void init(const char *titol, const uint16_t width, const uint16_t height, const int zoom); + void init(const char *titol, const uint16_t width, const uint16_t height, const int zoom, const float ratio=1.0); /// @brief Finalització del sistema (tancar coses de SDL, superficies fixes, etc...) void quit(); diff --git a/source/jgame.cpp b/source/jgame.cpp index 9e0e217..afe4ec8 100644 --- a/source/jgame.cpp +++ b/source/jgame.cpp @@ -54,7 +54,13 @@ int main(int argc, char *argv[]) #endif SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO); - + + #ifdef DEBUG + SDL_SetLogPriorities(SDL_LOG_PRIORITY_DEBUG); + #endif + + SDL_LogInfo(SDL_LOG_CATEGORY_SYSTEM, "JAPI v%s\n", JAPI_VERSION); + game::windowHasFocus = true; game::init(); input::init(draw::getZoom()); diff --git a/source/jgame.h b/source/jgame.h index d09989f..f906555 100644 --- a/source/jgame.h +++ b/source/jgame.h @@ -5,6 +5,8 @@ #include "jaudio.h" #include "jfile.h" +#define JAPI_VERSION "0.8" + namespace game { extern bool windowHasFocus;