Skip to content

Commit 4d58ba9

Browse files
committed
lovr.system.is/setWindowFullscreen;
GLFW only for now, best effort. Tested on X11 so far.
1 parent 9f98041 commit 4d58ba9

File tree

6 files changed

+75
-18
lines changed

6 files changed

+75
-18
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ dev
5757
- Add `lovr.audio.getStream`.
5858
- Add `AudioStream`.
5959
- Add `Sound:get/setFrame`.
60+
- Add `lovr.system.is/setWindowFullscreen`.
6061

6162
### Change
6263

@@ -123,6 +124,7 @@ dev
123124
- Deprecate `lovr.math.mat4` (use `lovr.math.newMat4`).
124125
- Deprecate `t.math.globals` (use `vector` and `quaternion`).
125126
- Deprecate `Layer:getPass` (use your own `Pass`).
127+
- Deprecate ability to use zero for window dimensions in `lovr.conf` (use `t.window.fullscreen`).
126128

127129
### Remove
128130

src/api/l_system.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,13 @@ static int l_lovrSystemOpenWindow(lua_State* L) {
177177
}
178178
lua_pop(L, 1);
179179

180+
// Deprecated method for setting fullscreen
181+
if (window.width == 0 || window.height == 0) {
182+
window.fullscreen = true;
183+
window.width = 720;
184+
window.height = 800;
185+
}
186+
180187
bool success = lovrSystemOpenWindow(&window);
181188
lovrRelease(image, lovrImageDestroy);
182189
luax_assert(L, success);
@@ -201,6 +208,18 @@ static int l_lovrSystemIsWindowFocused(lua_State* L) {
201208
return 1;
202209
}
203210

211+
static int l_lovrSystemIsWindowFullscreen(lua_State* L) {
212+
bool fullscreen = lovrSystemIsWindowFullscreen();
213+
lua_pushboolean(L, fullscreen);
214+
return 1;
215+
}
216+
217+
static int l_lovrSystemSetWindowFullscreen(lua_State* L) {
218+
bool fullscreen = lua_toboolean(L, 1);
219+
lovrSystemSetWindowFullscreen(fullscreen);
220+
return 0;
221+
}
222+
204223
static int l_lovrSystemGetWindowWidth(lua_State* L) {
205224
uint32_t width, height;
206225
lovrSystemGetWindowSize(&width, &height);
@@ -363,6 +382,8 @@ static const luaL_Reg lovrSystem[] = {
363382
{ "isWindowOpen", l_lovrSystemIsWindowOpen },
364383
{ "isWindowVisible", l_lovrSystemIsWindowVisible },
365384
{ "isWindowFocused", l_lovrSystemIsWindowFocused },
385+
{ "isWindowFullscreen", l_lovrSystemIsWindowFullscreen },
386+
{ "setWindowFullscreen", l_lovrSystemSetWindowFullscreen },
366387
{ "getWindowWidth", l_lovrSystemGetWindowWidth },
367388
{ "getWindowHeight", l_lovrSystemGetWindowHeight },
368389
{ "getWindowDimensions", l_lovrSystemGetWindowDimensions },

src/core/os.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@ bool os_window_open(const os_window_config* config);
190190
bool os_window_is_open(void);
191191
bool os_window_is_visible(void);
192192
bool os_window_is_focused(void);
193+
bool os_window_is_fullscreen(void);
194+
void os_window_set_fullscreen(bool fullscreen);
193195
void os_window_get_size(uint32_t* width, uint32_t* height);
194196
float os_window_get_pixel_density(void);
195197
void os_window_message_box(const char* message);

src/core/os_glfw.h

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ static struct {
137137
fn_mousewheel_move* onMouseWheelMove;
138138
uint32_t width;
139139
uint32_t height;
140+
bool fullscreen;
140141
} glfwState;
141142

142143
static void onError(int code, const char* description) {
@@ -163,8 +164,6 @@ static void onWindowFocus(GLFWwindow* window, int focused) {
163164

164165
static void onWindowResize(GLFWwindow* window, int width, int height) {
165166
if (glfwState.onWindowResize && width > 0 && height > 0) {
166-
glfwState.width = width;
167-
glfwState.height = height;
168167
glfwState.onWindowResize(width, height);
169168
}
170169
}
@@ -377,29 +376,22 @@ bool os_window_open(const os_window_config* config) {
377376
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
378377
glfwWindowHint(GLFW_RESIZABLE, config->resizable);
379378

380-
if (config->width == 0 && config->height == 0) {
381-
glfwWindowHint(GLFW_DECORATED, GLFW_FALSE);
382-
}
383-
384-
bool center = config->centered && !config->fullscreen && config->width > 0 && config->height > 0;
379+
bool center = config->centered && !config->fullscreen;
385380

386381
if (center) {
387382
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
388383
}
389384

390385
GLFWmonitor* monitor = glfwGetPrimaryMonitor();
391386
const GLFWvidmode* mode = glfwGetVideoMode(monitor);
392-
uint32_t width = config->width ? config->width : (uint32_t) mode->width;
393-
uint32_t height = config->height ? config->height : (uint32_t) mode->height;
387+
uint32_t width = config->fullscreen ? (uint32_t) mode->width : config->width;
388+
uint32_t height = config->fullscreen ? (uint32_t) mode->height : config->height;
394389

395390
if (config->fullscreen) {
396-
glfwWindowHint(GLFW_RED_BITS, mode->redBits);
397-
glfwWindowHint(GLFW_GREEN_BITS, mode->greenBits);
398-
glfwWindowHint(GLFW_BLUE_BITS, mode->blueBits);
399-
glfwWindowHint(GLFW_REFRESH_RATE, mode->refreshRate);
391+
glfwWindowHint(GLFW_DECORATED, GLFW_FALSE);
400392
}
401393

402-
glfwState.window = glfwCreateWindow(width, height, config->title, config->fullscreen ? monitor : NULL, NULL);
394+
glfwState.window = glfwCreateWindow(width, height, config->title, NULL, NULL);
403395

404396
if (!glfwState.window) {
405397
return false;
@@ -429,8 +421,9 @@ bool os_window_open(const os_window_config* config) {
429421
glfwSetMouseButtonCallback(glfwState.window, onMouseButton);
430422
glfwSetCursorPosCallback(glfwState.window, onMouseMove);
431423
glfwSetScrollCallback(glfwState.window, onMouseWheelMove);
432-
glfwState.width = width;
433-
glfwState.height = height;
424+
glfwState.width = config->width;
425+
glfwState.height = config->height;
426+
glfwState.fullscreen = config->fullscreen;
434427
return true;
435428
}
436429

@@ -446,9 +439,38 @@ bool os_window_is_focused(void) {
446439
return glfwState.window && glfwGetWindowAttrib(glfwState.window, GLFW_FOCUSED);
447440
}
448441

442+
bool os_window_is_fullscreen(void) {
443+
return glfwState.fullscreen;
444+
}
445+
446+
void os_window_set_fullscreen(bool fullscreen) {
447+
if (!glfwState.window) {
448+
return;
449+
}
450+
451+
glfwState.fullscreen = fullscreen;
452+
453+
if (fullscreen) {
454+
os_window_get_size(&glfwState.width, &glfwState.height);
455+
GLFWmonitor* monitor = glfwGetPrimaryMonitor();
456+
const GLFWvidmode* mode = glfwGetVideoMode(monitor);
457+
glfwSetWindowSize(glfwState.window, mode->width, mode->height);
458+
glfwSetWindowAttrib(glfwState.window, GLFW_DECORATED, false);
459+
glfwSetWindowPos(glfwState.window, 0, 0);
460+
} else {
461+
int x, y, w, h;
462+
int width = (int) glfwState.width;
463+
int height = (int) glfwState.height;
464+
GLFWmonitor* monitor = glfwGetPrimaryMonitor();
465+
glfwGetMonitorWorkarea(monitor, &x, &y, &w, &h);
466+
glfwSetWindowSize(glfwState.window, width, height);
467+
glfwSetWindowAttrib(glfwState.window, GLFW_DECORATED, true);
468+
glfwSetWindowPos(glfwState.window, x + (w - width) / 2, y + (h - height) / 2);
469+
}
470+
}
471+
449472
void os_window_get_size(uint32_t* width, uint32_t* height) {
450-
*width = glfwState.width;
451-
*height = glfwState.height;
473+
glfwGetWindowSize(glfwState.window, width, height);
452474
}
453475

454476
float os_window_get_pixel_density(void) {

src/modules/system/system.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,14 @@ bool lovrSystemIsWindowFocused(void) {
159159
return os_window_is_focused();
160160
}
161161

162+
bool lovrSystemIsWindowFullscreen(void) {
163+
return os_window_is_fullscreen();
164+
}
165+
166+
void lovrSystemSetWindowFullscreen(bool fullscreen) {
167+
return os_window_set_fullscreen(fullscreen);
168+
}
169+
162170
void lovrSystemGetWindowSize(uint32_t* width, uint32_t* height) {
163171
os_window_get_size(width, height);
164172
}

src/modules/system/system.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ bool lovrSystemOpenWindow(struct os_window_config* config);
1919
bool lovrSystemIsWindowOpen(void);
2020
bool lovrSystemIsWindowVisible(void);
2121
bool lovrSystemIsWindowFocused(void);
22+
bool lovrSystemIsWindowFullscreen(void);
23+
void lovrSystemSetWindowFullscreen(bool fullscreen);
2224
void lovrSystemGetWindowSize(uint32_t* width, uint32_t* height);
2325
float lovrSystemGetWindowDensity(void);
2426
void lovrSystemPollEvents(void);

0 commit comments

Comments
 (0)