From 0d1836a8ba3cc57c74171caf2699fa93f2fdc0a3 Mon Sep 17 00:00:00 2001 From: Linus Probert Date: Thu, 30 Aug 2018 00:09:55 +0200 Subject: [PATCH] Passable fullscreen mode - Centering is still a bit off --- src/input.c | 4 ++-- src/input.h | 1 + src/main.c | 47 ++++++++++++++++++++++++++++++++++++----------- 3 files changed, 39 insertions(+), 13 deletions(-) diff --git a/src/input.c b/src/input.c index cb8182d..9625d83 100644 --- a/src/input.c +++ b/src/input.c @@ -109,6 +109,8 @@ get_event_modkey(SDL_Event *event) key = KEY_CTRL_M; break; case SDLK_d: key = KEY_CTRL_D; break; + case SDLK_f: + key = KEY_CTRL_F; break; } } else if (event->key.keysym.mod & (KMOD_LSHIFT | KMOD_RSHIFT)) { switch (event->key.keysym.sym) { @@ -122,8 +124,6 @@ get_event_modkey(SDL_Event *event) key = KEY_SHIFT_NUM4; break; case SDLK_5: key = KEY_SHIFT_NUM5; break; - default: - key = 0; break; } } return key; diff --git a/src/input.h b/src/input.h index 78e3f1b..f08b438 100644 --- a/src/input.h +++ b/src/input.h @@ -47,6 +47,7 @@ #define KEY_SHIFT_NUM3 0x20 #define KEY_SHIFT_NUM4 0x40 #define KEY_SHIFT_NUM5 0x80 +#define KEY_CTRL_F 0x100 #define MBUTTON_LEFT 1 #define MBUTTON_MIDDLE 2 diff --git a/src/main.c b/src/main.c index 756241f..b5448a8 100644 --- a/src/main.c +++ b/src/main.c @@ -152,6 +152,7 @@ static float deltaTime = 1.0; static double renderScale = 1.0; static Turn currentTurn = PLAYER; static GameState gGameState; +static SDL_Rect mainViewport; static SDL_Rect gameViewport; static SDL_Rect skillBarViewport; static SDL_Rect bottomGuiViewport; @@ -250,26 +251,30 @@ bool initSDL(void) } static void -initViewports(void) +initViewports(Uint32 offset) { - gameViewport = (SDL_Rect) { 0, 0, + mainViewport = (SDL_Rect) { offset, 0, + SCREEN_HEIGHT, SCREEN_WIDTH + }; + + gameViewport = (SDL_Rect) { offset, 0, GAME_VIEW_WIDTH, GAME_VIEW_HEIGHT }; - skillBarViewport = (SDL_Rect) { 0, GAME_VIEW_HEIGHT, + skillBarViewport = (SDL_Rect) { offset, GAME_VIEW_HEIGHT, SKILL_BAR_WIDTH, SKILL_BAR_HEIGHT }; - bottomGuiViewport = (SDL_Rect) { 0, GAME_VIEW_HEIGHT + SKILL_BAR_HEIGHT, + bottomGuiViewport = (SDL_Rect) { offset, GAME_VIEW_HEIGHT + SKILL_BAR_HEIGHT, BOTTOM_GUI_WIDTH, BOTTOM_GUI_WIDTH }; - statsGuiViewport = (SDL_Rect) { GAME_VIEW_WIDTH, 0, + statsGuiViewport = (SDL_Rect) { offset + GAME_VIEW_WIDTH, 0, RIGHT_GUI_WIDTH, STATS_GUI_HEIGHT }; - minimapViewport = (SDL_Rect) { GAME_VIEW_WIDTH, STATS_GUI_HEIGHT, + minimapViewport = (SDL_Rect) { offset + GAME_VIEW_WIDTH, STATS_GUI_HEIGHT, RIGHT_GUI_WIDTH, MINIMAP_GUI_HEIGHT }; menuViewport = (SDL_Rect) { - (SCREEN_WIDTH - GAME_VIEW_WIDTH)/2, - (SCREEN_HEIGHT - GAME_VIEW_HEIGHT)/2, + offset + ((SCREEN_WIDTH - GAME_VIEW_WIDTH) >> 1), + (SCREEN_HEIGHT - GAME_VIEW_HEIGHT) >> 1, GAME_VIEW_WIDTH, GAME_VIEW_HEIGHT }; @@ -278,7 +283,7 @@ initViewports(void) static bool initGame(void) { - initViewports(); + initViewports(0); input_init(&input); texturecache_init(gRenderer); gCamera = camera_create(gRenderer); @@ -585,6 +590,26 @@ handle_main_input(void) else gui_log("Tooltips disabled"); } + + if (input_modkey_is_pressed(&input, KEY_CTRL_F)) { + bool isFullscreen = SDL_GetWindowFlags(gWindow) & SDL_WINDOW_FULLSCREEN_DESKTOP; + if (isFullscreen) { + initViewports(0); + SDL_SetWindowFullscreen(gWindow, 0); + } + else { + int w, h; + SDL_RenderGetLogicalSize(gRenderer, &w, &h); + + SDL_SetWindowFullscreen(gWindow, SDL_WINDOW_FULLSCREEN_DESKTOP); + + SDL_DisplayMode dMode; + SDL_GetWindowDisplayMode(gWindow, &dMode); + double ratio = (double) w / (double) dMode.w; + double offset = (double) (dMode.w - w) / 2; + initViewports((Uint32) (offset * ratio)); + } + } } static bool @@ -755,7 +780,7 @@ render_gui(void) skillbar_render(gSkillBar, gPlayer, gCamera); SDL_RenderSetViewport(gRenderer, &bottomGuiViewport); gui_render_log(gGui, gCamera); - SDL_RenderSetViewport(gRenderer, NULL); + SDL_RenderSetViewport(gRenderer, &mainViewport); } static void @@ -902,7 +927,7 @@ run_menu(void) map_render_top_layer(gMap, gRoomMatrix, gCamera); roommatrix_render_lightmap(gRoomMatrix, gCamera); - SDL_RenderSetViewport(gRenderer, NULL); + SDL_RenderSetViewport(gRenderer, &mainViewport); if (gGameState == MENU) menu_render(mainMenu, gCamera);