2020-01-16 22:45:47 +03:00
|
|
|
import sokol
|
|
|
|
import sokol.sapp
|
|
|
|
import sokol.gfx
|
|
|
|
import sokol.sgl
|
2021-12-06 23:39:43 +03:00
|
|
|
import fontstash
|
2020-01-16 22:45:47 +03:00
|
|
|
import sokol.sfons
|
|
|
|
import os
|
|
|
|
|
|
|
|
struct AppState {
|
|
|
|
mut:
|
2022-04-11 22:23:06 +03:00
|
|
|
pass_action gfx.PassAction
|
|
|
|
font_context &fontstash.Context
|
|
|
|
font_normal int
|
2020-01-16 22:45:47 +03:00
|
|
|
}
|
|
|
|
|
2021-12-06 10:53:46 +03:00
|
|
|
[console]
|
2020-01-16 22:45:47 +03:00
|
|
|
fn main() {
|
2022-01-02 21:36:01 +03:00
|
|
|
mut color_action := gfx.ColorAttachmentAction{
|
|
|
|
action: .clear
|
|
|
|
value: gfx.Color{
|
2021-04-07 21:39:23 +03:00
|
|
|
r: 0.3
|
|
|
|
g: 0.3
|
|
|
|
b: 0.32
|
|
|
|
a: 1.0
|
|
|
|
}
|
2020-01-16 22:45:47 +03:00
|
|
|
}
|
2022-01-02 21:36:01 +03:00
|
|
|
mut pass_action := gfx.PassAction{}
|
2020-01-16 22:45:47 +03:00
|
|
|
pass_action.colors[0] = color_action
|
|
|
|
state := &AppState{
|
|
|
|
pass_action: pass_action
|
2022-04-11 22:23:06 +03:00
|
|
|
font_context: voidptr(0) // &fontstash.Context(0)
|
2020-01-16 22:45:47 +03:00
|
|
|
}
|
2020-01-17 22:05:45 +03:00
|
|
|
title := 'V Metal/GL Text Rendering'
|
2021-12-26 14:02:51 +03:00
|
|
|
desc := sapp.Desc{
|
2020-01-16 22:45:47 +03:00
|
|
|
user_data: state
|
|
|
|
init_userdata_cb: init
|
|
|
|
frame_userdata_cb: frame
|
2020-01-17 22:05:45 +03:00
|
|
|
window_title: title.str
|
|
|
|
html5_canvas_name: title.str
|
2020-01-16 22:45:47 +03:00
|
|
|
}
|
|
|
|
sapp.run(&desc)
|
|
|
|
}
|
|
|
|
|
2020-06-04 11:35:40 +03:00
|
|
|
fn init(mut state AppState) {
|
2020-08-23 08:47:50 +03:00
|
|
|
desc := sapp.create_desc()
|
|
|
|
gfx.setup(&desc)
|
2022-01-03 16:05:24 +03:00
|
|
|
s := &sgl.Desc{}
|
2022-04-11 22:23:06 +03:00
|
|
|
sgl.setup(s)
|
|
|
|
state.font_context = sfons.create(512, 512, 1)
|
2020-01-16 22:45:47 +03:00
|
|
|
// or use DroidSerif-Regular.ttf
|
2021-12-06 10:53:46 +03:00
|
|
|
if bytes := os.read_bytes(os.resource_abs_path(os.join_path('..', 'assets', 'fonts',
|
|
|
|
'RobotoMono-Regular.ttf')))
|
|
|
|
{
|
2020-01-16 22:45:47 +03:00
|
|
|
println('loaded font: $bytes.len')
|
2022-04-11 22:23:06 +03:00
|
|
|
state.font_normal = state.font_context.add_font_mem('sans', bytes, false)
|
2020-01-16 22:45:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn frame(user_data voidptr) {
|
|
|
|
mut state := &AppState(user_data)
|
|
|
|
state.render_font()
|
2020-03-26 10:54:33 +03:00
|
|
|
gfx.begin_default_pass(&state.pass_action, sapp.width(), sapp.height())
|
2020-01-16 22:45:47 +03:00
|
|
|
sgl.draw()
|
|
|
|
gfx.end_pass()
|
|
|
|
gfx.commit()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (state &AppState) render_font() {
|
2021-12-06 23:39:43 +03:00
|
|
|
mut sx := f32(0.0)
|
|
|
|
mut sy := f32(0.0)
|
|
|
|
mut dx := f32(0.0)
|
|
|
|
mut dy := f32(0.0)
|
2020-03-19 09:24:49 +03:00
|
|
|
lh := f32(0.0)
|
2021-12-06 23:39:43 +03:00
|
|
|
white := sfons.rgba(255, 255, 255, 255)
|
|
|
|
black := sfons.rgba(0, 0, 0, 255)
|
|
|
|
brown := sfons.rgba(192, 128, 0, 128)
|
|
|
|
blue := sfons.rgba(0, 192, 255, 255)
|
|
|
|
|
2022-04-11 22:23:06 +03:00
|
|
|
font_context := state.font_context
|
|
|
|
font_context.clear_state()
|
2020-01-16 22:45:47 +03:00
|
|
|
sgl.defaults()
|
2020-01-17 22:05:45 +03:00
|
|
|
sgl.matrix_mode_projection()
|
2021-12-06 23:39:43 +03:00
|
|
|
sgl.ortho(0.0, f32(sapp.width()), f32(sapp.height()), 0.0, -1.0, 1.0)
|
2020-01-16 22:45:47 +03:00
|
|
|
sx = 0
|
|
|
|
sy = 50
|
2020-01-17 22:05:45 +03:00
|
|
|
dx = sx
|
2020-01-16 22:45:47 +03:00
|
|
|
dy = sy
|
2022-04-11 22:23:06 +03:00
|
|
|
font_context.set_font(state.font_normal)
|
|
|
|
font_context.set_size(100.0)
|
2020-03-19 09:24:49 +03:00
|
|
|
ascender := f32(0.0)
|
|
|
|
descender := f32(0.0)
|
2022-04-11 22:23:06 +03:00
|
|
|
font_context.vert_metrics(&ascender, &descender, &lh)
|
2020-01-16 22:45:47 +03:00
|
|
|
dx = sx
|
|
|
|
dy += lh
|
2022-04-11 22:23:06 +03:00
|
|
|
font_context.set_color(white)
|
|
|
|
dx = font_context.draw_text(dx, dy, 'The quick ')
|
|
|
|
font_context.set_font(state.font_normal)
|
|
|
|
font_context.set_size(48.0)
|
|
|
|
font_context.set_color(brown)
|
|
|
|
dx = font_context.draw_text(dx, dy, 'brown ')
|
|
|
|
font_context.set_font(state.font_normal)
|
|
|
|
font_context.set_size(24.0)
|
|
|
|
font_context.set_color(white)
|
|
|
|
dx = font_context.draw_text(dx, dy, 'fox ')
|
2020-01-16 22:45:47 +03:00
|
|
|
dx = sx
|
|
|
|
dy += lh * 1.2
|
2022-04-11 22:23:06 +03:00
|
|
|
font_context.set_size(20.0)
|
|
|
|
font_context.set_font(state.font_normal)
|
|
|
|
font_context.set_color(blue)
|
|
|
|
font_context.draw_text(dx, dy, 'Now is the time for all good men to come to the aid of the party.')
|
2020-01-16 22:45:47 +03:00
|
|
|
dx = 300
|
|
|
|
dy = 350
|
2022-04-11 22:23:06 +03:00
|
|
|
font_context.set_alignment(.left | .baseline)
|
|
|
|
font_context.set_size(60.0)
|
|
|
|
font_context.set_font(state.font_normal)
|
|
|
|
font_context.set_color(white)
|
|
|
|
font_context.set_spacing(5.0)
|
|
|
|
font_context.set_blur(6.0)
|
|
|
|
font_context.draw_text(dx, dy, 'Blurry...')
|
2020-01-16 22:45:47 +03:00
|
|
|
dx = 300
|
|
|
|
dy += 50.0
|
2022-04-11 22:23:06 +03:00
|
|
|
font_context.set_size(28.0)
|
|
|
|
font_context.set_font(state.font_normal)
|
|
|
|
font_context.set_color(white)
|
|
|
|
font_context.set_spacing(0.0)
|
|
|
|
font_context.set_blur(3.0)
|
|
|
|
font_context.draw_text(dx, dy + 2, 'DROP SHADOW')
|
|
|
|
font_context.set_color(black)
|
|
|
|
font_context.set_blur(0)
|
|
|
|
font_context.draw_text(dx, dy, 'DROP SHADOW')
|
|
|
|
font_context.set_size(18.0)
|
|
|
|
font_context.set_font(state.font_normal)
|
|
|
|
font_context.set_color(white)
|
2020-01-16 22:45:47 +03:00
|
|
|
dx = 50
|
|
|
|
dy = 350
|
2020-06-02 17:09:41 +03:00
|
|
|
line(f32(dx - 10), f32(dy), f32(dx + 250), f32(dy))
|
2022-04-11 22:23:06 +03:00
|
|
|
font_context.set_alignment(.left | .top)
|
|
|
|
dx = font_context.draw_text(dx, dy, 'Top')
|
2020-01-16 22:45:47 +03:00
|
|
|
dx += 10
|
2022-04-11 22:23:06 +03:00
|
|
|
font_context.set_alignment(.left | .middle)
|
|
|
|
dx = font_context.draw_text(dx, dy, 'Middle')
|
2020-01-16 22:45:47 +03:00
|
|
|
dx += 10
|
2022-04-11 22:23:06 +03:00
|
|
|
font_context.set_alignment(.left | .baseline)
|
|
|
|
dx = font_context.draw_text(dx, dy, 'Baseline')
|
2020-01-16 22:45:47 +03:00
|
|
|
dx += 10
|
2022-04-11 22:23:06 +03:00
|
|
|
font_context.set_alignment(.left | .bottom)
|
|
|
|
font_context.draw_text(dx, dy, 'Bottom')
|
2020-01-16 22:45:47 +03:00
|
|
|
dx = 150
|
|
|
|
dy = 400
|
2020-06-02 17:09:41 +03:00
|
|
|
line(f32(dx), f32(dy - 30), f32(dx), f32(dy + 80.0))
|
2022-04-11 22:23:06 +03:00
|
|
|
font_context.set_alignment(.left | .baseline)
|
|
|
|
font_context.draw_text(dx, dy, 'Left')
|
2020-01-16 22:45:47 +03:00
|
|
|
dy += 30
|
2022-04-11 22:23:06 +03:00
|
|
|
font_context.set_alignment(.center | .baseline)
|
|
|
|
font_context.draw_text(dx, dy, 'Center')
|
2020-01-16 22:45:47 +03:00
|
|
|
dy += 30
|
2022-04-11 22:23:06 +03:00
|
|
|
font_context.set_alignment(.right | .baseline)
|
|
|
|
font_context.draw_text(dx, dy, 'Right')
|
|
|
|
sfons.flush(font_context)
|
2020-01-16 22:45:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn line(sx f32, sy f32, ex f32, ey f32) {
|
2020-01-17 22:05:45 +03:00
|
|
|
sgl.begin_lines()
|
|
|
|
sgl.c4b(255, 255, 0, 128)
|
|
|
|
sgl.v2f(sx, sy)
|
|
|
|
sgl.v2f(ex, ey)
|
|
|
|
sgl.end()
|
2020-01-16 22:45:47 +03:00
|
|
|
}
|