Move window opening to lovr.system;

This commit is contained in:
bjorn 2022-05-09 12:43:19 -07:00
parent 91c48e57d5
commit e820e40e08
8 changed files with 159 additions and 74 deletions

View File

@ -56,14 +56,12 @@ function lovr.boot()
globals = true
},
window = {
width = 1080,
width = 800,
height = 600,
fullscreen = false,
resizable = false,
msaa = 0,
title = 'LÖVR',
icon = nil,
vsync = 1
icon = nil
}
}
@ -97,6 +95,10 @@ function lovr.boot()
end
end
if lovr.system and conf.window then
lovr.system.openWindow(conf.window)
end
if lovr.graphics then
lovr.graphics.init()
end

View File

@ -1,8 +1,11 @@
#include "api.h"
#include "system/system.h"
#include "data/image.h"
#include "core/os.h"
#include "util.h"
#include <lua.h>
#include <lauxlib.h>
#include <string.h>
StringEntry lovrKeyboardKey[] = {
[KEY_A] = ENTRY("a"),
@ -114,10 +117,88 @@ static int l_lovrSystemRequestPermission(lua_State* L) {
return 0;
}
static int l_lovrSystemOpenWindow(lua_State* L) {
os_window_config window;
memset(&window, 0, sizeof(window));
if (!lua_toboolean(L, 1)) {
return 0;
}
luaL_checktype(L, 1, LUA_TTABLE);
lua_getfield(L, 1, "width");
window.width = luaL_optinteger(L, -1, 1080);
lua_pop(L, 1);
lua_getfield(L, 1, "height");
window.height = luaL_optinteger(L, -1, 600);
lua_pop(L, 1);
lua_getfield(L, 1, "fullscreen");
window.fullscreen = lua_toboolean(L, -1);
lua_pop(L, 1);
lua_getfield(L, 1, "resizable");
window.resizable = lua_toboolean(L, -1);
lua_pop(L, 1);
lua_getfield(L, 1, "title");
window.title = luaL_optstring(L, -1, "LÖVR");
lua_pop(L, 1);
lua_getfield(L, 1, "icon");
Image* image = NULL;
if (!lua_isnil(L, -1)) {
image = luax_checkimage(L, -1);
window.icon.data = lovrImageGetLayerData(image, 0, 0);
window.icon.width = lovrImageGetWidth(image);
window.icon.height = lovrImageGetHeight(image);
}
lua_pop(L, 1);
lovrSystemOpenWindow(&window);
lovrRelease(image, lovrImageDestroy);
return 0;
}
static int l_lovrSystemIsWindowOpen(lua_State* L) {
bool open = lovrSystemIsWindowOpen();
lua_pushboolean(L, open);
return 1;
}
static int l_lovrSystemGetWindowWidth(lua_State* L) {
lua_pushnumber(L, lovrSystemGetWindowWidth());
return 1;
}
static int l_lovrSystemGetWindowHeight(lua_State* L) {
lua_pushnumber(L, lovrSystemGetWindowHeight());
return 1;
}
static int l_lovrSystemGetWindowDimensions(lua_State* L) {
lua_pushnumber(L, lovrSystemGetWindowWidth());
lua_pushnumber(L, lovrSystemGetWindowHeight());
return 2;
}
static int l_lovrSystemGetWindowDensity(lua_State* L) {
lua_pushnumber(L, lovrSystemGetWindowDensity());
return 1;
}
static const luaL_Reg lovrSystem[] = {
{ "getOS", l_lovrSystemGetOS },
{ "getCoreCount", l_lovrSystemGetCoreCount },
{ "requestPermission", l_lovrSystemRequestPermission },
{ "openWindow", l_lovrSystemOpenWindow },
{ "isWindowOpen", l_lovrSystemIsWindowOpen },
{ "getWindowWidth", l_lovrSystemGetWindowWidth },
{ "getWindowHeight", l_lovrSystemGetWindowHeight },
{ "getWindowDimensions", l_lovrSystemGetWindowDimensions },
{ "getWindowDensity", l_lovrSystemGetWindowDensity },
{ NULL, NULL }
};

View File

@ -4,14 +4,11 @@
#pragma once
typedef struct {
typedef struct os_window_config {
uint32_t width;
uint32_t height;
bool fullscreen;
bool resizable;
bool debug;
int vsync;
int msaa;
const char* title;
struct {
void* data;
@ -162,9 +159,6 @@ bool os_window_open(const os_window_config* config);
bool os_window_is_open(void);
void os_window_get_size(int* width, int* height);
void os_window_get_fbsize(int* width, int* height);
void os_window_set_vsync(int interval);
void os_window_swap(void);
fn_gl_proc* os_get_gl_proc_address(const char* function);
size_t os_get_home_directory(char* buffer, size_t size);
size_t os_get_data_directory(char* buffer, size_t size);

View File

@ -362,7 +362,6 @@ bool os_window_open(const os_window_config* config) {
EGLint contextAttributes[] = {
EGL_CONTEXT_CLIENT_VERSION, 3,
EGL_CONTEXT_OPENGL_DEBUG, config->debug,
EGL_NONE
};
@ -403,18 +402,6 @@ void os_window_get_fbsize(int* width, int* height) {
*height = 0;
}
void os_window_set_vsync(int interval) {
//
}
void os_window_swap() {
//
}
fn_gl_proc* os_get_gl_proc_address(const char* function) {
return eglGetProcAddress(function);
}
size_t os_get_home_directory(char* buffer, size_t size) {
return 0;
}

View File

@ -1,5 +1,9 @@
#include <stdio.h>
#ifdef LOVR_VK
#include <vulkan/vulkan.h>
#endif
#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>
@ -209,6 +213,10 @@ bool os_window_open(const os_window_config* config) {
return false;
}
#ifdef LOVR_VK
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
#endif
#ifdef LOVR_LINUX_EGL
glfwWindowHint(GLFW_CONTEXT_CREATION_API, GLFW_EGL_CONTEXT_API);
#endif
@ -216,11 +224,6 @@ bool os_window_open(const os_window_config* config) {
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE);
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, config->debug);
#ifndef LOVR_LINUX_EGL
glfwWindowHint(GLFW_CONTEXT_NO_ERROR, !config->debug);
#endif
glfwWindowHint(GLFW_SAMPLES, config->msaa);
glfwWindowHint(GLFW_RESIZABLE, config->resizable);
glfwWindowHint(GLFW_SRGB_CAPABLE, GLFW_TRUE);
@ -250,13 +253,14 @@ bool os_window_open(const os_window_config* config) {
});
}
#ifndef LOVR_VK
glfwMakeContextCurrent(glfwState.window);
#endif
glfwSetWindowCloseCallback(glfwState.window, onWindowClose);
glfwSetWindowFocusCallback(glfwState.window, onWindowFocus);
glfwSetWindowSizeCallback(glfwState.window, onWindowResize);
glfwSetKeyCallback(glfwState.window, onKeyboardEvent);
glfwSetCharCallback(glfwState.window, onTextEvent);
os_window_set_vsync(config->vsync);
return true;
}
@ -282,22 +286,6 @@ void os_window_get_fbsize(int* width, int* height) {
}
}
void os_window_set_vsync(int interval) {
#if EMSCRIPTEN
glfwSwapInterval(1);
#else
glfwSwapInterval(interval);
#endif
}
void os_window_swap() {
glfwSwapBuffers(glfwState.window);
}
fn_gl_proc* os_get_gl_proc_address(const char* function) {
return (fn_gl_proc*) glfwGetProcAddress(function);
}
void os_on_quit(fn_quit* callback) {
glfwState.onQuitRequest = callback;
}

View File

@ -279,7 +279,6 @@ bool os_window_open(const os_window_config* flags) {
attributes.alpha = false;
attributes.depth = true;
attributes.stencil = true;
attributes.antialias = flags->msaa > 1;
attributes.preserveDrawingBuffer = false;
attributes.majorVersion = 2;
attributes.minorVersion = 0;
@ -309,19 +308,6 @@ void os_window_get_fbsize(int* width, int* height) {
*height = state.framebufferHeight;
}
void os_window_set_vsync(int interval) {
//
}
void os_window_swap() {
//
}
fn_gl_proc* os_get_gl_proc_address(const char* function) {
emscripten_webgl_enable_extension(state.context, function);
return NULL;
}
void os_on_quit(fn_quit* callback) {
state.onQuitRequest = callback;
}

View File

@ -4,7 +4,13 @@
#include "util.h"
#include <string.h>
static void onKeyboardEvent(os_button_action action, os_key key, uint32_t scancode, bool repeat) {
static struct {
bool initialized;
int windowWidth;
int windowHeight;
} state;
static void onKey(os_button_action action, os_key key, uint32_t scancode, bool repeat) {
lovrEventPush((Event) {
.type = action == BUTTON_PRESSED ? EVENT_KEYPRESSED : EVENT_KEYRELEASED,
.data.key.code = key,
@ -13,7 +19,7 @@ static void onKeyboardEvent(os_button_action action, os_key key, uint32_t scanco
});
}
static void onTextEvent(uint32_t codepoint) {
static void onText(uint32_t codepoint) {
Event event;
event.type = EVENT_TEXTINPUT;
event.data.text.codepoint = codepoint;
@ -22,23 +28,31 @@ static void onTextEvent(uint32_t codepoint) {
lovrEventPush(event);
}
static void onPermissionEvent(os_permission permission, bool granted) {
Event event;
event.type = EVENT_PERMISSION;
event.data.permission.permission = permission;
event.data.permission.granted = granted;
lovrEventPush(event);
static void onPermission(os_permission permission, bool granted) {
lovrEventPush((Event) {
.type = EVENT_PERMISSION,
.data.permission.permission = permission,
.data.permission.granted = granted
});
}
static struct {
bool initialized;
} state;
static void onQuit() {
lovrEventPush((Event) {
.type = EVENT_QUIT,
.data.quit.exitCode = 0
});
}
static void onResize(int width, int height) {
state.windowWidth = width;
state.windowHeight = height;
}
bool lovrSystemInit() {
if (state.initialized) return false;
os_on_key(onKeyboardEvent);
os_on_text(onTextEvent);
os_on_permission(onPermissionEvent);
os_on_key(onKey);
os_on_text(onText);
os_on_permission(onPermission);
state.initialized = true;
return true;
}
@ -62,3 +76,29 @@ uint32_t lovrSystemGetCoreCount() {
void lovrSystemRequestPermission(Permission permission) {
os_request_permission((os_permission) permission);
}
void lovrSystemOpenWindow(os_window_config* window) {
lovrAssert(os_window_open(window), "Could not open window");
os_on_resize(onResize);
os_on_quit(onQuit);
os_window_get_fbsize(&state.windowWidth, &state.windowHeight);
}
bool lovrSystemIsWindowOpen() {
return os_window_is_open();
}
uint32_t lovrSystemGetWindowWidth() {
return state.windowWidth;
}
uint32_t lovrSystemGetWindowHeight() {
return state.windowHeight;
}
float lovrSystemGetWindowDensity() {
int width, height, fbwidth, fbheight;
os_window_get_size(&width, &height);
os_window_get_fbsize(&fbwidth, &fbheight);
return (width == 0 || fbwidth == 0) ? 0.f : (float) fbwidth / width;
}

View File

@ -3,6 +3,8 @@
#pragma once
struct os_window_config;
typedef enum {
PERMISSION_AUDIO_CAPTURE
} Permission;
@ -12,3 +14,8 @@ void lovrSystemDestroy(void);
const char* lovrSystemGetOS(void);
uint32_t lovrSystemGetCoreCount(void);
void lovrSystemRequestPermission(Permission permission);
void lovrSystemOpenWindow(struct os_window_config* config);
bool lovrSystemIsWindowOpen(void);
uint32_t lovrSystemGetWindowWidth(void);
uint32_t lovrSystemGetWindowHeight(void);
float lovrSystemGetWindowDensity(void);