diff options
| author | Freya Murphy <freya@freyacat.org> | 2025-12-11 10:49:50 -0500 |
|---|---|---|
| committer | Freya Murphy <freya@freyacat.org> | 2025-12-11 10:51:40 -0500 |
| commit | fa8fa6784559ed0fc8d780e36880273f77e272c4 (patch) | |
| tree | 7456a4e9148d47e409ba837bafdc6238b6c757db /src/main.c | |
| parent | add ubos (diff) | |
| download | voxel-fa8fa6784559ed0fc8d780e36880273f77e272c4.tar.gz voxel-fa8fa6784559ed0fc8d780e36880273f77e272c4.tar.bz2 voxel-fa8fa6784559ed0fc8d780e36880273f77e272c4.zip | |
i did a lot
Diffstat (limited to 'src/main.c')
| -rw-r--r-- | src/main.c | 125 |
1 files changed, 96 insertions, 29 deletions
@@ -1,51 +1,118 @@ #include <GLFW/glfw3.h> +#include <threads.h> #include "voxel.h" #include "window.h" -#include "shader.h" -#include "mesh.h" -int main(void) +World world; +Camera camera; + +void sleep(double seconds) +{ + struct timespec req, rem; + req.tv_sec = seconds; + req.tv_nsec = 0; + + if (seconds < 0) + return; + + while (thrd_sleep(&req, &rem) == thrd_error) + req = rem; +} + +/// handles rendering +VOXEL_RESULT render_main(void *) +{ + Renderer renderer; + + if (window_init_gl() != VOXEL_OK) + return VOXEL_ERROR; + + if (renderer_init(&renderer) != VOXEL_OK) + return VOXEL_ERROR; + + while (!window_closed()) { + if (window.resize) { + glViewport(0, 0, window.width, window.height); + window.resize = false; + } + + renderer_start(&renderer, &camera); + world_render(&world, &renderer); + renderer_stop(&renderer); + + window_swap(); + } + + renderer_free(&renderer); + return VOXEL_OK; +} + +#define TPS 20 + +/// handles game logic +VOXEL_RESULT logic_main(void *) { - Shader *shader; - Chunk *chunk; - Mesh mesh; - Camera camera; - mat4 proj_view; + double last_tick, next_tick; + last_tick = 0; + next_tick = 0; - if (window_init()) - return 1; + while (!window_closed()) { + world_update(&world, &camera); - shader = shader_init("assets/vertex.glsl", "assets/fragment.glsl"); - if (!shader) - return 1; + next_tick = last_tick + (1.0 / TPS); + sleep(next_tick - last_tick); + last_tick = next_tick; + } - chunk = chunk_init(0, 0, 0); - chunk_generate(chunk); - mesh = chunk_mesh(chunk); + return VOXEL_OK; +} - camera = camera_init(); +/// handles user input +VOXEL_RESULT input_main(void *) +{ + double last_tick, next_tick; + last_tick = 0; + next_tick = 0; while (!window_closed()) { window_update(); - if (key_down(GLFW_KEY_ESCAPE)) + if (key_down(GLFW_KEY_ESCAPE)) { + window.close = true; break; + } camera_update(&camera); - camera_proj_view(&camera, proj_view); - - shader_bind(shader); - shader_load_mat4(0, proj_view); - mesh_bind(&mesh); - mesh_draw(&mesh); - mesh_unbind(&mesh); - shader_unbind(); - // main game loop - window_swap(); + next_tick = last_tick + (1.0 / TPS); + sleep(next_tick - last_tick); + last_tick = next_tick; } + return VOXEL_OK; +} + +VOXEL_RESULT main(void) +{ + thrd_t render_thread, logic_thread, input_thread; + + if (window_init() != VOXEL_OK) + return VOXEL_ERROR; + + camera_init(&camera); + world_init(&world, 0); + + thrd_create(&logic_thread, logic_main, NULL); + thrd_create(&input_thread, input_main, NULL); + thrd_create(&render_thread, render_main, NULL); + + thrd_join(logic_thread, NULL); + thrd_join(input_thread, NULL); + thrd_join(render_thread, NULL); + + world_free(&world); window_close(); - return 0; + + return VOXEL_OK; } |