examples: tetris: draw score

pull/1532/head
Nicolas Sauzede 2019-08-09 07:28:37 +02:00 committed by Alexander Medvednikov
parent 796b9dab74
commit efa540e883
2 changed files with 52 additions and 20 deletions

View File

@ -9,6 +9,7 @@ import gl
import gg import gg
import glfw import glfw
import math import math
import freetype
const ( const (
BlockSize = 20 // pixels BlockSize = 20 // pixels
@ -80,6 +81,8 @@ struct Block {
struct Game { struct Game {
mut: mut:
// Score of the current game
score int
// Position of the current tetro // Position of the current tetro
pos_x int pos_x int
pos_y int pos_y int
@ -100,6 +103,10 @@ struct Game {
rotation_idx int rotation_idx int
// gg context for drawing // gg context for drawing
gg *gg.GG gg *gg.GG
// ft context for font drawing
ft *freetype.Context
// gx text config for font drawing
tcfg *gx.TextCfg
} }
fn main() { fn main() {
@ -113,12 +120,29 @@ fn main() {
window_title: 'V Tetris' window_title: 'V Tetris'
window_user_ptr: game window_user_ptr: game
}) })
ft: 0
tcfg: 0
} }
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()
game.gg.window.onkeydown(key_down) game.gg.window.onkeydown(key_down)
go game.run() // Run the game loop in a new thread go game.run() // Run the game loop in a new thread
gg.clear(gx.White) gg.clear(gx.White)
// Try to load font
game.ft = freetype.new_context(gg.Cfg{
width: WinWidth
height: WinHeight
use_ortho: true
font_size: 18
}, 1)
if game.ft != 0 {
// if font loaded, define default font color etc..
game.tcfg = &gx.TextCfg{
align:gx.ALIGN_LEFT
size:12
color:gx.rgb(0, 0, 170)
}
}
for { for {
gg.clear(gx.White) gg.clear(gx.White)
game.draw_scene() game.draw_scene()
@ -148,6 +172,7 @@ fn (g mut Game) init_game() {
first_row[j] = - 1 first_row[j] = - 1
last_row[j] = - 1 last_row[j] = - 1
} }
g.score = 0
} }
fn (g mut Game) parse_tetros() { fn (g mut Game) parse_tetros() {
@ -222,6 +247,7 @@ fn (g mut Game) delete_completed_line(y int) {
return return
} }
} }
g.score += 10
// Move everything down by 1 position // Move everything down by 1 position
for yy := y - 1; yy >= 1; yy-- { for yy := y - 1; yy >= 1; yy-- {
for x := 1; x <= FieldWidth; x++ { for x := 1; x <= FieldWidth; x++ {
@ -282,9 +308,16 @@ fn (g &Game) draw_field() {
} }
} }
fn (g &Game) draw_score() {
if g.tcfg != 0 {
g.ft.draw_text(1, 2, 'score: ' + g.score.str(), g.tcfg)
}
}
fn (g &Game) draw_scene() { fn (g &Game) draw_scene() {
g.draw_tetro() g.draw_tetro()
g.draw_field() g.draw_field()
g.draw_score()
} }
fn parse_binary_tetro(t_ int) []Block { fn parse_binary_tetro(t_ int) []Block {
@ -348,4 +381,3 @@ fn key_down(wnd voidptr, key, code, action, mods int) {
game.move_tetro() // drop faster when the player presses <down> game.move_tetro() // drop faster when the player presses <down>
} }
} }

View File

@ -160,7 +160,7 @@ pub fn new_context(cfg gg.Cfg, scale int) *Context {
} }
if !os.file_exists(font_path) { if !os.file_exists(font_path) {
println('failed to load RobotoMono-Regular.ttf') println('failed to load RobotoMono-Regular.ttf')
exit(1) return 0
} }
# FT_Face face; # FT_Face face;
# if (FT_New_Face(ft, font_path.str, 0, &face)) # if (FT_New_Face(ft, font_path.str, 0, &face))