aboutsummaryrefslogtreecommitdiff
path: root/teensy/teensy_renderer.c
blob: d457ef6f4b44c03d2f492379808bec90a80fb148 (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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#include "teensy_renderer.h"

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdarg.h>

#include "teensy_context.h"
#include "teensy_list.h"

#define BLEND_COLOR ty_color(255, 0, 255)

struct ty_renderer r;

static
bool is_renderer_init(void)
{
    return r.screen.data != NULL && r.screen.width * r.screen.height != 0;
}

static
void image_bounds_check(struct ty_image img, struct ty_vec2i pos)
{
#ifdef TEENSY_DEBUG
    if (
        pos.x < 0 ||
        pos.x >= img.width ||
        pos.y < 0 ||
        pos.y >= img.height
    ) {
        ty_log_fatal(
            TY_ERR_RENDER_OOB,
            "OOB (%d, %d). bounds are (%d, %d)",
            pos.x, pos.y,
            img.width, img.height
        );
    }
#else 
    (void)img;
    (void)pos;
#endif
}

void ty_init_renderer(void)
{
    r.screen = ty_create_image(
        ctx.hints.scr_width,
        ctx.hints.scr_height,
        NULL
    );
    r.target = r.screen;
}

void ty_deinit_renderer(void)
{
    assert(is_renderer_init());

    ty_free_image(r.screen);
}

struct ty_image ty_create_image(int w, int h, const struct ty_color* data)
{
    struct ty_image img;

    img.width  = w;
    img.height = h;

    size_t size = sizeof(struct ty_color) * w * h;
    img.data = ty_alloc(size);
    if (data)
        memcpy(img.data, data, size);
    else
        memset(img.data, 0, size);

    return img;
}

void ty_free_image(struct ty_image img)
{
    ty_free(img.data);
}

struct ty_color ty_img_get_pixel(struct ty_image img, struct ty_vec2i pos)
{
    image_bounds_check(img, pos);
    return img.data[pos.y * img.width + pos.x];
}

void ty_img_set_pixel(
    struct ty_image img,
    struct ty_vec2i pos,
    struct ty_color color
) {
    image_bounds_check(img, pos);
    img.data[pos.y * img.width + pos.x] = color;
}

// This function technically is not needed, but I may want unicode support in
// the future, when an abstraction like this is needed.
void ty_font_add_glyph(struct ty_font *font, uint8_t c, struct ty_image img)
{
    font->glyphs[c] = img;
}

int ty_font_width(struct ty_font *font, const char *fmt, ...)
{
    va_list args;
    va_start(args, fmt);
    int len = vsnprintf(NULL, 0, fmt, args);
    char *text = ty_talloc(sizeof(char) * (len + 1));
    va_end(args);

    va_start(args, fmt);
    vsnprintf(text, len + 1, fmt, args);
    va_end(args);

    int width = 0;
    for (uint8_t *c = (uint8_t*)text; *c != '\0'; c++)
        width += font->glyphs[*c].width;
    return width;
}

void ty_draw_set_target(const struct ty_image *img)
{
    if (!img) {
        r.target = r.screen;
        return;
    }
    memcpy(&r.target, img, sizeof(r.target));
}

void ty_draw_clear(struct ty_color col)
{
    assert(is_renderer_init());
    for (int i = 0; i < r.target.width * r.target.height; i++) {
        r.target.data[i] = col;
    }
}

void ty_draw_image(struct ty_image img, struct ty_vec2i pos)
{
    int x1 = fmin(fmax(pos.x, 0), r.target.width);
    int y1 = fmin(fmax(pos.y, 0), r.target.height);
    int x2 = fmin(fmax(pos.x + img.width, 0), r.target.width);
    int y2 = fmin(fmax(pos.y + img.height, 0), r.target.height);

    for (int dy = y1; dy < y2; dy++) {
        for (int dx = x1; dx < x2; dx++) {
            struct ty_color px = ty_img_get_pixel(
                img,
                ty_vec2i(dx - x1, dy - y1)
            );
            if (memcmp(&px, &BLEND_COLOR, sizeof(px)) == 0)
                continue;
            ty_img_set_pixel(
                r.target,
                ty_vec2i(dx, dy),
                px
            );
        }
    }
}

void ty_draw_image_ex(
    struct ty_image img,
    struct ty_recti src,
    struct ty_recti dst
) {
    int x1 = fmin(fmax(dst.x, 0), r.target.width);
    int y1 = fmin(fmax(dst.y, 0), r.target.height);
    int x2 = fmin(fmax(dst.x + dst.w, 0), r.target.width);
    int y2 = fmin(fmax(dst.y + dst.h, 0), r.target.height);

    for (int dy = y1; dy < y2; dy++) {
        for (int dx = x1; dx < x2; dx++) {
            int img_x = ((dx - x1) * src.w) / dst.w + src.x;
            int img_y = ((dy - y1) * src.h) / dst.h + src.y;
            struct ty_color px = ty_img_get_pixel(img, ty_vec2i(img_x, img_y));
            if (memcmp(&px, &BLEND_COLOR, sizeof(px)) == 0)
                continue;

            ty_img_set_pixel(
                r.target,
                ty_vec2i(dx, dy),
                px
            );
        }
    }
}

struct ty_vec2i clamp_point(struct ty_vec2i p, struct ty_recti rect)
{
    p.x = fmin(fmax(p.x, rect.x), rect.w);
    p.y = fmin(fmax(p.y, rect.y), rect.h);
    return p;
}

void ty_draw_image_rot(
    struct ty_image img,
    struct ty_vec2i pos,
    struct ty_vec2i offset,
    double rot
) {
    double s = sin(rot);
    double c = cos(rot);
 
    // x1 = c*x - s*y
    // y1 = s*x + c*y

    // FIXME: the corners of the rotated image get cut off.
    for (int img_y = 0; img_y < img.height; img_y++) {
        for (int img_x = 0; img_x < img.width; img_x++) {
            double pixel_x = img_x + offset.x;
            double pixel_y = img_y + offset.y;

            int dx = floor(pixel_x * c - pixel_y * s - offset.x);
            int dy = floor(pixel_x * s + pixel_y * c - offset.y);

            if (dx < 0 || dx >= img.width || dy < 0 || dy >= img.height)
                continue;

            struct ty_color px = ty_img_get_pixel(img, ty_vec2i(dx, dy));
            ty_img_set_pixel(r.target, ty_vec2i(pos.x + img_x, pos.y + img_y), px);
        }
    }
}

void ty_draw_rect(struct ty_recti rect, struct ty_color color)
{
    int x1 = fmin(fmax(rect.x, 0), r.target.width);
    int y1 = fmin(fmax(rect.y, 0), r.target.height);

    int x2 = fmin(fmax(rect.x + rect.w, 0), r.target.width);
    int y2 = fmin(fmax(rect.y + rect.h, 0), r.target.height);

    for (int dy = y1; dy < y2; dy++) {
        for (int dx = x1; dx < x2; dx++) {
            ty_img_set_pixel(
                r.target,
                ty_vec2i(dx, dy),
                color
            );
        }
    }
}

void ty_draw_line(
    struct ty_vec2i start,
    struct ty_vec2i end,
    struct ty_color color
) {
    int dx = end.x - start.x;
    int dy = end.y - start.y;

    if (abs(dx) > abs(dy)) {
        if (start.x > end.x) {
            int tmp = start.x;
            start.x = end.x;
            end.x   = tmp;

            tmp     = start.y;
            start.y = end.y;
            end.y   = tmp;
        }

        for (int x = start.x; x < end.x; x++) {
            int y = start.y + dy * (x - start.x) / dx;

            ty_img_set_pixel(
                r.target,
                ty_vec2i(x, y),
                color
            );
        }
    } else {
        if (start.y > end.y) {
            int tmp = start.x;
            start.x = end.x;
            end.x   = tmp;

            tmp     = start.y;
            start.y = end.y;
            end.y   = tmp;
        }

        for (int y = start.y; y < end.y; y++) {
            int x = start.x + dx * (y - start.y) / dy;

            ty_img_set_pixel(
                r.target,
                ty_vec2i(x, y),
                color
            );
        }
    }
}

void ty_draw_text(struct ty_font *font, struct ty_vec2i pos, const char *text)
{
    for (const uint8_t *c = (const uint8_t*)text; *c != '\0'; c++) {
        struct ty_image glyph = font->glyphs[*c];
        ty_draw_image(glyph, pos);
        pos.x += glyph.width;
    }
}

void ty_draw_text_fmt(
    struct ty_font *font,
    struct ty_vec2i pos,
    const char *fmt,
    ...
) {
    va_list args;
    va_start(args, fmt);
    int len = vsnprintf(NULL, 0, fmt, args);
    char *text = ty_talloc(sizeof(char) * (len + 1));
    va_end(args);

    va_start(args, fmt);
    vsnprintf(text, len + 1, fmt, args);
    va_end(args);

    ty_draw_text(font, pos, text);
}

void ty_draw_end(void)
{
    assert(is_renderer_init());
}