| 1 | import gg |
| 2 | import sokol.sapp |
| 3 | import sokol.sgl |
| 4 | import x.ttf |
| 5 | import x.ttf.render_sokol |
| 6 | import os |
| 7 | |
| 8 | const custom_font_path = os.args[1] or { |
| 9 | os.resource_abs_path(os.join_path('..', 'assets', 'fonts', 'Imprima-Regular.ttf')) |
| 10 | } |
| 11 | const custom_txt_path = os.args[2] or { os.resource_abs_path('draw_static_text.txt') } |
| 12 | |
| 13 | const custom_text_start_y = 80 |
| 14 | |
| 15 | const win_width = 400 |
| 16 | const win_height = 400 |
| 17 | const bg_color = gg.Color{50, 255, 50, 255} |
| 18 | |
| 19 | const block_txt = os.read_file(custom_txt_path) or { '' } |
| 20 | const font_paths = [ |
| 21 | os.resource_abs_path(os.join_path('..', 'assets', 'fonts', 'RobotoMono-Regular.ttf')), |
| 22 | custom_font_path, |
| 23 | ] |
| 24 | |
| 25 | // UI |
| 26 | struct App_data { |
| 27 | pub mut: |
| 28 | gg &gg.Context = unsafe { nil } |
| 29 | init_flag bool |
| 30 | tf []ttf.TTF_File |
| 31 | ttf_render []render_sokol.TTF_render_Sokol |
| 32 | text_ready_flag bool |
| 33 | } |
| 34 | |
| 35 | fn my_init(mut app App_data) { |
| 36 | app.init_flag = true |
| 37 | texts := ['Hello, font: ${os.file_name(custom_font_path)}', block_txt]! |
| 38 | dump(texts[0]) |
| 39 | for i in 0 .. 2 { |
| 40 | mut txt := unsafe { &app.ttf_render[i] } |
| 41 | txt.destroy_texture() |
| 42 | txt.create_text_block(texts[i], 1024, 1024, 24) |
| 43 | txt.create_texture() |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | fn draw_frame(mut app App_data) { |
| 48 | app.gg.begin() |
| 49 | sgl.defaults() |
| 50 | sgl.matrix_mode_projection() |
| 51 | sgl.ortho(0.0, f32(sapp.width()), f32(sapp.height()), 0.0, -1.0, 1.0) |
| 52 | sgl.c4b(0, 0, 0, 255) // black |
| 53 | // draw text only if the app is already initialized |
| 54 | if app.init_flag != true { |
| 55 | app.gg.end() |
| 56 | } |
| 57 | |
| 58 | // draw hello |
| 59 | mut txt1 := unsafe { &app.ttf_render[0] } |
| 60 | txt1.draw_text_bmp(app.gg, 5, 5) |
| 61 | |
| 62 | // draw the custom text |
| 63 | txt2 := unsafe { &app.ttf_render[1] } |
| 64 | txt2.draw_text_bmp(app.gg, 30, custom_text_start_y) |
| 65 | app.gg.end() |
| 66 | } |
| 67 | |
| 68 | fn main() { |
| 69 | println('Use `v run draw_static_text.v [FONT_PATH] [TEXT_FILE_PATH]`') |
| 70 | println('> Current command: os.args[0] ${custom_font_path} ${custom_txt_path}') |
| 71 | mut app := &App_data{} |
| 72 | app.gg = gg.new_context( |
| 73 | width: win_width |
| 74 | height: win_height |
| 75 | window_title: 'Draw custom text, with custom font' |
| 76 | user_data: app |
| 77 | bg_color: bg_color |
| 78 | frame_fn: draw_frame |
| 79 | init_fn: my_init |
| 80 | ) |
| 81 | |
| 82 | // load the TTF fonts: |
| 83 | for font_path in font_paths { |
| 84 | mut tf := ttf.TTF_File{} |
| 85 | tf.buf = os.read_bytes(font_path) or { panic(err) } |
| 86 | println('Read TrueTypeFont file [${font_path}], len: ${tf.buf.len}') |
| 87 | tf.init() |
| 88 | println('Unit per EM: ${tf.units_per_em}') |
| 89 | app.tf << tf |
| 90 | } |
| 91 | |
| 92 | // TTF hello render |
| 93 | app.ttf_render << &render_sokol.TTF_render_Sokol{ |
| 94 | bmp: &ttf.BitMap{ |
| 95 | tf: &app.tf[0] |
| 96 | buf: unsafe { malloc_noscan(32000000) } |
| 97 | buf_size: (32000000) |
| 98 | color: 0xFF0000FF |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | // TTF custom text render |
| 103 | app.ttf_render << &render_sokol.TTF_render_Sokol{ |
| 104 | bmp: &ttf.BitMap{ |
| 105 | tf: &app.tf[1] |
| 106 | buf: unsafe { malloc_noscan(32000000) } |
| 107 | buf_size: (32000000) |
| 108 | color: 0x2020EEFF |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | // setup sokol_gfx |
| 113 | app.gg.run() |
| 114 | } |
| 115 | |