gg: move text rendering to gg.ft

pull/5174/head^2
Alexander Medvednikov 2020-06-02 15:35:37 +02:00
parent 653a27005b
commit 1139d3b458
3 changed files with 100 additions and 105 deletions

View File

@ -11,7 +11,8 @@ import gg2 as gg
import sokol import sokol
import sokol.sapp import sokol.sapp
import math import math
import freetype import gg2.ft
import os
const ( const (
block_size = 20 // pixels block_size = 20 // pixels
@ -129,72 +130,47 @@ struct Game {
// gg context for drawing // gg context for drawing
gg &gg.GG gg &gg.GG
// ft context for font drawing // ft context for font drawing
ft &freetype.FreeType //ft &freetype.FreeType
//ft &ft.FT
font_loaded bool font_loaded bool
} }
fn frame(game &Game) { fn frame(game &Game) {
//C.sfons_flush(game.ft.fons)
game.gg.begin() game.gg.begin()
game.draw_scene() game.draw_scene()
game.gg.end() game.gg.end()
} }
fn main() { fn main() {
mut game := &Game{} // 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{
//ft: f
}
game.gg = gg.new_context( game.gg = gg.new_context(
bg_color: gx.white bg_color: gx.white
width: win_width width: win_width
height: win_height height: win_height
use_ortho: true // This is needed for 2D drawing use_ortho: true // This is needed for 2D drawing
create_window: true create_window: true
window_title: 'V tetris' window_title: 'V Tetris'
frame_fn: frame frame_fn: frame
user_data: game user_data: game
//on_key_down: key_down //on_key_down: key_down
event_cb: on_event event_cb: on_event
) )
//font_path: os.resource_abs_path('assets/fonts/RobotoMono-Regular.ttf')
/*
gconfig := gg.Config{
width: win_width
height: win_height
use_ortho: true // This is needed for 2D drawing
create_window: true
window_title: 'V Tetris'
//window_user_ptr: game
}
fconfig := gg.Config{
width: win_width
height: win_height
use_ortho: true
font_path: '../assets/fonts/RobotoMono-Regular.ttf'
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() game.init_game()
//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
game.gg.run() // Run the render loop in the main thread game.gg.run() // Run the render loop in the main thread
/*
game.font_loaded = game.ft != 0
for {
game.draw_scene()
game.gg.render()
if game.gg.window.should_close() {
game.gg.window.destroy()
return
}
}
*/
} }
fn (mut g Game) init_game() { fn (mut g Game) init_game() {
@ -358,17 +334,17 @@ fn (g &Game) draw_field() {
fn (mut g Game) draw_ui() { fn (mut g Game) draw_ui() {
if g.font_loaded { 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 { 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.gg.draw_text(1, win_height / 2 + 0 * text_size, 'Game Over', 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) //g.ft.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.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 + 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)

76
vlib/gg2/ft/ft.v 100644
View File

@ -0,0 +1,76 @@
module ft
import sokol.sfons
import gx
import os
const (
default_font_size = 24
)
// TODO remove globals
/*
__global g_fons &C.FONScontext
__global g_font_normal int
__global g_font_path string
*/
pub struct FT {
pub:
fons &C.FONScontext
font_normal int
}
pub struct Config {
font_path string
}
pub fn new(c Config) ?&FT{
if c.font_path == '' || !os.exists(c.font_path) {
println('failed to load font "$c.font_path"')
return none
}
bytes := os.read_bytes(c.font_path) or {
println('failed to load font "$c.font_path"')
return none
}
s := &C.sgl_desc_t{}
C.sgl_setup(s)
fons :=sfons.create(512, 512, 1)
return &FT{
fons : fons
font_normal: C.fonsAddFontMem(fons, 'sans', bytes.data, bytes.len, false)
}
}
pub fn (gg &FT) draw_text(x, y int, text string, cfg gx.TextCfg) {
/*
gg.fons.set_font(gg.font_normal)
gg.fons.set_size(f32(cfg.size))
ascender := f32(0.0)
descender := f32(0.0)
lh := f32(0.0)
gg.fons.vert_metrics(&ascender, &descender, &lh)
color:= C.sfons_rgba(cfg.color.r, cfg.color.g, cfg.color.b, 255)
C.fonsSetColor(gg.fons, color)
C.fonsDrawText(gg.fons, x, y, text.str, 0)
*/
}
pub fn (ctx &FT) draw_text_def(x, y int, text string) {
cfg := gx.TextCfg {
color: gx.black
size: default_font_size
align: gx.align_left
}
ctx.draw_text(x, y, text, cfg)
}
pub fn (mut gg FT) init_font() {
// TODO
////gg.fons =g_fons
//gg.font_normal=g_font_normal
}

View File

@ -10,11 +10,6 @@ import sokol
import sokol.sapp import sokol.sapp
import sokol.sgl import sokol.sgl
import sokol.gfx import sokol.gfx
import sokol.sfons
const (
default_font_size = 24
)
pub struct Config { pub struct Config {
pub: pub:
@ -25,7 +20,6 @@ pub:
resizable bool resizable bool
user_data voidptr user_data voidptr
font_size int font_size int
font_path string
create_window bool create_window bool
// window_user_ptr voidptr // window_user_ptr voidptr
window_title string window_title string
@ -45,18 +39,8 @@ pub mut:
clear_pass C.sg_pass_action clear_pass C.sg_pass_action
window C.sapp_desc window C.sapp_desc
render_fn fn() render_fn fn()
//////////// font fields
fons &C.FONScontext
font_normal int
} }
// TODO remove globals
/*
__global g_fons &C.FONScontext
__global g_font_normal int
__global g_font_path string
*/
fn init_sokol_window(user_data voidptr) { fn init_sokol_window(user_data voidptr) {
desc := C.sg_desc{ desc := C.sg_desc{
mtl_device: sapp.metal_get_device() mtl_device: sapp.metal_get_device()
@ -70,18 +54,6 @@ fn init_sokol_window(user_data voidptr) {
gfx.setup(&desc) gfx.setup(&desc)
sgl_desc := C.sgl_desc_t{} sgl_desc := C.sgl_desc_t{}
sgl.setup(&sgl_desc) sgl.setup(&sgl_desc)
/*
g_fons = sfons.create(512, 512, 1)
if g_font_path.len == 0 || !os.exists(g_font_path) {
println('failed to load font "$g_font_path"')
return
}
bytes := os.read_bytes(g_font_path) or {
println('failed to load font "$g_font_path"')
return
}
g_font_normal = C.fonsAddFontMem(g_fons, 'sans', bytes.data, bytes.len, false)
*/
} }
fn eventcb(e &C.sapp_event, b voidptr){ fn eventcb(e &C.sapp_event, b voidptr){
@ -101,7 +73,6 @@ pub fn new_context(cfg Config) &GG {
height: cfg.height height: cfg.height
//high_dpi: true //high_dpi: true
} }
//g_font_path = cfg.font_path
if cfg.use_ortho {} if cfg.use_ortho {}
else {} else {}
return &GG{ return &GG{
@ -111,37 +82,9 @@ pub fn new_context(cfg Config) &GG {
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)
scale: 1 // scale scale: 1 // scale
fons:0
} }
} }
pub fn (gg &GG) draw_text(x, y int, text string, cfg gx.TextCfg) {
gg.fons.set_font(gg.font_normal)
gg.fons.set_size(f32(cfg.size))
ascender := f32(0.0)
descender := f32(0.0)
lh := f32(0.0)
gg.fons.vert_metrics(&ascender, &descender, &lh)
color:= C.sfons_rgba(cfg.color.r, cfg.color.g, cfg.color.b, 255)
C.fonsSetColor(gg.fons, color)
C.fonsDrawText(gg.fons, x, y, text.str, 0)
}
pub fn (ctx &GG) draw_text_def(x, y int, text string) {
cfg := gx.TextCfg {
color: gx.black
size: default_font_size
align: gx.align_left
}
ctx.draw_text(x, y, text, cfg)
}
pub fn (mut gg GG) init_font() {
// TODO
////gg.fons =g_fons
//gg.font_normal=g_font_normal
}
pub fn (gg &GG) run() { pub fn (gg &GG) run() {
sapp.run(&gg.window) sapp.run(&gg.window)
} }