gg: move text rendering to gg.ft
parent
653a27005b
commit
1139d3b458
|
@ -11,7 +11,8 @@ import gg2 as gg
|
|||
import sokol
|
||||
import sokol.sapp
|
||||
import math
|
||||
import freetype
|
||||
import gg2.ft
|
||||
import os
|
||||
|
||||
const (
|
||||
block_size = 20 // pixels
|
||||
|
@ -129,72 +130,47 @@ struct Game {
|
|||
// gg context for drawing
|
||||
gg &gg.GG
|
||||
// ft context for font drawing
|
||||
ft &freetype.FreeType
|
||||
//ft &freetype.FreeType
|
||||
//ft &ft.FT
|
||||
font_loaded bool
|
||||
}
|
||||
|
||||
fn frame(game &Game) {
|
||||
//C.sfons_flush(game.ft.fons)
|
||||
game.gg.begin()
|
||||
game.draw_scene()
|
||||
game.gg.end()
|
||||
}
|
||||
|
||||
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(
|
||||
bg_color: gx.white
|
||||
width: win_width
|
||||
height: win_height
|
||||
use_ortho: true // This is needed for 2D drawing
|
||||
create_window: true
|
||||
window_title: 'V tetris'
|
||||
window_title: 'V Tetris'
|
||||
frame_fn: frame
|
||||
user_data: game
|
||||
//on_key_down: key_down
|
||||
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.gg.window.onkeydown(key_down)
|
||||
go game.run() // Run the game loop in a new 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() {
|
||||
|
@ -358,17 +334,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.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.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)
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
@ -10,11 +10,6 @@ import sokol
|
|||
import sokol.sapp
|
||||
import sokol.sgl
|
||||
import sokol.gfx
|
||||
import sokol.sfons
|
||||
|
||||
const (
|
||||
default_font_size = 24
|
||||
)
|
||||
|
||||
pub struct Config {
|
||||
pub:
|
||||
|
@ -25,7 +20,6 @@ pub:
|
|||
resizable bool
|
||||
user_data voidptr
|
||||
font_size int
|
||||
font_path string
|
||||
create_window bool
|
||||
// window_user_ptr voidptr
|
||||
window_title string
|
||||
|
@ -45,18 +39,8 @@ pub mut:
|
|||
clear_pass C.sg_pass_action
|
||||
window C.sapp_desc
|
||||
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) {
|
||||
desc := C.sg_desc{
|
||||
mtl_device: sapp.metal_get_device()
|
||||
|
@ -70,18 +54,6 @@ fn init_sokol_window(user_data voidptr) {
|
|||
gfx.setup(&desc)
|
||||
sgl_desc := C.sgl_desc_t{}
|
||||
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){
|
||||
|
@ -101,7 +73,6 @@ pub fn new_context(cfg Config) &GG {
|
|||
height: cfg.height
|
||||
//high_dpi: true
|
||||
}
|
||||
//g_font_path = cfg.font_path
|
||||
if cfg.use_ortho {}
|
||||
else {}
|
||||
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,
|
||||
f32(cfg.bg_color.b) / 255.0, 1.0)
|
||||
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() {
|
||||
sapp.run(&gg.window)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue