-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.wl
149 lines (127 loc) · 2.86 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
use "importc"
import(C) "SDL/SDL.h"
import(C) "SDL/SDL_image.h"
import(C) "math.h"
import "map.wl"
import "player.wl"
import "camera.wl"
import "killwall.wl"
bool running = true
SDL_Surface^ surf
Map map
Player player
Camera camera
SDL_Surface^[] font
void init() {
surf = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE)
IMG_Init(IMG_INIT_PNG)
map = new Map()
player = new Player()
camera = new Camera()
font = new SDL_Surface^[10]
for(int i = 0; i < 10; i++) {
char[32] path
sprintf(path.ptr, "res/font/%d.png", i)
font[i] = IMG_Load(path.ptr)
}
// something wrong with ref counting :(
retain camera
retain player
retain map
}
void deinit() {
IMG_Quit()
SDL_Quit()
}
void input() {
SDL_PumpEvents()
uint8^ keystate = SDL_GetKeyState(null)
if(keystate[SDLK_ESCAPE]) {
running = false
}
player.input(keystate)
}
void draw() {
map.draw(surf, camera)
player.draw(surf, camera)
map.drawOverlay(surf, camera)
SDL_Flip(surf)
SDL_FillRect(surf, null, 0xffffffff)
}
void update() {
camera.setFocus(player.x, player.y)
camera.update(map)
player.update(map)
}
int oldTime = 0
int newTime = 0
void delay() {
oldTime = newTime
newTime = SDL_GetTicks()
printf("%d\n", newTime - oldTime);
if(newTime - oldTime < 16) {
SDL_Delay(newTime - oldTime)
}
}
void title() {
player.reset()
map.reset()
SDL_Surface ^img = IMG_Load("res/title.png")
while(running) {
SDL_PumpEvents()
uint8^ keystate = SDL_GetKeyState(null)
if(keystate[SDLK_SPACE]) break
if(keystate[SDLK_ESCAPE]) running = false
SDL_UpperBlit(img, null, surf, null)
SDL_Flip(surf)
delay()
}
SDL_FreeSurface(img)
}
void drawScore() {
int score = player.blocksPassed
if(score < 0) score = 0
uint ndig = floor(log10(score)) + 1
uint dign = 1
while(score) {
uint sdig = score % 10
score = score / 10
SDL_Rect r = [300 + (ndig-dign) * 32, 220, 32, 64]
SDL_UpperBlit(font[sdig], null, surf, &r)
dign++
}
}
void score() {
SDL_Surface ^img = IMG_Load("res/score.png")
while(running) {
SDL_PumpEvents()
uint8^ keystate = SDL_GetKeyState(null)
if(keystate[SDLK_SPACE]) break
if(keystate[SDLK_ESCAPE]) running = false
SDL_UpperBlit(img, null, surf, null)
drawScore()
SDL_Flip(surf)
delay()
}
SDL_FreeSurface(img)
}
void run() {
init()
while(running) {
title()
int deathTimeout = 200
while(deathTimeout > 0 && running) {
input()
update()
draw()
delay()
if(!player.isAlive()) deathTimeout--
}
score()
}
deinit()
}
int main(int argc, char^^ argv) {
run()
return 0
}