+1
include/keraforge.h
+1
include/keraforge.h
+58
include/keraforge/time.h
+58
include/keraforge/time.h
···
1
+
#ifndef __kf_time__
2
+
#define __kf_time__
3
+
4
+
5
+
#include <keraforge/_header.h>
6
+
#include <time.h>
7
+
8
+
9
+
enum kf_timermode
10
+
{
11
+
/* Manually stopped timer. Upon stopping, age is
12
+
updated to contain stop-start, then the callback
13
+
is invoked if present. Ticking is not necessary. */
14
+
kf_timermode_stopwatch,
15
+
/* Repeating timer. Every time length is reached,
16
+
the callback will be invoked if present. stop is
17
+
unused until kf_timer_stop() is called, which
18
+
will prevent further repeats of this timer.
19
+
Ticking is necessary. */
20
+
kf_timermode_repeat,
21
+
/* Timer that stops after reaching length. After
22
+
reaching length, the callback will be invoked
23
+
if present. Ticking is necessary. */
24
+
kf_timermode_oneshot,
25
+
};
26
+
27
+
struct kf_timer
28
+
{
29
+
/* The mode for this timer. */
30
+
enum kf_timermode mode;
31
+
/* The time when this timer was last ticked. */
32
+
time_t now;
33
+
/* When this timer started. */
34
+
time_t start;
35
+
/* When this timer stopped. Will default to 0,
36
+
indicating that the timer is still running. */
37
+
time_t stop;
38
+
/* How long this timer has lasted for.
39
+
Stopwatch: This will be stop-start, updated
40
+
once kf_timer_stop() is called.
41
+
Repeat: This will be reset to 0 every iteration.
42
+
One Shot: This will be reset to 0 with
43
+
kf_timer_start().
44
+
*/
45
+
time_t age;
46
+
/* How long this timer lasts for. */
47
+
time_t length;
48
+
/* Executed when this timer finishes. */
49
+
void (*callback)(struct kf_timer *timer);
50
+
};
51
+
52
+
53
+
void kf_timer_start(struct kf_timer *timer);
54
+
void kf_timer_tick(struct kf_timer *timer);
55
+
void kf_timer_stop(struct kf_timer *timer);
56
+
57
+
58
+
#endif
+32
src/time.c
+32
src/time.c
···
1
+
#include <keraforge.h>
2
+
3
+
4
+
void kf_timer_start(struct kf_timer *timer)
5
+
{
6
+
time(&timer->start);
7
+
time(&timer->now);
8
+
timer->stop = 0;
9
+
10
+
if (timer->mode == kf_timermode_oneshot)
11
+
timer->age = 0;
12
+
}
13
+
14
+
void kf_timer_tick(struct kf_timer *timer)
15
+
{
16
+
time_t now = time(NULL);
17
+
time_t dif = now - timer->now;
18
+
timer->age += dif;
19
+
timer->now = now;
20
+
21
+
switch (timer->mode)
22
+
{
23
+
case kf_timermode_stopwatch:
24
+
break;
25
+
case kf_timermode_repeat:
26
+
case kf_timermode_oneshot:
27
+
break;
28
+
}
29
+
30
+
}
31
+
32
+
void kf_timer_stop(struct kf_timer *timer);