Skip to content

Commit

Permalink
shell: improve support for windowed mode
Browse files Browse the repository at this point in the history
Resolves #896.
  • Loading branch information
rr- committed Sep 10, 2023
1 parent bfab140 commit 863d021
Show file tree
Hide file tree
Showing 13 changed files with 139 additions and 52 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
- moved the enable_game_modes option from the gameflow to the config tool and added a gameflow option to override (#962)
- moved the enable_save_crystals option from the gameflow to the config tool (#962)
- improved Spanish localization for the config tool
- improved support for windowed mode (#896)

## [2.15.3](https://github.com/rr-/Tomb1Main/compare/2.15.2...2.15.3) - 2023-08-15
- fixed Lara stuttering when performing certain animations (#901, regression from 2.14)
Expand Down
15 changes: 14 additions & 1 deletion src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,12 @@ bool Config_ReadFromJSON(const char *cfg_data)

// User settings
READ_INTEGER(rendering.render_mode, GFX_RM_LEGACY);
READ_BOOL(rendering.enable_fullscreen, true);
READ_BOOL(rendering.enable_maximized, true);
READ_INTEGER(rendering.window_x, -1);
READ_INTEGER(rendering.window_y, -1);
READ_INTEGER(rendering.window_width, -1);
READ_INTEGER(rendering.window_height, -1);
READ_BOOL(rendering.enable_bilinear_filter, true);
READ_BOOL(rendering.enable_perspective_filter, true);
READ_BOOL(rendering.enable_vsync, true);
Expand Down Expand Up @@ -316,6 +322,8 @@ bool Config_ReadFromJSON(const char *cfg_data)
if (root) {
json_value_free(root);
}

g_Config.loaded = true;
return result;
}

Expand Down Expand Up @@ -430,8 +438,13 @@ bool Config_Write(void)
WRITE_BOOL(enable_game_modes);
WRITE_BOOL(enable_save_crystals);

// User settings
WRITE_INTEGER(rendering.render_mode);
WRITE_BOOL(rendering.enable_fullscreen);
WRITE_BOOL(rendering.enable_maximized);
WRITE_INTEGER(rendering.window_x);
WRITE_INTEGER(rendering.window_y);
WRITE_INTEGER(rendering.window_width);
WRITE_INTEGER(rendering.window_height);
WRITE_BOOL(rendering.enable_bilinear_filter);
WRITE_BOOL(rendering.enable_perspective_filter);
WRITE_BOOL(rendering.enable_vsync);
Expand Down
8 changes: 8 additions & 0 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ typedef enum {
} BAR_SHOW_MODE;

typedef struct {
bool loaded;

bool disable_healing_between_levels;
bool disable_medpacks;
bool disable_magnums;
Expand Down Expand Up @@ -127,6 +129,12 @@ typedef struct {

struct {
GFX_RENDER_MODE render_mode;
bool enable_fullscreen;
bool enable_maximized;
int32_t window_x;
int32_t window_y;
int32_t window_width;
int32_t window_height;
bool enable_perspective_filter;
bool enable_bilinear_filter;
bool enable_vsync;
Expand Down
5 changes: 0 additions & 5 deletions src/game/output.c
Original file line number Diff line number Diff line change
Expand Up @@ -389,11 +389,6 @@ void Output_SetWindowSize(int width, int height)
S_Output_SetWindowSize(width, height);
}

void Output_SetFullscreen(bool fullscreen)
{
S_Output_SetFullscreen(fullscreen);
}

void Output_ApplyRenderSettings(void)
{
S_Output_ApplyRenderSettings();
Expand Down
1 change: 0 additions & 1 deletion src/game/output.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ bool Output_Init(void);
void Output_Shutdown(void);

void Output_SetWindowSize(int width, int height);
void Output_SetFullscreen(bool fullscreen);
void Output_ApplyRenderSettings(void);
void Output_DownloadTextures(int page_count);

Expand Down
2 changes: 1 addition & 1 deletion src/game/shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ static char *Shell_GetScreenshotName(void)

void Shell_Init(const char *gameflow_path)
{
S_Shell_SeedRandom();
S_Shell_Init();

if (!Output_Init()) {
Shell_ExitSystem("Could not initialise video system");
Expand Down
11 changes: 0 additions & 11 deletions src/gfx/context.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ typedef struct GFX_CONTEXT {
int32_t window_width;
int32_t window_height;

bool is_fullscreen; // fullscreen flag
bool is_rendered; // rendering flag
char *scheduled_screenshot_path;
GFX_FBO_Renderer renderer_fbo;
Expand Down Expand Up @@ -180,16 +179,6 @@ void GFX_Context_SetVSync(bool vsync)
SDL_GL_SetSwapInterval(vsync);
}

bool GFX_Context_IsFullscreen(void)
{
return m_Context.is_fullscreen;
}

void GFX_Context_SetFullscreen(bool fullscreen)
{
m_Context.is_fullscreen = fullscreen;
}

void GFX_Context_SetWindowSize(int32_t width, int32_t height)
{
LOG_INFO("Window size: %dx%d", width, height);
Expand Down
2 changes: 0 additions & 2 deletions src/gfx/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ typedef enum GFX_RENDER_MODE {
void GFX_Context_Attach(void *window_handle);
void GFX_Context_Detach(void);
void GFX_Context_SetVSync(bool vsync);
bool GFX_Context_IsFullscreen(void);
void GFX_Context_SetFullscreen(bool fullscreen);
void GFX_Context_SetWindowSize(int32_t width, int32_t height);
void GFX_Context_SetDisplaySize(int32_t width, int32_t height);
void GFX_Context_SetRenderingMode(GFX_RENDER_MODE target_mode);
Expand Down
5 changes: 3 additions & 2 deletions src/specific/s_fmv.c
Original file line number Diff line number Diff line change
Expand Up @@ -2240,11 +2240,12 @@ static void S_FMV_EventLoop(VideoState *is)
is->audio_volume = 0;
break;

case SDL_WINDOWEVENT_SIZE_CHANGED:
case SDL_WINDOWEVENT_MOVED:
case SDL_WINDOWEVENT_RESIZED:
is->width = event.window.data1;
is->height = event.window.data2;
is->force_refresh = true;
Output_SetWindowSize(event.window.data1, event.window.data2);
S_Shell_HandleWindowResize();
break;

case SDL_WINDOWEVENT_EXPOSED:
Expand Down
5 changes: 0 additions & 5 deletions src/specific/s_output.c
Original file line number Diff line number Diff line change
Expand Up @@ -966,11 +966,6 @@ void S_Output_SetWindowSize(int width, int height)
GFX_Context_SetWindowSize(width, height);
}

void S_Output_SetFullscreen(bool fullscreen)
{
GFX_Context_SetFullscreen(fullscreen);
}

bool S_Output_Init(void)
{
for (int i = 0; i < GFX_MAX_TEXTURES; i++) {
Expand Down
1 change: 0 additions & 1 deletion src/specific/s_output.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ void S_Output_ClearDepthBuffer(void);
void S_Output_DrawEmpty(void);

void S_Output_SetWindowSize(int width, int height);
void S_Output_SetFullscreen(bool fullscreen);
void S_Output_ApplyRenderSettings(void);

void S_Output_SetPalette(RGB888 palette[256]);
Expand Down
130 changes: 109 additions & 21 deletions src/specific/s_shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,125 @@

static int m_ArgCount = 0;
static char **m_ArgStrings = NULL;
static bool m_Fullscreen = true;
static SDL_Window *m_Window = NULL;

static void S_Shell_PostWindowResize(void);
static void S_Shell_SeedRandom(void);
static void S_Shell_SetWindowPos(int32_t x, int32_t y, bool update);
static void S_Shell_SetWindowSize(int32_t width, int32_t height, bool update);
static void S_Shell_SetWindowMaximized(bool is_enabled, bool update);
static void S_Shell_SetFullscreen(bool is_enabled, bool update);

void S_Shell_SeedRandom(void)
static void S_Shell_SeedRandom(void)
{
time_t lt = time(0);
struct tm *tptr = localtime(&lt);
Random_SeedControl(tptr->tm_sec + 57 * tptr->tm_min + 3543 * tptr->tm_hour);
Random_SeedDraw(tptr->tm_sec + 43 * tptr->tm_min + 3477 * tptr->tm_hour);
}

static void S_Shell_PostWindowResize(void)
static void S_Shell_SetWindowPos(int32_t x, int32_t y, bool update)
{
if (x <= 0 || y <= 0) {
return;
}

// only save window position if it's in windowed state.
if (!g_Config.rendering.enable_fullscreen
&& !g_Config.rendering.enable_maximized) {
g_Config.rendering.window_x = x;
g_Config.rendering.window_y = y;
}

if (update) {
SDL_SetWindowPosition(m_Window, x, y);
}
}

static void S_Shell_SetWindowSize(int32_t width, int32_t height, bool update)
{
if (width <= 0 || height <= 0) {
return;
}

// only save window size if it's in windowed state.
if (!g_Config.rendering.enable_fullscreen
&& !g_Config.rendering.enable_maximized) {
g_Config.rendering.window_width = width;
g_Config.rendering.window_height = height;
}

Output_SetWindowSize(width, height);

if (update) {
SDL_SetWindowSize(m_Window, width, height);
}
}

static void S_Shell_SetWindowMaximized(bool is_enabled, bool update)
{
g_Config.rendering.enable_maximized = is_enabled;

if (update && is_enabled) {
SDL_MaximizeWindow(m_Window);
}
}

static void S_Shell_SetFullscreen(bool is_enabled, bool update)
{
g_Config.rendering.enable_fullscreen = is_enabled;

if (update) {
SDL_SetWindowFullscreen(
m_Window, is_enabled ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0);
SDL_ShowCursor(is_enabled ? SDL_DISABLE : SDL_ENABLE);
}
}

void S_Shell_ToggleFullscreen(void)
{
S_Shell_SetFullscreen(!g_Config.rendering.enable_fullscreen, true);

// save the updated config, but ensure it was loaded first
if (g_Config.loaded) {
Config_Write();
}
}

void S_Shell_HandleWindowResize(void)
{
int x;
int y;
int width;
int height;
bool is_maximized;

Uint32 window_flags = SDL_GetWindowFlags(m_Window);
is_maximized = window_flags & SDL_WINDOW_MAXIMIZED;
SDL_GetWindowSize(m_Window, &width, &height);
Output_SetWindowSize(width, height);
SDL_GetWindowPosition(m_Window, &x, &y);
LOG_INFO("%dx%d+%d,%d (maximized: %d)", width, height, x, y, is_maximized);

S_Shell_SetWindowMaximized(is_maximized, false);
S_Shell_SetWindowPos(x, y, false);
S_Shell_SetWindowSize(width, height, false);

// save the updated config, but ensure it was loaded first
if (g_Config.loaded) {
Config_Write();
}
}

void S_Shell_Init(void)
{
S_Shell_SeedRandom();

S_Shell_SetFullscreen(g_Config.rendering.enable_fullscreen, true);
S_Shell_SetWindowPos(
g_Config.rendering.window_x, g_Config.rendering.window_y, true);
S_Shell_SetWindowSize(
g_Config.rendering.window_width, g_Config.rendering.window_height,
true);
S_Shell_SetWindowMaximized(g_Config.rendering.enable_maximized, true);
}

void S_Shell_ShowFatalError(const char *message)
Expand All @@ -60,16 +160,6 @@ void S_Shell_ShowFatalError(const char *message)
S_Shell_TerminateGame(1);
}

void S_Shell_ToggleFullscreen(void)
{
m_Fullscreen = !m_Fullscreen;
Output_SetFullscreen(m_Fullscreen);
SDL_SetWindowFullscreen(
m_Window, m_Fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0);
SDL_ShowCursor(m_Fullscreen ? SDL_DISABLE : SDL_ENABLE);
S_Shell_PostWindowResize();
}

void S_Shell_TerminateGame(int exit_code)
{
Shell_Shutdown();
Expand Down Expand Up @@ -115,11 +205,11 @@ void S_Shell_SpinMessageLoop(void)
Sound_SetMasterVolume(0);
break;

case SDL_WINDOWEVENT_RESIZED: {
Output_SetWindowSize(event.window.data1, event.window.data2);
case SDL_WINDOWEVENT_MOVED:
case SDL_WINDOWEVENT_RESIZED:
S_Shell_HandleWindowResize();
break;
}
}
break;

case SDL_CONTROLLERDEVICEADDED:
Expand Down Expand Up @@ -197,9 +287,7 @@ int main(int argc, char **argv)
return 1;
}

S_Shell_PostWindowResize();

SDL_ShowCursor(SDL_DISABLE);
S_Shell_HandleWindowResize();

Shell_Main();

Expand Down
5 changes: 3 additions & 2 deletions src/specific/s_shell.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@

void S_Shell_ShowFatalError(const char *message);

void S_Shell_SeedRandom(void);
void S_Shell_Init(void);
void S_Shell_SpinMessageLoop(void);
bool S_Shell_GetCommandLine(int *arg_count, char ***args);
void *S_Shell_GetWindowHandle(void);
void S_Shell_TerminateGame(int exit_code);
void S_Shell_ToggleFullscreen(void);
void S_Shell_HandleWindowResize(void);
void S_Shell_TerminateGame(int exit_code);
int S_Shell_GetCurrentDisplayWidth(void);
int S_Shell_GetCurrentDisplayHeight(void);

0 comments on commit 863d021

Please sign in to comment.