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 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 <down>
}
else { }
k_right {
game.move_right(1)
}
k_down {
game.move_tetro() // drop faster when the player presses <down>
}
else { }
}
}

View File

@ -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
}

View File

@ -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)
}