gg: simplify text rendering
parent
cfa8e0a81a
commit
1f3d7d393e
|
@ -9,7 +9,6 @@ import rand
|
||||||
import time
|
import time
|
||||||
import gx
|
import gx
|
||||||
import gg
|
import gg
|
||||||
import gg.ft
|
|
||||||
import sokol.sapp
|
import sokol.sapp
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -127,8 +126,6 @@ struct Game {
|
||||||
rotation_idx int
|
rotation_idx int
|
||||||
// gg context for drawing
|
// gg context for drawing
|
||||||
gg &gg.Context = voidptr(0)
|
gg &gg.Context = voidptr(0)
|
||||||
// ft context for font drawing
|
|
||||||
ft &ft.FT = voidptr(0)
|
|
||||||
font_loaded bool
|
font_loaded bool
|
||||||
// frame/time counters:
|
// frame/time counters:
|
||||||
frame int
|
frame int
|
||||||
|
@ -138,11 +135,6 @@ struct Game {
|
||||||
}
|
}
|
||||||
|
|
||||||
const ( fpath = os.resource_abs_path('../assets/fonts/RobotoMono-Regular.ttf') )
|
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]
|
[if showfps]
|
||||||
fn (game &Game) showfps() {
|
fn (game &Game) showfps() {
|
||||||
|
@ -159,7 +151,6 @@ fn (game &Game) showfps() {
|
||||||
|
|
||||||
fn frame(game &Game) {
|
fn frame(game &Game) {
|
||||||
game.frame_sw.restart()
|
game.frame_sw.restart()
|
||||||
game.ft.flush()
|
|
||||||
game.gg.begin()
|
game.gg.begin()
|
||||||
game.draw_scene()
|
game.draw_scene()
|
||||||
game.showfps()
|
game.showfps()
|
||||||
|
@ -170,7 +161,6 @@ fn frame(game &Game) {
|
||||||
fn main() {
|
fn main() {
|
||||||
mut game := &Game{
|
mut game := &Game{
|
||||||
gg: 0
|
gg: 0
|
||||||
ft: 0
|
|
||||||
}
|
}
|
||||||
game.gg = gg.new_context(
|
game.gg = gg.new_context(
|
||||||
bg_color: gx.white
|
bg_color: gx.white
|
||||||
|
@ -181,9 +171,9 @@ fn main() {
|
||||||
window_title: 'V Tetris'
|
window_title: 'V Tetris'
|
||||||
//
|
//
|
||||||
user_data: game
|
user_data: game
|
||||||
init_fn: init_gui
|
|
||||||
frame_fn: frame
|
frame_fn: frame
|
||||||
event_fn: on_event
|
event_fn: on_event
|
||||||
|
font_path: fpath
|
||||||
//wait_events: true
|
//wait_events: true
|
||||||
)
|
)
|
||||||
game.init_game()
|
game.init_game()
|
||||||
|
@ -346,19 +336,17 @@ fn (g &Game) draw_field() {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (mut g Game) draw_ui() {
|
fn (mut g Game) draw_ui() {
|
||||||
if g.font_loaded {
|
g.gg.draw_text(1, 3, g.score.str(), text_cfg)
|
||||||
g.ft.draw_text(1, 3, g.score.str(), text_cfg)
|
|
||||||
if g.state == .gameover {
|
if g.state == .gameover {
|
||||||
g.gg.draw_rect(0, win_height / 2 - text_size, win_width,
|
g.gg.draw_rect(0, win_height / 2 - text_size, win_width,
|
||||||
5 * text_size, ui_color)
|
5 * text_size, ui_color)
|
||||||
g.ft.draw_text(1, win_height / 2 + 0 * text_size, 'Game Over', over_cfg)
|
g.gg.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.gg.draw_text(1, win_height / 2 + 2 * text_size, 'Space to restart', over_cfg)
|
||||||
} else if g.state == .paused {
|
} else if g.state == .paused {
|
||||||
g.gg.draw_rect(0, win_height / 2 - text_size, win_width,
|
g.gg.draw_rect(0, win_height / 2 - text_size, win_width,
|
||||||
5 * text_size, ui_color)
|
5 * text_size, ui_color)
|
||||||
g.ft.draw_text(1, win_height / 2 + 0 * text_size, 'Game Paused', text_cfg)
|
g.gg.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, win_height / 2 + 2 * text_size, 'SPACE to resume', text_cfg)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
//g.gg.draw_rect(0, block_size, win_width, limit_thickness, ui_color)
|
//g.gg.draw_rect(0, block_size, win_width, limit_thickness, ui_color)
|
||||||
}
|
}
|
||||||
|
|
44
vlib/gg/gg.v
44
vlib/gg/gg.v
|
@ -9,7 +9,7 @@ import sokol
|
||||||
import sokol.sapp
|
import sokol.sapp
|
||||||
import sokol.sgl
|
import sokol.sgl
|
||||||
import sokol.gfx
|
import sokol.gfx
|
||||||
//import gg.ft
|
import gg.ft
|
||||||
|
|
||||||
pub type FNCb fn(x voidptr)
|
pub type FNCb fn(x voidptr)
|
||||||
pub type FNEvent fn(e voidptr, 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
|
keydown_fn FNKeyDown = voidptr(0) // special case of event_fn
|
||||||
char_fn FNChar = 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
|
wait_events bool // set this to true for UIs, to save power
|
||||||
font_path string
|
|
||||||
fullscreen bool
|
fullscreen bool
|
||||||
scale f32 = 1.0 // vid needs this
|
scale f32 = 1.0 // vid needs this
|
||||||
|
//init_text bool
|
||||||
|
font_path string
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Context {
|
pub struct Context {
|
||||||
|
render_text bool
|
||||||
pub mut:
|
pub mut:
|
||||||
scale f32 = 1.0 // will get set to 2.0 for retina, will remain 1.0 for normal
|
scale f32 = 1.0 // will get set to 2.0 for retina, will remain 1.0 for normal
|
||||||
width int
|
width int
|
||||||
|
@ -53,6 +55,8 @@ pub mut:
|
||||||
clear_pass C.sg_pass_action
|
clear_pass C.sg_pass_action
|
||||||
window C.sapp_desc
|
window C.sapp_desc
|
||||||
config Config
|
config Config
|
||||||
|
ft &ft.FT
|
||||||
|
font_inited bool
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Size { pub: width int height int }
|
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 {
|
if g.scale < 0.1 {
|
||||||
g.scale = 1.0
|
g.scale = 1.0
|
||||||
}
|
}
|
||||||
is_high_dpi := sapp.high_dpi()
|
//is_high_dpi := sapp.high_dpi()
|
||||||
fb_w := sapp.width()
|
//fb_w := sapp.width()
|
||||||
fb_h := sapp.height()
|
//fb_h := sapp.height()
|
||||||
println('g.scale=$g.scale is_high_dpi=$is_high_dpi fb_w=$fb_w fb_h=$fb_h')
|
//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) {
|
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) {
|
fn gg_frame_fn(user_data voidptr) {
|
||||||
mut g := &Context(user_data)
|
mut g := &Context(user_data)
|
||||||
if g.config.frame_fn != voidptr(0) {
|
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,
|
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)
|
f32(cfg.bg_color.b) / 255.0, 1.0)
|
||||||
config: cfg
|
config: cfg
|
||||||
|
render_text: cfg.font_path != ''
|
||||||
|
ft: 0
|
||||||
}
|
}
|
||||||
|
|
||||||
//C.printf('new_context() %p\n', cfg.user_data)
|
//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() {
|
pub fn (gg &Context) begin() {
|
||||||
|
if gg.render_text && gg.font_inited {
|
||||||
|
gg.ft.flush()
|
||||||
|
}
|
||||||
sgl.defaults()
|
sgl.defaults()
|
||||||
sgl.matrix_mode_projection()
|
sgl.matrix_mode_projection()
|
||||||
sgl.ortho(0.0, f32(sapp.width()), f32(sapp.height()), 0.0, -1.0, 1.0)
|
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_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_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()
|
fn C.WaitMessage()
|
||||||
|
|
||||||
|
|
|
@ -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)',
|
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)
|
call_expr.pos)
|
||||||
} else if !method.is_variadic && call_expr.args.len > nr_args {
|
} 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)
|
call_expr.pos)
|
||||||
return method.return_type
|
return method.return_type
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue