diff options
| -rw-r--r-- | CMakeLists.txt | 17 | ||||
| -rw-r--r-- | src/engine.c | 175 | ||||
| -rw-r--r-- | src/utils.c | 2 |
3 files changed, 162 insertions, 32 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 7f6f35a..47625f3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,6 +13,7 @@ else() endif() include(CMakeDependentOption) +include(FetchContent) option(ASAN "Enable address sanitizer. Only enabled when DAW_BUILD_DEBUG=ON") option(UBSAN "Enable undefined behaviour sanitizer. Only enabled when DAW_BUILD_DEBUG=ON") @@ -68,7 +69,7 @@ execute_process(COMMAND OUTPUT_STRIP_TRAILING_WHITESPACE ) -# +## Versioning string(TIMESTAMP COMPILATION_DATE "%F") set(ENGINE_VERSION_MAJOR ${PROJECT_VERSION_MAJOR}) @@ -77,7 +78,16 @@ set(ENGINE_VERSION_PATCH ${PROJECT_VERSION_PATCH}) set(ENGINE_VERSION_TWEAK ${GIT_SHA}) string(TOLOWER "${CMAKE_BUILD_TYPE}" ENGINE_BUILD_TYPE) -## +## Packages +find_package(glfw3 3.3 REQUIRED) +find_package(GLEW 2.2 REQUIRED) +FetchContent_Declare(cglm + GIT_REPOSITORY https://github.com/recp/cglm.git + GIT_TAG v0.9.2 +) +FetchContent_MakeAvailable(cglm) + +## Compilation information set(ENGINE_SOURCES src/btree.c @@ -108,6 +118,9 @@ target_include_directories(daw PUBLIC target_link_libraries(${PROJECT_NAME} SDL2 SDL2_image SDL2_ttf + glfw + GLEW + cglm $<$<NOT:$<PLATFORM_ID:Windows>>:m> $<$<BOOL:${DAW_BUILD_HOTRELOAD}>:dl> $<$<AND:$<NOT:$<C_COMPILER_ID:MSVC>>,$<BOOL:${DAW_BUILD_UBSAN}>>:ubsan> diff --git a/src/engine.c b/src/engine.c index af91467..5401bd1 100644 --- a/src/engine.c +++ b/src/engine.c @@ -1,6 +1,11 @@ #include <errno.h> #include <stdlib.h> #include <string.h> +#include <stdbool.h> + +#include <GL/glew.h> +#define GLFW_INCLUDE_NONE +#include <GLFW/glfw3.h> #include <SDL2/SDL.h> #include <SDL2/SDL_image.h> @@ -142,40 +147,16 @@ void engine_update_window(Window* w, SDL_WindowEvent* e) { return; } -Platform* engine_init(const char* windowtitle, v2_i32 windowsize, - const f32 render_scale, const u32 flags, - const usize initial_memory, const Asset_FontSpec* fonts[], - const Asset_TextureSpec* textures[]) { - -#ifdef BENCHMARK - u32 init_start = SDL_GetTicks(); -#endif - -#if defined(__linux) || defined(__linux__) || defined(linux) - { - pid_t pid = getpid(); - INFO("Starting with pid %lu", pid); - } -#endif +struct sdl_ctx {SDL_Window* w; SDL_Renderer* r;} sdl_ctx; +struct glfw_ctx {GLFWwindow* w;} glfw_ctx; - Platform* p = (Platform*)malloc(sizeof(Platform)); - Window* w = (Window*)malloc(sizeof(Window)); +struct sdl_ctx initialize_SDL( + const char* windowtitle, v2_i32 windowsize, + const u32 flags + ) { SDL_Window* window = NULL; SDL_Renderer* renderer = NULL; - /* initialize resources */ - struct Resources* resources = - (struct Resources*)malloc(sizeof(struct Resources)); - resources->textures_len = 0; - resources->fonts_len = 0; - resources->texturepaths_len = 0; - resources->fontpaths_len = 0; - resources->texture_paths = NULL; - resources->font_paths = NULL; - resources->textures = NULL; - resources->fonts = NULL; - - { /* Init subsystems */ INFO_("initializing sdl..."); if (SDL_Init(SDL_INIT_VIDEO) < 0) { ERROR("failed to initialize sdl: %s\n", SDL_GetError()); @@ -222,8 +203,142 @@ Platform* engine_init(const char* windowtitle, v2_i32 windowsize, exit(EXIT_FAILURE); } else printf("ok\n"); + + return (struct sdl_ctx){.w = window,.r = renderer}; +} + + +/* 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); +} + +struct QueueFamilyIndices { + int64_t graphicsFamily; + int64_t presentFamily; +}; + + + +struct glfw_ctx initialize_GLFW( + const char* windowtitle, v2_i32 windowsize, + const u32 flags + ) { + 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); + glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); + + glfwWindowHint(GLFW_SAMPLES, 0); // Disable anti aliasing + + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6); + glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed + glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); + + window = glfwCreateWindow(windowsize.x, windowsize.y, windowtitle, NULL, 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"); + + glfwMakeContextCurrent(window); + + + glewExperimental = true; + if (glewInit() != GLEW_OK) { + ERROR("failed to initialize glew!\n"); + exit(EXIT_FAILURE); } + // TODO: Replace with callback + //glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE); + + //while(!(glfwWindowShouldClose(window) && glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS)) { + + // glClear( GL_COLOR_BUFFER_BIT ); + + // glfwSwapBuffers(window); + + // glfwPollEvents(); + //} + + //glfwDestroyWindow(window); + + //glfwTerminate(); + + return (struct glfw_ctx){.w = NULL}; +} + + +/* Creates the window, initializes IO, Rendering, Fonts and engine-specific + * resources. */ +Platform* engine_init(const char* windowtitle, v2_i32 windowsize, + const f32 render_scale, const u32 flags, + const usize initial_memory, const Asset_FontSpec* fonts[], + const Asset_TextureSpec* textures[]) { + +#ifdef BENCHMARK + u32 init_start = glfwGetTime(); +#endif + +#if defined(__linux) || defined(__linux__) || defined(linux) + { + pid_t pid = getpid(); + INFO("Starting with pid %lu", pid); + } +#endif + + Platform* p = (Platform*)malloc(sizeof(Platform)); + Window* w = (Window*)malloc(sizeof(Window)); + SDL_Window* window = NULL; + SDL_Renderer* renderer = NULL; + + /* initialize resources */ + struct Resources* resources = + (struct Resources*)malloc(sizeof(struct Resources)); + resources->textures_len = 0; + resources->fonts_len = 0; + resources->texturepaths_len = 0; + resources->fontpaths_len = 0; + resources->texture_paths = NULL; + resources->font_paths = NULL; + resources->textures = NULL; + resources->fonts = NULL; + + /* Init subsystems */ + //{ + // struct sdl_ctx sdl_ctx = initialize_SDL(windowtitle, windowsize, flags); + // window = sdl_ctx.w; + // renderer = sdl_ctx.r; + //} + /* Init glfw haha */ + { + struct glfw_ctx ctx = initialize_GLFW(windowtitle, windowsize, flags); + } + exit(0); + + + { /* Resource loading */ /* Count resources */ diff --git a/src/utils.c b/src/utils.c index f91ef4d..0f3d218 100644 --- a/src/utils.c +++ b/src/utils.c @@ -6,6 +6,8 @@ #include <engine/logging.h> #include <engine/utils.h> +/* These should all be in some external facing module "tools" */ + f32 lerp(f32 dt, f32 a, f32 b) { return (a * (1.0f - dt)) + (b * dt); } i32 int_lerp(f32 dt, i32 a, i32 b) { |
