-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLoader.adept
175 lines (143 loc) · 5.84 KB
/
Loader.adept
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import 'main.adept'
struct Loader (
thread pthread_t,
completed_mutex pthread_mutex_t,
complete bool,
percentage float, // 0.0f...1.0f
waiting bool,
should_stop bool,
current_loading_end_x float,
target_loading_end_x float,
message String
) {
func create {
randomSetSeed()
#unless random_use_mt19937
rand() // For some reason, the PRNG doesn't seem to be very random
// unless we call rand() once before we use it. No idea why!
// This shouldn't be required, but whatever
#end
this.complete = false
this.percentage = 0.0f
this.waiting = true
this.current_loading_end_x = 0.0f
this.target_loading_end_x = 0.0f
this.message = this.randomMessage()
pthread_mutex_init(&this.completed_mutex, null)
if pthread_create(&this.thread, null, func &loadThread(*Loader) as ptr as func(ptr) ptr, this as ptr) {
printf('Failed to start loader thread\n')
return
}
}
func destroy {
pthread_mutex_destroy(&this.completed_mutex)
}
func loadThread {
// Thread for loading assets
gamedata.init()
if this.getShouldStop(), return
this.setPercentage(1.0f / 4.0f)
sfx.load()
if this.getShouldStop(), return
this.setPercentage(2.0f / 4.0f)
soundtrack.init(func &loadedOneSongCallback(*Loader) as ptr as func(ptr) void, this as ptr)
if this.getShouldStop(), return
this.setPercentage(4.0f / 4.0f)
this.setComplete(true)
}
func loadedOneSongCallback {
this.setPercentage(this.getPercentage() + 1.0f / 2.0f / SOUND_TRACK_COUNT as float)
}
func isComplete bool {
pthread_mutex_lock(&this.completed_mutex)
defer pthread_mutex_unlock(&this.completed_mutex)
return this.complete && fabs(this.target_loading_end_x - this.current_loading_end_x) <= 0.001f
}
func setComplete(value bool) {
pthread_mutex_lock(&this.completed_mutex)
this.complete = value
pthread_mutex_unlock(&this.completed_mutex)
}
func getPercentage float {
// NOTE: Returns 0.0f...1.0f
pthread_mutex_lock(&this.completed_mutex)
defer pthread_mutex_unlock(&this.completed_mutex)
return this.percentage
}
func setPercentage(value float) {
pthread_mutex_lock(&this.completed_mutex)
this.percentage = value
pthread_mutex_unlock(&this.completed_mutex)
}
func getShouldStop bool {
// NOTE: Returns 0.0f...1.0f
pthread_mutex_lock(&this.completed_mutex)
defer pthread_mutex_unlock(&this.completed_mutex)
return this.should_stop
}
func setShouldStop(value bool) {
pthread_mutex_lock(&this.completed_mutex)
this.should_stop = value
pthread_mutex_unlock(&this.completed_mutex)
}
func didFinishLoading bool {
if !this.waiting || !this.isComplete() || fabs(this.target_loading_end_x - this.current_loading_end_x) > 0.001f, return false
this.waiting = false
return true
}
func stop {
// NOTE: Because we don't have a way of determining which things were loaded
// and need to be destroyed, we won't setShouldStop(true)
if this.waiting && !this.isComplete() && pthread_join(this.thread, null) {
printf('Failed to join loader thread\n')
return
}
}
func drawLoadingAnimation {
drawTextCentered(this.message, captViewWidth() / 2.0f, captViewHeight() / 2.0f)
beginning float = captViewWidth() / 4.0f
length float = captViewWidth() / 2.0f
y float = captViewHeight() / 2.0f + 96.0f
start_x float = beginning
end_x float = beginning + length * loader.getPercentage()
this.target_loading_end_x = end_x
if this.current_loading_end_x == 0.0f, this.current_loading_end_x = this.target_loading_end_x
if this.current_loading_end_x < this.target_loading_end_x {
this.current_loading_end_x += (this.target_loading_end_x - this.current_loading_end_x) / 6.0f
if this.current_loading_end_x >= this.target_loading_end_x,
this.current_loading_end_x = this.target_loading_end_x
}
drawLine(beginning, y, beginning + length, y, 8.0f, EndCapStyle::ROUND, &textures.unloaded_line)
drawLine(start_x, y, this.current_loading_end_x, y, 8.0f, EndCapStyle::ROUND, &textures.loading_line)
}
func randomMessage String {
switch random(24) as int {
case 0, return "Reading the rule book"
case 1, return "Igniting fires"
case 2, return "Shuffling cards"
case 3, return "Swirling potions"
case 4, return "Hanging the disco ball"
case 5, return "Handing out shields"
case 6, return "Training wizards"
case 7, return "Doubling throckmortins"
case 8, return "Maintaining bee colonies"
case 9, return "Reversing the punch"
case 10, return "Obtaining sticks"
case 11, return "Reviving the dead"
case 12, return "NORMAL LOADING"
case 13, return "Taming ducks"
case 14, return "Figuring out the meaning to life"
case 15, return "Baking a pie"
case 16, return "Generating jokes"
case 17, return "Stealing hearts"
case 18, return "Watching advertisements"
case 19, return "Sharpening swords"
case 20, return "Printing Italians"
case 21, return "Paying taxes"
case 22, return "Feeling sad"
case 23, return "Raising cholesterol levels"
}
return "Loading"
}
}
loader Loader