tetris: it now works with v2 (on linux) :-)

pull/4440/head
Delyan Angelov 2020-04-16 13:30:33 +03:00
parent 19723c927b
commit 1318c27699
3 changed files with 52 additions and 40 deletions

View File

@ -12,6 +12,15 @@ import glfw
import math import math
import freetype 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 ( const (
BlockSize = 20 // pixels BlockSize = 20 // pixels
FieldHeight = 20 // # of blocks FieldHeight = 20 // # of blocks
@ -134,16 +143,17 @@ struct Game {
fn main() { fn main() {
glfw.init_glfw() glfw.init_glfw()
mut game := &Game{
gg: gg.new_context(gg.Cfg { gconfig := gg.Cfg {
width: WinWidth width: WinWidth
height: WinHeight height: WinHeight
use_ortho: true // This is needed for 2D drawing use_ortho: true // This is needed for 2D drawing
create_window: true create_window: true
window_title: 'V Tetris' window_title: 'V Tetris'
//window_user_ptr: game //window_user_ptr: game
}) }
ft: freetype.new_context(gg.Cfg{
fconfig := gg.Cfg{
width: WinWidth width: WinWidth
height: WinHeight height: WinHeight
use_ortho: true use_ortho: true
@ -151,7 +161,10 @@ fn main() {
font_size: 18 font_size: 18
scale: 2 scale: 2
window_user_ptr: 0 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.gg.window.set_user_ptr(game) // TODO remove this when `window_user_ptr:` works
game.init_game() 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)) mut game := &Game(glfw.get_window_user_pointer(wnd))
// global keys // global keys
match key { match key {
glfw.KEY_ESCAPE { k_escape {
eprintln('should close')
glfw.set_should_close(wnd, true) glfw.set_should_close(wnd, true)
} }
glfw.key_space { k_space {
if game.state == .running { if game.state == .running {
game.state = .paused game.state = .paused
} else if game.state == .paused { } else if game.state == .paused {
@ -410,7 +424,7 @@ fn key_down(wnd voidptr, key, code, action, mods int) {
} }
// keys while game is running // keys while game is running
match key { match key {
glfw.KeyUp { k_up {
// Rotate the tetro // Rotate the tetro
old_rotation_idx := game.rotation_idx old_rotation_idx := game.rotation_idx
game.rotation_idx++ game.rotation_idx++
@ -426,13 +440,13 @@ fn key_down(wnd voidptr, key, code, action, mods int) {
//game.pos_x = 1 //game.pos_x = 1
} }
} }
glfw.KeyLeft { k_left {
game.move_right(-1) game.move_right(-1)
} }
glfw.KeyRight { k_right {
game.move_right(1) game.move_right(1)
} }
glfw.KeyDown { k_down {
game.move_tetro() // drop faster when the player presses <down> game.move_tetro() // drop faster when the player presses <down>
} }
else { } else { }

View File

@ -55,6 +55,7 @@ pub:
scale int scale int
} }
pub type RenderFn fn()
pub struct GG { pub struct GG {
shader gl.Shader shader gl.Shader
// use_ortho bool // use_ortho bool
@ -70,7 +71,7 @@ pub mut:
width int width int
height int height int
window &glfw.Window window &glfw.Window
render_fn fn() render_fn RenderFn
} }

View File

@ -39,16 +39,14 @@ pub const (
) )
pub const ( pub const (
KEY_ESCAPE = 256 key_escape = 256
key_space = 32 key_space = 32
KEY_LEFT_SUPER = 343 key_left_super = 343
)
pub const ( key_up = 265
KeyUp = 265 key_left = 263
KeyLeft = 263 key_right = 262
KeyRight = 262 key_down = 264
KeyDown = 264
) )
fn C.glfwGetWindowUserPointer() voidptr fn C.glfwGetWindowUserPointer() voidptr
@ -373,4 +371,3 @@ pub fn (size Size) str() string {
pub fn get_window_user_pointer(gwnd voidptr) voidptr { pub fn get_window_user_pointer(gwnd voidptr) voidptr {
return C.glfwGetWindowUserPointer(gwnd) return C.glfwGetWindowUserPointer(gwnd)
} }