aboutsummaryrefslogtreecommitdiff
path: root/editor/editor.c
blob: 7f88f595bf782f37c838fa660e13157986665cd3 (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
#include "editor.h"

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

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

Editor editor = {0};

void tick(void)
{
    ty_draw_clear(TY_COLOR_BLACK);

    switch (editor.state) {
    case EDITOR_TILE:
        tile_proc();
        tile_draw();
        break;
    }

    tyui_draw();
}

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

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

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

    ty_free_image(font_img);
}

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

    load_font(&editor.font, "font.qoi");

    editor.state = EDITOR_TILE;

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

        ty_free_temp_allocs();
    }

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

    tyui_deinit();
    ty_deinit();

    return 0;
}