aboutsummaryrefslogtreecommitdiff
path: root/tests/main.c
blob: 878f8ff09754465f5c3cacc1fb1eda30bf754f79 (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
#include <stdio.h>
#include <string.h>
#include <mgl/mgl.h>
#include <mgl/window/window.h>
#include <mgl/window/event.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>
#include <mgl/graphics/vertex_buffer.h>
#include <mgl/graphics/shader.h>
#include <mgl/system/clock.h>

typedef struct {
    mgl_texture *texture;
    mgl_font *font;
    mgl_vertex_buffer *vertex_buffer1;
    mgl_vertex_buffer *vertex_buffer2;
    mgl_shader_program *shader_program;
    mgl_clock clock;
} Userdata;

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

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

    mgl_shader_program_set_uniform_vec2f(u->shader_program, "resolution", (mgl_vec2f){ window->size.x, window->size.y });

    mgl_sprite sprite;
    mgl_sprite_init(&sprite, u->texture, 100.0f - 10.0f, 0.0f);
    mgl_sprite_set_color(&sprite, (mgl_color){255, 255, 255, 128});
    mgl_shader_program_use(u->shader_program);
    mgl_sprite_draw(context, &sprite);
    mgl_shader_program_use(NULL);

    char str[255];
    snprintf(str, sizeof(str), "Hello world!\nelapsed time: %f", mgl_clock_get_elapsed_time_seconds(&u->clock));

    mgl_text text;
    mgl_text_init(&text, u->font, str, strlen(str), NULL);
    mgl_text_draw(context, &text);
    mgl_text_deinit(&text);

    mgl_vertex_buffer_set_position(u->vertex_buffer1, (mgl_vec2f){ window->cursor_position.x, window->cursor_position.y });
    mgl_vertex_buffer_draw(context, u->vertex_buffer1, &u->font->texture);

    mgl_vertex_buffer_set_position(u->vertex_buffer2, (mgl_vec2f){ window->cursor_position.x, window->cursor_position.y + 500 });
    mgl_vertex_buffer_draw(context, u->vertex_buffer2, &u->font->texture);

    mgl_view prev_view;
    mgl_window_get_view(window, &prev_view);

    mgl_vec2i view_size = {
        .x = 200,
        .y = 200
    };
    mgl_view new_view = {
        .position = { window->size.x/2 - view_size.x/2, window->size.y/2 - view_size.y/2 },
        .size = view_size
    };
    mgl_window_set_view(window, &new_view);

    mgl_vertex vertices[4] = {
        (mgl_vertex){
            .position = {-new_view.position.x + window->cursor_position.x, -new_view.position.y + window->cursor_position.y},
            .texcoords = {0.0f, 0.0f},
            .color = {255, 0, 0, 255}
        },
        (mgl_vertex){
            .position = {-new_view.position.x + window->cursor_position.x + 400.0f, -new_view.position.y + window->cursor_position.y},
            .texcoords = {0.0f, 0.0f},
            .color = {0, 255, 0, 255},
        },
        (mgl_vertex){
            .position = {-new_view.position.x + window->cursor_position.x + 400.0f, -new_view.position.y + window->cursor_position.y + 400.0f},
            .texcoords = {0.0f, 0.0f},
            .color = {0, 0, 255, 255},
        },
        (mgl_vertex){
            .position = {-new_view.position.x + window->cursor_position.x, -new_view.position.y + window->cursor_position.y + 400.0f},
            .texcoords = {0.0f, 0.0f},
            .color = {255, 255, 0, 255}
        }
    };

    mgl_vertices_draw(context, vertices, 4, MGL_PRIMITIVE_QUADS);
    mgl_window_set_view(window, &prev_view);
}

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

    mgl_texture texture;
    mgl_font font;
    mgl_vertex_buffer vertex_buffer1;
    mgl_vertex_buffer vertex_buffer2;
    mgl_shader_program shader_program;

    Userdata userdata;
    userdata.texture = &texture;
    userdata.font = &font;
    userdata.vertex_buffer1 = &vertex_buffer1;
    userdata.vertex_buffer2 = &vertex_buffer2;
    userdata.shader_program = &shader_program;
    mgl_clock_init(&userdata.clock);

    mgl_window window;
    if(mgl_window_create(&window, "mgl", 1280, 720) != 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;

    if(mgl_shader_program_init(&shader_program) != 0)
        return 1;

    if(mgl_shader_program_add_shader_from_file(&shader_program, "tests/circle_mask.glsl", MGL_SHADER_FRAGMENT) != 0)
        return 1;

    if(mgl_shader_program_finalize(&shader_program) != 0)
        return 1;

    mgl_vertex_buffer_init(&vertex_buffer1);
    mgl_vertex_buffer_init(&vertex_buffer2);

    mgl_vertex vertices1[4] = {
        (mgl_vertex){
            .position = {0.0f, 0.0f},
            .texcoords = {0.0f, 0.0f},
            .color = {255, 0, 0, 100}
        },
        (mgl_vertex){
            .position = {font.texture.width, 0.0f},
            .texcoords = {1.0f, 0.0f},
            .color = {0, 255, 0, 100},
        },
        (mgl_vertex){
            .position = {font.texture.width, font.texture.height/2},
            .texcoords = {1.0f, 0.5f},
            .color = {0, 0, 255, 100},
        },
        (mgl_vertex){
            .position = {0.0f, font.texture.height/2},
            .texcoords = {0.0f, 0.5f},
            .color = {255, 0, 255, 100}
        }
    };

    mgl_vertex vertices2[4] = {
        (mgl_vertex){
            .position = {0.0f, 0.0f},
            .texcoords = {0.0f, 0.5f},
            .color = {255, 0, 0, 100}
        },
        (mgl_vertex){
            .position = {font.texture.width, 0.0f},
            .texcoords = {1.0f, 0.5f},
            .color = {0, 255, 0, 100},
        },
        (mgl_vertex){
            .position = {font.texture.width, font.texture.height/2},
            .texcoords = {1.0f, 1.0f},
            .color = {0, 0, 255, 100},
        },
        (mgl_vertex){
            .position = {0.0f, font.texture.height/2},
            .texcoords = {0.0f, 1.0f},
            .color = {255, 0, 255, 100}
        }
    };

    if(mgl_vertex_buffer_update(&vertex_buffer1, vertices1, 4, MGL_PRIMITIVE_QUADS, MGL_USAGE_STATIC) != 0)
        return 1;

    if(mgl_vertex_buffer_update(&vertex_buffer2, vertices2, 4, MGL_PRIMITIVE_QUADS, MGL_USAGE_STATIC) != 0)
        return 1;

    fprintf(stderr, "Font texture width: %d, texture height: %d\n", font.texture.width, font.texture.height);

    mgl_event event;
    for(;;) {
        while(mgl_window_poll_event(&window, &event)) {
            
        }

        mgl_window_clear(&window, (mgl_color){0, 0, 0, 255});
        draw(&window, &userdata);
        mgl_window_display(&window);
    }

    mgl_vertex_buffer_deinit(&vertex_buffer2);
    mgl_vertex_buffer_deinit(&vertex_buffer1);
    mgl_shader_program_deinit(&shader_program);
    mgl_font_unload(&font);
    mgl_texture_unload(&texture);
    mgl_window_deinit(&window);
    mgl_deinit();
    return 0;
}