diff options
Diffstat (limited to 'src/window')
| -rw-r--r-- | src/window/window.c | 71 | ||||
| -rw-r--r-- | src/window/window.h | 36 | 
2 files changed, 107 insertions, 0 deletions
diff --git a/src/window/window.c b/src/window/window.c new file mode 100644 index 0000000..bb371cd --- /dev/null +++ b/src/window/window.c @@ -0,0 +1,71 @@ +#include "window.h" + +bool honey_setup_window(lua_State* L) +{ +    honey_window_information* info = malloc(sizeof(honey_window_information)); +    if (info == NULL) { +        fprintf(stderr, "[honey] ERROR: failed to allocate memory for window information!\n"); +        return false; +    } +     +    glfwInit(); +    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); +    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); +    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); + +    info->window = glfwCreateWindow(640, 480, "honey", NULL, NULL); +    if (info->window == NULL) { +        fprintf(stderr, "[honey] ERROR: failed to create window!\n"); +        glfwTerminate(); +        return false; +    } +    /* store lua state in window, so it's accessible from GLFW callbacks */ +    glfwSetWindowUserPointer(info->window, L); +    glfwMakeContextCurrent(info->window); + +    if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress)) { +        fprintf(stderr, "[honey] ERROR: failed to initialize GLAD!\n"); +        glfwTerminate(); +        return false; +    } + +    // Enable blending +    glEnable(GL_BLEND); +    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + +    honey_lua_element window_elements[] = { +        { "internal", HONEY_LIGHTUSERDATA, { .pointer = info } }, +        { "set_fullscreen", HONEY_FUNC, { .function = honey_window_set_fullscreen } }, +    }; + +    honey_lua_create_table(L, window_elements, 2); +    return true; +} + +/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ + +int honey_window_set_fullscreen(lua_State* L) +{ +    if (!honey_lua_validate_types(L, 1, HONEY_BOOL)) +        lua_error(L); + +    bool fullscreen = lua_toboolean(L, 1); + +    lua_getglobal(L, "honey"); +    lua_getfield(L, -1, "window"); +    lua_getfield(L, -1, "internal"); +    honey_window_information* info = lua_touserdata(L, -1); +    lua_pop(L, 2); + +    if (fullscreen) { +        glfwGetWindowSize(info->window, &(info->width), &(info->height)); +         +        GLFWmonitor* monitor = glfwGetPrimaryMonitor(); +        const GLFWvidmode* mode = glfwGetVideoMode(monitor); +        glfwSetWindowMonitor(info->window, monitor, 0, 0, mode->width, mode->height, mode->refreshRate); +    } +    else { +        glfwSetWindowMonitor(info->window, NULL, 20, 20, info->width, info->height, 0); +    } +    return 0; +} diff --git a/src/window/window.h b/src/window/window.h new file mode 100644 index 0000000..c0083ba --- /dev/null +++ b/src/window/window.h @@ -0,0 +1,36 @@ +/** @file */ + +#ifndef HONEY_WINDOW_H +#define HONEY_WINDOW_H + +#include "../common.h" + +#define HONEY_WINDOW_DEFAULT_WIDTH 640 +#define HONEY_WINDOW_DEFAULT_HEIGHT 480 + +typedef struct { +    honey_window window; +    int width; +    int height; +    bool fullscreen; +} honey_window_information; + +/** @brief Push the various honey.window table to the stack. + * + * @param[in] L The lua state to push to + * @param[in] window The window created by honey_setup() + * + * @returns Nothing. + */ +bool honey_setup_window(lua_State* L); + +/** @brief Set whether or not the window is fullscreen. + * + * Lua parameters: + * @param[in] fullscreen Boolean set to true if the window is to be fullscreen and false otherwise. + * + * @returns Nothing. + */ +int honey_window_set_fullscreen(lua_State* L); + +#endif  | 
