This repository has been archived by the owner on Sep 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bricks.m
399 lines (347 loc) · 9.7 KB
/
bricks.m
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
//
// Clone of well-known video game about falling bricks :)
// Copyright (C) Mate Kukri, 2023
//
import mem
import libc
import sdl2
// Represents a color
struct Color(r: Uint8, g: Uint8, b: Uint8)
// Background color of the playfield
const BACKGROUND: Color = Color(50, 50, 50)
// Colormap (the playfield indexes this)
const COLOR_INVALID : Uint8 = 0
const COLOR_CYAN : Uint8 = 1
const COLOR_BLUE : Uint8 = 2
const COLOR_ORANGE : Uint8 = 3
const COLOR_YELLOW : Uint8 = 4
const COLOR_GREEN : Uint8 = 5
const COLOR_PURPLE : Uint8 = 6
const COLOR_RED : Uint8 = 7
const COLOR_GRAY : Uint8 = 8
const COLORMAP: [9]Color = [
Color(-1, -1, -1 ), // Invalid
Color(0, 240, 241), // Cyan
Color(0, 0, 240), // Blue
Color(237, 161, 0 ), // Orange
Color(240, 240, 0 ), // Yellow
Color(0, 240, 0 ), // Green
Color(160, 0, 241), // Purple
Color(241, 0, 0 ), // Red
Color(150, 150, 150) // Gray
]
// Playfield size
const PLAYFIELD_W: Uintn = 10
const PLAYFIELD_H: Uintn = 24
// Brick size
const BRICK_W : Uintn = 32
const BRICK_H : Uintn = 32
// Window size
const WINDOW_W : Uintn = PLAYFIELD_W * BRICK_W
const WINDOW_H : Uintn = PLAYFIELD_H * BRICK_H
// Position in 2D space
struct Vec2 (x: Int32, y: Int32)
// Description of brick
struct Brick (
// Color index
color_idx: Uint8,
// Width of its bounding box
box_width: Int32,
// Points of the piece
points: [4]Vec2,
// Offset of the bounding box (from the top left)
offset: Vec2
)
//
// Constructors for various brick variants
//
function create_I() -> Brick {
Brick (
color_idx: COLOR_CYAN,
box_width: 4,
points: [ Vec2(0,1), Vec2(1,1), Vec2(2,1), Vec2(3,1) ],
offset: Vec2(0,0)
)
}
function create_J() -> Brick {
Brick (
color_idx: COLOR_BLUE,
box_width: 3,
points: [ Vec2(0,0), Vec2(0,1), Vec2(1,1), Vec2(2,1) ],
offset: Vec2(0,0)
)
}
function create_L() -> Brick {
Brick (
color_idx: COLOR_ORANGE,
box_width: 3,
points: [ Vec2(0,1), Vec2(1,1), Vec2(2,1), Vec2(2,0) ],
offset: Vec2(0,0)
)
}
function create_O() -> Brick {
Brick (
color_idx: COLOR_YELLOW,
box_width: 2,
points: [ Vec2(0,0), Vec2(1,0), Vec2(0,1), Vec2(1,1) ],
offset: Vec2(0,0)
)
}
function create_S() -> Brick {
Brick (
color_idx: COLOR_GREEN,
box_width: 3,
points: [ Vec2(0,1), Vec2(1,1), Vec2(1,0), Vec2(2,0) ],
offset: Vec2(0,0)
)
}
function create_T() -> Brick {
Brick (
color_idx: COLOR_PURPLE,
box_width: 3,
points: [ Vec2(0,1), Vec2(1,1), Vec2(2,1), Vec2(1,0) ],
offset: Vec2(0,0)
)
}
function create_Z() -> Brick {
Brick (
color_idx: COLOR_RED,
box_width: 3,
points: [ Vec2(0,0), Vec2(1,0), Vec2(1,1), Vec2(2,1) ],
offset: Vec2(0,0)
)
}
function create_random() -> Brick {
let ctors = [ create_I, create_J, create_L, create_O, create_S, create_T,
create_Z ];
let index = (libc::rand() % 7) as <Uintn>;
ctors[index]()
}
function move_brick(
brick: *mut Brick,
playfield: *[PLAYFIELD_H][PLAYFIELD_W]Uint8,
disp_x: Int32,
disp_y: Int32
) -> Bool {
let mut ok = true;
let oldpoints = (*brick).points;
let mut i = 0;
while i < 4 {
let p = &(*brick).points[i];
i += 1;
(*p).x += disp_x;
(*p).y += disp_y;
if (*p).y < 0 || (*p).y >= PLAYFIELD_H { ok = false; break }
if (*p).x < 0 || (*p).x >= PLAYFIELD_W { ok = false; break }
if (*playfield)[(*p).y as <Uintn>]
[(*p).x as <Uintn>] != 0 { ok = false; break }
}
if ok {
(*brick).offset.x += disp_x;
(*brick).offset.y += disp_y;
} else {
(*brick).points = oldpoints;
}
ok
}
function flatten_brick(
brick: *Brick,
playfield: *mut[PLAYFIELD_H][PLAYFIELD_W]Uint8
) {
// Add brick tiles to playfield
let mut i = 0;
while i < 4 {
let p = &(*brick).points[i];
i += 1;
(*playfield)[(*p).y as <Uintn>]
[(*p).x as <Uintn>] = (*brick).color_idx;
}
// Scan for complete lines
let mut y = 0;
while y < PLAYFIELD_H {
let mut x = 0;
let mut complete = true;
while x < PLAYFIELD_W {
if (*playfield)[y][x] == 0 { complete = false; break }
x += 1;
}
if complete {
mem::zero_bytes(&(*playfield)[y][0], PLAYFIELD_W);
let mut cy = y;
while cy > 0 {
mem::copy_bytes(&(*playfield)[cy][0], &(*playfield)[cy - 1][0], PLAYFIELD_W);
cy -= 1;
}
}
y += 1;
}
}
function rotate_brick(
brick: *mut Brick,
playfield: *[PLAYFIELD_H][PLAYFIELD_W]Uint8
) {
let mut ok = true;
let oldpoints = (*brick).points;
let mut i = 0;
while i < 4 {
let p = &(*brick).points[i];
i += 1;
let tmp = (*p).x - (*brick).offset.x;
(*p).x = 1 - ((*p).y - (*brick).offset.y - ((*brick).box_width - 2)) + (*brick).offset.x;
(*p).y = tmp + (*brick).offset.y;
if (*p).y < 0 || (*p).y >= PLAYFIELD_H { ok = false; break }
if (*p).x < 0 || (*p).x >= PLAYFIELD_W { ok = false; break }
if (*playfield)[(*p).y as <Uintn>][(*p).x as <Uintn>] != 0 { ok = false; break }
}
if !ok {
(*brick).points = oldpoints;
}
}
function main() -> Int32 {
// Initialize SDL2 and SDL2_image
if sdl2::SDL_Init(sdl2::SDL_INIT_EVERYTHING) != 0 {
libc::fprintf(libc::stderr,
c"Failed to initialze SDL2: %s\n",
sdl2::SDL_GetError());
return 1;
}
sdl2::IMG_Init(sdl2::IMG_INIT_PNG);
// Seed random generator
libc::srand(sdl2::SDL_GetTicks());
// Create window
let window = sdl2::SDL_CreateWindow(
title: c"Bricks",
x: sdl2::SDL_WINDOWPOS_CENTERED,
y: sdl2::SDL_WINDOWPOS_CENTERED,
w: WINDOW_W,
h: WINDOW_H,
flags: 0);
// Create renderer
let renderer = sdl2::SDL_CreateRenderer(window,
index: -1,
flags: 0);
// Set playfield background color
sdl2::SDL_SetRenderDrawColor(renderer,
BACKGROUND.r, BACKGROUND.g, BACKGROUND.b, a: 0);
// Load brick texture
let texture = sdl2::IMG_LoadTexture(renderer, c"brick.png");
// Initialize game state
// FIXME: add language construct for copied array initializer
let mut playfield: [PLAYFIELD_H][PLAYFIELD_W]Uint8 = !;
let mut y = 0;
while y < PLAYFIELD_H {
let mut x = 0;
while x < PLAYFIELD_W {
playfield[y][x] = 0;
x += 1;
}
y += 1;
}
let mut fall_interval: Uint32 = 500;
let mut move_interval: Uint32 = 70;
let mut prev_fall = sdl2::SDL_GetTicks();
let mut prev_move = sdl2::SDL_GetTicks();
let mut spawn_time = 0;
let mut moving_left = false;
let mut moving_right = false;
let mut spawn_piece = false;
let mut brick = create_random();
// Event loop
loop {
// Handle events
let mut event = !;
let mut quit = false;
while sdl2::SDL_PollEvent(&event) != 0 {
if event._type == sdl2::SDL_KEYUP {
let scancode = event.key.keysym.scancode;
if scancode == sdl2::SDL_SCANCODE_DOWN {
fall_interval = 500;
} else if scancode == sdl2::SDL_SCANCODE_LEFT {
moving_left = false;
} else if scancode == sdl2::SDL_SCANCODE_RIGHT {
moving_right = false;
}
} else if event._type == sdl2::SDL_KEYDOWN {
let scancode = event.key.keysym.scancode;
if scancode == sdl2::SDL_SCANCODE_UP {
rotate_brick(&brick, &playfield);
} else if scancode == sdl2::SDL_SCANCODE_DOWN {
fall_interval = 40;
} else if scancode == sdl2::SDL_SCANCODE_LEFT {
moving_left = true;
} else if scancode == sdl2::SDL_SCANCODE_RIGHT {
moving_right = true;
}
} else if event._type == sdl2::SDL_QUIT {
quit = true;
}
}
// Exit event loop
if quit {
break
}
// Draw
sdl2::SDL_RenderClear(renderer);
// User controlled brick
let c = COLORMAP[brick.color_idx as <Uintn>];
sdl2::SDL_SetTextureColorMod(texture, c.r, c.g, c.b);
let mut i = 0;
while i < 4 {
let rect = sdl2::SDL_Rect(x: brick.points[i].x * BRICK_W,
y: brick.points[i].y * BRICK_H,
w: BRICK_W,
h: BRICK_H);
sdl2::SDL_RenderCopy(renderer, texture, nil, &rect);
i += 1;
}
// Stationery part of the build
let mut y = 0;
while y < PLAYFIELD_H {
let mut x = 0;
while x < PLAYFIELD_W {
let c_idx = playfield[y as <Uintn>][x as <Uintn>];
if c_idx != 0 {
let c = COLORMAP[c_idx as <Uintn>];
sdl2::SDL_SetTextureColorMod(texture, c.r, c.g, c.b);
let rect = sdl2::SDL_Rect(x: x * BRICK_W,
y: y * BRICK_H,
w: BRICK_W,
h: BRICK_H);
sdl2::SDL_RenderCopy(renderer, texture, nil, &rect);
}
x += 1;
}
y += 1;
}
sdl2::SDL_RenderPresent(renderer);
// Update
let cur_time = sdl2::SDL_GetTicks();
if !spawn_piece && cur_time > prev_fall + fall_interval {
if !move_brick(&brick, &playfield, 0, 1) {
spawn_piece = true;
spawn_time = cur_time + 200;
}
prev_fall = cur_time;
}
if !spawn_piece && cur_time > prev_move + move_interval {
if moving_left {
move_brick(&brick, &playfield, -1, 0);
}
if moving_right {
move_brick(&brick, &playfield, 1, 0);
}
prev_move = cur_time;
}
if spawn_piece && cur_time > spawn_time {
flatten_brick(&brick, &playfield);
brick = create_random();
spawn_piece = false;
}
}
// Cleanup
sdl2::SDL_DestroyRenderer(renderer);
sdl2::SDL_DestroyWindow(window);
sdl2::IMG_Quit();
sdl2::SDL_Quit();
0
}