From 1318c27699804c2a6e06b0132d8fd4c70faa8753 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Thu, 16 Apr 2020 13:30:33 +0300 Subject: [PATCH] tetris: it now works with v2 (on linux) :-) --- examples/tetris/tetris.v | 74 ++++++++++++++++++++++++---------------- vlib/gg/gg.v | 3 +- vlib/glfw/glfw.v | 15 ++++---- 3 files changed, 52 insertions(+), 40 deletions(-) diff --git a/examples/tetris/tetris.v b/examples/tetris/tetris.v index 1b1366953d..3480cf556d 100644 --- a/examples/tetris/tetris.v +++ b/examples/tetris/tetris.v @@ -12,6 +12,15 @@ import glfw import math import freetype +const ( + k_up = glfw.key_up + k_left = glfw.key_left + k_right = glfw.key_right + k_down = glfw.key_down + k_escape = glfw.key_escape + k_space = glfw.key_space +) + const ( BlockSize = 20 // pixels FieldHeight = 20 // # of blocks @@ -134,16 +143,17 @@ struct Game { fn main() { glfw.init_glfw() - mut game := &Game{ - gg: gg.new_context(gg.Cfg { + + gconfig := gg.Cfg { width: WinWidth height: WinHeight use_ortho: true // This is needed for 2D drawing create_window: true window_title: 'V Tetris' //window_user_ptr: game - }) - ft: freetype.new_context(gg.Cfg{ + } + + fconfig := gg.Cfg{ width: WinWidth height: WinHeight use_ortho: true @@ -151,7 +161,10 @@ fn main() { font_size: 18 scale: 2 window_user_ptr: 0 - }) + } + mut game := &Game{ + gg: gg.new_context(gconfig) + ft: freetype.new_context(fconfig) } game.gg.window.set_user_ptr(game) // TODO remove this when `window_user_ptr:` works game.init_game() @@ -389,10 +402,11 @@ fn key_down(wnd voidptr, key, code, action, mods int) { mut game := &Game(glfw.get_window_user_pointer(wnd)) // global keys match key { - glfw.KEY_ESCAPE { + k_escape { + eprintln('should close') glfw.set_should_close(wnd, true) } - glfw.key_space { + k_space { if game.state == .running { game.state = .paused } else if game.state == .paused { @@ -410,31 +424,31 @@ fn key_down(wnd voidptr, key, code, action, mods int) { } // keys while game is running match key { - glfw.KeyUp { - // Rotate the tetro - old_rotation_idx := game.rotation_idx - game.rotation_idx++ - if game.rotation_idx == tetro_size { - game.rotation_idx = 0 - } - game.get_tetro() - if !game.move_right(0) { - game.rotation_idx = old_rotation_idx + k_up { + // Rotate the tetro + old_rotation_idx := game.rotation_idx + game.rotation_idx++ + if game.rotation_idx == tetro_size { + 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 + } } - if game.pos_x < 0 { - //game.pos_x = 1 + k_left { + game.move_right(-1) } - } - glfw.KeyLeft { - game.move_right(-1) - } - glfw.KeyRight { - game.move_right(1) - } - glfw.KeyDown { - game.move_tetro() // drop faster when the player presses - } - else { } + k_right { + game.move_right(1) + } + k_down { + game.move_tetro() // drop faster when the player presses + } + else { } } } diff --git a/vlib/gg/gg.v b/vlib/gg/gg.v index 6bddad7b6a..e1edb30bd7 100644 --- a/vlib/gg/gg.v +++ b/vlib/gg/gg.v @@ -55,6 +55,7 @@ pub: scale int } +pub type RenderFn fn() pub struct GG { shader gl.Shader // use_ortho bool @@ -70,7 +71,7 @@ pub mut: width int height int window &glfw.Window - render_fn fn() + render_fn RenderFn } diff --git a/vlib/glfw/glfw.v b/vlib/glfw/glfw.v index a90077f53e..d32957694a 100644 --- a/vlib/glfw/glfw.v +++ b/vlib/glfw/glfw.v @@ -39,16 +39,14 @@ pub const ( ) pub const ( - KEY_ESCAPE = 256 + key_escape = 256 key_space = 32 - KEY_LEFT_SUPER = 343 -) + key_left_super = 343 -pub const ( - KeyUp = 265 - KeyLeft = 263 - KeyRight = 262 - KeyDown = 264 + key_up = 265 + key_left = 263 + key_right = 262 + key_down = 264 ) fn C.glfwGetWindowUserPointer() voidptr @@ -373,4 +371,3 @@ pub fn (size Size) str() string { pub fn get_window_user_pointer(gwnd voidptr) voidptr { return C.glfwGetWindowUserPointer(gwnd) } -