gg: simplify text rendering

pull/5710/head
Alexander Medvednikov 2020-07-06 19:45:00 +02:00
parent cfa8e0a81a
commit 1f3d7d393e
3 changed files with 48 additions and 34 deletions

View File

@ -9,7 +9,6 @@ import rand
import time
import gx
import gg
import gg.ft
import sokol.sapp
const (
@ -127,8 +126,6 @@ struct Game {
rotation_idx int
// gg context for drawing
gg &gg.Context = voidptr(0)
// ft context for font drawing
ft &ft.FT = voidptr(0)
font_loaded bool
// frame/time counters:
frame int
@ -138,11 +135,6 @@ struct Game {
}
const ( fpath = os.resource_abs_path('../assets/fonts/RobotoMono-Regular.ttf') )
fn init_gui(mut game Game){
x := ft.new({ font_path: fpath, scale: sapp.dpi_scale() }) or {panic(err)}
game.ft = x
game.font_loaded = true
}
[if showfps]
fn (game &Game) showfps() {
@ -159,7 +151,6 @@ fn (game &Game) showfps() {
fn frame(game &Game) {
game.frame_sw.restart()
game.ft.flush()
game.gg.begin()
game.draw_scene()
game.showfps()
@ -170,7 +161,6 @@ fn frame(game &Game) {
fn main() {
mut game := &Game{
gg: 0
ft: 0
}
game.gg = gg.new_context(
bg_color: gx.white
@ -181,9 +171,9 @@ fn main() {
window_title: 'V Tetris'
//
user_data: game
init_fn: init_gui
frame_fn: frame
event_fn: on_event
font_path: fpath
//wait_events: true
)
game.init_game()
@ -346,19 +336,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)
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)
} 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.gg.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.gg.draw_text(1, win_height / 2 + 0 * text_size, 'Game Over', over_cfg)
g.gg.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.gg.draw_text(1, win_height / 2 + 0 * text_size, 'Game Paused', text_cfg)
g.gg.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)
}

View File

@ -9,7 +9,7 @@ import sokol
import sokol.sapp
import sokol.sgl
import sokol.gfx
//import gg.ft
import gg.ft
pub type FNCb fn(x voidptr)
pub type FNEvent fn(e voidptr, x voidptr)
@ -40,12 +40,14 @@ pub:
keydown_fn FNKeyDown = voidptr(0) // special case of event_fn
char_fn FNChar = voidptr(0) // special case of event_fn
wait_events bool // set this to true for UIs, to save power
font_path string
fullscreen bool
scale f32 = 1.0 // vid needs this
//init_text bool
font_path string
}
pub struct Context {
render_text bool
pub mut:
scale f32 = 1.0 // will get set to 2.0 for retina, will remain 1.0 for normal
width int
@ -53,6 +55,8 @@ pub mut:
clear_pass C.sg_pass_action
window C.sapp_desc
config Config
ft &ft.FT
font_inited bool
}
pub struct Size { pub: width int height int }
@ -77,19 +81,24 @@ fn gg_init_sokol_window(user_data voidptr) {
if g.scale < 0.1 {
g.scale = 1.0
}
is_high_dpi := sapp.high_dpi()
fb_w := sapp.width()
fb_h := sapp.height()
println('g.scale=$g.scale is_high_dpi=$is_high_dpi fb_w=$fb_w fb_h=$fb_h')
//is_high_dpi := sapp.high_dpi()
//fb_w := sapp.width()
//fb_h := sapp.height()
//println('g.scale=$g.scale is_high_dpi=$is_high_dpi fb_w=$fb_w fb_h=$fb_h')
//if g.config.init_text {
if g.config.font_path != '' {
g.ft = ft.new({ font_path: g.config.font_path, scale: sapp.dpi_scale() }) or {panic(err)}
g.font_inited = true
}
if g.config.init_fn != voidptr(0) {
g.config.init_fn( g.config.user_data )
g.config.init_fn(g.config.user_data)
}
}
fn gg_frame_fn(user_data voidptr) {
mut g := &Context(user_data)
if g.config.frame_fn != voidptr(0) {
g.config.frame_fn( g.config.user_data )
g.config.frame_fn(g.config.user_data)
}
}
@ -149,6 +158,8 @@ pub fn new_context(cfg Config) &Context{
clear_pass: gfx.create_clear_pass( f32(cfg.bg_color.r) / 255.0, f32(cfg.bg_color.g) / 255.0,
f32(cfg.bg_color.b) / 255.0, 1.0)
config: cfg
render_text: cfg.font_path != ''
ft: 0
}
//C.printf('new_context() %p\n', cfg.user_data)
@ -225,6 +236,9 @@ pub fn create_image_from_memory(buf byteptr) u32 {
}
pub fn (gg &Context) begin() {
if gg.render_text && gg.font_inited {
gg.ft.flush()
}
sgl.defaults()
sgl.matrix_mode_projection()
sgl.ortho(0.0, f32(sapp.width()), f32(sapp.height()), 0.0, -1.0, 1.0)
@ -253,10 +267,22 @@ pub fn (ctx &Context) draw_image(x, y, width, height f32, image u32) {
}
pub fn (ctx &Context) draw_rounded_rect(x, y, width, height, radius f32, color gx.Color) {
}
}
pub fn (ctx &Context) draw_empty_rounded_rect(x, y, width, height, radius f32, border_color gx.Color) {
}
pub fn (ctx &Context) draw_text(x, y int, text string, cfg gx.TextCfg) {
if !ctx.font_inited {
return
}
ctx.ft.draw_text(x, y, text, cfg)
}
pub fn (ctx &Context) draw_text_def(x, y int, text string) {
ctx.ft.draw_text_def(x, y, text)
}
fn C.WaitMessage()

View File

@ -885,7 +885,7 @@ pub fn (mut c Checker) call_method(mut call_expr ast.CallExpr) table.Type {
c.error('too few arguments in call to `${left_type_sym.name}.$method_name` ($call_expr.args.len instead of $min_required_args)',
call_expr.pos)
} else if !method.is_variadic && call_expr.args.len > nr_args {
c.error('!too many arguments in call to `${left_type_sym.name}.$method_name` ($call_expr.args.len instead of $nr_args)',
c.error('too many arguments in call to `${left_type_sym.name}.$method_name` ($call_expr.args.len instead of $nr_args)',
call_expr.pos)
return method.return_type
}