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