-
Notifications
You must be signed in to change notification settings - Fork 4
/
tetris_v.v
513 lines (479 loc) · 12.4 KB
/
tetris_v.v
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
// Ported to SDL2 V wrapper by Nicolas Sauzede 2019
import rand
import time
import math
import vsdl
const (
Title = 'V Tetris'
FontName = 'fonts/RobotoMono-Regular.ttf'
BlockSize = 20 // pixels
FieldHeight = 20 // # of blocks
FieldWidth = 10
TetroSize = 4
WinWidth = BlockSize * FieldWidth
WinHeight = BlockSize * FieldHeight
TimerPeriod = 250 // ms
TextSize = 16
AudioBufSize = 1024
)
const (
// Tetros' 4 possible states are encoded in binaries
BTetros = [
// 0000 0
// 0000 0
// 0110 6
// 0110 6
[66, 66, 66, 66],
// 0000 0
// 0000 0
// 0010 2
// 0111 7
[27, 131, 72, 232],
// 0000 0
// 0000 0
// 0011 3
// 0110 6
[36, 231, 36, 231],
// 0000 0
// 0000 0
// 0110 6
// 0011 3
[63, 132, 63, 132],
// 0000 0
// 0011 3
// 0001 1
// 0001 1
[311, 17, 223, 74],
// 0000 0
// 0011 3
// 0010 2
// 0010 2
[322, 71, 113, 47],
// Special case since 15 can't be used
// 1111
[1111, 9, 1111, 9],
]
// Each tetro has its unique color
Colors = [
SdlColor{byte(0), byte(0), byte(0), byte(0)}, // unused ?
SdlColor{byte(0), byte(0x62), byte(0xc0), byte(0)}, // quad : darkblue 0062c0
SdlColor{byte(0xca), byte(0x7d), byte(0x5f), byte(0)}, // tricorn : lightbrown ca7d5f
SdlColor{byte(0), byte(0xc1), byte(0xbf), byte(0)}, // short topright : lightblue 00c1bf
SdlColor{byte(0), byte(0xc1), byte(0), byte(0)}, // short topleft : lightgreen 00c100
SdlColor{byte(0xbf), byte(0xbe), byte(0), byte(0)}, // long topleft : yellowish bfbe00
SdlColor{byte(0xd1), byte(0), byte(0xbf), byte(0)}, // long topright : pink d100bf
SdlColor{byte(0xd1), byte(0), byte(0), byte(0)}, // longest : lightred d10000
SdlColor{byte(0), byte(170), byte(170), byte(0)}, // unused ?
]
)
// TODO: type Tetro [TetroSize]struct{ x, y int }
struct Block {
mut:
x int
y int
}
enum GameState {
paused running gameover
}
struct AudioContext {
mut:
// music *C.Mix_Music
music voidptr
volume int
// waves [3]*MixChunk
waves [3]voidptr
}
struct SdlContext {
mut:
// VIDEO
w int
h int
window voidptr
renderer voidptr
screen &vsdl2.SdlSurface
texture voidptr
// AUDIO
actx AudioContext
}
struct Game {
mut:
// Score of the current game
score int
// Count consecutive lines for scoring
lines int
// State of the current game
state GameState
// Quit game ?
should_close bool
// Position of the current tetro
pos_x int
pos_y int
// field[y][x] contains the color of the block with (x,y) coordinates
// "-1" border is to avoid bounds checking.
// -1 -1 -1 -1
// -1 0 0 -1
// -1 0 0 -1
// -1 -1 -1 -1
field [][]int
// TODO: tetro Tetro
tetro []Block
// TODO: tetros_cache []Tetro
tetros_cache []Block
// Index of the current tetro. Refers to its color.
tetro_idx int
// Index of the next tetro. Refers to its color.
tetro_next int
// Index of the rotation (0-3)
rotation_idx int
// SDL2 context for drawing
sdl SdlContext
// TTF context for font drawing
font voidptr
}
fn (sdl mut SdlContext) set_sdl_context(w int, h int, title string) {
C.SDL_Init(C.SDL_INIT_VIDEO | C.SDL_INIT_AUDIO)
C.atexit(C.SDL_Quit)
C.TTF_Init()
C.atexit(C.TTF_Quit)
bpp := 32
C.SDL_CreateWindowAndRenderer(w, h, 0, &sdl.window, &sdl.renderer)
C.SDL_SetWindowTitle(sdl.window, title.str)
sdl.w = w
sdl.h = h
sdl.screen = &vsdl2.SdlSurface(C.SDL_CreateRGBSurface(0, w, h, bpp, 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000))
sdl.texture = C.SDL_CreateTexture(sdl.renderer, C.SDL_PIXELFORMAT_ARGB8888, C.SDL_TEXTUREACCESS_STREAMING, w, h)
C.Mix_Init(0)
C.atexit(C.Mix_Quit)
if C.Mix_OpenAudio(48000,C.MIX_DEFAULT_FORMAT,2,AudioBufSize) < 0 {
println('couldn\'t open audio')
}
sdl.actx.music = C.Mix_LoadMUS('sounds/TwintrisThosenine.mod')
sdl.actx.waves[0] = C.Mix_LoadWAV('sounds/block.wav')
sdl.actx.waves[1] = C.Mix_LoadWAV('sounds/line.wav')
sdl.actx.waves[2] = C.Mix_LoadWAV('sounds/double.wav')
sdl.actx.volume = C.SDL_MIX_MAXVOLUME
if C.Mix_PlayMusic(sdl.actx.music, 1) != -1 {
C.Mix_VolumeMusic(sdl.actx.volume)
}
}
fn main() {
println('V Tetris -- with Colors, Sounds & Music borrowed from venerable Twintris')
println('vsdl.version: $vsdl.version')
mut game := &Game{}
game.sdl.set_sdl_context(WinWidth, WinHeight, Title)
game.font = C.TTF_OpenFont(FontName.str, TextSize)
game.init_game()
go game.run() // Run the game loop in a new thread
for {
game.draw_scene()
// game.handle_events() // CRASHES if done in function ???
ev := SdlEvent{}
for 0 < C.SDL_PollEvent(&ev) {
switch ev._type {
case C.SDL_QUIT:
game.should_close = true
break
case C.SDL_KEYDOWN:
// global keys
switch int(ev.key.keysym.sym) {
case C.SDLK_ESCAPE:
game.should_close = true
break
case C.SDLK_SPACE:
switch game.state {
case .running:
game.state = .paused
case .paused:
game.state = .running
case .gameover:
game.init_game()
game.state = .running
}
}
if game.state != .running {
break
}
// keys while game is running
switch int(ev.key.keysym.sym) {
case C.SDLK_UP:
// Rotate the tetro
old_rotation_idx := game.rotation_idx
game.rotation_idx++
if game.rotation_idx == TetroSize {
game.rotation_idx = 0
}
game.get_tetro()
if !game.move_right(0) {
game.rotation_idx = old_rotation_idx
game.get_tetro()
}
if game.pos_x < 0 {
game.pos_x = 1
}
case C.SDLK_LEFT:
game.move_right(-1)
case C.SDLK_RIGHT:
game.move_right(1)
case C.SDLK_DOWN:
game.move_tetro() // drop faster when the player presses <down>
}
}
}
if game.should_close {
break
}
C.SDL_Delay(20) // short delay between redraw
}
if game.font != voidptr(0) {
C.TTF_CloseFont(game.font)
}
if game.sdl.actx.music != voidptr(0) {
C.Mix_FreeMusic(game.sdl.actx.music)
}
C.Mix_CloseAudio()
if game.sdl.actx.waves[0] != voidptr(0) {
C.Mix_FreeChunk(game.sdl.actx.waves[0])
}
if game.sdl.actx.waves[1] != voidptr(0) {
C.Mix_FreeChunk(game.sdl.actx.waves[1])
}
if game.sdl.actx.waves[2] != voidptr(0) {
C.Mix_FreeChunk(game.sdl.actx.waves[2])
}
}
fn (g mut Game) init_game() {
g.parse_tetros()
rand.seed(time.now().unix)
g.generate_tetro()
g.field = []array_int // TODO: g.field = [][]int
// Generate the field, fill it with 0's, add -1's on each edge
for i := 0; i < FieldHeight + 2; i++ {
mut row := [0].repeat(FieldWidth + 2)
row[0] = - 1
row[FieldWidth + 1] = - 1
g.field << row
}
mut first_row := g.field[0]
mut last_row := g.field[FieldHeight + 1]
for j := 0; j < FieldWidth + 2; j++ {
first_row[j] = - 1
last_row[j] = - 1
}
g.score = 0
g.state = .running
}
fn (g mut Game) parse_tetros() {
for b_tetros in BTetros {
for b_tetro in b_tetros {
for t in parse_binary_tetro(b_tetro) {
g.tetros_cache << t
}
}
}
}
fn (g mut Game) run() {
for {
if g.state == .running {
g.move_tetro()
n := g.delete_completed_lines()
if n > 0 {
g.lines += n
} else {
if g.lines > 0 {
if g.lines > 1 {
C.Mix_PlayChannel(0, g.sdl.actx.waves[2], 0)
} else if g.lines == 1 {
C.Mix_PlayChannel(0, g.sdl.actx.waves[1], 0)
}
g.score += 10 * g.lines * g.lines
g.lines = 0
}
}
}
time.sleep_ms(TimerPeriod) // medium delay between game step
}
}
fn (g mut Game) move_tetro() {
// Check each block in current tetro
for block in g.tetro {
y := block.y + g.pos_y + 1
x := block.x + g.pos_x
// Reached the bottom of the screen or another block?
// TODO: if g.field[y][x] != 0
//if g.field[y][x] != 0 {
row := g.field[y]
if row[x] != 0 {
// The new tetro has no space to drop => end of the game
if g.pos_y < 2 {
g.state = .gameover
return
}
// Drop it and generate a new one
g.drop_tetro()
g.generate_tetro()
C.Mix_PlayChannel(0, g.sdl.actx.waves[0], 0)
return
}
}
g.pos_y++
}
fn (g mut Game) move_right(dx int) bool {
// Reached left/right edge or another tetro?
for i := 0; i < TetroSize; i++ {
tetro := g.tetro[i]
y := tetro.y + g.pos_y
x := tetro.x + g.pos_x + dx
row := g.field[y]
if row[x] != 0 {
// Do not move
return false
}
}
g.pos_x += dx
return true
}
fn (g &Game) delete_completed_lines() int {
mut n := 0
for y := FieldHeight; y >= 1; y-- {
n += g.delete_completed_line(y)
}
return n
}
fn (g &Game) delete_completed_line(y int) int {
for x := 1; x <= FieldWidth; x++ {
f := g.field[y]
if f[x] == 0 {
return 0
}
}
// Move everything down by 1 position
for yy := y - 1; yy >= 1; yy-- {
for x := 1; x <= FieldWidth; x++ {
mut a := g.field[yy + 1]
b := g.field[yy]
a[x] = b[x]
}
}
return 1
}
// Draw a rand tetro index
fn (g mut Game) rand_tetro() int {
cur := g.tetro_next
g.tetro_next = rand.next(BTetros.len)
return cur
}
// Place a new tetro on top
fn (g mut Game) generate_tetro() {
g.pos_y = 0
g.pos_x = FieldWidth / 2 - TetroSize / 2
g.tetro_idx = g.rand_tetro()
g.rotation_idx = 0
g.get_tetro()
}
// Get the right tetro from cache
fn (g mut Game) get_tetro() {
idx := g.tetro_idx * TetroSize * TetroSize + g.rotation_idx * TetroSize
g.tetro = g.tetros_cache.slice(idx, idx + TetroSize)
}
fn (g &Game) drop_tetro() {
for i := 0; i < TetroSize; i++ {
tetro := g.tetro[i]
x := tetro.x + g.pos_x
y := tetro.y + g.pos_y
// Remember the color of each block
// TODO: g.field[y][x] = g.tetro_idx + 1
mut row := g.field[y]
row[x] = g.tetro_idx + 1
}
}
fn (g &Game) draw_block(i, j, color_idx int) {
rect := vsdl2.SdlRect {(j - 1) * BlockSize, (i - 1) * BlockSize,
BlockSize - 1, BlockSize - 1}
scol := Colors[color_idx]
rr := scol.r
gg := scol.g
bb := scol.b
col := C.SDL_MapRGB(g.sdl.screen.format, rr, gg, bb)
C.SDL_FillRect(g.sdl.screen, &rect, col)
}
fn (g &Game) draw_text(x int, y int, text string, rr int, gg int, bb int) {
tcol := SdlColor {byte(3), byte(2), byte(1), byte(0)}
tsurf := C.TTF_RenderText_Solid(g.font, text.str, tcol)
ttext := C.SDL_CreateTextureFromSurface(g.sdl.renderer, tsurf)
texw := 0
texh := 0
C.SDL_QueryTexture(ttext, 0, 0, &texw, &texh)
dstrect := vsdl2.SdlRect { x, y, texw, texh }
C.SDL_RenderCopy(g.sdl.renderer, ttext, 0, &dstrect)
C.SDL_DestroyTexture(ttext)
vsdl2.free_surface(tsurf)
}
fn (g &Game) draw_tetro() {
for i := 0; i < TetroSize; i++ {
tetro := g.tetro[i]
g.draw_block(g.pos_y + tetro.y, g.pos_x + tetro.x, g.tetro_idx + 1)
}
}
fn (g &Game) draw_field() {
for i := 1; i < FieldHeight + 1; i++ {
for j := 1; j < FieldWidth + 1; j++ {
f := g.field[i]
if f[j] > 0 {
g.draw_block(i, j, f[j])
}
}
}
}
fn (g &Game) draw_score() {
if g.font != voidptr(0) {
g.draw_text(1, 2, 'score: ' + g.score.str() + ' nxt=' + g.tetro_next.str(), 0, 0, 0)
if g.state == .gameover {
g.draw_text(1, WinHeight / 2 + 0 * TextSize, 'Game Over', 0, 0, 0)
g.draw_text(1, WinHeight / 2 + 2 * TextSize, 'SPACE to restart', 0, 0, 0)
} else if g.state == .paused {
g.draw_text(1, WinHeight / 2 + 0 * TextSize, 'Game Paused', 0, 0, 0)
g.draw_text(1, WinHeight / 2 + 2 * TextSize, 'SPACE to resume', 0, 0, 0)
}
}
}
fn (g &Game) draw_scene() {
rect := vsdl2.SdlRect {0,0,g.sdl.w,g.sdl.h}
col := C.SDL_MapRGB(g.sdl.screen.format, 255, 255, 255)
C.SDL_FillRect(g.sdl.screen, &rect, col)
g.draw_tetro()
g.draw_field()
C.SDL_UpdateTexture(g.sdl.texture, 0, g.sdl.screen.pixels, g.sdl.screen.pitch)
C.SDL_RenderClear(g.sdl.renderer)
C.SDL_RenderCopy(g.sdl.renderer, g.sdl.texture, 0, 0)
g.draw_score()
C.SDL_RenderPresent(g.sdl.renderer)
}
fn parse_binary_tetro(t_ int) []Block {
mut t := t_
res := [Block{}].repeat(4)
mut cnt := 0
horizontal := t == 9// special case for the horizontal line
for i := 0; i <= 3; i++ {
// Get ith digit of t
p := int(math.pow(10, 3 - i))
mut digit := int(t / p)
t %= p
// Convert the digit to binary
for j := 3; j >= 0; j-- {
bin := digit % 2
digit /= 2
if bin == 1 || (horizontal && i == TetroSize - 1) {
// TODO: res[cnt].x = j
// res[cnt].y = i
mut point := &res[cnt]
point.x = j
point.y = i
cnt++
}
}
}
return res
}