summaryrefslogtreecommitdiff
path: root/micro/context.c
blob: 226b942f4474657c52512c803e8157fdeb705e0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include "context.h"
#include "renderer.h"

#include <math.h>

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);
}