diff --git a/Makefile b/Makefile index 2929a13..1cc02c9 100644 --- a/Makefile +++ b/Makefile @@ -9,6 +9,8 @@ CFILES=$(shell cd src && find -L * -type f -name '*.c') OBJDIR=obj OBJ=$(addprefix $(OBJDIR)/, $(CFILES:.c=.o)) +INSTALL_PREFIX=/usr/local + $(LIBRARY): $(OBJ) Makefile $(AR) rcs $(LIBRARY) $(OBJ) @@ -20,6 +22,11 @@ run: $(LIBRARY) make -C examples test cd examples && ./test +install: $(LIBRARY) + cp $(LIBRARY) $(INSTALL_PREFIX)/lib + mkdir -p $(INSTALL_PREFIX)/include/gearlib + cp -r include/* $(INSTALL_PREFIX)/include + clean: rm -rf $(OBJ) $(LIBRARY) make -C examples clean diff --git a/examples/Makefile b/examples/Makefile index ad126f4..876b0b9 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -4,11 +4,11 @@ CFLAGS = -O3 -I../include LDFLAGS = -L ../ -lglfw -lgearlib -lm CFILES=$(shell find -L * -type f -name '*.c') -BINARIES=$(CFILES:.c=.exe) +BINARIES=$(CFILES:.c=) all: $(BINARIES) -%.exe: %.c +%: %.c $(CC) $< -o $@ $(CFLAGS) $(LDFLAGS) clean: diff --git a/examples/quad b/examples/quad new file mode 100755 index 0000000..c0f8255 Binary files /dev/null and b/examples/quad differ diff --git a/examples/quad.c b/examples/quad.c new file mode 100644 index 0000000..1ed26b7 --- /dev/null +++ b/examples/quad.c @@ -0,0 +1,39 @@ +#include + +int main() { + Window window = create_window(800, 600, "textures"); + glfwSwapInterval(0); // fps unlock + setup_quads(); + + Camera* camera = create_camera(vec2(0.0f, 0.0f)); + UniformBuffer* camera_buffer = create_uniform_buffer(sizeof(CameraMatrices)); + + double time = glfwGetTime(); + + while (!glfwWindowShouldClose(window)) { + process_input(window); + update_camera(camera, window); + set_uniform_data(camera_buffer, camera->m); + + glClearColor(vec4_spread(BLUE)); + glClear(GL_COLOR_BUFFER_BIT); + + draw_quad(vec2(10, 10), vec2(500, 500), RED); + + flush(); + + double end_time = glfwGetTime(); + double frame_time = end_time - time; + time = end_time; + + double fps = 1.0 / frame_time; + printf("frame_time: %lf fps: %lf\n", frame_time, fps); + + glfwSwapBuffers(window); + glfwPollEvents(); + } + + glfwDestroyWindow(window); + glfwTerminate(); + return 0; +} diff --git a/examples/test b/examples/test new file mode 100755 index 0000000..bb9f119 Binary files /dev/null and b/examples/test differ diff --git a/examples/textures b/examples/textures new file mode 100755 index 0000000..e2e6fff Binary files /dev/null and b/examples/textures differ diff --git a/include/gearlib.h b/include/gearlib.h index 83e5f6a..f6da1a6 100644 --- a/include/gearlib.h +++ b/include/gearlib.h @@ -3,18 +3,19 @@ #define MAX_VERTICES 2500 -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include #endif diff --git a/include/KHR/khrplatform.h b/include/gearlib/KHR/khrplatform.h similarity index 100% rename from include/KHR/khrplatform.h rename to include/gearlib/KHR/khrplatform.h diff --git a/include/batch.h b/include/gearlib/batch.h similarity index 97% rename from include/batch.h rename to include/gearlib/batch.h index 34ae055..01bb936 100644 --- a/include/batch.h +++ b/include/gearlib/batch.h @@ -6,7 +6,6 @@ #include #include -#include typedef struct BatchStats { uint32_t draw_calls; diff --git a/include/camera.h b/include/gearlib/camera.h similarity index 100% rename from include/camera.h rename to include/gearlib/camera.h diff --git a/include/colors.h b/include/gearlib/colors.h similarity index 100% rename from include/colors.h rename to include/gearlib/colors.h diff --git a/include/debugging.h b/include/gearlib/debugging.h similarity index 100% rename from include/debugging.h rename to include/gearlib/debugging.h diff --git a/include/events.h b/include/gearlib/events.h similarity index 100% rename from include/events.h rename to include/gearlib/events.h diff --git a/include/glad/gl.h b/include/gearlib/glad/gl.h similarity index 100% rename from include/glad/gl.h rename to include/gearlib/glad/gl.h diff --git a/include/init.h b/include/gearlib/init.h similarity index 86% rename from include/init.h rename to include/gearlib/init.h index a863c8e..2957fc2 100644 --- a/include/init.h +++ b/include/gearlib/init.h @@ -1,7 +1,7 @@ #ifndef __INIT_H__ #define __INIT_H__ -#include +#include typedef GLFWwindow* Window; diff --git a/include/opengl.h b/include/gearlib/opengl.h similarity index 72% rename from include/opengl.h rename to include/gearlib/opengl.h index 73ddc0e..aa31212 100644 --- a/include/opengl.h +++ b/include/gearlib/opengl.h @@ -1,7 +1,7 @@ #ifndef __OPENGL_H__ #define __OPENGL_H__ -#include +#include #include #endif diff --git a/include/quad.h b/include/gearlib/quad.h similarity index 100% rename from include/quad.h rename to include/gearlib/quad.h diff --git a/include/raymath.h b/include/gearlib/raymath.h similarity index 100% rename from include/raymath.h rename to include/gearlib/raymath.h diff --git a/include/renderer.h b/include/gearlib/renderer.h similarity index 100% rename from include/renderer.h rename to include/gearlib/renderer.h diff --git a/include/shaders.h b/include/gearlib/shaders.h similarity index 100% rename from include/shaders.h rename to include/gearlib/shaders.h diff --git a/include/slibs.h b/include/gearlib/slibs.h similarity index 95% rename from include/slibs.h rename to include/gearlib/slibs.h index c16b334..757555c 100644 --- a/include/slibs.h +++ b/include/gearlib/slibs.h @@ -11,12 +11,7 @@ #define sl_auto(name, x) typeof(x) name = x -#define sl_new(type, ...) \ - ({ \ - type *ptr = malloc(sizeof(type)); \ - *ptr = (type){__VA_ARGS__}; \ - ptr; \ - }) +#define sl_new(type) calloc(sizeof(type), 1) #define sl_init(type, ...) \ (type) { __VA_ARGS__ } diff --git a/include/stb_image.h b/include/gearlib/stb_image.h similarity index 100% rename from include/stb_image.h rename to include/gearlib/stb_image.h diff --git a/include/textures.h b/include/gearlib/textures.h similarity index 100% rename from include/textures.h rename to include/gearlib/textures.h diff --git a/include/uniform_buffer.h b/include/gearlib/uniform_buffer.h similarity index 100% rename from include/uniform_buffer.h rename to include/gearlib/uniform_buffer.h diff --git a/include/vertex.h b/include/gearlib/vertex.h similarity index 100% rename from include/vertex.h rename to include/gearlib/vertex.h diff --git a/src/batch.c b/src/batch.c index ab2556d..07a2c89 100644 --- a/src/batch.c +++ b/src/batch.c @@ -1,11 +1,10 @@ #include #include -#include BatchList batches = { 0 }; RenderBatch* create_batch(size_t vert_size, uint32_t max_verts) { - RenderBatch* batch = calloc(sizeof(RenderBatch), 1); + RenderBatch* batch = sl_new(RenderBatch); sl_vec_push(batches, batch); glCreateBuffers(1, &batch->vbo); @@ -36,13 +35,6 @@ void flush_batch(RenderBatch* batch) { glUseProgram(batch->shader); - /*glUniformMatrix4fv(0, 1, GL_FALSE, - mat4ToFloat(mat4Multiply(batch->camera->view, batch->camera->projection)));*/ - - - //glUniformMatrix4fv(1, 1, GL_FALSE, mat4ToFloat(batch->camera->view)); - //glUniformMatrix4fv(2, 1, GL_FALSE, mat4ToFloat(batch->camera->projection)); - if(batch->flush_callback != NULL) batch->flush_callback(batch); diff --git a/src/camera.c b/src/camera.c index 6cd1dfd..3dfddce 100644 --- a/src/camera.c +++ b/src/camera.c @@ -2,9 +2,9 @@ #include Camera* create_camera(vec2 pos) { - Camera* camera = calloc(sizeof(Camera), 1); + Camera* camera = sl_new(Camera); camera->position = vec3(pos, -3.0f); - camera->m = calloc(sizeof(CameraMatrices), 1); + camera->m = sl_new(CameraMatrices); return camera; } diff --git a/src/textures.c b/src/textures.c index 1192f21..128d053 100644 --- a/src/textures.c +++ b/src/textures.c @@ -104,7 +104,7 @@ RenderBatch* create_texture_quad_batch() { compile_shader("assets/texture.vert", GL_VERTEX_SHADER), compile_shader("assets/texture.frag", GL_FRAGMENT_SHADER), 0); - texture_quad_batch->data = calloc(sizeof(TextureQuadBatchData), 1); + texture_quad_batch->data = sl_new(TextureQuadBatchData); texture_quad_batch->flush_callback = &texture_flush_callback; batch_add_attrib(texture_quad_batch, (VertexAttrib){