v/examples/ttf_font/draw_static_text.v
Eliyaan (Nopana) bbb61ab368
Some checks failed
Graphics CI / gg-regressions (push) Waiting to run
vlib modules CI / build-module-docs (push) Waiting to run
native backend CI / native-backend-ubuntu (push) Waiting to run
vab CI / v-compiles-os-android (push) Waiting to run
native backend CI / native-backend-windows (push) Waiting to run
Shy and PV CI / v-compiles-puzzle-vibes (push) Waiting to run
Sanitized CI / sanitize-undefined-clang (push) Waiting to run
Sanitized CI / sanitize-undefined-gcc (push) Waiting to run
Sanitized CI / tests-sanitize-address-clang (push) Waiting to run
Sanitized CI / sanitize-address-msvc (push) Waiting to run
Sanitized CI / sanitize-address-gcc (push) Waiting to run
Sanitized CI / sanitize-memory-clang (push) Waiting to run
sdl CI / v-compiles-sdl-examples (push) Waiting to run
Time CI / time-linux (push) Waiting to run
Time CI / time-macos (push) Waiting to run
Time CI / time-windows (push) Waiting to run
toml CI / toml-module-pass-external-test-suites (push) Waiting to run
Tools CI / tools-linux (clang) (push) Waiting to run
Tools CI / tools-linux (gcc) (push) Waiting to run
Tools CI / tools-linux (tcc) (push) Waiting to run
Tools CI / tools-macos (clang) (push) Waiting to run
Tools CI / tools-windows (gcc) (push) Waiting to run
Tools CI / tools-windows (msvc) (push) Waiting to run
Tools CI / tools-windows (tcc) (push) Waiting to run
Tools CI / tools-docker-ubuntu-musl (push) Waiting to run
vab CI / vab-compiles-v-examples (push) Waiting to run
wasm backend CI / wasm-backend (ubuntu-22.04) (push) Waiting to run
wasm backend CI / wasm-backend (windows-2022) (push) Waiting to run
Workflow Lint / lint-yml-workflows (push) Has been cancelled
gg, gx: deprecate gx and replace all occurences with gg (which now contains all the functionality of gx) (#24966)
2025-08-14 19:53:56 +03:00

113 lines
2.7 KiB
V

import gg
import sokol.sapp
import sokol.sgl
import x.ttf
import os
const custom_font_path = os.args[1] or {
os.resource_abs_path(os.join_path('..', 'assets', 'fonts', 'Imprima-Regular.ttf'))
}
const custom_txt_path = os.args[2] or { os.resource_abs_path('draw_static_text.txt') }
const custom_text_start_y = 80
const win_width = 400
const win_height = 400
const bg_color = gg.Color{50, 255, 50, 255}
const block_txt = os.read_file(custom_txt_path) or { '' }
const font_paths = [
os.resource_abs_path(os.join_path('..', 'assets', 'fonts', 'RobotoMono-Regular.ttf')),
custom_font_path,
]
// UI
struct App_data {
pub mut:
gg &gg.Context = unsafe { nil }
init_flag bool
tf []ttf.TTF_File
ttf_render []ttf.TTF_render_Sokol
text_ready_flag bool
}
fn my_init(mut app App_data) {
app.init_flag = true
texts := ['Hello, font: ${os.file_name(custom_font_path)}', block_txt]!
dump(texts[0])
for i in 0 .. 2 {
mut txt := unsafe { &app.ttf_render[i] }
txt.destroy_texture()
txt.create_text_block(texts[i], 1024, 1024, 24)
txt.create_texture()
}
}
fn draw_frame(mut app App_data) {
app.gg.begin()
sgl.defaults()
sgl.matrix_mode_projection()
sgl.ortho(0.0, f32(sapp.width()), f32(sapp.height()), 0.0, -1.0, 1.0)
sgl.c4b(0, 0, 0, 255) // black
// draw text only if the app is already initialized
if app.init_flag != true {
app.gg.end()
}
// draw hello
mut txt1 := unsafe { &app.ttf_render[0] }
txt1.draw_text_bmp(app.gg, 5, 5)
// draw the custom text
txt2 := unsafe { &app.ttf_render[1] }
txt2.draw_text_bmp(app.gg, 30, custom_text_start_y)
app.gg.end()
}
fn main() {
println('Use `v run draw_static_text.v [FONT_PATH] [TEXT_FILE_PATH]`')
println('> Current command: os.args[0] ${custom_font_path} ${custom_txt_path}')
mut app := &App_data{}
app.gg = gg.new_context(
width: win_width
height: win_height
window_title: 'Draw custom text, with custom font'
user_data: app
bg_color: bg_color
frame_fn: draw_frame
init_fn: my_init
)
// load the TTF fonts:
for font_path in font_paths {
mut tf := ttf.TTF_File{}
tf.buf = os.read_bytes(font_path) or { panic(err) }
println('Read TrueTypeFont file [${font_path}], len: ${tf.buf.len}')
tf.init()
println('Unit per EM: ${tf.units_per_em}')
app.tf << tf
}
// TTF hello render
app.ttf_render << &ttf.TTF_render_Sokol{
bmp: &ttf.BitMap{
tf: &app.tf[0]
buf: unsafe { malloc_noscan(32000000) }
buf_size: (32000000)
color: 0xFF0000FF
}
}
// TTF custom text render
app.ttf_render << &ttf.TTF_render_Sokol{
bmp: &ttf.BitMap{
tf: &app.tf[1]
buf: unsafe { malloc_noscan(32000000) }
buf_size: (32000000)
color: 0x2020EEFF
}
}
// setup sokol_gfx
app.gg.run()
}