diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/core/include/engine/engine.h | 2 | ||||
| -rw-r--r-- | src/core/src/loop.c | 112 | ||||
| -rw-r--r-- | src/rendering/include/engine/rendering/window.h | 10 | ||||
| -rw-r--r-- | src/rendering/src/platform_glfw.c | 3 | ||||
| -rw-r--r-- | src/rendering/src/window.c | 227 |
5 files changed, 16 insertions, 338 deletions
diff --git a/src/core/include/engine/engine.h b/src/core/include/engine/engine.h index d28c024..d0a61f2 100644 --- a/src/core/include/engine/engine.h +++ b/src/core/include/engine/engine.h @@ -31,7 +31,7 @@ typedef struct { /* Essential functions */ Instance* engine_init(const char* windowtitle, i32 windowWidth, i32 windowHeight, - const f32 render_scale, const u32 flags, + const u32 flags, const usize initial_memory); i32 engine_run(Instance* p, StateType initial_state, void* state_arg); diff --git a/src/core/src/loop.c b/src/core/src/loop.c index 6a91f46..cd264a7 100644 --- a/src/core/src/loop.c +++ b/src/core/src/loop.c @@ -33,6 +33,7 @@ #include <engine/rendering/window.h> #include <engine/rendering/rendering.h> +#include <engine/rendering/platform_glfw.h> #define DEFAULT_NUM_PROCS 8 @@ -101,7 +102,7 @@ i32 cmp_int(const void* a, const void* b) { /* Creates the window, initializes IO, Rendering, Fonts and engine-specific * resources. */ Instance* engine_init(const char* windowtitle, i32 windowWidth, i32 windowHeight, - const f32 render_scale, const u32 flags, + const u32 flags, const usize initial_memory) { INFO("Engine version " ENGINE_VERSION); @@ -113,7 +114,7 @@ Instance* engine_init(const char* windowtitle, i32 windowWidth, i32 windowHeight INFO("Starting with pid %lu", pid); } #endif - v2_i32 windowsize = (v2_i32){.x = windowWidth, .y = windowHeight}; + v2_i32 windowsize = (v2_i32){.x = windowWidth, .y = windowHeight}; Instance* p = (Instance*)calloc(1, sizeof(Instance)); Window* w = (Window*)calloc(1, sizeof(Window)); @@ -122,106 +123,7 @@ Instance* engine_init(const char* windowtitle, i32 windowWidth, i32 windowHeight Resources* resources = calloc(1, sizeof(Resources)); // TODO: Initialize them :) - w = init_window_glfw(windowtitle, (ivec2){windowsize.x, windowsize.y}, flags); - // Dont forget to init the renderer - init_render_opengl(w, flags); - - - //{ /* Resource loading */ - - // /* Count resources */ - // usize n_textures = 0; - // usize n_fonts = 0; - - // if (textures != NULL) - // while (textures[n_textures] != NULL) n_textures++; - // if (fonts != NULL) - // while (fonts[n_fonts] != NULL) n_fonts++; - - // INFO("Number of textures: " TERM_COLOR_YELLOW "%d" TERM_COLOR_RESET, - // n_textures); - // INFO("Number of fonts: " TERM_COLOR_YELLOW "%d" TERM_COLOR_RESET, n_fonts); - - // /* Save the textures and fonts, if we should need to reload them later */ - // resources->texture_paths = (Asset_TextureSpec**)textures; - // resources->font_paths = (Asset_FontSpec**)fonts; - - // /* Allocate memory for textures and fonts */ - // resources->textures = (Texture**)malloc(sizeof(Texture*) * n_textures); - // //resources->fonts = (TTF_Font**)malloc(sizeof(TTF_Font*) * n_fonts); - // resources->textures_size = n_textures; - - // for (usize i = 0; i < n_textures; i++) resources->textures[i] = NULL; - // //for (usize i = 0; i < n_fonts; i++) resources->fonts[i] = NULL; - - // /* Load textures */ - // for (usize i = 0; i < n_textures; i++) { - // Texture* t = NULL; - // INFO_("loading texture \"" TERM_COLOR_YELLOW "%s" TERM_COLOR_RESET - // "\"...", - // textures[i]->path); - -//// t = load_texture(renderer, textures[i]); -//// if (t == NULL) { -//// puts(""); -//// ERROR("failed to load texture\n"); -//// exit(EXIT_FAILURE); -//// } -//// -//// if (t->tilesize < 8) { -//// puts(""); -//// ERROR("texture too small!\n"); -//// exit(EXIT_FAILURE); -//// } -//// - // //if (t->texture == NULL) { - // // puts(""); - // // ERROR("failed to load texture\n"); - // //} else { - // // printf("ok\n"); - // // resources->textures[i] = t; - // // resources->textures_len++; - // //} - // } - - // /* Load fonts */ - // for (usize i = 0; i < n_fonts; i++) { - // INFO_("loading font \"" TERM_COLOR_YELLOW "%s" TERM_COLOR_RESET "\"...", - // fonts[i]->font_path); - - // //TTF_Font* font = TTF_OpenFont(fonts[i]->font_path, fonts[i]->ptsize); - // //if (!font) { - // // ERROR("failed to load font: %s\n", TTF_GetError()); - // //} else { - // // printf("ok\n"); - // // resources->fonts[i] = font; - // // resources->fonts_len++; - // //} - // } - - // if (resources->textures_len != n_textures) { - // WARN("Done. %d/%d textures loaded.", resources->textures_len, n_textures); - // } else { - // INFO("Done. All %d textures loaded.", n_textures); - // } - - // if (resources->fonts_len != n_fonts) { - // WARN("Done. %d/%d fonts loaded.", resources->fonts_len, n_fonts); - // } else { - // INFO("Done. All %d fonts loaded.", n_fonts); - // } - - // resources->texturepaths_len = resources->textures_len; - // resources->fontpaths_len = resources->fonts_len; - //} - - //{ /* Adjust window and such */ - // /* Set actual windowsize, which might be forced by OS */ - // INFO("Adjusting window size..."); - // //windowsize = get_canvas_size(renderer); - - // INFO("Windowsize: <%d,%d>", windowsize.x, windowsize.y); - //} + w = Platform_GLFW.window_init(p, windowtitle, (ivec2){windowsize.x, windowsize.y}, flags); p->window = w; p->quit = false; @@ -319,7 +221,7 @@ i32 engine_run(Instance* p, StateType initial_state, void* state_arg) { //const f64 delta = (f64)dt / (f64)fps_cap; /* Events */ - window_poll_events(); + Platform_GLFW.window_poll(); i_flush_bindings(dt, callbacks_len, callbacks, mem->data); /* Update */ @@ -389,7 +291,7 @@ i32 engine_run(Instance* p, StateType initial_state, void* state_arg) { ticks++; } while( - !window_should_close(p->window) + !Platform_GLFW.window_should_close(p->window) && state != STATE_quit ); @@ -414,7 +316,7 @@ void engine_stop(Instance* p) { // } //} - destroy_window(p->window); + Platform_GLFW.window_destroy(p->window); free(p->resources); } diff --git a/src/rendering/include/engine/rendering/window.h b/src/rendering/include/engine/rendering/window.h index edb510a..83384bc 100644 --- a/src/rendering/include/engine/rendering/window.h +++ b/src/rendering/include/engine/rendering/window.h @@ -38,16 +38,6 @@ typedef struct { void* context; } Window; -// Window function -Window* init_window_glfw(const char* windowtitle, ivec2 windowsize, const u32 flags); -void destroy_window(Window* w); -void window_resize(Window* window, int width, int height); - -bool window_should_close(Window* w); - -// Renderer intializer(s) -void init_render_opengl(Window* w, const u32 flags); - void get_mousepos(double *x, double *y); #ifdef __cplusplus diff --git a/src/rendering/src/platform_glfw.c b/src/rendering/src/platform_glfw.c index 8237593..38c55a0 100644 --- a/src/rendering/src/platform_glfw.c +++ b/src/rendering/src/platform_glfw.c @@ -4,6 +4,7 @@ #undef GLAD_GL_IMPLEMENTATION // TODO: import vulkan thingymajig once I get around to it. +// TODO: move OpenGL initialization code at some point #undef GLFW_INCLUDE_NONE #include <GLFW/glfw3.h> @@ -101,6 +102,8 @@ Window* window_init_glfw(const Instance *restrict i, const char *restrict window INFO("WINDOW SIZE: %d x %d -> %d x %d", wsize[0], wsize[1], ret->windowsize[0], ret->windowsize[1]); } + // TODO: set this to `ret` once all the garbage is moved to `struct Window` + glfwSetWindowUserPointer(window, (void*)i); render_init_opengl(ret, flags); diff --git a/src/rendering/src/window.c b/src/rendering/src/window.c index 4cf35f2..e784158 100644 --- a/src/rendering/src/window.c +++ b/src/rendering/src/window.c @@ -19,54 +19,6 @@ extern Instance* GLOBAL_PLATFORM; -void (*window_poll_events)(void) = NULL; - -/* wrapper to get time in ms */ -u64 (*get_time)(void) = NULL; - -/* GLFW And vulkan spaghetti boiler */ -void glfw_err_callback(int code, const char* description) { - ERROR("glfw [%d]: %s\n", code, description); - // Terminate? - exit(EXIT_FAILURE); -} - -void window_resize_callback(GLFWwindow* window, int width, int height) { - (void)width; (void)height; - const GladGLContext* gl = GLOBAL_PLATFORM->window->context; - glfwSwapBuffers(window); - gl->Finish(); -} - -void framebuffer_resize_callback(GLFWwindow* window, int width, int height) { - (void)window; - const GladGLContext* gl = GLOBAL_PLATFORM->window->context; - Camera* c = GLOBAL_PLATFORM->cam; - gl->Viewport(0,0, width, height); - GLOBAL_PLATFORM->window->windowsize[0] = width; - GLOBAL_PLATFORM->window->windowsize[1] = height; - - r_reset_camera(c); -} - -void window_resize(Window* window, int width, int height) { - /* TODO: Handle different frameworks */ - window_resize_callback(window->window, width, height); - framebuffer_resize_callback(window->window, width, height); -} - -/* This is very glfw specific */ -GladGLContext* create_context(GLFWwindow *window) { - glfwMakeContextCurrent(window); - - GladGLContext* context = (GladGLContext*)malloc(sizeof(GladGLContext)); - if (!context) return NULL; - - int version = gladLoadGLContext(context, glfwGetProcAddress); - INFO("Loaded OpenGL %d.%d", GLAD_VERSION_MAJOR(version), GLAD_VERSION_MINOR(version)); - - return context; -} /* Should honestly just write my own */ static inline u64 glfw_gettime_msec(void) { @@ -87,183 +39,14 @@ static inline u64 platform_get_time_usec(void) { } return (u64)(t.tv_sec * 1000000 + t.tv_nsec / 1000); } -#define DAW_WINDOW_VSYNC (1 << 0) -#define DAW_WINDOW_FULLSCREEN (1 << 1) -#define DAW_WINDOW_RESIZEABLE (1 << 2) - -Window* init_window_glfw( - const char* windowtitle, ivec2 windowsize, - const u32 flags - ) { - Window* ret = NULL; - GLFWwindow* window = NULL; - - glfwSetErrorCallback(&glfw_err_callback); - - INFO_("initializing glfw..."); - if (glfwInit() == GLFW_FALSE) { - const char *desc; - int code = glfwGetError(&desc); - ERROR("failed to initialize glfw [%d]: %s\n", code, *desc); - exit(EXIT_FAILURE); - } else - printf("ok\n"); - - - INFO_("initializing window..."); - //glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); - if (!(flags & DAW_WINDOW_RESIZEABLE)) { - glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); - } - - glfwWindowHint(GLFW_DECORATED, GLFW_FALSE); - glfwWindowHint(GLFW_FLOATING, GLFW_TRUE); - - glfwWindowHint(GLFW_SAMPLES, 0); // Disable anti aliasing - - // Use a modern opengl version - glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); - glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6); - - // Lean and mean - glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); - -#ifdef __APPLE__ - // To make MacOS happy; should not be needed - glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); -#endif - - /* "On Wayland specifically, you need to swap the buffers - * once of a window for it to become visible." */ - { - GLFWmonitor* mon = NULL; - if (flags & DAW_WINDOW_FULLSCREEN) mon = glfwGetPrimaryMonitor(); - - window = glfwCreateWindow(windowsize[0], windowsize[1], windowtitle, mon, NULL); - } - - if (window == NULL) { - ERROR("Failed to create GLFW window!\n"); - const char *desc; - int code = glfwGetError(&desc); - ERROR("failed to initialize glfw window [%d]: %s\n", code, desc); - exit(EXIT_FAILURE); - } else - printf("ok\n"); - - // Setup callbacks - glfwSetFramebufferSizeCallback(window, framebuffer_resize_callback); - glfwSetWindowSizeCallback(window, window_resize_callback); - glfwSetKeyCallback(window, (GLFWkeyfun)key_callback); - - // Create the window datastructure - ret = (Window*)calloc(1, sizeof(Window)); - ret->framework = WINDOW_FRAMEWORK_GLFW; - ret->renderer = WINDOW_RENDERER_NONE; - ret->window = window; - /* Last parameter is used for the renderer */ - ret->context = NULL; - - { - ivec2 wsize; - vec2 wscaling; - glfwGetWindowContentScale(window, &wscaling[0], &wscaling[1]); - glfwGetWindowSize(window, &wsize[0], &wsize[1]); - - ret->windowsize[0] = (i32)((f32)wsize[0] * wscaling[0]); - ret->windowsize[1] = (i32)((f32)wsize[1] * wscaling[1]); - - INFO("WINDOW CONTENT SCALING: %.2f x %.2f", wscaling[0], wscaling[1]); - INFO("WINDOW SIZE: %d x %d -> %d x %d", wsize[0], wsize[1], ret->windowsize[0], ret->windowsize[1]); - } - - - window_poll_events = &glfwPollEvents; - //get_time = &platform_get_time_usec; - get_time = &platform_get_time_usec; - - return ret; -} - -/* Initializes opengl using the window */ -void init_render_opengl(Window* w, const u32 flags) { - if (w == NULL || w->window == NULL) { - ERROR("Window is not initialized"); - return; - } - - if (w->renderer != WINDOW_RENDERER_NONE || w->context != NULL) { - ERROR("Window already initialized with a renderer!"); - return; - } - - if (w->framework != WINDOW_FRAMEWORK_GLFW) { - ERROR("Trying to initialize OpenGL with incompatible window"); - return; - } - - // This is GLFW specific - GladGLContext *ctx = create_context((GLFWwindow*)w->window); - - if (ctx == NULL) { - ERROR("Failed to create glad context"); - exit(EXIT_FAILURE); - } - - if (w->framework == WINDOW_FRAMEWORK_GLFW) { - if (flags & DAW_WINDOW_VSYNC) { - glfwSwapInterval(1); - } else { - glfwSwapInterval(0); - } - - - ctx->Viewport(0, 0, w->windowsize[0], w->windowsize[1]); - } - -#ifdef _DEBUG - ctx->ClearColor((float)0x10 / 255.f, (float)0x0a / 255.f, (float)0x33 / 255.f, 0.f); -#else - ctx->ClearColor(0x0, 0x0, 0x0, 0.f); -#endif - - // Make sure faces closest to the camera are drawn on-top of faces that are - // further away - ctx->Enable(GL_DEPTH_TEST); - ctx->DepthFunc(GL_LESS); - - w->context = ctx; - w->renderer = WINDOW_RENDERER_OPENGL; -} -void destroy_window_glfw(GLFWwindow* w) { - glfwDestroyWindow(w); +/* wrapper to get time in ms */ +u64 (*get_time)(void) = platform_get_time_usec; - // If we ever do multi-window support, we need to make sure this is the last - // window before terminating - glfwTerminate(); -} -void destroy_window(Window* w) { - switch(w->framework) { - case WINDOW_FRAMEWORK_GLFW: - destroy_window_glfw(w->window); - w->window = NULL; - break; - default: - ERROR("Destroying unknown window type."); - } - - switch(w->renderer) { - case WINDOW_RENDERER_OPENGL: - // Missing unloader function in glad MX library - free(w->context); - w->context = NULL; - break; - default: - ERROR("Destroying unknown renderer type."); - } -} +#define DAW_WINDOW_VSYNC (1 << 0) +#define DAW_WINDOW_FULLSCREEN (1 << 1) +#define DAW_WINDOW_RESIZEABLE (1 << 2) void get_mousepos(double *x, double *y) { Window* w = GLOBAL_PLATFORM->window; |
