1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

examples: make v -prod build-examples pass without warnings/errors

This commit is contained in:
Delyan Angelov
2020-10-18 09:48:13 +03:00
parent 67ecc04580
commit 8b2e704741
9 changed files with 594 additions and 512 deletions

View File

@ -1,7 +1,6 @@
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module main
import os
@ -12,27 +11,27 @@ import gg
import sokol.sapp
const (
block_size = 20 // pixels
field_height = 20 // # of blocks
field_width = 10
tetro_size = 4
win_width = block_size * field_width
win_height = block_size * field_height
timer_period = 250 // ms
text_size = 24
block_size = 20 // pixels
field_height = 20 // # of blocks
field_width = 10
tetro_size = 4
win_width = block_size * field_width
win_height = block_size * field_height
timer_period = 250 // ms
text_size = 24
limit_thickness = 3
)
const (
text_cfg = gx.TextCfg{
align:.left
size:text_size
color:gx.rgb(0, 0, 0)
align: .left
size: text_size
color: gx.rgb(0, 0, 0)
}
over_cfg = gx.TextCfg{
align:.left
size:text_size
color:gx.white
align: .left
size: text_size
color: gx.white
}
)
@ -43,7 +42,7 @@ const (
// 0110 6 0010 2 0011 3 0110 6 0001 1 0010 2
// 0110 6 0111 7 0110 6 0011 3 0001 1 0010 2
// There is a special case 1111, since 15 can't be used.
b_tetros = [
b_tetros = [
[66, 66, 66, 66],
[27, 131, 72, 232],
[36, 231, 36, 231],
@ -53,80 +52,84 @@ const (
[1111, 9, 1111, 9],
]
// Each tetro has its unique color
colors = [
gx.rgb(0, 0, 0), // unused ?
gx.rgb(255, 242, 0), // yellow quad
gx.rgb(174, 0, 255), // purple triple
gx.rgb(60, 255, 0), // green short topright
gx.rgb(255, 0, 0), // red short topleft
gx.rgb(255, 180, 31), // orange long topleft
gx.rgb(33, 66, 255), // blue long topright
gx.rgb(74, 198, 255), // lightblue longest
gx.rgb(0, 170, 170), // unused ?
colors = [
gx.rgb(0, 0, 0), // unused ?
gx.rgb(255, 242, 0), // yellow quad
gx.rgb(174, 0, 255), // purple triple
gx.rgb(60, 255, 0), // green short topright
gx.rgb(255, 0, 0), // red short topleft
gx.rgb(255, 180, 31), // orange long topleft
gx.rgb(33, 66, 255), // blue long topright
gx.rgb(74, 198, 255), // lightblue longest
gx.rgb(0, 170, 170), // unused ?
]
background_color = gx.white
ui_color = gx.rgba(255,0,0, 210)
ui_color = gx.rgba(255, 0, 0, 210)
)
// TODO: type Tetro [tetro_size]struct{ x, y int }
struct Block {
mut:
mut:
x int
y int
}
enum GameState {
paused running gameover
paused
running
gameover
}
struct Game {
mut:
mut:
// Score of the current game
score int
score int
// Lines of the current game
lines int
lines int
// State of the current game
state GameState
state GameState
// Position of the current tetro
pos_x int
pos_y int
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
field [][]int
// TODO: tetro Tetro
tetro []Block
tetro []Block
// TODO: tetros_cache []Tetro
tetros_cache []Block
tetros_cache []Block
// Index of the current tetro. Refers to its color.
tetro_idx int
tetro_idx int
// Idem for the next tetro
next_tetro_idx int
next_tetro_idx int
// Index of the rotation (0-3)
rotation_idx int
rotation_idx int
// gg context for drawing
gg &gg.Context = voidptr(0)
font_loaded bool
show_ghost bool
gg &gg.Context = voidptr(0)
font_loaded bool
show_ghost bool
// frame/time counters:
frame int
frame_old int
frame_sw time.StopWatch = time.new_stopwatch({})
second_sw time.StopWatch = time.new_stopwatch({})
frame int
frame_old int
frame_sw time.StopWatch = time.new_stopwatch({})
second_sw time.StopWatch = time.new_stopwatch({})
}
const ( fpath = os.resource_abs_path('../assets/fonts/RobotoMono-Regular.ttf') )
const (
fpath = os.resource_abs_path('../assets/fonts/RobotoMono-Regular.ttf')
)
[if showfps]
fn (mut game Game) showfps() {
game.frame++
last_frame_ms := f64(game.frame_sw.elapsed().microseconds())/1000.0
ticks := f64(game.second_sw.elapsed().microseconds())/1000.0
last_frame_ms := f64(game.frame_sw.elapsed().microseconds()) / 1000.0
ticks := f64(game.second_sw.elapsed().microseconds()) / 1000.0
if ticks > 999.0 {
fps := f64(game.frame - game.frame_old)*ticks/1000.0
fps := f64(game.frame - game.frame_old) * ticks / 1000.0
$if debug {
eprintln('fps: ${fps:5.1f} | last frame took: ${last_frame_ms:6.3f}ms | frame: ${game.frame:6} ')
}
@ -143,25 +146,22 @@ fn frame(mut game Game) {
game.gg.end()
}
fn main() {
mut game := &Game{
gg: 0
}
game.gg = gg.new_context(
game.gg = gg.new_context({
bg_color: gx.white
width: win_width
height: win_height
use_ortho: true // This is needed for 2D drawing
create_window: true
window_title: 'V Tetris'
//
window_title: 'V Tetris' //
user_data: game
frame_fn: frame
event_fn: on_event
font_path: fpath
//wait_events: true
)
font_path: fpath // wait_events: true
})
game.init_game()
go game.run() // Run the game loop in a new thread
game.gg.run() // Run the render loop in the main thread
@ -169,21 +169,21 @@ fn main() {
fn (mut g Game) init_game() {
g.parse_tetros()
g.next_tetro_idx = rand.intn(b_tetros.len) // generate initial "next"
g.next_tetro_idx = rand.intn(b_tetros.len) // generate initial "next"
g.generate_tetro()
g.field = []
// Generate the field, fill it with 0's, add -1's on each edge
for _ in 0..field_height + 2 {
for _ in 0 .. field_height + 2 {
mut row := [0].repeat(field_width + 2)
row[0] = - 1
row[field_width + 1] = - 1
row[0] = -1
row[field_width + 1] = -1
g.field << row
}
mut first_row := g.field[0]
mut last_row := g.field[field_height + 1]
for j in 0..field_width + 2 {
first_row[j] = - 1
last_row[j] = - 1
for j in 0 .. field_width + 2 {
first_row[j] = -1
last_row[j] = -1
}
g.score = 0
g.lines = 0
@ -206,7 +206,7 @@ fn (mut g Game) run() {
g.move_tetro()
g.delete_completed_lines()
}
//glfw.post_empty_event() // force window redraw
// glfw.post_empty_event() // force window redraw
time.sleep_ms(timer_period)
}
}
@ -214,7 +214,7 @@ fn (mut g Game) run() {
fn (g &Game) draw_ghost() {
if g.state != .gameover && g.show_ghost {
pos_y := g.move_ghost()
for i in 0..tetro_size {
for i in 0 .. tetro_size {
tetro := g.tetro[i]
g.draw_block_color(pos_y + tetro.y, g.pos_x + tetro.x, gx.gray)
}
@ -262,7 +262,7 @@ fn (mut g Game) move_tetro() bool {
fn (mut g Game) move_right(dx int) bool {
// Reached left/right edge or another tetro?
for i in 0..tetro_size {
for i in 0 .. tetro_size {
tetro := g.tetro[i]
y := tetro.y + g.pos_y
x := tetro.x + g.pos_x + dx
@ -314,12 +314,12 @@ fn (mut g Game) generate_tetro() {
// Get the right tetro from cache
fn (mut g Game) get_tetro() {
idx := g.tetro_idx * tetro_size * tetro_size + g.rotation_idx * tetro_size
g.tetro = g.tetros_cache[idx..idx+tetro_size]
g.tetro = g.tetros_cache[idx..idx + tetro_size]
}
// TODO mut
fn (mut g Game) drop_tetro() {
for i in 0..tetro_size{
for i in 0 .. tetro_size {
tetro := g.tetro[i]
x := tetro.x + g.pos_x
y := tetro.y + g.pos_y
@ -329,7 +329,7 @@ fn (mut g Game) drop_tetro() {
}
fn (g &Game) draw_tetro() {
for i in 0..tetro_size {
for i in 0 .. tetro_size {
tetro := g.tetro[i]
g.draw_block(g.pos_y + tetro.y, g.pos_x + tetro.x, g.tetro_idx + 1)
}
@ -338,25 +338,26 @@ fn (g &Game) draw_tetro() {
fn (g &Game) draw_next_tetro() {
if g.state != .gameover {
idx := g.next_tetro_idx * tetro_size * tetro_size
next_tetro := g.tetros_cache[idx..idx+tetro_size]
next_tetro := g.tetros_cache[idx..idx + tetro_size]
pos_y := 0
pos_x := field_width / 2 - tetro_size / 2
for i in 0..tetro_size {
for i in 0 .. tetro_size {
block := next_tetro[i]
g.draw_block_color(pos_y + block.y, pos_x + block.x, gx.rgb(220, 220, 220))
}
}
}
fn (g &Game) draw_block_color(i, j int, color gx.Color) {
g.gg.draw_rect(f32((j - 1) * block_size), f32((i - 1) * block_size),
f32(block_size - 1), f32(block_size - 1), color)
fn (g &Game) draw_block_color(i int, j int, color gx.Color) {
g.gg.draw_rect(f32((j - 1) * block_size), f32((i - 1) * block_size), f32(block_size - 1),
f32(block_size - 1), color)
}
fn (g &Game) draw_block(i, j, color_idx int) {
fn (g &Game) draw_block(i int, j int, color_idx int) {
color := if g.state == .gameover { gx.gray } else { colors[color_idx] }
g.draw_block_color(i, j, color)
}
fn (g &Game) draw_field() {
for i := 1; i < field_height + 1; i++ {
for j := 1; j < field_width + 1; j++ {
@ -373,17 +374,15 @@ fn (mut g Game) draw_ui() {
lines := g.lines.str()
g.gg.draw_text(win_width - lines.len * text_size, 3, lines, text_cfg)
if g.state == .gameover {
g.gg.draw_rect(0, win_height / 2 - text_size, win_width,
5 * text_size, ui_color)
g.gg.draw_rect(0, win_height / 2 - text_size, win_width, 5 * text_size, ui_color)
g.gg.draw_text(1, win_height / 2 + 0 * text_size, 'Game Over', over_cfg)
g.gg.draw_text(1, win_height / 2 + 2 * text_size, 'Space to restart', over_cfg)
} else if g.state == .paused {
g.gg.draw_rect(0, win_height / 2 - text_size, win_width,
5 * text_size, ui_color)
g.gg.draw_rect(0, win_height / 2 - text_size, win_width, 5 * text_size, ui_color)
g.gg.draw_text(1, win_height / 2 + 0 * text_size, 'Game Paused', text_cfg)
g.gg.draw_text(1, win_height / 2 + 2 * text_size, 'SPACE to resume', text_cfg)
}
//g.gg.draw_rect(0, block_size, win_width, limit_thickness, ui_color)
// g.gg.draw_rect(0, block_size, win_width, limit_thickness, ui_color)
}
fn (mut g Game) draw_scene() {
@ -398,8 +397,8 @@ fn parse_binary_tetro(t_ int) []Block {
mut t := t_
mut res := [Block{}].repeat(4)
mut cnt := 0
horizontal := t == 9// special case for the horizontal line
ten_powers := [1000,100,10,1]
horizontal := t == 9 // special case for the horizontal line
ten_powers := [1000, 100, 10, 1]
for i := 0; i <= 3; i++ {
// Get ith digit of t
p := ten_powers[i]
@ -420,7 +419,7 @@ fn parse_binary_tetro(t_ int) []Block {
}
fn on_event(e &sapp.Event, mut game Game) {
//println('code=$e.char_code')
// println('code=$e.char_code')
if e.typ == .key_down {
game.key_down(e.key_code)
}
@ -444,7 +443,6 @@ fn (mut game Game) key_down(key sapp.KeyCode) {
}
else {}
}
if game.state != .running {
return
}
@ -463,7 +461,7 @@ fn (mut game Game) key_down(key sapp.KeyCode) {
game.get_tetro()
}
if game.pos_x < 0 {
//game.pos_x = 1
// game.pos_x = 1
}
}
.left {
@ -476,11 +474,12 @@ fn (mut game Game) key_down(key sapp.KeyCode) {
game.move_tetro() // drop faster when the player presses <down>
}
.d {
for game.move_tetro() {}
for game.move_tetro() {
}
}
.g {
game.show_ghost = !game.show_ghost
}
else { }
else {}
}
}