From 957c64c7b8b5e98d8a03dd84c7e27e7991fb9dbc Mon Sep 17 00:00:00 2001 From: iamcheeseman Date: Mon, 6 Apr 2026 17:04:05 -0400 Subject: Initial commit --- micro/context.c | 114 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 micro/context.c (limited to 'micro/context.c') diff --git a/micro/context.c b/micro/context.c new file mode 100644 index 0000000..226b942 --- /dev/null +++ b/micro/context.c @@ -0,0 +1,114 @@ +#include "context.h" +#include "renderer.h" + +#include + +struct texture tex; +struct render_texture rtex; + +static +void init_glfw(struct ctx *ctx) +{ + if (glfwInit() < 0) { + log_fatal(ERR_GLFW_INIT, "could not init GLFW."); + } + + glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); + + ctx->win = glfwCreateWindow( + ctx->creation_hints.win_width, + ctx->creation_hints.win_height, + ctx->creation_hints.win_title, + NULL, + NULL + ); + + if (!ctx->win) { + log_fatal(ERR_GLFW_INIT, "could not create GLFW window."); + } + + glfwMakeContextCurrent(ctx->win); + + glfwSwapInterval(0); +} + +static +void deinit_glfw(struct ctx *ctx) +{ + glfwDestroyWindow(ctx->win); + glfwTerminate(); +} + +void init_ctx(struct ctx *ctx, struct ctx_hints creation_hints) +{ + ctx->creation_hints = creation_hints; + ctx->ticrate = 1.0 / creation_hints.ticrate; + ctx->prev_tic_ts = 0; + init_glfw(ctx); + init_renderer(&ctx->renderer, ctx); + + tex = load_texture("test_img.qoi"); + rtex = create_render_texture(320, 320, false); +} + +void deinit_ctx(struct ctx *ctx) +{ + free_texture(tex); + free_render_texture(rtex); + deinit_renderer(&ctx->renderer); + deinit_glfw(ctx); +} + +bool is_game_running(struct ctx *ctx) +{ + return !glfwWindowShouldClose(ctx->win); +} + +int game_tick(struct ctx *ctx) +{ + double current_time = glfwGetTime(); + double time_since_tic = current_time - ctx->prev_tic_ts; + + int tics = time_since_tic / ctx->ticrate; + if (tics > 0) { + ctx->prev_tic_ts = current_time; + glfwPollEvents(); + } + return tics; +} + +void game_draw(struct ctx *ctx) +{ + bind_render_texture(&ctx->renderer, NULL); + + draw_clear((struct color){0.2, 0.2, 0.2, 1}); + + bind_render_texture(&ctx->renderer, &rtex); + draw_clear((struct color){ + 1 - (cos(glfwGetTime()) + 1) / 2, + (sin(glfwGetTime()) + 1) / 2, + (cos(glfwGetTime()) + 1) / 2, + 1 + }); + + draw_rect(&ctx->renderer, 10, 10, 50, 50); + + set_blend_mode(&ctx->renderer, BLEND_ADD); + draw_texture(&ctx->renderer, tex, 100, 100); + draw_texture(&ctx->renderer, tex, 104, 104); + + set_blend_mode(&ctx->renderer, BLEND_ALPHA); + draw_texture(&ctx->renderer, tex, 100, 121); + bind_render_texture(&ctx->renderer, NULL); + + draw_texture( + &ctx->renderer, + rtex.color, + (400 - 320 * 0.5) + sin(glfwGetTime()) * 100, + 200 - 320 * 0.5 + ); + + end_draw(&ctx->renderer); + + glfwSwapBuffers(ctx->win); +} -- cgit v1.3-2-g0d8e