1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

examples: use fontstash wrapper calls (#12718)

This commit is contained in:
Larpon
2021-12-06 21:39:43 +01:00
committed by GitHub
parent 1cd703d96b
commit 047f059fb8
7 changed files with 145 additions and 143 deletions

View File

@ -2,13 +2,14 @@ import sokol
import sokol.sapp
import sokol.gfx
import sokol.sgl
import fontstash
import sokol.sfons
import os
struct AppState {
mut:
pass_action C.sg_pass_action
fons &C.FONScontext
fons &fontstash.Context
font_normal int
}
@ -27,7 +28,7 @@ fn main() {
pass_action.colors[0] = color_action
state := &AppState{
pass_action: pass_action
fons: &C.FONScontext(0)
fons: voidptr(0) // &fontstash.Context(0)
}
title := 'V Metal/GL Text Rendering'
desc := C.sapp_desc{
@ -51,8 +52,7 @@ fn init(mut state AppState) {
'RobotoMono-Regular.ttf')))
{
println('loaded font: $bytes.len')
state.font_normal = C.fonsAddFontMem(state.fons, c'sans', bytes.data, bytes.len,
false)
state.font_normal = state.fons.add_font_mem('sans', bytes, false)
}
}
@ -66,96 +66,97 @@ fn frame(user_data voidptr) {
}
fn (state &AppState) render_font() {
mut sx := 0.0
mut sy := 0.0
mut dx := 0.0
mut dy := 0.0
mut sx := f32(0.0)
mut sy := f32(0.0)
mut dx := f32(0.0)
mut dy := f32(0.0)
lh := f32(0.0)
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()
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)
fons := state.fons
fons.clear_state()
sgl.defaults()
sgl.matrix_mode_projection()
sgl.ortho(0.0, f32(C.sapp_width()), f32(C.sapp_height()), 0.0, -1.0, 1.0)
sgl.ortho(0.0, f32(sapp.width()), f32(sapp.height()), 0.0, -1.0, 1.0)
sx = 0
sy = 50
dx = sx
dy = sy
state.fons.set_font(state.font_normal)
state.fons.set_size(100.0)
fons.set_font(state.font_normal)
fons.set_size(100.0)
ascender := f32(0.0)
descender := f32(0.0)
state.fons.vert_metrics(&ascender, &descender, &lh)
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)
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)
fons.set_color(white)
dx = fons.draw_text(dx, dy, 'The quick ')
fons.set_font(state.font_normal)
fons.set_size(48.0)
fons.set_color(brown)
dx = fons.draw_text(dx, dy, 'brown ')
fons.set_font(state.font_normal)
fons.set_size(24.0)
fons.set_color(white)
dx = fons.draw_text(dx, dy, 'fox ')
dx = sx
dy += lh * 1.2
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)
fons.set_size(20.0)
fons.set_font(state.font_normal)
fons.set_color(blue)
fons.draw_text(dx, dy, 'Now is the time for all good men to come to the aid of the party.')
dx = 300
dy = 350
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)
fons.set_align(C.FONS_ALIGN_LEFT | C.FONS_ALIGN_BASELINE)
fons.set_size(60.0)
fons.set_font(state.font_normal)
fons.set_color(white)
fons.set_spacing(5.0)
fons.set_blur(6.0)
fons.draw_text(dx, dy, 'Blurry...')
dx = 300
dy += 50.0
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)
fons.set_size(28.0)
fons.set_font(state.font_normal)
fons.set_color(white)
fons.set_spacing(0.0)
fons.set_blur(3.0)
fons.draw_text(dx, dy + 2, 'DROP SHADOW')
fons.set_color(black)
fons.set_blur(0)
fons.draw_text(dx, dy, 'DROP SHADOW')
fons.set_size(18.0)
fons.set_font(state.font_normal)
fons.set_color(white)
dx = 50
dy = 350
line(f32(dx - 10), f32(dy), f32(dx + 250), f32(dy))
C.fonsSetAlign(state.fons, C.FONS_ALIGN_LEFT | C.FONS_ALIGN_TOP)
dx = C.fonsDrawText(state.fons, dx, dy, c'Top', C.NULL)
fons.set_align(C.FONS_ALIGN_LEFT | C.FONS_ALIGN_TOP)
dx = fons.draw_text(dx, dy, 'Top')
dx += 10
C.fonsSetAlign(state.fons, C.FONS_ALIGN_LEFT | C.FONS_ALIGN_MIDDLE)
dx = C.fonsDrawText(state.fons, dx, dy, c'Middle', C.NULL)
fons.set_align(C.FONS_ALIGN_LEFT | C.FONS_ALIGN_MIDDLE)
dx = fons.draw_text(dx, dy, 'Middle')
dx += 10
C.fonsSetAlign(state.fons, C.FONS_ALIGN_LEFT | C.FONS_ALIGN_BASELINE)
dx = C.fonsDrawText(state.fons, dx, dy, c'Baseline', C.NULL)
fons.set_align(C.FONS_ALIGN_LEFT | C.FONS_ALIGN_BASELINE)
dx = fons.draw_text(dx, dy, 'Baseline')
dx += 10
C.fonsSetAlign(state.fons, C.FONS_ALIGN_LEFT | C.FONS_ALIGN_BOTTOM)
C.fonsDrawText(state.fons, dx, dy, c'Bottom', C.NULL)
fons.set_align(C.FONS_ALIGN_LEFT | C.FONS_ALIGN_BOTTOM)
fons.draw_text(dx, dy, 'Bottom')
dx = 150
dy = 400
line(f32(dx), f32(dy - 30), f32(dx), f32(dy + 80.0))
C.fonsSetAlign(state.fons, C.FONS_ALIGN_LEFT | C.FONS_ALIGN_BASELINE)
C.fonsDrawText(state.fons, dx, dy, c'Left', C.NULL)
fons.set_align(C.FONS_ALIGN_LEFT | C.FONS_ALIGN_BASELINE)
fons.draw_text(dx, dy, 'Left')
dy += 30
C.fonsSetAlign(state.fons, C.FONS_ALIGN_CENTER | C.FONS_ALIGN_BASELINE)
C.fonsDrawText(state.fons, dx, dy, c'Center', C.NULL)
fons.set_align(C.FONS_ALIGN_CENTER | C.FONS_ALIGN_BASELINE)
fons.draw_text(dx, dy, 'Center')
dy += 30
C.fonsSetAlign(state.fons, C.FONS_ALIGN_RIGHT | C.FONS_ALIGN_BASELINE)
C.fonsDrawText(state.fons, dx, dy, c'Right', C.NULL)
C.sfons_flush(state.fons)
fons.set_align(C.FONS_ALIGN_RIGHT | C.FONS_ALIGN_BASELINE)
fons.draw_text(dx, dy, 'Right')
C.sfons_flush(fons)
}
fn line(sx f32, sy f32, ex f32, ey f32) {

View File

@ -2,9 +2,9 @@ import sokol
import sokol.sapp
import sokol.gfx
import sokol.sgl
import fontstash
import sokol.sfons
import os
import time
const (
text = '
@ -56,7 +56,7 @@ Let my heart be still a moment and this mystery explore;—
struct AppState {
mut:
pass_action C.sg_pass_action
fons &C.FONScontext
fons &fontstash.Context
font_normal int
inited bool
}
@ -76,7 +76,7 @@ fn main() {
pass_action.colors[0] = color_action
state := &AppState{
pass_action: pass_action
fons: &C.FONScontext(0)
fons: voidptr(0) // &fontstash.Context(0)
}
title := 'V Metal/GL Text Rendering'
desc := C.sapp_desc{
@ -104,47 +104,45 @@ fn init(user_data voidptr) {
'RobotoMono-Regular.ttf')))
{
println('loaded font: $bytes.len')
state.font_normal = C.fonsAddFontMem(state.fons, c'sans', bytes.data, bytes.len,
false)
state.font_normal = state.fons.add_font_mem('sans', bytes, false)
}
}
fn frame(user_data voidptr) {
t := time.ticks()
mut state := &AppState(user_data)
state.render_font()
gfx.begin_default_pass(&state.pass_action, sapp.width(), sapp.height())
sgl.draw()
gfx.end_pass()
gfx.commit()
println(time.ticks() - t)
}
const (
black = C.sfons_rgba(0, 0, 0, 255)
black = sfons.rgba(0, 0, 0, 255)
)
fn (mut state AppState) render_font() {
lh := 30
mut dy := lh
mut fons := state.fons
if !state.inited {
state.fons.clear_state()
fons.clear_state()
sgl.defaults()
sgl.matrix_mode_projection()
sgl.ortho(0.0, f32(C.sapp_width()), f32(C.sapp_height()), 0.0, -1.0, 1.0)
state.fons.set_font(state.font_normal)
state.fons.set_size(100.0)
C.fonsSetColor(state.fons, black)
C.fonsSetFont(state.fons, state.font_normal)
C.fonsSetSize(state.fons, 35.0)
sgl.ortho(0.0, f32(sapp.width()), f32(sapp.height()), 0.0, -1.0, 1.0)
fons.set_font(state.font_normal)
fons.set_size(100.0)
fons.set_color(black)
fons.set_font(state.font_normal)
fons.set_size(35.0)
state.inited = true
}
for line in lines {
C.fonsDrawText(state.fons, 40, dy, line.str, C.NULL)
fons.draw_text(40, dy, line)
dy += lh
}
C.sfons_flush(state.fons)
sfons.flush(fons)
}
fn line(sx f32, sy f32, ex f32, ey f32) {