aboutsummaryrefslogtreecommitdiff
path: root/tests/main.c
blob: 5e219320563b6ebf2d552a778bd395df03dac2ed (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
#include <stdio.h>
#include <mgl/mgl.h>
#include <mgl/window.h>
#include <mgl/graphics/texture.h>
#include <mgl/graphics/rectangle.h>
#include <mgl/graphics/sprite.h>
#include <mgl/graphics/font.h>
#include <mgl/graphics/text.h>

typedef struct {
    mgl_texture *texture;
    mgl_font *font;
} Userdata;

static void draw(mgl_window *window, void *userdata) {
    mgl_context *context = mgl_get_context();
    Userdata *u = userdata;

    mgl_rectangle rect = {
        .color = { 1.0f, 0.0f, 0.0f, 1.0f },
        .position = { window->cursor_position.x, window->cursor_position.y },
        .size = { 100.0f, 500.0f }
    };
    mgl_rectangle_draw(context, &rect);

    mgl_sprite sprite;
    mgl_sprite_init(&sprite, u->texture, 100.0f - 10.0f, 0.0f);
    mgl_sprite_set_color(&sprite, 1.0f, 1.0f, 1.0f, 0.5f);
    mgl_sprite_draw(context, &sprite);

    mgl_text text;
    mgl_text_init(&text, u->font, "hello world!\nGood bye world!", 0.0f, 0.0f);
    mgl_text_draw(context, &text);
    mgl_text_deinit(&text);
}

int main(int argc, char **argv) {
    if(mgl_init() != 0)
        return 1;

    mgl_texture texture;
    mgl_font font;

    Userdata userdata;
    userdata.texture = &texture;
    userdata.font = &font;

    mgl_window_callback window_callback;
    window_callback.userdata = &userdata;
    window_callback.draw = draw;

    mgl_window window;
    if(mgl_window_create(&window, "mgl", 1280, 720, &window_callback) != 0)
        return 1;

    if(mgl_texture_load_from_file(&texture, "tests/X11.png", NULL) != 0)
        return 1;

    if(mgl_font_load_from_file(&font, "/usr/share/fonts/noto/NotoSans-Regular.ttf", 32) != 0)
        return 1;

    for(;;) {
        mgl_window_events_poll(&window);
        mgl_window_draw(&window);
    }

    mgl_font_unload(&font);
    mgl_texture_unload(&texture);
    mgl_window_deinit(&window);
    mgl_deinit();
    return 0;
}