diff options
Diffstat (limited to 'src/core')
| -rw-r--r-- | src/core/include/engine/engine.h | 58 | ||||
| -rw-r--r-- | src/core/src/loop.c | 833 |
2 files changed, 286 insertions, 605 deletions
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; } |
