diff options
| author | 0scar <qgt268@alumni.ku.dk> | 2024-02-06 11:52:32 +0000 |
|---|---|---|
| committer | 0scar <qgt268@alumni.ku.dk> | 2024-02-06 11:52:32 +0000 |
| commit | c7a3dcdc8a26eb5bf98ce2079abd66fd64534459 (patch) | |
| tree | ff5c572f94c3bf3e11e7f9e75e617d4454cc263a | |
| parent | 3705b14a4b2ac0d6baa418f08991424cfad89891 (diff) | |
Fixup window handling & rendering
| -rw-r--r-- | CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/core/include/engine/engine.h | 58 | ||||
| -rw-r--r-- | src/core/src/loop.c | 833 | ||||
| -rw-r--r-- | src/main.c | 1 | ||||
| -rw-r--r-- | src/rendering/include/engine/rendering/rendering.h | 62 | ||||
| -rw-r--r-- | src/rendering/include/engine/rendering/window.h | 43 | ||||
| -rw-r--r-- | src/rendering/src/gl.c | 119 | ||||
| -rw-r--r-- | src/rendering/src/rendering.c | 30 | ||||
| -rw-r--r-- | src/rendering/src/window.c | 185 | ||||
| -rw-r--r-- | src/resources/include/engine/resources.h | 26 | ||||
| -rw-r--r-- | src/resources/include/engine/resources/texture.h | 14 | ||||
| -rw-r--r-- | src/resources/src/textures.c | 20 | ||||
| -rw-r--r-- | src/ui/CMakeLists.txt | 4 | ||||
| -rw-r--r-- | src/ui/include/engine/ui.h | 5 | ||||
| -rw-r--r-- | src/ui/src/positioning.c | 15 | ||||
| -rw-r--r-- | src/ui/src/rendering.c | 22 |
16 files changed, 763 insertions, 676 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 39f8e76..95a2adb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -101,7 +101,6 @@ add_link_options(${RELEASE_OPTS}) #set_property(SOURCE src/core/include/engine/engine.h APPEND PROPERTY OBJECT_DEPENDS ${GLAD_HEADER}) add_library(${PROJECT_NAME} - src/main.c ${GLAD_HEADER} ) @@ -113,7 +112,6 @@ target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_BINARY_DIR}/include ${ENGINE_INCLUDE}) - # Add the modules add_subdirectory(src) diff --git a/src/core/include/engine/engine.h b/src/core/include/engine/engine.h index ba4e227..6e208d3 100644 --- a/src/core/include/engine/engine.h +++ b/src/core/include/engine/engine.h @@ -20,14 +20,14 @@ typedef struct { i32 x, y, w, h; } RenderUnit; -typedef struct Window Window; +#include <engine/rendering/window.h> #define NUM_GLOBAL_BINDINGS 1 typedef struct { void* data; /* Contains textures and such */ u64 data_len; - Window* window; + Window window; bool quit; u64 frame; @@ -78,7 +78,7 @@ void render_adjust_zoom(f32 diff); void render_add_unit(RenderUnit* u); f64 get_time(void); -v2_i32 get_windowsize(void); +//v2_i32 get_windowsize(void); v2_i32* get_mousepos(void); /* Input handling */ @@ -86,48 +86,14 @@ void engine_input_ctx_push(i_ctx* ctx); void engine_input_ctx_pop(void); void engine_input_ctx_reset(void); -#include <engine/rendering/rendering.h> +//#include <engine/rendering/rendering.h> -#ifdef ENGINE_INTERNALS - -#include <glad/gl.h> -#define GLFW_INCLUDE_NONE -#include <GLFW/glfw3.h> - -/* Window */ -struct Window { - GLFWwindow* window; - GladGLContext* context; - f32 render_scale; - - v2_i32 windowsize; - - i32* game_w; - i32* game_h; -}; - -typedef struct { - const i32 tilesize; - const i32 width; - const i32 height; -} Texture; - -struct Resources { - usize textures_len; - usize textures_size; - usize fonts_len; - - usize texturepaths_len; - usize fontpaths_len; - - /* Paths for our sources, kept in case the user wants to reload them */ - Asset_TextureSpec** texture_paths; - Asset_FontSpec** font_paths; - - /* Our actual sources */ - Texture** textures; - //TTF_Font** fonts; -}; - -#endif +//#ifdef ENGINE_INTERNALS +// +//#include <glad/gl.h> +//#define GLFW_INCLUDE_NONE +//#include <GLFW/glfw3.h> +// +// +//#endif #endif diff --git a/src/core/src/loop.c b/src/core/src/loop.c index 837d193..721e66f 100644 --- a/src/core/src/loop.c +++ b/src/core/src/loop.c @@ -3,13 +3,6 @@ #include <string.h> #include <stdbool.h> -#define GLAD_GL_IMPLEMENTATION -#include <glad/gl.h> -#undef GLAD_GL_IMPLEMENTATION - -#undef GLFW_INCLUDE_NONE -#include <GLFW/glfw3.h> - #include <cglm/cglm.h> #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) @@ -24,13 +17,15 @@ #endif -#define ENGINE_INTERNALS +#include <engine/core/state.h> #include <engine/engine.h> #include <engine/utils/btree.h> #include <engine/utils/hashmap.h> #include <engine/utils/list.h> -#include <engine/core/state.h> +#include <engine/rendering/window.h> +#include <engine/rendering/rendering.h> + #define DEFAULT_NUM_PROCS 8 @@ -48,116 +43,6 @@ extern i32 drawcall_len; #endif -// http://www.opengl-tutorial.org/beginners-tutorials/tutorial-2-the-first-triangle/ -GLuint LoadShaders(const GladGLContext* gl, const char * vertex_file_path, const char * fragment_file_path){ - - // Create the shaders - GLuint VertexShaderID = gl->CreateShader(GL_VERTEX_SHADER); - GLuint FragmentShaderID = gl->CreateShader(GL_FRAGMENT_SHADER); - - // Read the Vertex Shader code from the file - char* VertexShaderCode; - FILE* VertexShaderStream = fopen(vertex_file_path, "r"); - if(VertexShaderStream != NULL){ - fseek(VertexShaderStream, 0, SEEK_END); - const i64 size = ftell(VertexShaderStream); - rewind(VertexShaderStream); - VertexShaderCode = calloc(size + 1, sizeof(char)); - - // Assume the whole file is successfully read - fread(VertexShaderCode, sizeof(char), size, VertexShaderStream); - //LOG("vertex source is %d bytes\n%s\n", size, VertexShaderCode); - - fclose(VertexShaderStream); - } else { - ERROR("Impossible to open %s. Are you in the right directory?\n", vertex_file_path); - getchar(); - return 0; - } - - // Read the Fragment Shader code from the file - char* FragmentShaderCode; - FILE* FragmentShaderStream = fopen(fragment_file_path, "r"); - if(FragmentShaderStream != NULL){ - fseek(FragmentShaderStream, 0, SEEK_END); - const i64 size = ftell(FragmentShaderStream); - rewind(FragmentShaderStream); - FragmentShaderCode = calloc(size + 1, sizeof(char)); - - // Assume the whole file is successfully read - fread(FragmentShaderCode, sizeof(char), size, FragmentShaderStream); - LOG("fragment source is %d bytes", size); - - fclose(FragmentShaderStream); - } else { - ERROR("Impossible to open %s. Are you in the right directory?\n", fragment_file_path); - getchar(); - return 0; - } - - GLint Result = GL_FALSE; - int InfoLogLength; - - // Compile Vertex Shader - INFO("Compiling shader: %s\n", vertex_file_path); - char const * VertexSourcePointer = VertexShaderCode; - gl->ShaderSource(VertexShaderID, 1, &VertexSourcePointer , NULL); - gl->CompileShader(VertexShaderID); - - // Check Vertex Shader - gl->GetShaderiv(VertexShaderID, GL_COMPILE_STATUS, &Result); - gl->GetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength); - if ( InfoLogLength > 0 ) { - char* msg = calloc(InfoLogLength + 1, sizeof(char)); - gl->GetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, msg); - ERROR("Compiling shader: %s\n", msg); - free(msg); - } - - // Compile Fragment Shader - INFO("Compiling shader: %s\n", fragment_file_path); - char const * FragmentSourcePointer = FragmentShaderCode; - gl->ShaderSource(FragmentShaderID, 1, &FragmentSourcePointer , NULL); - gl->CompileShader(FragmentShaderID); - - // Check Fragment Shader - gl->GetShaderiv(FragmentShaderID, GL_COMPILE_STATUS, &Result); - gl->GetShaderiv(FragmentShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength); - if ( InfoLogLength > 0 ){ - char* msg = calloc(InfoLogLength + 1, sizeof(char)); - gl->GetShaderInfoLog(FragmentShaderID, InfoLogLength, NULL, msg); - ERROR("Compiling shader: %s\n", msg); - free(msg); - } - - // Link the program - INFO("Linking program\n"); - GLuint ProgramID = gl->CreateProgram(); - gl->AttachShader(ProgramID, VertexShaderID); - gl->AttachShader(ProgramID, FragmentShaderID); - gl->LinkProgram(ProgramID); - - // Check the program - gl->GetProgramiv(ProgramID, GL_LINK_STATUS, &Result); - gl->GetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength); - if ( InfoLogLength > 0 ){ - char* msg = calloc(InfoLogLength + 1, sizeof(char)); - gl->GetShaderInfoLog(ProgramID, InfoLogLength, NULL, msg); - ERROR("Compiling shader: %s\n", msg); - free(msg); - } - - gl->DetachShader(ProgramID, VertexShaderID); - gl->DetachShader(ProgramID, FragmentShaderID); - - gl->DeleteShader(VertexShaderID); - gl->DeleteShader(FragmentShaderID); - - - free(VertexShaderCode); - free(FragmentShaderCode); - return ProgramID; -} @@ -195,146 +80,19 @@ i32 cmp_int(const void* a, const void* b) { return *x - *y; } -v2_i32 get_canvas_size(void* window) { - v2_i32 realsize; - glfwGetWindowSize(window, &(realsize.x), &(realsize.y)); - - /* Set logical render size */ - return realsize; -} - -Texture* load_texture(void* render, const Asset_TextureSpec* ts) { - Texture* t = NULL; - - if (ts == NULL) { - ERROR("Invalid Asset_TextureSpec\n"); - return NULL; - } - - //t = (Texture*)malloc(sizeof(Texture)); - //t->texture = new_texture; - ///* Assigning const value */ - //*(i32*)&t->tilesize = tw; - //*(i32*)&t->width = ts->width; - //*(i32*)&t->height = ts->height; - - return t; -} +//v2_i32 get_canvas_size(void* window) { +// v2_i32 realsize; +// glfwGetWindowSize(window, &(realsize.x), &(realsize.y)); +// +// /* Set logical render size */ +// return realsize; +//} void engine_update_window(Window* w, void* e) { - switch ((i32)e) { - default: - WARN("Unhandled window event 0x%04x", (i32)e); - break; - } + //WARN("Unhandled function", (i32)e); return; } -struct glfw_ctx { - GLFWwindow* w; - GladGLContext* c; -} glfw_ctx; - -/* 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; -}; - -GladGLContext* create_context(GLFWwindow *window) { - glfwMakeContextCurrent(window); - - GladGLContext* context = (GladGLContext*) calloc(1, 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; -} - -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 - - // Use a modern opengl version - glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); - glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6); - - 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 - * of a window for it to become visible." */ - 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); - - // Remember to load GL :) (hours wasted because i forgot: approx 4) - GladGLContext *ctx = create_context(window); - //printf("GL %d.%d\n", GLAD_VERSION_MAJOR(version), GLAD_VERSION_MINOR(version)); - - //ctx->Viewport(0, 0, 200, 200); - - if (ctx == NULL) { - ERROR("Failed to create glad context"); - exit(EXIT_FAILURE); - } - - glfwSetFramebufferSizeCallback(window, window_size_callback); - glfwSwapInterval(0); - -#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 - - ctx->Enable(GL_DEPTH_TEST); - ctx->DepthFunc(GL_LESS); - - return (struct glfw_ctx){.w = window, .c = ctx}; -} - /* Creates the window, initializes IO, Rendering, Fonts and engine-specific * resources. */ @@ -343,10 +101,6 @@ Platform* engine_init(const char* windowtitle, v2_i32 windowsize, const usize initial_memory, const Asset_FontSpec* fonts[], const Asset_TextureSpec* textures[]) { -#ifdef BENCHMARK - f64 init_start = get_time(); -#endif - #if defined(__linux) || defined(__linux__) || defined(linux) { pid_t pid = getpid(); @@ -355,247 +109,238 @@ Platform* engine_init(const char* windowtitle, v2_i32 windowsize, #endif Platform* p = (Platform*)malloc(sizeof(Platform)); - Window* w = (Window*)malloc(sizeof(Window)); + Window w = (Window)malloc(sizeof(Window)); /* 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->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; - { - struct glfw_ctx ctx = initialize_GLFW(windowtitle, windowsize, flags); - w->window = ctx.w; - w->context = ctx.c; - } + w = init_window_glfw(windowtitle, (ivec2){windowsize.x, windowsize.y}, flags); + // Dont forget to init the renderer + init_render_opengl(w); - const GladGLContext *gl = w->context; + //const GladGLContext *gl = w->context; - struct RenderObject *testobject = malloc(sizeof(struct RenderObject)); + //struct RenderObject *testobject = malloc(sizeof(struct RenderObject)); - gl->GenVertexArrays(1, &(testobject->vao)); - gl->BindVertexArray(testobject->vao); + //gl->GenVertexArrays(1, &(testobject->vao)); + //gl->BindVertexArray(testobject->vao); - p->testobject = testobject; + //p->testobject = testobject; - testobject->g_vertex_buffer_data[0] = -1.0f; - testobject->g_vertex_buffer_data[1] = -1.0f; - testobject->g_vertex_buffer_data[2] = 0.0f; + //testobject->g_vertex_buffer_data[0] = -1.0f; + //testobject->g_vertex_buffer_data[1] = -1.0f; + //testobject->g_vertex_buffer_data[2] = 0.0f; - testobject->g_vertex_buffer_data[3] = 1.0f; - testobject->g_vertex_buffer_data[4] = -1.0f; - testobject->g_vertex_buffer_data[5] = 0.0f; + //testobject->g_vertex_buffer_data[3] = 1.0f; + //testobject->g_vertex_buffer_data[4] = -1.0f; + //testobject->g_vertex_buffer_data[5] = 0.0f; - testobject->g_vertex_buffer_data[6] = 0.0f; - testobject->g_vertex_buffer_data[7] = 1.0f; - testobject->g_vertex_buffer_data[8] = 0.0f; + //testobject->g_vertex_buffer_data[6] = 0.0f; + //testobject->g_vertex_buffer_data[7] = 1.0f; + //testobject->g_vertex_buffer_data[8] = 0.0f; - static const float bufdata[] = { - -1.0f,-1.0f,-1.0f, // triangle 1 : begin - -1.0f,-1.0f, 1.0f, - -1.0f, 1.0f, 1.0f, // triangle 1 : end - 1.0f, 1.0f,-1.0f, // triangle 2 : begin - -1.0f,-1.0f,-1.0f, - -1.0f, 1.0f,-1.0f, // triangle 2 : end - 1.0f,-1.0f, 1.0f, - -1.0f,-1.0f,-1.0f, - 1.0f,-1.0f,-1.0f, - 1.0f, 1.0f,-1.0f, - 1.0f,-1.0f,-1.0f, - -1.0f,-1.0f,-1.0f, - -1.0f,-1.0f,-1.0f, - -1.0f, 1.0f, 1.0f, - -1.0f, 1.0f,-1.0f, - 1.0f,-1.0f, 1.0f, - -1.0f,-1.0f, 1.0f, - -1.0f,-1.0f,-1.0f, - -1.0f, 1.0f, 1.0f, - -1.0f,-1.0f, 1.0f, - 1.0f,-1.0f, 1.0f, - 1.0f, 1.0f, 1.0f, - 1.0f,-1.0f,-1.0f, - 1.0f, 1.0f,-1.0f, - 1.0f,-1.0f,-1.0f, - 1.0f, 1.0f, 1.0f, - 1.0f,-1.0f, 1.0f, - 1.0f, 1.0f, 1.0f, - 1.0f, 1.0f,-1.0f, - -1.0f, 1.0f,-1.0f, - 1.0f, 1.0f, 1.0f, - -1.0f, 1.0f,-1.0f, - -1.0f, 1.0f, 1.0f, - 1.0f, 1.0f, 1.0f, - -1.0f, 1.0f, 1.0f, - 1.0f,-1.0f, 1.0f - }; + //static const float bufdata[] = { + // -1.0f,-1.0f,-1.0f, // triangle 1 : begin + // -1.0f,-1.0f, 1.0f, + // -1.0f, 1.0f, 1.0f, // triangle 1 : end + // 1.0f, 1.0f,-1.0f, // triangle 2 : begin + // -1.0f,-1.0f,-1.0f, + // -1.0f, 1.0f,-1.0f, // triangle 2 : end + // 1.0f,-1.0f, 1.0f, + // -1.0f,-1.0f,-1.0f, + // 1.0f,-1.0f,-1.0f, + // 1.0f, 1.0f,-1.0f, + // 1.0f,-1.0f,-1.0f, + // -1.0f,-1.0f,-1.0f, + // -1.0f,-1.0f,-1.0f, + // -1.0f, 1.0f, 1.0f, + // -1.0f, 1.0f,-1.0f, + // 1.0f,-1.0f, 1.0f, + // -1.0f,-1.0f, 1.0f, + // -1.0f,-1.0f,-1.0f, + // -1.0f, 1.0f, 1.0f, + // -1.0f,-1.0f, 1.0f, + // 1.0f,-1.0f, 1.0f, + // 1.0f, 1.0f, 1.0f, + // 1.0f,-1.0f,-1.0f, + // 1.0f, 1.0f,-1.0f, + // 1.0f,-1.0f,-1.0f, + // 1.0f, 1.0f, 1.0f, + // 1.0f,-1.0f, 1.0f, + // 1.0f, 1.0f, 1.0f, + // 1.0f, 1.0f,-1.0f, + // -1.0f, 1.0f,-1.0f, + // 1.0f, 1.0f, 1.0f, + // -1.0f, 1.0f,-1.0f, + // -1.0f, 1.0f, 1.0f, + // 1.0f, 1.0f, 1.0f, + // -1.0f, 1.0f, 1.0f, + // 1.0f,-1.0f, 1.0f + //}; - static const GLfloat g_color_buffer_data[] = { - 0.583f, 0.771f, 0.014f, - 0.609f, 0.115f, 0.436f, - 0.327f, 0.483f, 0.844f, - 0.822f, 0.569f, 0.201f, - 0.435f, 0.602f, 0.223f, - 0.310f, 0.747f, 0.185f, - 0.597f, 0.770f, 0.761f, - 0.559f, 0.436f, 0.730f, - 0.359f, 0.583f, 0.152f, - 0.483f, 0.596f, 0.789f, - 0.559f, 0.861f, 0.639f, - 0.195f, 0.548f, 0.859f, - 0.014f, 0.184f, 0.576f, - 0.771f, 0.328f, 0.970f, - 0.406f, 0.615f, 0.116f, - 0.676f, 0.977f, 0.133f, - 0.971f, 0.572f, 0.833f, - 0.140f, 0.616f, 0.489f, - 0.997f, 0.513f, 0.064f, - 0.945f, 0.719f, 0.592f, - 0.543f, 0.021f, 0.978f, - 0.279f, 0.317f, 0.505f, - 0.167f, 0.620f, 0.077f, - 0.347f, 0.857f, 0.137f, - 0.055f, 0.953f, 0.042f, - 0.714f, 0.505f, 0.345f, - 0.783f, 0.290f, 0.734f, - 0.722f, 0.645f, 0.174f, - 0.302f, 0.455f, 0.848f, - 0.225f, 0.587f, 0.040f, - 0.517f, 0.713f, 0.338f, - 0.053f, 0.959f, 0.120f, - 0.393f, 0.621f, 0.362f, - 0.673f, 0.211f, 0.457f, - 0.820f, 0.883f, 0.371f, - 0.982f, 0.099f, 0.879f -}; + //static const GLfloat g_color_buffer_data[] = { + // 0.583f, 0.771f, 0.014f, + // 0.609f, 0.115f, 0.436f, + // 0.327f, 0.483f, 0.844f, + // 0.822f, 0.569f, 0.201f, + // 0.435f, 0.602f, 0.223f, + // 0.310f, 0.747f, 0.185f, + // 0.597f, 0.770f, 0.761f, + // 0.559f, 0.436f, 0.730f, + // 0.359f, 0.583f, 0.152f, + // 0.483f, 0.596f, 0.789f, + // 0.559f, 0.861f, 0.639f, + // 0.195f, 0.548f, 0.859f, + // 0.014f, 0.184f, 0.576f, + // 0.771f, 0.328f, 0.970f, + // 0.406f, 0.615f, 0.116f, + // 0.676f, 0.977f, 0.133f, + // 0.971f, 0.572f, 0.833f, + // 0.140f, 0.616f, 0.489f, + // 0.997f, 0.513f, 0.064f, + // 0.945f, 0.719f, 0.592f, + // 0.543f, 0.021f, 0.978f, + // 0.279f, 0.317f, 0.505f, + // 0.167f, 0.620f, 0.077f, + // 0.347f, 0.857f, 0.137f, + // 0.055f, 0.953f, 0.042f, + // 0.714f, 0.505f, 0.345f, + // 0.783f, 0.290f, 0.734f, + // 0.722f, 0.645f, 0.174f, + // 0.302f, 0.455f, 0.848f, + // 0.225f, 0.587f, 0.040f, + // 0.517f, 0.713f, 0.338f, + // 0.053f, 0.959f, 0.120f, + // 0.393f, 0.621f, 0.362f, + // 0.673f, 0.211f, 0.457f, + // 0.820f, 0.883f, 0.371f, + // 0.982f, 0.099f, 0.879f + //}; -// LOG("sizeof(bufdata) = %lu", sizeof(bufdata)); -// LOG("sizeof(g_vertex_buffer_data) = %lu", sizeof(testobject->g_vertex_buffer_data)); -// +//// LOG("sizeof(bufdata) = %lu", sizeof(bufdata)); +//// LOG("sizeof(g_vertex_buffer_data) = %lu", sizeof(testobject->g_vertex_buffer_data)); +//// - // Generate 1 buffer, put the resulting identifier in vertexbuffer - gl->GenBuffers(1, &(testobject->vbo)); - // The following commands will talk about our 'vertexbuffer' buffer - gl->BindBuffer(GL_ARRAY_BUFFER, testobject->vbo); - // Give our vertices to OpenGL. - gl->BufferData(GL_ARRAY_BUFFER, sizeof(bufdata), bufdata, GL_STATIC_DRAW); + //// Generate 1 buffer, put the resulting identifier in vertexbuffer + //gl->GenBuffers(1, &(testobject->vbo)); + //// The following commands will talk about our 'vertexbuffer' buffer + //gl->BindBuffer(GL_ARRAY_BUFFER, testobject->vbo); + //// Give our vertices to OpenGL. + //gl->BufferData(GL_ARRAY_BUFFER, sizeof(bufdata), bufdata, GL_STATIC_DRAW); - // Same for the color buffer - gl->GenBuffers(1, &(testobject->col)); - gl->BindBuffer(GL_ARRAY_BUFFER, testobject->col); - gl->BufferData(GL_ARRAY_BUFFER, sizeof(g_color_buffer_data), g_color_buffer_data, GL_STATIC_DRAW); + //// Same for the color buffer + //gl->GenBuffers(1, &(testobject->col)); + //gl->BindBuffer(GL_ARRAY_BUFFER, testobject->col); + //gl->BufferData(GL_ARRAY_BUFFER, sizeof(g_color_buffer_data), g_color_buffer_data, GL_STATIC_DRAW); - testobject->shaderprogram = LoadShaders(gl, "shader.vertexshader", "shader.fragmentshader"); - INFO("Shaderprogram %d", testobject->shaderprogram); + //testobject->shaderprogram = LoadShaders(gl, "shader.vertexshader", "shader.fragmentshader"); + //INFO("Shaderprogram %d", testobject->shaderprogram); - { /* Resource loading */ + //{ /* Resource loading */ - /* Count resources */ - usize n_textures = 0; - usize n_fonts = 0; + // /* 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++; + // 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); + // 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; + // /* 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; + // /* 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; + // 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); + // /* 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++; - //} - } +//// 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); + // /* 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++; - //} - } + // //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); - } + // 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); + // } - resources->texturepaths_len = resources->textures_len; - resources->fontpaths_len = resources->fonts_len; - } + // 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); + // } - { /* Adjust window and such */ - /* Set actual windowsize, which might be forced by OS */ - INFO("Adjusting window size..."); - //windowsize = get_canvas_size(renderer); + // resources->texturepaths_len = resources->textures_len; + // resources->fontpaths_len = resources->fonts_len; + //} - INFO("Windowsize: <%d,%d>", windowsize.x, windowsize.y); - } + //{ /* Adjust window and such */ + // /* Set actual windowsize, which might be forced by OS */ + // INFO("Adjusting window size..."); + // //windowsize = get_canvas_size(renderer); - //w->renderer = renderer; - //w->window = window; - w->render_scale = render_scale; - w->windowsize = windowsize; - w->game_w = NULL; - w->game_h = NULL; + // INFO("Windowsize: <%d,%d>", windowsize.x, windowsize.y); + //} p->data = (void*)resources; p->data_len = sizeof(struct Resources); @@ -628,11 +373,6 @@ Platform* engine_init(const char* windowtitle, v2_i32 windowsize, // TODO: Add global bindings -#ifdef BENCHMARK - f64 init_stop = get_time(); - INFO("Initialization took %dms", init_stop - init_start); -#endif - INFO("Available cores: %d", nproc()); GLOBAL_PLATFORM = p; @@ -676,19 +416,6 @@ i32 engine_run(Platform* p, StateType initial_state) { u64 ticks = 0; /* Profiling values */ -#ifdef BENCHMARK - u64 profile_tick_counter = 0; - // u64 profile_slack = 0; - u64 profile_rendering = 0; - u64 profile_gameloop = 0; - u64 profile_input = 0; - u64 profile_input_handling = 0; - u64 profile_num_drawcalls = 0; - u32 profile_interval_timer = time; - const u32 profile_interval_ms = 5000; - const f32 profile_interval_scale = (f32)(profile_interval_ms) / 100.0f; -#endif - const f64 frame_interval = 1000.0 / FPS_CAP; StateType (*update_func)(void*) = State_updateFunc(state); @@ -696,26 +423,16 @@ i32 engine_run(Platform* p, StateType initial_state) { f64 last_fps_measurement = get_time(); /* Main loop */ - INFO("Program: %d", p->testobject->shaderprogram); - GladGLContext *gl = p->window->context; + //INFO("Program: %d", p->testobject->shaderprogram); + //GladGLContext *gl = p->window->context; do { const f64 now = get_time(); const f64 dt = now - time; time = now; - /* Wait frame_interval */ - if (dt < frame_interval) { -#ifndef BENCHMARK - //delay(frame_interval - dt); - -#else - /* We want to know how much time is spend sleeping */ - // profile_slack += frame_interval - dt; -#endif - } if (now - last_fps_measurement > 1.000) { last_fps_measurement = now; - printf("\n FPS: %.1f \t ticks: %lu", (double)ticks / now, ticks); + //printf("\n FPS: %.1f \t ticks: %lu", (double)ticks / now, ticks); } #ifdef BENCHMARK @@ -754,7 +471,7 @@ i32 engine_run(Platform* p, StateType initial_state) { } #endif - glfwPollEvents(); + //glfwPollEvents(); /* Events */ // if (p->mouse_lclick) { // p->mouseup.x = -1; @@ -815,67 +532,67 @@ i32 engine_run(Platform* p, StateType initial_state) { render_begin(p->window); - gl->UseProgram(p->testobject->shaderprogram); + //gl->UseProgram(p->testobject->shaderprogram); - { - vec3 cam = {4., 3., 3.}; // perspective - mat4 per; // perspective - mat4 v; // view - mat4 model = GLM_MAT4_IDENTITY_INIT; - mat4 modelviewprojection; + //{ + // vec3 cam = {4., 3., 3.}; // perspective + // mat4 per; // perspective + // mat4 v; // view + // mat4 model = GLM_MAT4_IDENTITY_INIT; + // mat4 modelviewprojection; - f32 ratio = (float)p->window->windowsize.x / (float)p->window->windowsize.y; - //glm_perspective(45.f , 600.f / 400.f, 0.1, 100.0f, per); - glm_ortho(-10 * ratio, 10 * ratio, -10, 10, -10, 10, per); + // f32 ratio = (float)p->window->windowsize.x / (float)p->window->windowsize.y; + // //glm_perspective(45.f , 600.f / 400.f, 0.1, 100.0f, per); + // glm_ortho(-10 * ratio, 10 * ratio, -10, 10, -10, 10, per); - glm_lookat(cam, GLM_VEC3_ZERO, GLM_YUP, v); + // glm_lookat(cam, GLM_VEC3_ZERO, GLM_YUP, v); - { mat4 t; - //modelviewprojection = p * v * model - glm_mat4_mul(v, model, t); - glm_rotate_at(t, (vec3){0,0,0}, get_time() / 2.f, (vec3){0,1,0}); //, (vec3)({0,1,0})); - glm_mat4_mul(per, t, modelviewprojection); - } + // { mat4 t; + // //modelviewprojection = p * v * model + // glm_mat4_mul(v, model, t); + // glm_rotate_at(t, (vec3){0,0,0}, get_time() / 2.f, (vec3){0,1,0}); //, (vec3)({0,1,0})); + // glm_mat4_mul(per, t, modelviewprojection); + // } - // TODO: Do this only once during initialization - u32 matrix = gl->GetUniformLocation(p->testobject->shaderprogram, "MVP"); + // // TODO: Do this only once during initialization + // u32 matrix = gl->GetUniformLocation(p->testobject->shaderprogram, "MVP"); - gl->UniformMatrix4fv(matrix, 1, GL_FALSE, &modelviewprojection[0][0]); - } + // gl->UniformMatrix4fv(matrix, 1, GL_FALSE, &modelviewprojection[0][0]); + //} - gl->EnableVertexAttribArray(0); - gl->BindBuffer(GL_ARRAY_BUFFER, p->testobject->vbo); - gl->VertexAttribPointer( - 0, // attribute 0. No particular reason for 0, but must match the layout in the shader. - 3, // size - GL_FLOAT, // type - GL_FALSE, // normalized? - 0, // stride - (void*)0 // array buffer offset - ); + //gl->EnableVertexAttribArray(0); + //gl->BindBuffer(GL_ARRAY_BUFFER, p->testobject->vbo); + //gl->VertexAttribPointer( + // 0, // attribute 0. No particular reason for 0, but must match the layout in the shader. + // 3, // size + // GL_FLOAT, // type + // GL_FALSE, // normalized? + // 0, // stride + // (void*)0 // array buffer offset + // ); - // Do the color buffer (?) - gl->EnableVertexAttribArray(1); - gl->BindBuffer(GL_ARRAY_BUFFER, p->testobject->col); - gl->VertexAttribPointer( - 1, // attribute. No particular reason for 1, but must match the layout in the shader. - 3, // size - GL_FLOAT, // type - GL_FALSE, // normalized? - 0, // stride - (void*)0 // array buffer offset - ); + //// Do the color buffer (?) + //gl->EnableVertexAttribArray(1); + //gl->BindBuffer(GL_ARRAY_BUFFER, p->testobject->col); + //gl->VertexAttribPointer( + // 1, // attribute. No particular reason for 1, but must match the layout in the shader. + // 3, // size + // GL_FLOAT, // type + // GL_FALSE, // normalized? + // 0, // stride + // (void*)0 // array buffer offset + // ); - gl->UseProgram(p->testobject->shaderprogram); - // Draw the triangle ! - gl->DrawArrays(GL_TRIANGLES, 0, 3*12); // Starting from vertex 0; 3 vertices total -> 1 triangle + //gl->UseProgram(p->testobject->shaderprogram); + //// Draw the triangle ! + //gl->DrawArrays(GL_TRIANGLES, 0, 3*12); // Starting from vertex 0; 3 vertices total -> 1 triangle - gl->DisableVertexAttribArray(0); - gl->DisableVertexAttribArray(1); + //gl->DisableVertexAttribArray(0); + //gl->DisableVertexAttribArray(1); render_present(p->window); // } @@ -888,26 +605,24 @@ i32 engine_run(Platform* p, StateType initial_state) { return 0; } -void stop(Platform* p) { +void engine_stop(Platform* p) { if (p == NULL) return; - { /* Deallocate resources */ - struct Resources* r = (struct Resources*)p->data; - if (r != NULL) { - /* Destroy textures */ - for (usize i = 0; i < r->textures_len; i++) { - if (r->textures[i] != NULL) { - r->textures[i] = NULL; - } - } - free(r->textures); + //{ /* Deallocate resources */ + // struct Resources* r = (struct Resources*)p->data; + // if (r != NULL) { + // /* Destroy textures */ + // for (usize i = 0; i < r->textures_len; i++) { + // if (r->textures[i] != NULL) { + // r->textures[i] = NULL; + // } + // } + // free(r->textures); - /* Destroy Fonts */ - } - } + // /* Destroy Fonts */ + // } + //} - glfwDestroyWindow(p->window->window); - glfwTerminate(); } @@ -970,5 +685,5 @@ void engine_input_ctx_reset(void) { } f64 get_time(void) { return glfwGetTime(); } -v2_i32 get_windowsize(void) { return GLOBAL_PLATFORM->window->windowsize; } +//ivec2 get_windowsize(void) { return GLOBAL_PLATFORM->window->windowsize; } v2_i32* get_mousepos(void) { return &GLOBAL_PLATFORM->mouse_pos; } diff --git a/src/main.c b/src/main.c deleted file mode 100644 index adef7b9..0000000 --- a/src/main.c +++ /dev/null @@ -1 +0,0 @@ -/* Intentionally left blank for now */ diff --git a/src/rendering/include/engine/rendering/rendering.h b/src/rendering/include/engine/rendering/rendering.h index 6ae6535..4bfbf16 100644 --- a/src/rendering/include/engine/rendering/rendering.h +++ b/src/rendering/include/engine/rendering/rendering.h @@ -30,43 +30,42 @@ typedef struct { #include <engine/ui.h> /* Rendering functions */ -void render_begin(Window* w); -void render_present(Window* w); +void render_begin(Window w); +void render_present(Window w); void drawcall_reset(void); -void render(Window* w); +void render(Window w); /* Misc */ -void window_size_callback(GLFWwindow* window, i32 width, i32 height); -void engine_window_resize_pointers(i32* w, i32* h); -void engine_window_resize_pointers_reset(void); +//void window_size_callback(GLFWwindow* window, i32 width, i32 height); +//void engine_window_resize_pointers(i32* w, i32* h); +//void engine_window_resize_pointers_reset(void); /* UI rendering */ /* See rendering_ui.c for implementation */ i64 engine_render_text(i32 font_id, Engine_color fg, char* text, v2_i32* size_out, bool wrapped); -void engine_draw_uitree(UITree* t); void engine_draw_sprite(Sprite* s, v2_i32* pos, f32 scale); void engine_draw_sprite_ex(Sprite* s, v2_i32* pos, f32 scale, Engine_color colormod); Sprite sprite_new(u64 tid, u8 coord); -#ifdef ENGINE_INTERNALS -#include <engine/engine.h> - -//#include <glad/gl.h> -//#define GLFW_INCLUDE_NONE -//#include <GLFW/glfw3.h> - -#define TEXTURES_INCREMENT 512 - +//#ifdef ENGINE_INTERNALS +//#include <engine/engine.h> +// +////#include <glad/gl.h> +////#define GLFW_INCLUDE_NONE +////#include <GLFW/glfw3.h> +// +//#define TEXTURES_INCREMENT 512 +// typedef enum { RenderDrawCallType_UITree, /*RenderDrawCallType_UIButton,*/ RenderDrawCallType_Text, RenderDrawCallType_Sprite, } RenderDrawCallType; - +// typedef struct { RenderDrawCallType type; union { @@ -79,23 +78,16 @@ typedef struct { } sprite; } data; } RenderDrawCall; - -struct RenderObject { - u32 vao; - u32 vbo; - u32 col; - u32 shaderprogram; - f32 g_vertex_buffer_data[9]; -}; - -void render_uitree(Window* w, UITree* t); - -void render_container(Window* w, UITree_container* t); -void render_button(Window* w, UITree_button* t); -void render_title(Window* w, UITree_title* t); -void render_text(Window* w, UITree_text* t); -v2_i32 elem_size(UITree* root); - -#endif +// +//struct RenderObject { +// u32 vao; +// u32 vbo; +// u32 col; +// u32 shaderprogram; +// f32 g_vertex_buffer_data[9]; +//}; +// +// +//#endif #endif diff --git a/src/rendering/include/engine/rendering/window.h b/src/rendering/include/engine/rendering/window.h new file mode 100644 index 0000000..16a0336 --- /dev/null +++ b/src/rendering/include/engine/rendering/window.h @@ -0,0 +1,43 @@ +#ifndef WINDOW_H +#define WINDOW_H + +#include <engine/core/types.h> +#include <cglm/cglm.h> + +enum Window_framework { + WINDOW_FRAMEWORK_NONE = 0, + WINDOW_FRAMEWORK_GLFW, +}; + +typedef enum Window_framework Window_framework; + +enum Window_renderer { + WINDOW_RENDERER_NONE = 0, + WINDOW_RENDERER_OPENGL, +}; + +typedef enum Window_renderer Window_renderer; + +struct Window { + // Specifies the framwork & renderer combo used. + Window_framework framework; + Window_renderer renderer; + // Window *buffer* size, in pixels. + ivec2 windowsize; + + // These are used differently depending on the framework / renderer combo. + // Subject to change to a union of backend-dependent structs + void* window; + void* context; +}; + +typedef struct Window* Window; + +// Window function +Window init_window_glfw(const char* windowtitle, ivec2 windowsize, const u32 flags); +void destroy_window(Window w); + +// Renderer intializer(s) +void init_render_opengl(Window w); + +#endif diff --git a/src/rendering/src/gl.c b/src/rendering/src/gl.c index e69de29..2958d49 100644 --- a/src/rendering/src/gl.c +++ b/src/rendering/src/gl.c @@ -0,0 +1,119 @@ +#include <stdlib.h> +#include <stdio.h> + +#include <glad/gl.h> + +#include <engine/core/types.h> +#include <engine/core/logging.h> + +// http://www.opengl-tutorial.org/beginners-tutorials/tutorial-2-the-first-triangle/ +GLuint LoadShaders( + const GladGLContext* gl, + const char * vertex_file_path, + const char * fragment_file_path) { + + // Create the shaders + GLuint VertexShaderID = gl->CreateShader(GL_VERTEX_SHADER); + GLuint FragmentShaderID = gl->CreateShader(GL_FRAGMENT_SHADER); + + // Read the Vertex Shader code from the file + char* VertexShaderCode; + FILE* VertexShaderStream = fopen(vertex_file_path, "r"); + if(VertexShaderStream != NULL){ + fseek(VertexShaderStream, 0, SEEK_END); + const i64 size = ftell(VertexShaderStream); + rewind(VertexShaderStream); + VertexShaderCode = calloc(size + 1, sizeof(char)); + + // Assume the whole file is successfully read + fread(VertexShaderCode, sizeof(char), size, VertexShaderStream); + + fclose(VertexShaderStream); + } else { + ERROR("Impossible to open %s. Are you in the right directory?\n", vertex_file_path); + getchar(); + return 0; + } + + // Read the Fragment Shader code from the file + char* FragmentShaderCode; + FILE* FragmentShaderStream = fopen(fragment_file_path, "r"); + if(FragmentShaderStream != NULL){ + fseek(FragmentShaderStream, 0, SEEK_END); + const i64 size = ftell(FragmentShaderStream); + rewind(FragmentShaderStream); + FragmentShaderCode = calloc(size + 1, sizeof(char)); + + // Assume the whole file is successfully read + fread(FragmentShaderCode, sizeof(char), size, FragmentShaderStream); + LOG("fragment source is %d bytes", size); + + fclose(FragmentShaderStream); + } else { + ERROR("Impossible to open %s. Are you in the right directory?\n", fragment_file_path); + getchar(); + return 0; + } + + GLint Result = GL_FALSE; + int InfoLogLength; + + // Compile Vertex Shader + INFO("Compiling shader: %s\n", vertex_file_path); + char const * VertexSourcePointer = VertexShaderCode; + gl->ShaderSource(VertexShaderID, 1, &VertexSourcePointer , NULL); + gl->CompileShader(VertexShaderID); + + // Check Vertex Shader + gl->GetShaderiv(VertexShaderID, GL_COMPILE_STATUS, &Result); + gl->GetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength); + if ( InfoLogLength > 0 ) { + char* msg = calloc(InfoLogLength + 1, sizeof(char)); + gl->GetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, msg); + ERROR("Compiling shader: %s\n", msg); + free(msg); + } + + // Compile Fragment Shader + INFO("Compiling shader: %s\n", fragment_file_path); + char const * FragmentSourcePointer = FragmentShaderCode; + gl->ShaderSource(FragmentShaderID, 1, &FragmentSourcePointer , NULL); + gl->CompileShader(FragmentShaderID); + + // Check Fragment Shader + gl->GetShaderiv(FragmentShaderID, GL_COMPILE_STATUS, &Result); + gl->GetShaderiv(FragmentShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength); + if ( InfoLogLength > 0 ){ + char* msg = calloc(InfoLogLength + 1, sizeof(char)); + gl->GetShaderInfoLog(FragmentShaderID, InfoLogLength, NULL, msg); + ERROR("Compiling shader: %s\n", msg); + free(msg); + } + + // Link the program + INFO("Linking program\n"); + GLuint ProgramID = gl->CreateProgram(); + gl->AttachShader(ProgramID, VertexShaderID); + gl->AttachShader(ProgramID, FragmentShaderID); + gl->LinkProgram(ProgramID); + + // Check the program + gl->GetProgramiv(ProgramID, GL_LINK_STATUS, &Result); + gl->GetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength); + if ( InfoLogLength > 0 ){ + char* msg = calloc(InfoLogLength + 1, sizeof(char)); + gl->GetShaderInfoLog(ProgramID, InfoLogLength, NULL, msg); + ERROR("Compiling shader: %s\n", msg); + free(msg); + } + + gl->DetachShader(ProgramID, VertexShaderID); + gl->DetachShader(ProgramID, FragmentShaderID); + + gl->DeleteShader(VertexShaderID); + gl->DeleteShader(FragmentShaderID); + + free(VertexShaderCode); + free(FragmentShaderCode); + return ProgramID; +} diff --git a/src/rendering/src/rendering.c b/src/rendering/src/rendering.c index 808897c..740e3a9 100644 --- a/src/rendering/src/rendering.c +++ b/src/rendering/src/rendering.c @@ -1,9 +1,7 @@ #include <stdio.h> #include <string.h> -//#define GLAD_GL_IMPLEMENTATION #include <glad/gl.h> -//#define GLFW_INCLUDE_NONE #include <GLFW/glfw3.h> @@ -25,13 +23,12 @@ i32 drawcall_len = 0; /* Clear the screen, * To be used inbetween draw calls */ -void render_begin(Window* w) { - //glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); +void render_begin(Window w) { glfwMakeContextCurrent(w->window); - w->context->Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + ((GladGLContext*)(w->context))->Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); } -void render_present(Window* w) { +void render_present(Window w) { // for (i32 i = 0; i < drawcall_len; i++) { // RenderDrawCall dc = drawcalls[i]; // switch (dc.type) { @@ -69,22 +66,14 @@ void render_present(Window* w) { void drawcall_reset(void) { drawcall_len = 0; } -void window_size_callback(GLFWwindow* window, int width, int height) { - GLOBAL_PLATFORM->window->context->Viewport(0,0, width, height); - //*GLOBAL_PLATFORM->window->game_w = width; - //*GLOBAL_PLATFORM->window->game_h = height; - GLOBAL_PLATFORM->window->windowsize.x = width; - GLOBAL_PLATFORM->window->windowsize.y = height; -} - void engine_window_resize_pointers(i32* w, i32* h) { - GLOBAL_PLATFORM->window->game_w = w; - GLOBAL_PLATFORM->window->game_h = h; + //GLOBAL_PLATFORM->window->game_w = w; + //GLOBAL_PLATFORM->window->game_h = h; } void engine_window_resize_pointers_reset(void) { - GLOBAL_PLATFORM->window->game_w = NULL; - GLOBAL_PLATFORM->window->game_h = NULL; + //GLOBAL_PLATFORM->window->game_w = NULL; + //GLOBAL_PLATFORM->window->game_h = NULL; } void engine_draw_uitree(UITree* t) { @@ -127,8 +116,9 @@ void engine_draw_sprite_ex(Sprite* s, v2_i32* pos, f32 scale, } Sprite sprite_new(u64 tid, u8 coord) { - const i32 ts = - ((struct Resources*)GLOBAL_PLATFORM->data)->textures[tid]->tilesize; + const i32 ts = 16; + // FIXME; used to be + //((struct Resources*)GLOBAL_PLATFORM->data)->textures[tid]->tilesize; return (Sprite){.texture_id = tid, (v2_i32){ .x = ts * (coord & 0x0F), diff --git a/src/rendering/src/window.c b/src/rendering/src/window.c index e69de29..ce5544a 100644 --- a/src/rendering/src/window.c +++ b/src/rendering/src/window.c @@ -0,0 +1,185 @@ +#include <stdio.h> + +/* TODO: REMOVE THIS INCLUSION */ +#include <engine/engine.h> + +#include <engine/core/types.h> +#include <engine/core/logging.h> + +#include <engine/rendering/window.h> + +#define GLAD_GL_IMPLEMENTATION +#include <glad/gl.h> +#undef GLAD_GL_IMPLEMENTATION + +#undef GLFW_INCLUDE_NONE +#include <GLFW/glfw3.h> + +#include <cglm/cglm.h> + +extern Platform* GLOBAL_PLATFORM; + +/* 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_size_callback(GLFWwindow* window, int width, int height) { + GladGLContext* gl = GLOBAL_PLATFORM->window->context; + gl->Viewport(0,0, width, height); + GLOBAL_PLATFORM->window->windowsize[0] = width; + GLOBAL_PLATFORM->window->windowsize[1] = height; +} + +GladGLContext* create_context(GLFWwindow *window) { + glfwMakeContextCurrent(window); + + GladGLContext* context = (GladGLContext*) calloc(1, 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; +} + +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); + glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); + + 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 + * of a window for it to become visible." */ + window = glfwCreateWindow(windowsize[0], windowsize[1], 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"); + + // Setup callbacks + // TODO: input handler callback + glfwSetFramebufferSizeCallback(window, window_size_callback); + + // Minor tweaks + glfwSwapInterval(0); + + // Create the window datastructure + ret = (Window)calloc(1, sizeof(Window)); + ret->framework = WINDOW_FRAMEWORK_GLFW; + ret->renderer = WINDOW_RENDERER_NONE; + ret->windowsize[0] = windowsize[0]; + ret->windowsize[1] = windowsize[1]; + ret->window = window; + // Last parameter is used for the renderer + ret->context = NULL; + + return ret; +} + +// Initializes opengl using the window +void init_render_opengl(Window w) { + 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); + + ctx->Viewport(0, 0, w->windowsize[0], w->windowsize[1]); + + if (ctx == NULL) { + ERROR("Failed to create glad context"); + exit(EXIT_FAILURE); + } + +#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); + + // 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 + w->context = NULL; + break; + default: + ERROR("Destroying unknown renderer type."); + } +} diff --git a/src/resources/include/engine/resources.h b/src/resources/include/engine/resources.h index 9de24ce..32e68ac 100644 --- a/src/resources/include/engine/resources.h +++ b/src/resources/include/engine/resources.h @@ -3,6 +3,7 @@ #include <engine/core/types.h> +// TODO /* We need some "global resources", available to all states. * These are resources such as common fonts, GUI frames, button background * images. @@ -49,6 +50,29 @@ typedef union { Asset_AudioSpec audio; } asset_t; +// The resource spec +struct Resources { + // Was: +// usize textures_len; +// usize textures_size; +// usize fonts_len; +// +// usize texturepaths_len; +// usize fontpaths_len; +// +// /* Paths for our sources, kept in case the user wants to reload them */ +// Asset_TextureSpec** texture_paths; +// Asset_FontSpec** font_paths; +// +// /* Our actual sources */ +// Texture** textures; +// //TTF_Font** fonts; + + // But with the new way: + // usize assets_len; + // asset_t assets*; +}; + #define Resource_FontDefinition(_path, _fontsize) \ (const Asset_FontSpec) { \ .type = Asset_font, .font_path = _path, .ptsize = _fontsize \ @@ -76,4 +100,6 @@ isize resource_load_audio(Asset_AudioSpec audio_def); * to `engine_run` */ isize resource_make_global(isize resource_id); +#include <engine/resources/texture.h> + #endif diff --git a/src/resources/include/engine/resources/texture.h b/src/resources/include/engine/resources/texture.h new file mode 100644 index 0000000..8266957 --- /dev/null +++ b/src/resources/include/engine/resources/texture.h @@ -0,0 +1,14 @@ +#ifndef TEXTURE_H +#define TEXTURE_H + +#include <engine/core/types.h> + +typedef struct { + const i32 tilesize; + const i32 width; + const i32 height; +} Texture; + +Texture* load_texture(void* render, const Asset_TextureSpec* ts); + +#endif diff --git a/src/resources/src/textures.c b/src/resources/src/textures.c index e69de29..622c4b5 100644 --- a/src/resources/src/textures.c +++ b/src/resources/src/textures.c @@ -0,0 +1,20 @@ +#include <engine/core/logging.h> +#include <engine/resources.h> + +Texture* load_texture(void* render, const Asset_TextureSpec* ts) { + Texture* t = NULL; + + if (ts == NULL) { + ERROR("Invalid Asset_TextureSpec\n"); + return NULL; + } + + //t = (Texture*)malloc(sizeof(Texture)); + //t->texture = new_texture; + ///* Assigning const value */ + //*(i32*)&t->tilesize = tw; + //*(i32*)&t->width = ts->width; + //*(i32*)&t->height = ts->height; + + return t; +} diff --git a/src/ui/CMakeLists.txt b/src/ui/CMakeLists.txt index fa91929..0feefa3 100644 --- a/src/ui/CMakeLists.txt +++ b/src/ui/CMakeLists.txt @@ -4,3 +4,7 @@ add_library(daw_ui ) target_include_directories(daw_ui PRIVATE ${DAW_INCLUDE_DIRS}) +target_link_libraries(daw_ui PRIVATE + OpenGL::GL + cglm + ) diff --git a/src/ui/include/engine/ui.h b/src/ui/include/engine/ui.h index eea81af..b860124 100644 --- a/src/ui/include/engine/ui.h +++ b/src/ui/include/engine/ui.h @@ -5,6 +5,8 @@ #include <engine/utils/list.h> #include <engine/utils/vector.h> +#include <engine/rendering/rendering.h> + #define DIRECTION_HORIZONTAL true #define DIRECTION_VERTICAL false @@ -261,4 +263,7 @@ Engine_color ui_get_default_bg(void); /* Returns -1 if not found */ u64 ui_check_click(UITree* root); +void engine_draw_uitree(UITree* t); +void render_uitree(Window w, UITree* t); + #endif diff --git a/src/ui/src/positioning.c b/src/ui/src/positioning.c index 6bb32fb..6c651d4 100644 --- a/src/ui/src/positioning.c +++ b/src/ui/src/positioning.c @@ -4,6 +4,7 @@ #include <engine/engine.h> #include <engine/utils/btree.h> #include <engine/utils.h> +#include <engine/ui.h> static Engine_color DEFAULT_FG = {0xFF, 0xFF, 0xFF, 0xFF}; static Engine_color DEFAULT_BG = {0x00, 0x00, 0x00, 0xFF}; @@ -57,11 +58,11 @@ v2_i32 get_pos(UITree* t); v2_i32 get_size(UITree* t); f32 ui_size_pixel_to_percent_width(i32 pixels) { - return (f32)pixels / (f32)GLOBAL_PLATFORM->window->windowsize.x; + return (f32)pixels / (f32)GLOBAL_PLATFORM->window->windowsize[0]; } f32 ui_size_pixel_to_percent_height(i32 pixels) { - return (f32)pixels / (f32)GLOBAL_PLATFORM->window->windowsize.y; + return (f32)pixels / (f32)GLOBAL_PLATFORM->window->windowsize[1]; } f32 ui_size_pixel_to_percent(i32 pixels) { @@ -69,11 +70,11 @@ f32 ui_size_pixel_to_percent(i32 pixels) { } i32 ui_size_percent_width_to_pixel(f32 percent) { - return (i32)(percent * GLOBAL_PLATFORM->window->windowsize.x); + return (i32)(percent * GLOBAL_PLATFORM->window->windowsize[0]); } i32 ui_size_percent_height_to_pixel(f32 percent) { - return (i32)(percent * GLOBAL_PLATFORM->window->windowsize.y); + return (i32)(percent * GLOBAL_PLATFORM->window->windowsize[1]); } i32 ui_size_percent_to_pixel(f32 percent) { @@ -818,8 +819,12 @@ void ui_resolve_constraints(void) { it = btree_iter_t_new(GLOBAL_UIROOTS); while ((i = btree_iter(GLOBAL_UIROOTS, it)) != NULL) { + v2_i32 sz = (v2_i32){ + .x = GLOBAL_PLATFORM->window->windowsize[0], + .y = GLOBAL_PLATFORM->window->windowsize[1] + }; ui_rearrange((UITree*)*i, (v2_i32){0, 0}, - GLOBAL_PLATFORM->window->windowsize); + sz); } free(it); } diff --git a/src/ui/src/rendering.c b/src/ui/src/rendering.c index 093f373..b95fb8b 100644 --- a/src/ui/src/rendering.c +++ b/src/ui/src/rendering.c @@ -2,16 +2,22 @@ #include <stdio.h> #include <string.h> -#define ENGINE_INTERNALS +#include <cglm/cglm.h> -#include <engine/engine.h> +//#include <engine/engine.h> #include <engine/rendering/rendering.h> -extern Platform* GLOBAL_PLATFORM; +//extern Platform* GLOBAL_PLATFORM; extern const char* uitype_str[]; -void render_uitree(Window* w, UITree* t) { +void render_container(Window w, UITree_container* t); +void render_button(Window w, UITree_button* t); +void render_title(Window w, UITree_title* t); +void render_text(Window w, UITree_text* t); +v2_i32 elem_size(UITree root); + +void render_uitree(Window w, UITree* t) { switch (t->type) { case uitype_container: render_container(w, &t->container); @@ -35,7 +41,7 @@ void render_uitree(Window* w, UITree* t) { } } -void render_container(Window* w, UITree_container* t) { +void render_container(Window w, UITree_container* t) { if (t->children != NULL && t->children_len > 0) { for (usize i = 0; i < t->children_len; i++) { @@ -44,13 +50,13 @@ void render_container(Window* w, UITree_container* t) { } } -void render_button(Window* w, UITree_button* t) { +void render_button(Window w, UITree_button* t) { } -void render_title(Window* w, UITree_title* t) { +void render_title(Window w, UITree_title* t) { } -void render_text(Window* w, UITree_text* t) { +void render_text(Window w, UITree_text* t) { } i64 add_texture(struct Resources* resptr, Texture* t) { |
