aboutsummaryrefslogtreecommitdiff
path: root/dc/dc.c
blob: 01915a623fca52d24b2a1ea15feef99c981cfc78 (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <stdio.h>

#include <math.h>
#include <teensy.h>
#include <teensy_ui.h>
#include <time.h>

#define QOI_IMPLEMENTATION
#include "third/qoi.h"

#define SCREEN_WIDTH 256
#define SCREEN_HEIGHT 256

#define TILE_SIZE 16

ty_Image img;
ty_Font font = {};
tyui_Id winid = 0;

double last_frame = 0;

bool toggled = false;

void tick(void)
{
    ty_draw_clear(TY_COLOR_BLACK);

    ty_draw_image(
        img,
        ty_vec2i(
            SCREEN_WIDTH*0.5 + sin(ty_get_time()) * 50 - (img.width*0.5),
            SCREEN_HEIGHT*0.5 - (img.height*0.5)
        )
    );

    ty_draw_image_ex(
        img,
        ty_recti(
            0, 0,
            img.width, img.height
        ),
        ty_recti(
            sin(-ty_get_time()) * 50, SCREEN_HEIGHT / 2,
            32, 64
        )
    );

    ty_draw_image_rot(
        img,
        ty_vec2i(SCREEN_WIDTH/2, SCREEN_HEIGHT/2),
        ty_vec2i(-img.width/2, -img.height/2),
        ty_get_time() * TY_PI / 2
    );

    ty_draw_text(&font, ty_vec2i(5, SCREEN_HEIGHT - 5 - 8), "hello, world!");

    if (ty_button_pressed(TY_BTN_LEFT_UP)) {
        toggled = !toggled;
    }

    if (toggled) {
        ty_draw_image(img, ty_vec2i(200, 200));
    }

    double frame_time = ty_get_time() - last_frame;
    last_frame = ty_get_time();

    tyui_layout((float[]){0.25, 0});
    tyui_text("%.2g ms", frame_time * 1000);
    tyui_text("%.2d fps", (int)(1.0 / frame_time));
    tyui_layout(NULL);
    tyui_text("%d", (int)ty_get_time());
    tyui_text("abcdefghijklmnopqrstuvwxyz");
    tyui_text("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
    tyui_text("0123456789");
    tyui_text("!\"'#$%&()*+,-./@[]\\");
    tyui_text("^_`{}|~:;<>=? ");

    if (tyui_begin_window("Test Window", ty_recti(5, 5, 100, 100), &winid)) {
        for (int i = 0; i < 3; i++)
            tyui_text("example text");
        if (tyui_button("press me"))
            toggled = !toggled;
        
        tyui_layout((float[]){0.1, 0.2, 0.5, 0.1, 0});
        static float xvalue = 10;
        tyui_text("x");
        tyui_text("%g", xvalue);
        tyui_slider(0, 100, 1, &xvalue);
        if (tyui_button("^"))
            xvalue++;
        if (tyui_button("v"))
            xvalue--;
        xvalue = floor(xvalue);

        static float yvalue = 50;
        tyui_text("y");
        tyui_text("%g", yvalue);
        tyui_slider(0, 100, 1, &yvalue);
        if (tyui_button("^"))
            yvalue++;
        if (tyui_button("v"))
            yvalue--;
        yvalue = floor(yvalue);
        tyui_layout(NULL);

        tyui_end_window();
    }

    tyui_draw();

    ty_Vec2i mouse_pos = ty_mouse_pos();
    ty_draw_rect(ty_recti(mouse_pos.x, mouse_pos.y, 1, 1), TY_COLOR_RED);

    ty_draw_end();
}

ty_Image load_qoi_image(const char *path)
{
    qoi_desc desc;
    void *data = qoi_read(path, &desc, 3);
    if (!data)
        ty_log_fatal(TY_ERR_IO, "failed to read image");
    ty_Image img = ty_create_image(desc.width, desc.height, data);
    free(data);

    return img;
}

int main(void)
{
    ty_Hints hints = {
        .scr_width = SCREEN_WIDTH,
        .scr_height = SCREEN_HEIGHT,
        .game_title = "Demonchime",
        .ticrate = 60,
    };
    ty_init(hints);
    tyui_init(&font);

    img = load_qoi_image("test_img.qoi");

    char font_chars[] = 
        "abcdefghijklmnopqrstuvwxyz0123456789"
        "!\"'#$%&()*+,-./@[]\\^_`{}|~:;<>=? "
    ;
    ty_Image font_img = load_qoi_image("font.qoi");

    int glyph_height = 8;

    for (int i = 0; font_chars[i] != '\0'; i++) {
        ty_Image glyph = ty_create_image(
            font_img.width,
            glyph_height,
            font_img.data + font_img.width * glyph_height * i
        );

        uint8_t c = *(uint8_t*)&font_chars[i];

        ty_font_add_glyph(&font, c, glyph);
        if (c >= 'a' && c <= 'z')
            ty_font_add_glyph(&font, c - ('a' - 'A'), glyph);
    }

    while (ty_is_game_running()) {
        int ticks = ty_tick();
        for (int i = 0; i < ticks; i++)
            tick();

        ty_free_temp_allocs();
        // ty_sleep(1000 / hints.ticrate);
    }

    ty_free_image(img);
    ty_free_image(font_img);

    for (int i = 0; i < CHAR_MAX; i++) {
        if (i >= 'a' && i <= 'z')
            continue;
        ty_Image glyph = font.glyphs[i];
        if (glyph.width * glyph.height == 0)
            continue;
        ty_free_image(glyph);
    }

    tyui_deinit();
    ty_deinit();
    return 0;
}