v / examples / ttf_font / example_ttf.v
167 lines · 161 sloc · 4.21 KB · ad9d9125f89df12b4ea18d7eb6a7cc47f00aae1d
Raw
1import gg
2import sokol.sapp
3import sokol.sgl
4import sokol.gfx
5import x.ttf
6import x.ttf.render_sokol
7import os
8
9// import math
10const win_width = 600
11const win_height = 700
12const bg_color = gg.white
13const font_paths = [
14 os.resource_abs_path(os.join_path('..', 'assets', 'fonts', 'Imprima-Regular.ttf')),
15 os.resource_abs_path(os.join_path('..', 'assets', 'fonts', 'Graduate-Regular.ttf')),
16]
17
18// UI
19struct App_data {
20pub mut:
21 gg &gg.Context = unsafe { nil }
22 sg_img gfx.Image
23 init_flag bool
24 frame_c int
25 tf []ttf.TTF_File
26 ttf_render []render_sokol.TTF_render_Sokol
27 text_ready_flag bool
28 mouse_x int = -1
29 mouse_y int = -1
30}
31
32fn my_init(mut app App_data) {
33 app.init_flag = true
34}
35
36fn draw_frame(mut app App_data) {
37 cframe_txt := 'Current Frame: ${app.frame_c}'
38 app.gg.begin()
39 sgl.defaults()
40 sgl.matrix_mode_projection()
41 sgl.ortho(0.0, f32(sapp.width()), f32(sapp.height()), 0.0, -1.0, 1.0)
42 sgl.c4b(0, 0, 0, 255) // black
43 // draw a line as background
44 sgl.begin_line_strip()
45 sgl.v2f(10, 10)
46 sgl.v2f(100, 100)
47 sgl.end()
48 // draw text only if the app is already initialized
49 if app.init_flag == true {
50 sgl.begin_line_strip()
51 sgl.v2f(410, 400)
52 sgl.v2f(510, 400)
53 sgl.end()
54 // update the text
55 mut txt1 := unsafe { &app.ttf_render[0] }
56 if app.frame_c % 2 == 0 {
57 txt1.destroy_texture()
58 txt1.create_text(cframe_txt, 43)
59 txt1.create_texture()
60 }
61 // ----- decomment if you want text rotation ----
62 // txt1.bmp.angle = 3.141592 / 180 * f32(app.frame_c % 360)
63 // txt1.draw_text_bmp(app.gg, 300, 350)
64 // txt1.bmp.angle = 0
65 txt1.draw_text_bmp(app.gg, 30, 60)
66 // block test
67 block_txt := "Today it is a good day!
68Tomorrow I'm not so sure :(
69Frame: ${app.frame_c}
70But Vwill prevail for sure, V is the way!!
71òàèì@ò!£$%&
72"
73 txt1 = unsafe { &app.ttf_render[1] }
74 if app.frame_c % 2 == 0 {
75 txt1.bmp.justify = false
76 if (app.frame_c >> 6) % 2 == 0 {
77 // txt1.align = .left
78 txt1.bmp.justify = true
79 }
80 txt1.bmp.align = .left
81 if (app.frame_c >> 6) % 3 == 0 {
82 txt1.bmp.align = .right
83 }
84 txt1.destroy_texture()
85 txt1.create_text_block(block_txt, 500, 500, 32)
86 txt1.create_texture()
87 }
88 // decomment if want block color change
89 // txt1.bmp.color = ttf.color_multiply(0xFF00FFFF, f32(app.frame_c % 255)/255.0)
90 // decomment if want block rotation wanted
91 // txt1.bmp.angle = 3.141592/180 * f32(app.frame_c % 45)
92 txt1.draw_text_bmp(app.gg, 30 + (app.frame_c >> 1) & 0xFF, 200)
93 // draw mouse position
94 if app.mouse_x >= 0 {
95 txt1 = unsafe { &app.ttf_render[2] }
96 txt1.destroy_texture()
97 txt1.create_text('${app.mouse_x},${app.mouse_y}', 25)
98 txt1.create_texture()
99 r := app.mouse_x % 255
100 g := app.mouse_y % 255
101 color := u32(r) << 24 | u32(g) << 16 | 0xFF
102 txt1.bmp.color = color
103 txt1.draw_text_bmp(app.gg, app.mouse_x, app.mouse_y)
104 }
105 app.frame_c++
106 }
107 app.gg.end()
108}
109
110fn my_event_manager(mut ev gg.Event, mut app App_data) {
111 if ev.typ == .mouse_move {
112 app.mouse_x = int(ev.mouse_x)
113 app.mouse_y = int(ev.mouse_y)
114 }
115}
116
117fn main() {
118 mut app := &App_data{}
119 app.gg = gg.new_context(
120 width: win_width
121 height: win_height
122 create_window: true
123 window_title: 'Test TTF module'
124 user_data: app
125 bg_color: bg_color
126 frame_fn: draw_frame
127 event_fn: my_event_manager
128 init_fn: my_init
129 )
130 // load TTF fonts
131 for font_path in font_paths {
132 mut tf := ttf.TTF_File{}
133 tf.buf = os.read_bytes(font_path) or { panic(err) }
134 println('TrueTypeFont file [${font_path}] len: ${tf.buf.len}')
135 tf.init()
136 println('Unit per EM: ${tf.units_per_em}')
137 app.tf << tf
138 }
139 // TTF render 0 Frame counter
140 app.ttf_render << &render_sokol.TTF_render_Sokol{
141 bmp: &ttf.BitMap{
142 tf: &app.tf[0]
143 buf: unsafe { malloc_noscan(32000000) }
144 buf_size: (32000000)
145 color: 0xFF0000FF
146 // style: .raw
147 // use_font_metrics: true
148 }
149 }
150 // TTF render 1 Text Block
151 app.ttf_render << &render_sokol.TTF_render_Sokol{
152 bmp: &ttf.BitMap{
153 tf: &app.tf[1]
154 // color : 0xFF0000_10
155 // style: .raw
156 // use_font_metrics: true
157 }
158 }
159 // TTF mouse position render
160 app.ttf_render << &render_sokol.TTF_render_Sokol{
161 bmp: &ttf.BitMap{
162 tf: &app.tf[0]
163 }
164 }
165 // setup sokol_gfx
166 app.gg.run()
167}
168