aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md3
-rw-r--r--include/mgl/graphics/vertex_buffer.h2
-rw-r--r--include/mgl/system/clock.h13
-rw-r--r--src/graphics/vertex_buffer.c10
-rw-r--r--src/system/clock.c27
-rw-r--r--tests/main.c8
6 files changed, 55 insertions, 8 deletions
diff --git a/README.md b/README.md
index 05b12e7..ac7c82d 100644
--- a/README.md
+++ b/README.md
@@ -11,4 +11,5 @@ Handle window close (window destroyed event, disconnected from server and socket
Bind texture and cache the bound texture to reduce calls to opengl.
Use gl triangle instead of quad.
Fix crash on exit.
-Support using multiple textures in shaders by using glActiveTexture for each one and set the uniform sampler2D value for each as as the index. \ No newline at end of file
+Support using multiple textures in shaders by using glActiveTexture for each one and set the uniform sampler2D value for each as as the index.
+Make sure clock is monotonic (there has been some issues with CLOCK\_MONOTONIC not being monotonic on linux).
diff --git a/include/mgl/graphics/vertex_buffer.h b/include/mgl/graphics/vertex_buffer.h
index 5cdadfa..0dfd0d6 100644
--- a/include/mgl/graphics/vertex_buffer.h
+++ b/include/mgl/graphics/vertex_buffer.h
@@ -27,6 +27,6 @@ void mgl_vertex_buffer_deinit(mgl_vertex_buffer *self);
void mgl_vertex_buffer_set_position(mgl_vertex_buffer *self, mgl_vec2f position);
int mgl_vertex_buffer_update(mgl_vertex_buffer *self, const mgl_vertex *vertices, size_t vertex_count);
/* |texture| can be NULL to not use any texture */
-void mgl_vertex_buffer_draw(mgl_context *context, mgl_vertex_buffer *self, const mgl_texture *texture);
+void mgl_vertex_buffer_draw(mgl_context *context, mgl_vertex_buffer *vertex_buffer, const mgl_texture *texture);
#endif /* MGL_VERTEX_BUFFER_H */
diff --git a/include/mgl/system/clock.h b/include/mgl/system/clock.h
new file mode 100644
index 0000000..f207ff7
--- /dev/null
+++ b/include/mgl/system/clock.h
@@ -0,0 +1,13 @@
+#ifndef MGL_CLOCK_H
+#define MGL_CLOCK_H
+
+typedef struct {
+ double captured_seconds;
+} mgl_clock;
+
+void mgl_clock_init(mgl_clock *self);
+/* Returns the elapsed time in seconds since the last restart or init, before resetting the clock */
+double mgl_clock_restart(mgl_clock *self);
+double mgl_clock_get_elapsed_time_seconds(mgl_clock *self);
+
+#endif /* MGL_CLOCK_H */
diff --git a/src/graphics/vertex_buffer.c b/src/graphics/vertex_buffer.c
index ccdcd84..5a8e768 100644
--- a/src/graphics/vertex_buffer.c
+++ b/src/graphics/vertex_buffer.c
@@ -78,17 +78,17 @@ int mgl_vertex_buffer_update(mgl_vertex_buffer *self, const mgl_vertex *vertices
}
/* TODO: Optimize bind texture */
-void mgl_vertex_buffer_draw(mgl_context *context, mgl_vertex_buffer *self, const mgl_texture *texture) {
- if(self->vertex_count == 0 || self->id == 0)
+void mgl_vertex_buffer_draw(mgl_context *context, mgl_vertex_buffer *vertex_buffer, const mgl_texture *texture) {
+ if(vertex_buffer->vertex_count == 0 || vertex_buffer->id == 0)
return;
/* TODO: Optimize this */
context->gl.glPushMatrix();
- context->gl.glTranslatef(self->position.x, self->position.y, 0.0f);
+ context->gl.glTranslatef(vertex_buffer->position.x, vertex_buffer->position.y, 0.0f);
context->gl.glBindTexture(GL_TEXTURE_2D, texture ? texture->id : 0);
- context->gl.glBindBuffer(GL_ARRAY_BUFFER, self->id);
- context->gl.glDrawArrays(mgl_primitive_type_to_gl_mode(self->primitive_type), 0, self->vertex_count);
+ context->gl.glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer->id);
+ context->gl.glDrawArrays(mgl_primitive_type_to_gl_mode(vertex_buffer->primitive_type), 0, vertex_buffer->vertex_count);
context->gl.glBindBuffer(GL_ARRAY_BUFFER, 0);
context->gl.glBindTexture(GL_TEXTURE_2D, 0);
diff --git a/src/system/clock.c b/src/system/clock.c
new file mode 100644
index 0000000..3ec2276
--- /dev/null
+++ b/src/system/clock.c
@@ -0,0 +1,27 @@
+#include "../../include/mgl/system/clock.h"
+#include <time.h>
+
+/* TODO: Implement for macOS */
+
+static double clock_get_monotonic_seconds() {
+ struct timespec ts;
+ ts.tv_sec = 0;
+ ts.tv_nsec = 0;
+ clock_gettime(CLOCK_MONOTONIC, &ts);
+ return (double)ts.tv_sec + (double)ts.tv_nsec * 0.000000001;
+}
+
+void mgl_clock_init(mgl_clock *self) {
+ self->captured_seconds = clock_get_monotonic_seconds();
+}
+
+double mgl_clock_restart(mgl_clock *self) {
+ const double new_time_seconds = clock_get_monotonic_seconds();
+ const double elapsed_time_seconds = self->captured_seconds - new_time_seconds;
+ self->captured_seconds = new_time_seconds;
+ return elapsed_time_seconds;
+}
+
+double mgl_clock_get_elapsed_time_seconds(mgl_clock *self) {
+ return clock_get_monotonic_seconds() - self->captured_seconds;
+}
diff --git a/tests/main.c b/tests/main.c
index 0001288..bffbf96 100644
--- a/tests/main.c
+++ b/tests/main.c
@@ -9,12 +9,14 @@
#include <mgl/graphics/text.h>
#include <mgl/graphics/vertex_buffer.h>
#include <mgl/graphics/shader.h>
+#include <mgl/system/clock.h>
typedef struct {
mgl_texture *texture;
mgl_font *font;
mgl_vertex_buffer *vertex_buffer;
mgl_shader_program *shader_program;
+ mgl_clock clock;
} Userdata;
static void draw(mgl_window *window, void *userdata) {
@@ -37,8 +39,11 @@ static void draw(mgl_window *window, void *userdata) {
mgl_sprite_draw(context, &sprite);
mgl_shader_program_use(NULL);
+ char str[255];
+ snprintf(str, sizeof(str), "Hello world!\nelapsed time: %f", mgl_clock_get_elapsed_time_seconds(&u->clock));
+
mgl_text text;
- mgl_text_init(&text, u->font, "hello world!\nGood bye world!", 0.0f, 0.0f);
+ mgl_text_init(&text, u->font, str, 0.0f, 0.0f);
mgl_text_draw(context, &text);
mgl_text_deinit(&text);
@@ -60,6 +65,7 @@ int main(int argc, char **argv) {
userdata.font = &font;
userdata.vertex_buffer = &vertex_buffer;
userdata.shader_program = &shader_program;
+ mgl_clock_init(&userdata.clock);
mgl_window window;
if(mgl_window_create(&window, "mgl", 1280, 720) != 0)