-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMU_time.h
More file actions
105 lines (91 loc) · 1.97 KB
/
Copy pathMU_time.h
File metadata and controls
105 lines (91 loc) · 1.97 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#pragma once
#include<SDL.h>
#include"MU_declaration.h"
static unsigned long long timeVal = 0;
static unsigned long timeDelta = 0;
inline unsigned long long musnake::updateTime() {
static unsigned flameCount = 0;
static unsigned timeCount = 0;
unsigned long long tv = SDL_GetTicks();
timeDelta = (unsigned long)(tv - timeVal);
timeVal = tv;
flameCount++;
timeCount += timeDelta;
if (timeCount > 1000) {
timeCount %= 1000;
fps = flameCount;
flameCount = 0;
}
return tv;
}
inline unsigned long long musnake::getTimeVal() {
return timeVal;
}
inline unsigned long musnake::getTimeDelta() {
return timeDelta;
}
inline void musnake::addDelayFunc(DelayFunc** list, void (*func)(unsigned long), unsigned long arg, long long delay) {
DelayFunc* ndf = new DelayFunc;
ndf->func = func;
ndf->arg = arg;
ndf->time = delay + getTimeVal();
if (!*list) {
*list = ndf;
ndf->next = nullptr;
}
else {
if (ndf->time <= (*list)->time) {
ndf->next = *list;
*list = ndf;
}
else {
DelayFunc* np = *list;
while (np->next) {
if (ndf->time <= np->next->time) {
ndf->next = np->next;
np->next = ndf;
return;
}
np = np->next;
}
np->next = ndf;
ndf->next = nullptr;
}
}
}
inline int musnake::removeDelayFuncByFuncArg(DelayFunc** list, void(*func)(unsigned long), unsigned long arg) {
DelayFunc* dp = *list;
if (!dp) return 1;
if (dp->func == func && dp->arg == arg) {
*list = dp->next;
delete dp;
return 0;
}
else {
while (DelayFunc* tp = dp->next) {
if (tp->func == func && tp->arg == arg) {
dp->next = dp->next->next;
delete tp;
return 0;
}
dp = dp->next;
}
return 1;
}
}
inline void musnake::triggerDelayFunc(DelayFunc** list) {
DelayFunc* np;
while (np = *list) {
if (np->time > (long long)getTimeVal()) break;
*list = np->next;
np->func(np->arg);
delete np;
}
}
inline void musnake::clearDelayFunc(DelayFunc** list) {
DelayFunc* np;
while (np = *list) {
*list = np->next;
delete np;
}
}