-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.wl
355 lines (315 loc) · 7.99 KB
/
main.wl
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
use "importc"
import(C) "/usr/include/stdlib.h"
import(C) "/usr/include/stdio.h"
import(C) "/usr/include/SDL/SDL.h"
import(C) "/usr/include/SDL/SDL_image.h"
import "matrix.wl"
import "sprite.wl"
import "owl.wl"
import "fireball.wl"
import "list.wl"
import "bg.wl"
import "particle.wl"
import "mouse.wl"
import "halo.wl"
import "bush.wl"
import "fire.wl"
import "score.wl"
import "hunger.wl"
SDL_Surface^ screen = null
SDL_Surface^ buffer = null
SDL_Surface^ light = null
SDL_Surface^ title = null
SDL_Surface^ howto = null
SDL_Surface^ highscore = null
uint8^ keystate = null
bool running = true
bool alive = true
uint hunger = 1500
uint fireballPower = 100
List^ fireballs = null
Owl^ owl = null
int getPixel(SDL_Surface^ s, int i, int j)
{
if(i < 0 || i >= s.w || j < 0 || j >= s.h)
return 0
int ^r = &(s.pixels[i * s.format.BytesPerPixel + j * s.pitch])
return ^r
}
void setPixel(SDL_Surface^ s, int i, int j, uint v)
{
if(i < 0 || i >= s.w || j < 0 || j >= s.h) return
int^ r = &(s.pixels[i * s.format.BytesPerPixel + j * s.pitch])
^r = v
}
int convolutePixel(int a, int b)
{
int c
uchar^ al = uchar^: &a
uchar^ bl = uchar^: &b
uchar^ cl = uchar^: &c
float mod = (float: bl[0]) / 255.0
if(mod < 0.0) mod = 0.0
if(mod > 1.0) mod = 1.0
cl[0] = al[0] * mod
cl[1] = al[1] * mod
cl[2] = al[2] * mod
cl[3] = al[3] * mod
return c
}
void dolight()
{
for(int j = 0; j < 240; j++)
for(int i = 0; i < 320; i++)
{
int bpxl = getPixel(buffer, i, j)
int lpxl = getPixel(light, i, j)
int epxl = convolutePixel(bpxl, lpxl)
setPixel(buffer, i, j, epxl)
}
}
float distanceTo(float ax, float ay, float bx, float by)
{
float dx = ax - bx
float dy = ay - by
return dx * dx + dy * dy //meh, who needs squareroot?
}
void addFireball()
{
if(fireballPower > 20)
{
fireballPower = fireballPower - 20
list_add(fireballs, fireball_new(owl.x, owl.y - 24))
}
}
// this is my favourite function
// and would be even better if not for the N^2 complexity
// ...
// used to be soooo much cooler when it only burned mice.
// named: BURNIFYMICE. now, it burnifies other stuff and its
// boring
void burnify()
{
list_begin(fireballs)
while(!list_end(fireballs))
{
Particle ^fb = list_get(fireballs)
list_begin(mice)
while(!list_end(mice))
{
Mouse^ ms = list_get(mice)
if(distanceTo(ms.x, ms.y, fb.x, fb.y) < 75)
mouse_enflame(ms)
list_next(mice)
}
list_begin(bushes)
while(!list_end(bushes))
{
Bush^ b = list_get(bushes)
if(distanceTo(b.x, b.y, fb.x, fb.y) < 120)
bush_enflame(b)
list_next(bushes)
}
list_next(fireballs)
}
}
void eatifyMice()
{
list_begin(mice)
while(!list_end(mice))
{
Mouse^ ms = list_get(mice)
if(distanceTo(ms.x, ms.y, owl.head.x, owl.head.y) < 75)
{
if(ms.cook == 0)
{
score_add(10)
hunger = hunger + 75
} else if(ms.cook == 1)
{
score_add(50)
hunger = hunger + 200
} else if(ms.cook == 2)
{
score_add(5)
hunger = hunger + 20
}
list_remove(mice)
}
list_next(mice)
}
}
void scaleBlit(SDL_Surface^ dst, SDL_Surface^ src)
{
for(int j = 0; j < 480; j++)
{
for(int i = 0; i < 640; i++)
{
int pxl = getPixel(src, i / 2, j / 2)
setPixel(dst, i, j, pxl)
}
}
//SDL_BlitSurface(buffer, &buffer.clip_rect, screen, &screen.clip_rect)
}
void draw()
{
SDL_FillRect(buffer, null, 0)
SDL_FillRect(light, null, 0)
background_draw(buffer)
mice_draw(buffer, light)
bushes_draw(buffer, light)
owl_draw(owl, buffer, light)
list_begin(fireballs)
while(!list_end(fireballs))
{
fireball_draw(list_get(fireballs), buffer, light)
list_next(fireballs)
}
frag_draw(buffer, light)
score_draw(buffer, light)
int hungerLevel = 0
if(hunger >= 2000) hungerLevel = 9
else if(hunger >= 1800) hungerLevel = 8
else if(hunger >= 1600) hungerLevel = 7
else if(hunger >= 1400) hungerLevel = 6
else if(hunger >= 1200) hungerLevel = 5
else if(hunger >= 1000) hungerLevel = 4
else if(hunger >= 800) hungerLevel = 3
else if(hunger >= 600) hungerLevel = 2
else if(hunger >= 400) hungerLevel = 1
else if(hunger >= 200) hungerLevel = 0
hunger_draw(buffer, light, hungerLevel)
dolight()
scaleBlit(screen, buffer)
SDL_Flip(screen)
}
void update()
{
hunger--
if(!hunger) alive = false
if(fireballPower < 200) fireballPower++
input()
background_update()
bushes_update()
owl_update(owl)
mice_update()
frag_update()
list_begin(fireballs)
while(!list_end(fireballs))
{
if(fireball_update(list_get(fireballs)))
{
list_remove(fireballs)
}
list_next(fireballs)
}
burnify()
eatifyMice()
SDL_Delay(2)
}
float OWLSPEED = 2.0
uint fireballTimer = 5
void input()
{
SDL_PumpEvents()
keystate = SDL_GetKeyState(null)
//if(keystate[SDLK_SPACE]) running = false
if(keystate[SDLK_ESCAPE]) {
running = false
alive = false
}
if(keystate[SDLK_UP]) owl.y = owl.y - OWLSPEED
if(keystate[SDLK_DOWN]) owl.y = owl.y + OWLSPEED
if(keystate[SDLK_RIGHT]) owl.x = owl.x + OWLSPEED
if(keystate[SDLK_LEFT]) owl.x = owl.x - OWLSPEED
if(owl.x < 0) owl.x = 0
if(owl.x > 320) owl.x = 320
if(owl.y < 0) owl.y = 0
if(owl.y > 240) owl.y = 240
if(keystate[SDLK_a] && fireballTimer == 0)
{
addFireball()
fireballTimer = 10
}
if(fireballTimer) fireballTimer--
}
void init()
{
screen = SDL_SetVideoMode(640, 480, 0, SDL_SWSURFACE)
buffer = SDL_CreateRGBSurface(SDL_SWSURFACE, 320, 240, screen.format.BitsPerPixel,
screen.format.Rmask, screen.format.Gmask, screen.format.Bmask, screen.format.Amask)
light = SDL_CreateRGBSurface(SDL_SWSURFACE, 320, 240, screen.format.BitsPerPixel,
screen.format.Rmask, screen.format.Gmask, screen.format.Bmask, screen.format.Amask)
title = IMG_Load("res/title.png")
howto = IMG_Load("res/instructions.png")
highscore = IMG_Load("res/highscore.png")
fireballs = list_new()
srand(0)
background_init()
score_init()
fire_init()
halo_init()
particle_init()
mice_init()
bush_init()
hunger_init()
owl = owl_new()
SDL_WM_SetCaption("Jam 2014", null)
}
void waitOnSurface(SDL_Surface^ sf)
{
printf("waiting\n")
bool escape = false
int timeout = 20
while(!escape)
{
if(timeout) timeout--
SDL_FillRect(buffer, null, 0)
SDL_BlitSurface(sf, null, buffer, null)
scaleBlit(screen, buffer)
SDL_PumpEvents()
keystate = SDL_GetKeyState(null)
if(keystate[SDLK_a] && !timeout) escape = true
SDL_Flip(screen)
SDL_Delay(16)
}
}
void waitOnHighscore(SDL_Surface^ sf)
{
printf("waiting\n")
bool escape = false
int timeout = 50
while(!escape)
{
if(timeout) timeout--
SDL_FillRect(buffer, null, 0)
SDL_BlitSurface(sf, null, buffer, null)
score_drawOffset(sf, light, 128, 128)
scaleBlit(screen, buffer)
SDL_PumpEvents()
keystate = SDL_GetKeyState(null)
if(keystate[SDLK_a] && !timeout) escape = true
SDL_Flip(screen)
SDL_Delay(16)
}
}
int main(int argc, char^^ argv)
{
init()
while(running)
{
waitOnSurface(title)
waitOnSurface(howto)
hunger = 1500
score_set(0)
alive = true
while(alive)
{
input()
update()
draw()
}
waitOnHighscore(highscore)
}
return 0
}