aboutsummaryrefslogtreecommitdiff
path: root/teensy/context.c
blob: 060254cf2df62ac7026ed7ef9a12b3eec29853ac (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
#include "context.h"
#include "renderer.h"

#include <math.h>
#include <time.h>

#include "platform.h"

struct ty_ctx ctx;
extern struct ty_renderer r;

void ty_init(struct ty_creation_hints creation_hints)
{
    ty_init_mem();

    ctx.creation_hints = creation_hints;
    ctx.ticrate        = 1.0 / creation_hints.ticrate;
    ctx.prev_tic_ts    = 0;

    ty_platform_init(&ctx);
    ty_init_renderer();
}

void ty_deinit(void)
{
    ty_platform_deinit();
    ty_deinit_renderer();
    ty_deinit_mem();
}

bool ty_is_game_running(void)
{
    return !ty_platform_os_wants_quit();
}

double ty_get_time(void)
{
    return ty_platform_get_time();
}

int ty_tick(void)
{
    double current_time   = ty_get_time();
    double time_since_tic = current_time - ctx.prev_tic_ts;

    int tics = time_since_tic / ctx.ticrate;
    if (tics > 0) {
        ctx.prev_tic_ts = current_time;
        ty_platform_frame(r.screen);
    }
    return tics;
}

// TODO: Find a better place for this
void ty_sleep(uint64_t ms)
{
        struct timespec ts;
        ts.tv_sec = ms / 1000;
        ts.tv_nsec = (ms % 1000) * 1000000;
        nanosleep(&ts, NULL);
}