2020-01-16 20:45:47 +01:00
|
|
|
import sokol
|
|
|
|
import sokol.sapp
|
|
|
|
import sokol.gfx
|
|
|
|
import sokol.sgl
|
|
|
|
import sokol.sfons
|
|
|
|
import os
|
|
|
|
import time
|
|
|
|
|
|
|
|
struct AppState {
|
|
|
|
mut:
|
|
|
|
pass_action C.sg_pass_action
|
2020-01-17 20:05:45 +01:00
|
|
|
fons &C.FONScontext
|
2020-01-16 20:45:47 +01:00
|
|
|
font_normal int
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2020-05-15 18:55:29 +02:00
|
|
|
mut color_action := C.sg_color_attachment_action{
|
2020-01-16 20:45:47 +01:00
|
|
|
action: C.SG_ACTION_CLEAR
|
|
|
|
}
|
|
|
|
color_action.val[0] = 0.3
|
|
|
|
color_action.val[1] = 0.3
|
|
|
|
color_action.val[2] = 0.32
|
|
|
|
color_action.val[3] = 1.0
|
2020-05-15 18:55:29 +02:00
|
|
|
mut pass_action := C.sg_pass_action{}
|
2020-01-16 20:45:47 +01:00
|
|
|
pass_action.colors[0] = color_action
|
|
|
|
state := &AppState{
|
|
|
|
pass_action: pass_action
|
|
|
|
fons: &C.FONScontext(0)
|
|
|
|
}
|
2020-01-17 20:05:45 +01:00
|
|
|
title := 'V Metal/GL Text Rendering'
|
2020-05-15 18:55:29 +02:00
|
|
|
desc := C.sapp_desc{
|
2020-01-16 20:45:47 +01:00
|
|
|
user_data: state
|
|
|
|
init_userdata_cb: init
|
|
|
|
frame_userdata_cb: frame
|
2020-01-17 20:05:45 +01:00
|
|
|
window_title: title.str
|
|
|
|
html5_canvas_name: title.str
|
2020-01-16 20:45:47 +01:00
|
|
|
}
|
|
|
|
sapp.run(&desc)
|
|
|
|
}
|
|
|
|
|
2020-06-04 10:35:40 +02:00
|
|
|
fn init(mut state AppState) {
|
2020-01-16 20:45:47 +01:00
|
|
|
// dont actually alocate this on the heap in real life
|
2020-05-15 18:55:29 +02:00
|
|
|
gfx.setup(&C.sg_desc{
|
2020-03-26 08:54:33 +01:00
|
|
|
mtl_device: sapp.metal_get_device()
|
|
|
|
mtl_renderpass_descriptor_cb: sapp.metal_get_renderpass_descriptor
|
|
|
|
mtl_drawable_cb: sapp.metal_get_drawable
|
|
|
|
d3d11_device: sapp.d3d11_get_device()
|
|
|
|
d3d11_device_context: sapp.d3d11_get_device_context()
|
|
|
|
d3d11_render_target_view_cb: sapp.d3d11_get_render_target_view
|
|
|
|
d3d11_depth_stencil_view_cb: sapp.d3d11_get_depth_stencil_view
|
2020-01-16 20:45:47 +01:00
|
|
|
})
|
|
|
|
s := &C.sgl_desc_t{}
|
|
|
|
C.sgl_setup(s)
|
2020-01-21 03:22:18 +01:00
|
|
|
state.fons = sfons.create(512, 512, 1)
|
2020-01-16 20:45:47 +01:00
|
|
|
// or use DroidSerif-Regular.ttf
|
2020-03-07 14:14:37 +01:00
|
|
|
if bytes := os.read_bytes(os.resource_abs_path('../assets/fonts/RobotoMono-Regular.ttf')) {
|
2020-01-16 20:45:47 +01:00
|
|
|
println('loaded font: $bytes.len')
|
2020-01-17 20:05:45 +01:00
|
|
|
state.font_normal = C.fonsAddFontMem(state.fons, 'sans', bytes.data, bytes.len, false)
|
2020-01-16 20:45:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn frame(user_data voidptr) {
|
2020-01-17 20:05:45 +01:00
|
|
|
t := time.ticks()
|
2020-01-16 20:45:47 +01:00
|
|
|
mut state := &AppState(user_data)
|
|
|
|
state.render_font()
|
2020-03-26 08:54:33 +01:00
|
|
|
gfx.begin_default_pass(&state.pass_action, sapp.width(), sapp.height())
|
2020-01-16 20:45:47 +01:00
|
|
|
sgl.draw()
|
|
|
|
gfx.end_pass()
|
|
|
|
gfx.commit()
|
2020-01-17 20:05:45 +01:00
|
|
|
// println(time.ticks()-t)
|
2020-01-16 20:45:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (state &AppState) render_font() {
|
|
|
|
mut sx := 0.0
|
|
|
|
mut sy := 0.0
|
|
|
|
mut dx := 0.0
|
|
|
|
mut dy := 0.0
|
2020-03-19 07:24:49 +01:00
|
|
|
lh := f32(0.0)
|
2020-01-17 20:05:45 +01:00
|
|
|
white := C.sfons_rgba(255, 255, 255, 255)
|
|
|
|
black := C.sfons_rgba(0, 0, 0, 255)
|
|
|
|
brown := C.sfons_rgba(192, 128, 0, 128)
|
|
|
|
blue := C.sfons_rgba(0, 192, 255, 255)
|
|
|
|
state.fons.clear_state()
|
2020-01-16 20:45:47 +01:00
|
|
|
sgl.defaults()
|
2020-01-17 20:05:45 +01:00
|
|
|
sgl.matrix_mode_projection()
|
|
|
|
sgl.ortho(0.0, f32(C.sapp_width()), f32(C.sapp_height()), 0.0, -1.0, 1.0)
|
2020-01-16 20:45:47 +01:00
|
|
|
sx = 0
|
|
|
|
sy = 50
|
2020-01-17 20:05:45 +01:00
|
|
|
dx = sx
|
2020-01-16 20:45:47 +01:00
|
|
|
dy = sy
|
|
|
|
state.fons.set_font(state.font_normal)
|
|
|
|
state.fons.set_size(100.0)
|
2020-03-19 07:24:49 +01:00
|
|
|
ascender := f32(0.0)
|
|
|
|
descender := f32(0.0)
|
2020-01-16 20:45:47 +01:00
|
|
|
state.fons.vert_metrics(&ascender, &descender, &lh)
|
|
|
|
dx = sx
|
|
|
|
dy += lh
|
|
|
|
C.fonsSetColor(state.fons, white)
|
|
|
|
dx = C.fonsDrawText(state.fons, dx, dy, c'The quick ', C.NULL)
|
2020-03-26 08:54:33 +01:00
|
|
|
C.fonsSetFont(state.fons, state.font_normal)
|
|
|
|
C.fonsSetSize(state.fons, 48.0)
|
|
|
|
C.fonsSetColor(state.fons, brown)
|
|
|
|
dx = C.fonsDrawText(state.fons, dx, dy, c'brown ', C.NULL)
|
|
|
|
C.fonsSetFont(state.fons, state.font_normal)
|
|
|
|
C.fonsSetSize(state.fons, 24.0)
|
|
|
|
C.fonsSetColor(state.fons, white)
|
|
|
|
dx = C.fonsDrawText(state.fons, dx, dy, c'fox ', C.NULL)
|
2020-01-16 20:45:47 +01:00
|
|
|
dx = sx
|
|
|
|
dy += lh * 1.2
|
2020-03-26 08:54:33 +01:00
|
|
|
C.fonsSetSize(state.fons, 20.0)
|
|
|
|
C.fonsSetFont(state.fons, state.font_normal)
|
|
|
|
C.fonsSetColor(state.fons, blue)
|
|
|
|
C.fonsDrawText(state.fons, dx, dy, c'Now is the time for all good men to come to the aid of the party.', C.NULL)
|
2020-01-16 20:45:47 +01:00
|
|
|
dx = 300
|
|
|
|
dy = 350
|
2020-03-26 08:54:33 +01:00
|
|
|
C.fonsSetAlign(state.fons, C.FONS_ALIGN_LEFT | C.FONS_ALIGN_BASELINE)
|
|
|
|
C.fonsSetSize(state.fons, 60.0)
|
|
|
|
C.fonsSetFont(state.fons, state.font_normal)
|
|
|
|
C.fonsSetColor(state.fons, white)
|
|
|
|
C.fonsSetSpacing(state.fons, 5.0)
|
|
|
|
C.fonsSetBlur(state.fons, 6.0)
|
|
|
|
C.fonsDrawText(state.fons, dx, dy, c'Blurry...', C.NULL)
|
2020-01-16 20:45:47 +01:00
|
|
|
dx = 300
|
|
|
|
dy += 50.0
|
2020-03-26 08:54:33 +01:00
|
|
|
C.fonsSetSize(state.fons, 28.0)
|
|
|
|
C.fonsSetFont(state.fons, state.font_normal)
|
|
|
|
C.fonsSetColor(state.fons, white)
|
|
|
|
C.fonsSetSpacing(state.fons, 0.0)
|
|
|
|
C.fonsSetBlur(state.fons, 3.0)
|
|
|
|
C.fonsDrawText(state.fons, dx, dy + 2, c'DROP SHADOW', C.NULL)
|
|
|
|
C.fonsSetColor(state.fons, black)
|
|
|
|
C.fonsSetBlur(state.fons, 0)
|
|
|
|
C.fonsDrawText(state.fons, dx, dy, c'DROP SHADOW', C.NULL)
|
|
|
|
C.fonsSetSize(state.fons, 18.0)
|
|
|
|
C.fonsSetFont(state.fons, state.font_normal)
|
|
|
|
C.fonsSetColor(state.fons, white)
|
2020-01-16 20:45:47 +01:00
|
|
|
dx = 50
|
|
|
|
dy = 350
|
2020-06-02 16:09:41 +02:00
|
|
|
line(f32(dx - 10), f32(dy), f32(dx + 250), f32(dy))
|
2020-01-16 20:45:47 +01:00
|
|
|
C.fonsSetAlign(state.fons, C.FONS_ALIGN_LEFT | C.FONS_ALIGN_TOP)
|
2020-01-17 20:05:45 +01:00
|
|
|
dx = C.fonsDrawText(state.fons, dx, dy, c'Top', C.NULL)
|
2020-01-16 20:45:47 +01:00
|
|
|
dx += 10
|
|
|
|
C.fonsSetAlign(state.fons, C.FONS_ALIGN_LEFT | C.FONS_ALIGN_MIDDLE)
|
2020-01-17 20:05:45 +01:00
|
|
|
dx = C.fonsDrawText(state.fons, dx, dy, c'Middle', C.NULL)
|
2020-01-16 20:45:47 +01:00
|
|
|
dx += 10
|
|
|
|
C.fonsSetAlign(state.fons, C.FONS_ALIGN_LEFT | C.FONS_ALIGN_BASELINE)
|
2020-01-17 20:05:45 +01:00
|
|
|
dx = C.fonsDrawText(state.fons, dx, dy, c'Baseline', C.NULL)
|
2020-01-16 20:45:47 +01:00
|
|
|
dx += 10
|
|
|
|
C.fonsSetAlign(state.fons, C.FONS_ALIGN_LEFT | C.FONS_ALIGN_BOTTOM)
|
2020-01-17 20:05:45 +01:00
|
|
|
C.fonsDrawText(state.fons, dx, dy, c'Bottom', C.NULL)
|
2020-01-16 20:45:47 +01:00
|
|
|
dx = 150
|
|
|
|
dy = 400
|
2020-06-02 16:09:41 +02:00
|
|
|
line(f32(dx), f32(dy - 30), f32(dx), f32(dy + 80.0))
|
2020-01-16 20:45:47 +01:00
|
|
|
C.fonsSetAlign(state.fons, C.FONS_ALIGN_LEFT | C.FONS_ALIGN_BASELINE)
|
2020-01-17 20:05:45 +01:00
|
|
|
C.fonsDrawText(state.fons, dx, dy, c'Left', C.NULL)
|
2020-01-16 20:45:47 +01:00
|
|
|
dy += 30
|
|
|
|
C.fonsSetAlign(state.fons, C.FONS_ALIGN_CENTER | C.FONS_ALIGN_BASELINE)
|
2020-01-17 20:05:45 +01:00
|
|
|
C.fonsDrawText(state.fons, dx, dy, c'Center', C.NULL)
|
2020-01-16 20:45:47 +01:00
|
|
|
dy += 30
|
|
|
|
C.fonsSetAlign(state.fons, C.FONS_ALIGN_RIGHT | C.FONS_ALIGN_BASELINE)
|
2020-01-17 20:05:45 +01:00
|
|
|
C.fonsDrawText(state.fons, dx, dy, c'Right', C.NULL)
|
2020-01-16 20:45:47 +01:00
|
|
|
C.sfons_flush(state.fons)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn line(sx f32, sy f32, ex f32, ey f32) {
|
2020-01-17 20:05:45 +01:00
|
|
|
sgl.begin_lines()
|
|
|
|
sgl.c4b(255, 255, 0, 128)
|
|
|
|
sgl.v2f(sx, sy)
|
|
|
|
sgl.v2f(ex, ey)
|
|
|
|
sgl.end()
|
2020-01-16 20:45:47 +01:00
|
|
|
}
|