2021-01-10 21:14:41 +03:00
|
|
|
import gg
|
|
|
|
import gx
|
|
|
|
import sokol.sapp
|
|
|
|
import sokol.sgl
|
2022-01-02 21:36:01 +03:00
|
|
|
import sokol.gfx
|
2021-01-10 21:14:41 +03:00
|
|
|
import x.ttf
|
|
|
|
import os
|
|
|
|
|
2021-01-10 21:36:50 +03:00
|
|
|
// import math
|
2021-01-10 21:14:41 +03:00
|
|
|
const (
|
|
|
|
win_width = 600
|
|
|
|
win_height = 700
|
|
|
|
bg_color = gx.white
|
|
|
|
font_paths = [
|
2021-12-06 10:53:46 +03:00
|
|
|
os.resource_abs_path(os.join_path('..', 'assets', 'fonts', 'Imprima-Regular.ttf')),
|
|
|
|
os.resource_abs_path(os.join_path('..', 'assets', 'fonts', 'Graduate-Regular.ttf')),
|
2021-01-10 21:14:41 +03:00
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2021-02-17 08:44:01 +03:00
|
|
|
// UI
|
2021-01-10 21:14:41 +03:00
|
|
|
struct App_data {
|
|
|
|
pub mut:
|
2021-01-10 21:36:50 +03:00
|
|
|
gg &gg.Context
|
2022-01-02 21:36:01 +03:00
|
|
|
sg_img gfx.Image
|
2021-01-10 21:36:50 +03:00
|
|
|
init_flag bool
|
|
|
|
frame_c int
|
2021-01-10 21:14:41 +03:00
|
|
|
tf []ttf.TTF_File
|
|
|
|
ttf_render []ttf.TTF_render_Sokol
|
|
|
|
text_ready_flag bool
|
2021-01-10 21:36:50 +03:00
|
|
|
mouse_x int = -1
|
|
|
|
mouse_y int = -1
|
2021-01-10 21:14:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn my_init(mut app App_data) {
|
|
|
|
app.init_flag = true
|
|
|
|
}
|
|
|
|
|
2021-01-10 21:36:50 +03:00
|
|
|
fn draw_frame(mut app App_data) {
|
|
|
|
cframe_txt := 'Current Frame: $app.frame_c'
|
2021-01-10 21:14:41 +03:00
|
|
|
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 a line as background
|
|
|
|
sgl.begin_line_strip()
|
|
|
|
sgl.v2f(10, 10)
|
|
|
|
sgl.v2f(100, 100)
|
|
|
|
sgl.end()
|
|
|
|
// draw text only if the app is already initialized
|
|
|
|
if app.init_flag == true {
|
|
|
|
sgl.begin_line_strip()
|
|
|
|
sgl.v2f(410, 400)
|
|
|
|
sgl.v2f(510, 400)
|
|
|
|
sgl.end()
|
|
|
|
// update the text
|
2021-04-25 21:40:38 +03:00
|
|
|
mut txt1 := unsafe { &app.ttf_render[0] }
|
2021-01-10 21:14:41 +03:00
|
|
|
if app.frame_c % 2 == 0 {
|
|
|
|
txt1.destroy_texture()
|
2021-01-10 21:36:50 +03:00
|
|
|
txt1.create_text(cframe_txt, 43)
|
2021-01-10 21:14:41 +03:00
|
|
|
txt1.create_texture()
|
|
|
|
}
|
2021-02-17 08:44:01 +03:00
|
|
|
// ----- decomment if you want text rotation ----
|
2021-01-10 21:36:50 +03:00
|
|
|
// txt1.bmp.angle = 3.141592 / 180 * f32(app.frame_c % 360)
|
|
|
|
// txt1.draw_text_bmp(app.gg, 300, 350)
|
|
|
|
// txt1.bmp.angle = 0
|
2021-01-10 21:14:41 +03:00
|
|
|
txt1.draw_text_bmp(app.gg, 30, 60)
|
|
|
|
// block test
|
|
|
|
block_txt := "Today it is a good day!
|
|
|
|
Tommorow I'm not so sure :(
|
|
|
|
Frame: $app.frame_c
|
|
|
|
But Vwill prevail for sure, V is the way!!
|
|
|
|
òàèì@ò!£$%&
|
|
|
|
"
|
2021-04-25 21:40:38 +03:00
|
|
|
txt1 = unsafe { &app.ttf_render[1] }
|
2021-01-10 21:14:41 +03:00
|
|
|
if app.frame_c % 2 == 0 {
|
|
|
|
txt1.bmp.justify = false
|
2021-01-10 21:36:50 +03:00
|
|
|
if (app.frame_c >> 6) % 2 == 0 {
|
|
|
|
// txt1.align = .left
|
2021-01-10 21:14:41 +03:00
|
|
|
txt1.bmp.justify = true
|
|
|
|
}
|
|
|
|
txt1.bmp.align = .left
|
2021-01-10 21:36:50 +03:00
|
|
|
if (app.frame_c >> 6) % 3 == 0 {
|
2021-01-10 21:14:41 +03:00
|
|
|
txt1.bmp.align = .right
|
|
|
|
}
|
|
|
|
txt1.destroy_texture()
|
|
|
|
txt1.create_text_block(block_txt, 500, 500, 32)
|
|
|
|
txt1.create_texture()
|
|
|
|
}
|
|
|
|
// decomment if want block color change
|
2021-01-10 21:36:50 +03:00
|
|
|
// txt1.bmp.color = ttf.color_multiply(0xFF00FFFF, f32(app.frame_c % 255)/255.0)
|
2021-01-10 21:14:41 +03:00
|
|
|
// decomment if want block rotation wanted
|
2021-01-10 21:36:50 +03:00
|
|
|
// txt1.bmp.angle = 3.141592/180 * f32(app.frame_c % 45)
|
|
|
|
txt1.draw_text_bmp(app.gg, 30 + (app.frame_c >> 1) & 0xFF, 200)
|
2021-01-10 21:14:41 +03:00
|
|
|
// draw mouse position
|
|
|
|
if app.mouse_x >= 0 {
|
2021-04-25 21:40:38 +03:00
|
|
|
txt1 = unsafe { &app.ttf_render[2] }
|
2021-01-10 21:14:41 +03:00
|
|
|
txt1.destroy_texture()
|
2021-01-10 21:36:50 +03:00
|
|
|
txt1.create_text('$app.mouse_x,$app.mouse_y', 25)
|
2021-01-10 21:14:41 +03:00
|
|
|
txt1.create_texture()
|
|
|
|
r := app.mouse_x % 255
|
|
|
|
g := app.mouse_y % 255
|
2021-11-29 03:48:49 +03:00
|
|
|
color := u32(r) << 24 | u32(g) << 16 | 0xFF
|
2021-01-10 21:14:41 +03:00
|
|
|
txt1.bmp.color = color
|
2021-01-10 21:36:50 +03:00
|
|
|
txt1.draw_text_bmp(app.gg, app.mouse_x, app.mouse_y)
|
2021-01-10 21:14:41 +03:00
|
|
|
}
|
2021-01-10 21:36:50 +03:00
|
|
|
app.frame_c++
|
2021-01-10 21:14:41 +03:00
|
|
|
}
|
|
|
|
app.gg.end()
|
|
|
|
}
|
|
|
|
|
2021-02-17 08:44:01 +03:00
|
|
|
fn my_event_manager(mut ev gg.Event, mut app App_data) {
|
2021-01-10 21:14:41 +03:00
|
|
|
if ev.typ == .mouse_move {
|
|
|
|
app.mouse_x = int(ev.mouse_x)
|
|
|
|
app.mouse_y = int(ev.mouse_y)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[console]
|
2021-01-10 21:36:50 +03:00
|
|
|
fn main() {
|
2021-01-10 21:14:41 +03:00
|
|
|
mut app := &App_data{
|
|
|
|
gg: 0
|
|
|
|
}
|
2021-01-10 21:36:50 +03:00
|
|
|
app.gg = gg.new_context(
|
2021-01-10 21:14:41 +03:00
|
|
|
width: win_width
|
|
|
|
height: win_height
|
|
|
|
create_window: true
|
|
|
|
window_title: 'Test TTF module'
|
|
|
|
user_data: app
|
|
|
|
bg_color: bg_color
|
|
|
|
frame_fn: draw_frame
|
|
|
|
event_fn: my_event_manager
|
|
|
|
init_fn: my_init
|
2021-01-10 21:36:50 +03:00
|
|
|
)
|
2021-01-10 21:14:41 +03:00
|
|
|
// load TTF fonts
|
|
|
|
for font_path in font_paths {
|
|
|
|
mut tf := ttf.TTF_File{}
|
2021-03-01 02:18:14 +03:00
|
|
|
tf.buf = os.read_bytes(font_path) or { panic(err) }
|
2021-01-10 21:36:50 +03:00
|
|
|
println('TrueTypeFont file [$font_path] len: $tf.buf.len')
|
2021-01-10 21:14:41 +03:00
|
|
|
tf.init()
|
2021-01-10 21:36:50 +03:00
|
|
|
println('Unit per EM: $tf.units_per_em')
|
2021-01-10 21:14:41 +03:00
|
|
|
app.tf << tf
|
|
|
|
}
|
|
|
|
// TTF render 0 Frame counter
|
2021-01-10 21:36:50 +03:00
|
|
|
app.ttf_render << &ttf.TTF_render_Sokol{
|
2021-01-10 21:14:41 +03:00
|
|
|
bmp: &ttf.BitMap{
|
|
|
|
tf: &(app.tf[0])
|
2021-06-15 14:47:11 +03:00
|
|
|
buf: unsafe { malloc_noscan(32000000) }
|
2021-01-10 21:14:41 +03:00
|
|
|
buf_size: (32000000)
|
2021-01-10 21:36:50 +03:00
|
|
|
color: 0xFF0000FF
|
|
|
|
// style: .raw
|
|
|
|
// use_font_metrics: true
|
2021-01-10 21:14:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// TTF render 1 Text Block
|
2021-01-10 21:36:50 +03:00
|
|
|
app.ttf_render << &ttf.TTF_render_Sokol{
|
2021-01-10 21:14:41 +03:00
|
|
|
bmp: &ttf.BitMap{
|
|
|
|
tf: &(app.tf[1])
|
2021-01-10 21:36:50 +03:00
|
|
|
// color : 0xFF0000_10
|
|
|
|
// style: .raw
|
|
|
|
// use_font_metrics: true
|
2021-01-10 21:14:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// TTF mouse position render
|
2021-01-10 21:36:50 +03:00
|
|
|
app.ttf_render << &ttf.TTF_render_Sokol{
|
2021-01-10 21:14:41 +03:00
|
|
|
bmp: &ttf.BitMap{
|
|
|
|
tf: &(app.tf[0])
|
|
|
|
}
|
|
|
|
}
|
2021-01-10 21:36:50 +03:00
|
|
|
// setup sokol_gfx
|
2021-01-10 21:14:41 +03:00
|
|
|
app.gg.run()
|
|
|
|
}
|