1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

tetris: restore text rendering with gg2.ft.draw_text

This commit is contained in:
Delyan Angelov
2020-06-03 23:08:55 +03:00
parent e91642e615
commit 576e80b3a9
3 changed files with 84 additions and 55 deletions

View File

@ -4,10 +4,12 @@
module main
import os
import rand
import time
import gx
import gg2 as gg
import gg2.ft
import sokol.sapp
const (
@ -124,10 +126,9 @@ struct Game {
// Index of the rotation (0-3)
rotation_idx int
// gg context for drawing
gg &gg.GG
gg &gg.GG = voidptr(0)
// ft context for font drawing
//ft &freetype.FreeType
//ft &ft.FT
ft &ft.FT = voidptr(0)
font_loaded bool
// frame/time counters:
frame int
@ -136,6 +137,13 @@ struct Game {
second_sw time.StopWatch = time.new_stopwatch({})
}
const ( fpath = os.resource_abs_path('../assets/fonts/RobotoMono-Regular.ttf') )
fn init_gui(mut game Game){
x := ft.new({ font_path: fpath }) or {panic(err)}
game.ft = x
game.font_loaded = true
}
[if showfps]
fn (game &Game) showfps() {
game.frame++
@ -151,27 +159,18 @@ fn (game &Game) showfps() {
fn frame(game &Game) {
game.frame_sw.restart()
//C.sfons_flush(game.ft.fons)
game.ft.flush()
game.gg.begin()
game.draw_scene()
game.showfps()
game.gg.end()
}
fn main() {
// TODO
/*
f := ft.new(
//font_path: os.resource_abs_path('../assets/fonts/RobotoMono-Regular.ttf')
font_path: ('../assets/fonts/RobotoMono-Regular.ttf')
) or {
println('failed to loat the font')
return
}
*/
mut game := &Game{
gg: 0
//ft: f
ft: 0
}
game.gg = gg.new_context(
bg_color: gx.white
@ -180,9 +179,10 @@ fn main() {
use_ortho: true // This is needed for 2D drawing
create_window: true
window_title: 'V Tetris'
frame_fn: frame
//
user_data: game
//on_key_down: key_down
init_fn: init_gui
frame_fn: frame
event_cb: on_event
)
game.init_game()
@ -352,17 +352,17 @@ fn (g &Game) draw_field() {
fn (mut g Game) draw_ui() {
if g.font_loaded {
//g.ft.draw_text(1, 3, g.score.str(), text_cfg)
g.ft.draw_text(1, 3, g.score.str(), text_cfg)
if g.state == .gameover {
g.gg.draw_rect(0, win_height / 2 - text_size, win_width,
5 * text_size, ui_color)
//g.ft.draw_text(1, win_height / 2 + 0 * text_size, 'Game Over', over_cfg)
//g.ft.draw_text(1, win_height / 2 + 2 * text_size, 'Space to restart', over_cfg)
g.ft.draw_text(1, win_height / 2 + 0 * text_size, 'Game Over', over_cfg)
g.ft.draw_text(1, win_height / 2 + 2 * text_size, 'Space to restart', over_cfg)
} else if g.state == .paused {
g.gg.draw_rect(0, win_height / 2 - text_size, win_width,
5 * text_size, ui_color)
//g.ft.draw_text(1, win_height / 2 + 0 * text_size, 'Game Paused', text_cfg)
//g.ft.draw_text(1, win_height / 2 + 2 * text_size, 'SPACE to resume', text_cfg)
g.ft.draw_text(1, win_height / 2 + 0 * text_size, 'Game Paused', text_cfg)
g.ft.draw_text(1, win_height / 2 + 2 * text_size, 'SPACE to resume', text_cfg)
}
}
//g.gg.draw_rect(0, block_size, win_width, limit_thickness, ui_color)