restructure project

This commit is contained in:
sam 2024-05-05 14:25:05 +12:00
parent cd8e58bd3f
commit 23f3c49296
20 changed files with 145 additions and 144 deletions

View file

@ -1,25 +1,24 @@
LIBRARY=libgearlib.a
CC:=tcc
AR=$(CC) -ar
CC:=gcc
AR=ar
CFLAGS=-O3 -Iinclude -g
LDFLAGS=-lglfw -lm
CFILES=$(shell cd src && find -L * -type f -name '*.c')
OBJ=$(addprefix obj/, $(CFILES:.c=.o))
OBJDIR=obj
OBJ=$(addprefix $(OBJDIR)/, $(CFILES:.c=.o))
$(LIBRARY): objdir $(OBJ) Makefile
$(CC) -ar rcs $(LIBRARY) $(OBJ)
$(LIBRARY): $(OBJ) Makefile
$(AR) rcs $(LIBRARY) $(OBJ)
obj/%.o: src/%.c
mkdir -p $(OBJDIR)
$(CC) $(CFLAGS) -c $< -o $@
objdir:
mkdir -p obj
run: $(LIBRARY)
make -C test main
cd test && ./main
make -C examples test
cd examples && ./test
clean:
rm -rf $(OBJ) $(LIBRARY)

View file

@ -1,4 +1,4 @@
CC=tcc
CC=gcc
CFLAGS=-O3 -I../include
LDFLAGS=-lglfw -L ../ -lgearlib -lm

View file

Before

Width:  |  Height:  |  Size: 123 KiB

After

Width:  |  Height:  |  Size: 123 KiB

View file

Before

Width:  |  Height:  |  Size: 209 KiB

After

Width:  |  Height:  |  Size: 209 KiB

View file

Before

Width:  |  Height:  |  Size: 288 KiB

After

Width:  |  Height:  |  Size: 288 KiB

BIN
examples/test Executable file

Binary file not shown.

35
examples/test.c Normal file
View file

@ -0,0 +1,35 @@
#include <gearlib.h>
int main() {
Window window = create_window(800, 600, "gearlib");
glfwSwapInterval(0); // fps unlock
enable_debugging();
setup_textures();
Camera* camera = create_camera(vec2(0.0f, 0.0f));
UniformBuffer* camera_buffer = create_uniform_buffer(sizeof(CameraMatrices));
Texture cat = load_texture("assets/cat.png");
while (!glfwWindowShouldClose(window)) {
process_input(window);
update_camera(camera, window);
set_uniform_data(camera_buffer, camera->m);
glClearColor(vec4_spread(BLACK));
glClear(GL_COLOR_BUFFER_BIT);
for(int i = 0; i < 32; i++) {
draw_texture(cat, vec2(rand() % 600, rand() % 600), vec2(500.0f, 500.0f), WHITE);
}
flush();
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}

BIN
examples/textures Executable file

Binary file not shown.

32
examples/textures.c Normal file
View file

@ -0,0 +1,32 @@
#include <gearlib.h>
int main() {
Window window = create_window(800, 600, "gearlib");
glfwSwapInterval(0); // fps unlock
setup_textures();
Camera* camera = create_camera(vec2(0.0f, 0.0f));
UniformBuffer* camera_buffer = create_uniform_buffer(sizeof(CameraMatrices));
Texture cat = load_texture("assets/cat.png");
while (!glfwWindowShouldClose(window)) {
process_input(window);
update_camera(camera, window);
set_uniform_data(camera_buffer, camera->m);
glClearColor(vec4_spread(BLACK));
glClear(GL_COLOR_BUFFER_BIT);
draw_texture(cat, vec2(250.0f, 250.0f), vec2(500.0f, 500.0f), WHITE);
flush();
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}

View file

@ -1,11 +1,23 @@
#ifndef __CAMERA_H__
#define __CAMERA_H__
typedef struct Camera {
enum CAMERA_PROJECTION {
CAMERA_ORTHOGRAPHIC = 0,
CAMERA_PERSPECTIVE
};
typedef struct CameraMatrices {
mat4 view;
mat4 projection;
} CameraMatrices;
typedef struct Camera {
vec3 position;
int projection;
struct CameraMatrices* m;
} Camera;
void set_active_camera(Camera* camera);
Camera* create_camera(vec2 pos);
void update_camera(Camera* camera, Window window);
#endif

View file

@ -15,5 +15,6 @@
#include <batch.h>
#include <quad.h>
#include <textures.h>
#include <uniform_buffer.h>
#endif

12
include/uniform_buffer.h Normal file
View file

@ -0,0 +1,12 @@
#ifndef __UNIFORM_BUFFER_H__
#define __UNIFORM_BUFFER_H__
typedef struct UniformBuffer {
size_t size;
uint32_t ubo;
} UniformBuffer;
UniformBuffer* create_uniform_buffer(size_t size);
void set_uniform_data(UniformBuffer* buffer, void* data);
#endif

View file

@ -1,7 +1,28 @@
#include <gearlib.h>
#include <stdlib.h>
void set_active_camera(Camera* camera) {
/*for(int i = 0; i < batches.size; i++) {
batches.data[i]->camera = camera;
}*/
Camera* create_camera(vec2 pos) {
Camera* camera = calloc(sizeof(Camera), 1);
camera->position = vec3(pos, -3.0f);
camera->m = calloc(sizeof(CameraMatrices), 1);
return camera;
}
void update_camera(Camera* camera, Window window) {
int width, height;
glfwGetWindowSize(window, &width, &height);
assert(camera != NULL);
camera->m->view = mat4Transpose(mat4Translate(vec3_spread(camera->position)));
switch(camera->projection) {
case CAMERA_ORTHOGRAPHIC:
camera->m->projection = mat4Transpose(mat4Ortho(0.0f, width, height, 0.0f, 0.0f, 1000.0f));
break;
case CAMERA_PERSPECTIVE:
camera->m->projection = mat4Transpose(mat4Perspective(45.0f * DEG2RAD, ((double)width / (double)height), 0.1f, 100.0f));
break;
}
}

16
src/uniform_buffer.c Normal file
View file

@ -0,0 +1,16 @@
#include <gearlib.h>
UniformBuffer* create_uniform_buffer(size_t size) {
UniformBuffer* buffer = calloc(sizeof(UniformBuffer), 1);
buffer->size = size;
glCreateBuffers(1, &buffer->ubo);
glNamedBufferStorage(buffer->ubo, buffer->size, NULL, GL_DYNAMIC_STORAGE_BIT);
glBindBufferBase(GL_UNIFORM_BUFFER, 0, buffer->ubo);
return buffer;
}
void set_uniform_data(UniformBuffer* buffer, void* data) {
glNamedBufferSubData(buffer->ubo, 0, buffer->size, data);
}

BIN
test/main

Binary file not shown.

View file

@ -1,127 +0,0 @@
#include <gearlib.h>
Camera* create_camera(vec2 pos) {
Camera* camera = calloc(sizeof(Camera), 1);
camera->view = mat4Transpose(mat4Translate(pos.x, pos.y, -3.0f));
return camera;
}
void update_camera(Camera* camera, Window window) {
int width, height;
glfwGetWindowSize(window, &width, &height);
assert(camera != NULL);
camera->projection = mat4Transpose(mat4Ortho(0.0f, width, height, 0.0f, 0.0f, 1000.0f));
//camera->projection = mat4Transpose(mat4Perspective(45.0f * DEG2RAD, ((double)width / (double)height), 0.1f, 100.0f));
}
typedef struct CameraData {
mat4 view_projection;
} CameraData;
int main() {
Window window = create_window(800, 600, "gearlib");
glfwSwapInterval(0);
enable_debugging();
setup_quads();
setup_textures();
Camera* camera = create_camera(vec2(0.0f, 0.0f));
set_active_camera(camera);
Texture cat = load_texture("assets/cat.png");
/*uint32_t cat = load_texture("assets/cat.png");
uint32_t cuddle = load_texture("assets/cuddle.png");
uint32_t parrots = load_texture("assets/parrots.png");*/
double time = glfwGetTime();
/*double total_time = 0;
int frames = 0;
int fps = 0;*/
uint32_t ubo;
glCreateBuffers(1, &ubo);
glNamedBufferStorage(ubo, sizeof(Camera), NULL, GL_DYNAMIC_STORAGE_BIT);
glBindBufferBase(GL_UNIFORM_BUFFER, 0, ubo);
float angle = 0;
while (!glfwWindowShouldClose(window)) {
process_input(window);
update_camera(camera, window);
glNamedBufferSubData(ubo, 0, sizeof(Camera), camera);
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
/*for(int i = 0; i < 33; i++) {
draw_texture(parrots, vec3(-0.4f, 0.4f, 0.0f), WHITE);
draw_texture(cat, vec3(0.0f, 0.0f, 0.0f), WHITE);
draw_texture(cuddle, vec3(0.0f, -0.4f, 0.0f), WHITE);
}*/
//draw_texture(cat, vec2(300, 400), vec2(700, 700), WHITE);
//for(int i = 0; i < 255; i++)
//draw_quad(vec2(i, i), vec2(10, 10), vec4(i / 255.0f, i / 255.0f, i / 255.0f, 1.0f));
mat4 transform = mat4Multiply(
mat4Multiply(
mat4Scale(100, 100, 1),
mat4Identity()),
/*mat4Rotate(vec3(1.0f, 0.5f, 0.0f), angle)),*/
mat4Translate(0, 0, 1));
transform = mat4Multiply(mat4Translate(0.5, 0.5f, 0.0f), transform);
draw_texture_trans(cat, transform, WHITE);
flush();
double end_time = glfwGetTime();
double frame_time = end_time - time;
//total_time += frame_time;
double fps = 1 / frame_time;
angle += frame_time;
time = end_time;
/*frames++;
if(total_time > 1.0) {
fps = frames;
frames = 0;
total_time = 0;
}*/
//printf("%lf, %lf\n", frame_time, fps);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}
/*
double time = glfwGetTime();
...
double end_time = glfwGetTime();
double frame_time = end_time - time;
double fps = 1 / frame_time;
time = end_time;
printf("%lf, %lf, %d, %d\n", frame_time, fps, 0, 0);
*/