]> git.lizzy.rs Git - dragonblocks_alpha.git/blob - src/day.c
24b6b4b4117d3995d759ae13f6686b712b78afd6
[dragonblocks_alpha.git] / src / day.c
1 #include <math.h>
2 #include <time.h>
3 #include "day.h"
4 #include "util.h"
5
6 bool timelapse = false;
7 static f64 time_of_day_offset;
8
9 f64 get_unix_time()
10 {
11         struct timespec ts;
12         clock_gettime(CLOCK_REALTIME, &ts);
13         return ((f64) ts.tv_sec + ts.tv_nsec / 1.0e9) * (timelapse ? 100.0 : 1.0);
14 }
15
16 f64 get_time_of_day()
17 {
18         return fmod(get_unix_time() - time_of_day_offset, MINUTES_PER_DAY);
19 }
20
21 void set_time_of_day(f64 new_time)
22 {
23         time_of_day_offset = get_unix_time() - new_time;
24 }
25
26 f64 get_sun_angle()
27 {
28         return 2.0 * M_PI * (f64) get_time_of_day() / MINUTES_PER_DAY + M_PI;
29 }
30
31 f64 get_daylight()
32 {
33         return f64_clamp(cos(get_sun_angle()) * 2.0 + 0.5, 0.0, 1.0);
34 }
35
36 void split_time_of_day(int *hours, int *minutes)
37 {
38         int time_of_day = get_time_of_day();
39
40         *minutes = time_of_day % MINUTES_PER_HOUR;
41         *hours = time_of_day / MINUTES_PER_HOUR;
42 }