#include #include #include #include #include #include #include #include #include #include #include #include #include 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_vertex vertices[4] = { (mgl_vertex){ .position = {window->cursor_position.x, 0.0f}, .texcoords = {0.0f, 0.0f}, .color = {0, 255, 0, 255} }, (mgl_vertex){ .position = {window->cursor_position.x + 100.0f, 0.0f}, .texcoords = {0.0f, 0.0f}, .color = {0, 255, 0, 255}, }, (mgl_vertex){ .position = {window->cursor_position.x + 100.0f, 100.0f}, .texcoords = {0.0f, 0.0f}, .color = {0, 255, 0, 255}, }, (mgl_vertex){ .position = {window->cursor_position.x, 100.0f}, .texcoords = {0.0f, 0.0f}, .color = {0, 255, 0, 255} } }; mgl_vertices_draw(context, vertices, 4, MGL_PRIMITIVE_QUADS); } 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; }