aboutsummaryrefslogtreecommitdiff
path: root/src/mgui/mgui.c
blob: fdb4cb405d781bfe224bd2e956335ae95e18c622 (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
#include "../../include/mgui/mgui.h"
#include "../../include/mgui/widget.h"
#include "../../include/async_image.h"
#include <mgl/window/event.h>
#include <mgl/system/clock.h>

static mgl_vec2i root_widget_size;
static mgl_clock global_timer;
static double frame_time;

void mgui_init(void) {
    mgl_clock_init(&global_timer);
    mgui_async_image_init();
    frame_time = 0.0;
}

void mgui_deinit(void) {
    mgui_async_image_deinit();
}

void mgui_on_event(mgui_widget *root_widget, mgl_window *window, mgl_event *event) {
    if(event->type == MGL_EVENT_RESIZED)
        root_widget_size = (mgl_vec2i){ event->size.width, event->size.height };
    mgui_widget_on_event(root_widget, window, event);
}

void mgui_draw(mgui_widget *root_widget, mgl_window *window) {
    /* TODO: Only do this if widget is dirty */
    mgui_widget_calculate_size(root_widget, root_widget_size);
    mgui_widget_set_position(root_widget, (mgl_vec2i){ root_widget->margin.left, root_widget->margin.top });
    mgui_widget_draw(root_widget, window);
    mgui_async_image_unload_unreferenced();
    frame_time = mgl_clock_restart(&global_timer);
    if(frame_time > 1.0)
        frame_time = 1.0;
}

double mgui_get_seconds_since_last_update(void) {
    double elapsed_time_sec = mgl_clock_get_elapsed_time_seconds(&global_timer);
    if(elapsed_time_sec > 1.0)
        elapsed_time_sec = 1.0;
    return elapsed_time_sec;
}

double mgui_get_frame_time_seconds(void) {
    return frame_time;
}