Skip to content

Commit

Permalink
Add video option to center the window
Browse files Browse the repository at this point in the history
  • Loading branch information
ZenithMDC committed Nov 22, 2024
1 parent 2e54640 commit 9da5fcc
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 10 deletions.
35 changes: 33 additions & 2 deletions port/fast3d/gfx_sdl2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,13 @@ static void gfx_sdl_init(const struct GfxWindowInitSettings *set) {
posY = 100;
}

if (set->centered) {
SDL_DisplayMode mode = {};
SDL_GetCurrentDisplayMode(0, &mode);
posX = mode.w / 2 - window_width / 2;
posY = mode.h / 2 - window_height / 2;
}

if (set->fullscreen_is_exclusive) {
fullscreen_flag = SDL_WINDOW_FULLSCREEN;
}
Expand Down Expand Up @@ -235,12 +242,35 @@ static void gfx_sdl_set_cursor_visibility(bool visible) {
}
}

static void gfx_sdl_set_closest_resolution(int32_t width, int32_t height) {
static void get_centered_positions_native(int32_t width, int32_t height, int32_t *posX, int32_t *posY) {
const int disp_idx = SDL_GetWindowDisplayIndex(wnd);
SDL_DisplayMode mode = {};
SDL_GetDesktopDisplayMode(disp_idx, &mode);
*posX = mode.w / 2 - width / 2;
*posY = mode.h / 2 - height / 2;
}

static void gfx_sdl_get_centered_positions(int32_t width, int32_t height, int32_t *posX, int32_t *posY) {
const int disp_idx = SDL_GetWindowDisplayIndex(wnd);
SDL_DisplayMode mode = {};
SDL_GetCurrentDisplayMode(disp_idx, &mode);
*posX = mode.w / 2 - width / 2;
*posY = mode.h / 2 - height / 2;
}

static void gfx_sdl_set_closest_resolution(int32_t width, int32_t height, bool should_center) {
const SDL_DisplayMode mode = {.w = width, .h = height};
const int disp_idx = SDL_GetWindowDisplayIndex(wnd);
SDL_DisplayMode closest = {};
if (SDL_GetClosestDisplayMode(0, &mode, &closest)) {
if (SDL_GetClosestDisplayMode(disp_idx, &mode, &closest)) {
SDL_SetWindowDisplayMode(wnd, &closest);
SDL_SetWindowSize(wnd, closest.w, closest.h);
if (should_center) {
int32_t posX = 0;
int32_t posY = 0;
get_centered_positions_native(closest.w, closest.h, &posX, &posY);
SDL_SetWindowPosition(wnd, posX, posY);
}
}
}

Expand Down Expand Up @@ -417,6 +447,7 @@ struct GfxWindowManagerAPI gfx_sdl = {
gfx_sdl_set_closest_resolution,
gfx_sdl_set_dimensions,
gfx_sdl_get_dimensions,
gfx_sdl_get_centered_positions,
gfx_sdl_handle_events,
gfx_sdl_start_frame,
gfx_sdl_swap_buffers_begin,
Expand Down
4 changes: 3 additions & 1 deletion port/fast3d/gfx_window_manager_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ struct GfxWindowInitSettings {
bool fullscreen;
bool fullscreen_is_exclusive;
bool maximized;
bool centered;
bool allow_hidpi;
};

Expand All @@ -29,9 +30,10 @@ struct GfxWindowManagerAPI {
void (*set_maximize)(bool enable);
void (*get_active_window_refresh_rate)(uint32_t* refresh_rate);
void (*set_cursor_visibility)(bool visible);
void (*set_closest_resolution)(int32_t width, int32_t height);
void (*set_closest_resolution)(int32_t width, int32_t height, bool should_center);
void (*set_dimensions)(uint32_t width, uint32_t height, int32_t posX, int32_t posY);
void (*get_dimensions)(uint32_t* width, uint32_t* height, int32_t* posX, int32_t* posY);
void (*get_centered_positions)(int32_t width, int32_t height, int32_t *posX, int32_t *posY);
void (*handle_events)(void);
bool (*start_frame)(void);
void (*swap_buffers_begin)(void);
Expand Down
2 changes: 2 additions & 0 deletions port/include/video.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ s32 videoGetFullscreen(void);
s32 videoGetFullscreenMode(void);
s32 videoGetMaximizeWindow(void);
void videoSetMaximizeWindow(s32 fs);
s32 videoGetCenterWindow(void);
void videoSetCenterWindow(s32 center);
u32 videoGetTextureFilter(void);
s32 videoGetTextureFilter2D(void);
s32 videoGetDetailTextures(void);
Expand Down
29 changes: 25 additions & 4 deletions port/src/optionsmenu.c
Original file line number Diff line number Diff line change
Expand Up @@ -724,14 +724,27 @@ static MenuItemHandlerResult menuhandlerMaximizeWindow(s32 operation, struct men
return 0;
}

static MenuItemHandlerResult menuhandlerCenterWindow(s32 operation, struct menuitem *item, union handlerdata *data)
{
switch (operation) {
case MENUOP_GET:
return videoGetCenterWindow();
case MENUOP_SET:
videoSetCenterWindow(data->checkbox.value);
break;
}

return 0;
}

static MenuItemHandlerResult menuhandlerResolution(s32 operation, struct menuitem *item, union handlerdata *data)
{
switch (operation) {
case MENUOP_CHECKDISABLED:
if (videoGetFullscreen() && videoGetFullscreenMode() == 0) {
return true;
}
break;
if (videoGetFullscreen() && videoGetFullscreenMode() == 0) {
return true;
}
break;
case MENUOP_GETOPTIONCOUNT:
data->dropdown.value = videoGetNumDisplayModes();
break;
Expand Down Expand Up @@ -899,6 +912,14 @@ struct menuitem g_ExtendedVideoMenuItems[] = {
0,
menuhandlerMaximizeWindow,
},
{
MENUITEMTYPE_CHECKBOX,
0,
MENUITEMFLAG_LITERAL_TEXT,
(uintptr_t)"Center Window",
0,
menuhandlerCenterWindow,
},
{
MENUITEMTYPE_CHECKBOX,
0,
Expand Down
38 changes: 35 additions & 3 deletions port/src/video.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ static s32 vidFramebuffers = true;
static s32 vidFullscreen = DEFAULT_VID_FULLSCREEN;
static s32 vidFullscreenExclusive = DEFAULT_VID_FULLSCREEN_EXCLUSIVE;
static s32 vidMaximize = false;
static s32 vidCenter = false;
static s32 vidAllowHiDpi = false;
static s32 vidVsync = 1;
static s32 vidMSAA = 1;
Expand Down Expand Up @@ -76,6 +77,7 @@ s32 videoInit(void)
.fullscreen = vidFullscreen,
.fullscreen_is_exclusive = vidFullscreenExclusive,
.maximized = vidMaximize,
.centered = vidCenter,
.allow_hidpi = vidAllowHiDpi
}
};
Expand Down Expand Up @@ -197,6 +199,11 @@ s32 videoGetMaximizeWindow(void)
return vidMaximize;
}

s32 videoGetCenterWindow(void)
{
return vidCenter;
}

f32 videoGetAspect(void)
{
return gfx_current_dimensions.aspect_ratio;
Expand Down Expand Up @@ -245,13 +252,20 @@ void videoSetDisplayMode(const s32 mode_idx)
cur_mode = x_pos + 1;
vidHeight = strtol(cur_mode, NULL, 10);

s32 posX = 100;
s32 posY = 100;
if (vidCenter) {
wmAPI->get_centered_positions(vidWidth, vidHeight, &posX, &posY);
}

if (vidFullscreen) {
wmAPI->set_closest_resolution(vidWidth, vidHeight);
wmAPI->set_closest_resolution(vidWidth, vidHeight, vidCenter);
} else {
if (vidMaximize) {
videoSetMaximizeWindow(false);
} else {
wmAPI->set_dimensions(vidWidth, vidHeight, posX, posY);
}
wmAPI->set_dimensions(vidWidth, vidHeight, 100, 100);
}
}

Expand Down Expand Up @@ -280,7 +294,7 @@ void videoSetFullscreen(s32 fs)
{
if (fs != vidFullscreen) {
vidFullscreen = !!fs;
wmAPI->set_closest_resolution(vidWidth, vidHeight);
wmAPI->set_closest_resolution(vidWidth, vidHeight, vidCenter);
wmAPI->set_fullscreen(vidFullscreen);
if (!vidFullscreen && vidMaximize) {
wmAPI->set_maximize(false);
Expand All @@ -304,6 +318,23 @@ void videoSetMaximizeWindow(s32 fs)
if (fs != vidMaximize) {
vidMaximize = !!fs;
wmAPI->set_maximize(vidMaximize);
if (vidCenter && !vidMaximize) {
s32 posX = 0;
s32 posY = 0;
wmAPI->get_centered_positions(vidWidth, vidHeight, &posX, &posY);
wmAPI->set_dimensions(vidWidth, vidHeight, posX, posY);
}
}
}

void videoSetCenterWindow(s32 center)
{
vidCenter = center;
if (vidCenter && !vidMaximize) {
s32 posX = 0;
s32 posY = 0;
wmAPI->get_centered_positions(vidWidth, vidHeight, &posX, &posY);
wmAPI->set_dimensions(vidWidth, vidHeight, posX, posY);
}
}

Expand Down Expand Up @@ -379,6 +410,7 @@ PD_CONSTRUCTOR static void videoConfigInit(void)
configRegisterInt("Video.DefaultWidth", &vidWidth, 0, 32767);
configRegisterInt("Video.DefaultHeight", &vidHeight, 0, 32767);
configRegisterInt("Video.ExclusiveFullscreen", &vidFullscreenExclusive, 0, 1);
configRegisterInt("Video.CenterWindow", &vidCenter, 0, 1);
configRegisterInt("Video.AllowHiDpi", &vidAllowHiDpi, 0, 1);
configRegisterInt("Video.VSync", &vidVsync, -1, 10);
configRegisterInt("Video.FramebufferEffects", &vidFramebuffers, 0, 1);
Expand Down

0 comments on commit 9da5fcc

Please sign in to comment.