From c4a6ba76a15d250109253f3eb636fce814fe4a58 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Thu, 4 Jun 2020 16:53:19 +0200 Subject: [PATCH] examples: fix bounce.v; clean up tetris.v --- examples/hot_reload/bounce.v | 55 +++++++++--------------------------- examples/tetris/tetris.v | 20 ++++--------- 2 files changed, 19 insertions(+), 56 deletions(-) diff --git a/examples/hot_reload/bounce.v b/examples/hot_reload/bounce.v index 2f8d6cd643..957eefe39f 100644 --- a/examples/hot_reload/bounce.v +++ b/examples/hot_reload/bounce.v @@ -3,9 +3,7 @@ module main import gx -import gl import gg -import glfw import time struct Game { @@ -17,7 +15,6 @@ mut: dx int height int width int - main_wnd &glfw.Window draw_fn voidptr } @@ -25,64 +22,44 @@ const ( window_width = 400 window_height = 300 width = 50 - red = gx.rgb(255, 0, 0) - green = gx.rgb(0, 255, 0) - blue = gx.rgb(0, 0, 255) - black = gx.rgb(0, 0, 0) ) fn main() { - glfw.init_glfw() mut game := &Game{ gg: 0 dx: 2 dy: 2 height: window_height width: window_width - main_wnd: 0 draw_fn: 0 } - window := glfw.create_window(glfw.WinCfg{ - width: window_width - height: window_height - borderless: false - title: 'Hot code reloading demo' - ptr: game - always_on_top: true - }) - // window.onkeydown(key_down) - game.main_wnd = window - window.make_context_current() - gg.init_gg() - game.gg = gg.new_context(gg.Cfg{ + game.gg = gg.new_context({ width: window_width height: window_height font_size: 20 use_ortho: true - window_user_ptr: 0 + user_data: game + window_title: 'Hot code reloading demo' + create_window: true + frame_fn: frame + bg_color: gx.white }) + // window.onkeydown(key_down) println('Starting the game loop...') go game.run() - for { - if window.should_close() { - break - } - gl.clear() - gl.clear_color(255, 255, 255, 255) - game.draw() - window.swap_buffers() - glfw.wait_events() - } + game.gg.run() } // Try uncommenting or changing the lines inside the live functions. // Guess what will happen: [live] -fn (game &Game) draw() { - game.gg.draw_rect(game.x, game.y, width, width, blue) +fn frame (mut game Game) { + game.gg.begin() + game.gg.draw_rect(game.x, game.y, width, width, gx.blue) game.gg.draw_rect(window_width - width - game.x + 10, 200 - game.y + width, width, width, gx.rgb(228, 10, 55)) game.gg.draw_rect(game.x - 25, 250 - game.y, width, width, gx.rgb(28, 240, 55)) + game.gg.end() } [live] @@ -101,16 +78,10 @@ fn (mut game Game) update_model() { fn (mut game Game) run() { for { game.update_model() - glfw.post_empty_event() // Refresh + //glfw.post_empty_event() // Refresh time.sleep_ms(17) // 60fps } } - - - - - - diff --git a/examples/tetris/tetris.v b/examples/tetris/tetris.v index 8724377508..36f1b067bb 100644 --- a/examples/tetris/tetris.v +++ b/examples/tetris/tetris.v @@ -195,7 +195,7 @@ fn (mut g Game) init_game() { g.parse_tetros() rand.seed(int(time.now().unix)) g.generate_tetro() - g.field = [] // TODO: g.field = [][]int + g.field = [] // Generate the field, fill it with 0's, add -1's on each edge for _ in 0..field_height + 2 { mut row := [0].repeat(field_width + 2) @@ -240,10 +240,7 @@ fn (mut g Game) move_tetro() bool { 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 { + if g.field[y][x] != 0 { // The new tetro has no space to drop => end of the game if g.pos_y < 2 { g.state = .gameover @@ -321,9 +318,7 @@ fn (g &Game) drop_tetro() { 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 + g.field[y][x] = g.tetro_idx + 1 } } @@ -377,7 +372,7 @@ fn (mut g Game) draw_scene() { fn parse_binary_tetro(t_ int) []Block { mut t := t_ - res := [Block{}].repeat(4) + mut res := [Block{}].repeat(4) mut cnt := 0 horizontal := t == 9// special case for the horizontal line ten_powers := [1000,100,10,1] @@ -391,11 +386,8 @@ fn parse_binary_tetro(t_ int) []Block { bin := digit % 2 digit /= 2 if bin == 1 || (horizontal && i == tetro_size - 1) { - // TODO: res[cnt].x = j - // res[cnt].y = i - mut point := &res[cnt] - point.x = j - point.y = i + res[cnt].x = j + res[cnt].y = i cnt++ } }