diff options
| -rw-r--r-- | src/core/include/engine/core/platform.h | 2 | ||||
| -rw-r--r-- | src/core/src/loop.c | 4 | ||||
| -rw-r--r-- | src/rendering/include/engine/rendering/rendering.h | 10 | ||||
| -rw-r--r-- | src/rendering/include/engine/rendering/window.h | 24 | ||||
| -rw-r--r-- | src/rendering/src/rendering.c | 143 | ||||
| -rw-r--r-- | src/rendering/src/window.c | 13 |
6 files changed, 151 insertions, 45 deletions
diff --git a/src/core/include/engine/core/platform.h b/src/core/include/engine/core/platform.h index 2e74fbe..9dc9a8b 100644 --- a/src/core/include/engine/core/platform.h +++ b/src/core/include/engine/core/platform.h @@ -17,7 +17,7 @@ */ typedef struct Platform { - Window window; + Window* window; bool quit; u64 frame; diff --git a/src/core/src/loop.c b/src/core/src/loop.c index f29ef25..642f771 100644 --- a/src/core/src/loop.c +++ b/src/core/src/loop.c @@ -85,7 +85,7 @@ Platform* engine_init(const char* windowtitle, i32 windowWidth, i32 windowHeight v2_i32 windowsize = (v2_i32){.x = windowWidth, .y = windowHeight}; Platform* p = (Platform*)malloc(sizeof(Platform)); - Window w = (Window)malloc(sizeof(Window)); + Window* w = (Window*)malloc(sizeof(Window)); /* initialize resources */ Resources* resources = malloc(sizeof(Resources)); @@ -395,7 +395,7 @@ i32 engine_run(Platform* p, StateType initial_state, void* state_arg) { 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); } glfwPollEvents(); diff --git a/src/rendering/include/engine/rendering/rendering.h b/src/rendering/include/engine/rendering/rendering.h index 50a61a7..bde298c 100644 --- a/src/rendering/include/engine/rendering/rendering.h +++ b/src/rendering/include/engine/rendering/rendering.h @@ -49,10 +49,10 @@ typedef struct { } RenderObject; /* 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); @@ -62,7 +62,7 @@ void render(Window w); 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); -void engine_draw_model(RenderObject* o, vec2 pos); +void engine_draw_model(RenderObject* o, vec3 pos); Sprite sprite_new(u64 tid, u8 coord); @@ -84,7 +84,7 @@ typedef struct { } sprite; struct { RenderObject* model; - vec2 pos; + vec3 pos; f32 scale; } model; } data; diff --git a/src/rendering/include/engine/rendering/window.h b/src/rendering/include/engine/rendering/window.h index f6da016..bd4eb9c 100644 --- a/src/rendering/include/engine/rendering/window.h +++ b/src/rendering/include/engine/rendering/window.h @@ -9,21 +9,17 @@ extern void* window_poll_events; extern f64 (*get_time)(void); #endif -enum Window_framework { +typedef enum { WINDOW_FRAMEWORK_NONE = 0, WINDOW_FRAMEWORK_GLFW, -}; +} Window_framework; -typedef enum Window_framework Window_framework; - -enum Window_renderer { +typedef enum { WINDOW_RENDERER_NONE = 0, WINDOW_RENDERER_OPENGL, -}; - -typedef enum Window_renderer Window_renderer; +} Window_renderer; -struct Window { +typedef struct { // Specifies the framwork & renderer combo used. Window_framework framework; Window_renderer renderer; @@ -34,16 +30,14 @@ struct Window { // Subject to change to a union of backend-dependent structs void* window; void* context; -}; - -typedef struct Window* Window; +} Window; // Window function -Window init_window_glfw(const char* windowtitle, ivec2 windowsize, const u32 flags); -void destroy_window(Window w); +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); +void init_render_opengl(Window* w); #undef API #endif diff --git a/src/rendering/src/rendering.c b/src/rendering/src/rendering.c index 7fcea58..89a6be3 100644 --- a/src/rendering/src/rendering.c +++ b/src/rendering/src/rendering.c @@ -23,21 +23,26 @@ i32 drawcall_len = 0; /* Clear the screen, * To be used inbetween draw calls */ -void render_begin(Window w) { +void render_begin(Window* w) { glfwMakeContextCurrent(w->window); ((GladGLContext*)(w->context))->Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); } -void render_present(Window w) { -// for (i32 i = 0; i < drawcall_len; i++) { -// RenderDrawCall dc = drawcalls[i]; -// switch (dc.type) { +void render_present(Window* w) { + /* This is GL specific, TODO: move the GL-specific code elsewhere. Maybe make + * this whole present GL specific? assign it as a fn ptr in the Window struct? */ + GladGLContext *gl = w->context; + const f32 ratio = (float)w->windowsize[0] / (float)w->windowsize[1]; + + for (i32 i = 0; i < drawcall_len; i++) { + RenderDrawCall dc = drawcalls[i]; + switch (dc.type) { // case RenderDrawCallType_Sprite: { //#ifdef _DEBUG // if (dc.data.sprite.sprite == NULL) { -// __asm__("int3;"); // WARN("Sprite %lx in drawcall %d/%d had NULL reference", // dc.data.sprite.sprite, i, drawcall_len); +// __asm__("int3;"); // // drawcall_len = 0; // exit(EXIT_FAILURE); @@ -48,10 +53,113 @@ void render_present(Window w) { // ((struct Resources*)GLOBAL_PLATFORM->data)->textures[s.texture_id]; // i32 ts = t->tilesize; // } break; -// default: -// break; -// } -// } + + + case RenderDrawCallType_Model: { +#ifdef _DEBUG + if (dc.data.sprite.sprite == NULL) { + WARN("Sprite %lx in drawcall %d/%d had NULL reference", + dc.data.sprite.sprite, i, drawcall_len); + __asm__("int3;"); + + drawcall_len = 0; + exit(EXIT_FAILURE); + } +#endif + RenderObject* o = dc.data.model.model; + vec3 pos; + glm_vec3_copy(dc.data.model.pos, pos); + gl->UseProgram(o->shader.program); + + + + + + { + vec3 cam = {3., 2., 3.}; // perspective + mat4 per; // perspective + mat4 v; // view + mat4 model = GLM_MAT4_IDENTITY_INIT; + mat4 modelviewprojection; + const float sz = 10.f; + + //LOG("<%.1f, %.1f, %.1f>", pos[0], pos[1], pos[2]); + model[3][0] = pos[0]; + model[3][1] = pos[1]; + model[3][2] = pos[2]; + + //glm_perspective(45.f , 600.f / 400.f, 0.1, 100.0f, per); + glm_ortho(-sz * ratio, sz * ratio, -sz, sz, -sz, sz, per); + + /* Lookat zero should be changed to whatever later */ + glm_lookat(cam, GLM_VEC3_ZERO, GLM_YUP, v); + + { mat4 t; + //modelviewprojection = p * v * model + glm_mat4_mul(v, model, t); + // TODO: Remove this later + glm_rotate_at(t, (vec3){0,0,0}, get_time() / 2.f, GLM_YUP); //, (vec3)({0,1,0})); + + glm_mat4_mul(per, t, modelviewprojection); + } + + // TODO: Do this only once during initialization + u32 matrix = gl->GetUniformLocation(o->shader.program, "MVP"); + + gl->UniformMatrix4fv(matrix, 1, GL_FALSE, &modelviewprojection[0][0]); + } + + + gl->EnableVertexAttribArray(0); + gl->BindBuffer(GL_ARRAY_BUFFER, o->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 + // ); + + + + + //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); + + + + + + + + + + + + + + } break; + default: + break; + } + } drawcall_len = 0; @@ -103,7 +211,7 @@ void engine_draw_sprite_ex(Sprite* s, v2_i32* pos, f32 scale, }}; } -void engine_draw_model(RenderObject* o, vec2 pos) { +void engine_draw_model(RenderObject* o, vec3 pos) { if (drawcall_len + 1 >= drawcall_limit) return; #ifdef _DEBUG if (o == NULL) __asm__("int3;"); @@ -116,7 +224,7 @@ void engine_draw_model(RenderObject* o, vec2 pos) { }}; - glm_vec2_copy(pos, dc.data.model.pos); + glm_vec3_copy(pos, dc.data.model.pos); drawcalls[drawcall_len++] = dc; } @@ -125,9 +233,10 @@ Sprite sprite_new(u64 tid, u8 coord) { 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), - .y = ts * ((coord & 0xF0) >> 4), - }}; + return (Sprite){ + .texture_id = tid, + (v2_i32){ + .x = ts * (coord & 0x0F), + .y = ts * ((coord & 0xF0) >> 4), + }}; } diff --git a/src/rendering/src/window.c b/src/rendering/src/window.c index de0aae6..431ce3f 100644 --- a/src/rendering/src/window.c +++ b/src/rendering/src/window.c @@ -49,11 +49,11 @@ GladGLContext* create_context(GLFWwindow *window) { return context; } -Window init_window_glfw( +Window* init_window_glfw( const char* windowtitle, ivec2 windowsize, const u32 flags ) { - Window ret = NULL; + Window* ret = NULL; GLFWwindow* window = NULL; glfwSetErrorCallback(&glfw_err_callback); @@ -102,8 +102,11 @@ Window init_window_glfw( // TODO: input handler callback glfwSetFramebufferSizeCallback(window, window_size_callback); + glfwMakeContextCurrent(window); + glfwSwapInterval(0); + // Create the window datastructure - ret = (Window)calloc(1, sizeof(struct Window)); + ret = (Window*)calloc(1, sizeof(Window)); ret->framework = WINDOW_FRAMEWORK_GLFW; ret->renderer = WINDOW_RENDERER_NONE; glm_ivec2_copy(windowsize, ret->windowsize); @@ -118,7 +121,7 @@ Window init_window_glfw( } // Initializes opengl using the window -void init_render_opengl(Window w) { +void init_render_opengl(Window* w) { if (w == NULL || w->window == NULL) { ERROR("Window is not initialized"); return; @@ -167,7 +170,7 @@ void destroy_window_glfw(GLFWwindow* w) { glfwTerminate(); } -void destroy_window(Window w) { +void destroy_window(Window* w) { switch(w->framework) { case WINDOW_FRAMEWORK_GLFW: destroy_window_glfw(w->window); |
