examples: reduce C. prefix usage, rename fontstash enums (#14011)
parent
cb969e0934
commit
07207db998
|
@ -9,7 +9,7 @@ import os
|
||||||
struct AppState {
|
struct AppState {
|
||||||
mut:
|
mut:
|
||||||
pass_action gfx.PassAction
|
pass_action gfx.PassAction
|
||||||
fons &fontstash.Context
|
font_context &fontstash.Context
|
||||||
font_normal int
|
font_normal int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -28,7 +28,7 @@ fn main() {
|
||||||
pass_action.colors[0] = color_action
|
pass_action.colors[0] = color_action
|
||||||
state := &AppState{
|
state := &AppState{
|
||||||
pass_action: pass_action
|
pass_action: pass_action
|
||||||
fons: voidptr(0) // &fontstash.Context(0)
|
font_context: voidptr(0) // &fontstash.Context(0)
|
||||||
}
|
}
|
||||||
title := 'V Metal/GL Text Rendering'
|
title := 'V Metal/GL Text Rendering'
|
||||||
desc := sapp.Desc{
|
desc := sapp.Desc{
|
||||||
|
@ -45,14 +45,14 @@ fn init(mut state AppState) {
|
||||||
desc := sapp.create_desc()
|
desc := sapp.create_desc()
|
||||||
gfx.setup(&desc)
|
gfx.setup(&desc)
|
||||||
s := &sgl.Desc{}
|
s := &sgl.Desc{}
|
||||||
C.sgl_setup(s)
|
sgl.setup(s)
|
||||||
state.fons = sfons.create(512, 512, 1)
|
state.font_context = sfons.create(512, 512, 1)
|
||||||
// or use DroidSerif-Regular.ttf
|
// or use DroidSerif-Regular.ttf
|
||||||
if bytes := os.read_bytes(os.resource_abs_path(os.join_path('..', 'assets', 'fonts',
|
if bytes := os.read_bytes(os.resource_abs_path(os.join_path('..', 'assets', 'fonts',
|
||||||
'RobotoMono-Regular.ttf')))
|
'RobotoMono-Regular.ttf')))
|
||||||
{
|
{
|
||||||
println('loaded font: $bytes.len')
|
println('loaded font: $bytes.len')
|
||||||
state.font_normal = state.fons.add_font_mem('sans', bytes, false)
|
state.font_normal = state.font_context.add_font_mem('sans', bytes, false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -76,8 +76,8 @@ fn (state &AppState) render_font() {
|
||||||
brown := sfons.rgba(192, 128, 0, 128)
|
brown := sfons.rgba(192, 128, 0, 128)
|
||||||
blue := sfons.rgba(0, 192, 255, 255)
|
blue := sfons.rgba(0, 192, 255, 255)
|
||||||
|
|
||||||
fons := state.fons
|
font_context := state.font_context
|
||||||
fons.clear_state()
|
font_context.clear_state()
|
||||||
sgl.defaults()
|
sgl.defaults()
|
||||||
sgl.matrix_mode_projection()
|
sgl.matrix_mode_projection()
|
||||||
sgl.ortho(0.0, f32(sapp.width()), f32(sapp.height()), 0.0, -1.0, 1.0)
|
sgl.ortho(0.0, f32(sapp.width()), f32(sapp.height()), 0.0, -1.0, 1.0)
|
||||||
|
@ -85,78 +85,78 @@ fn (state &AppState) render_font() {
|
||||||
sy = 50
|
sy = 50
|
||||||
dx = sx
|
dx = sx
|
||||||
dy = sy
|
dy = sy
|
||||||
fons.set_font(state.font_normal)
|
font_context.set_font(state.font_normal)
|
||||||
fons.set_size(100.0)
|
font_context.set_size(100.0)
|
||||||
ascender := f32(0.0)
|
ascender := f32(0.0)
|
||||||
descender := f32(0.0)
|
descender := f32(0.0)
|
||||||
fons.vert_metrics(&ascender, &descender, &lh)
|
font_context.vert_metrics(&ascender, &descender, &lh)
|
||||||
dx = sx
|
dx = sx
|
||||||
dy += lh
|
dy += lh
|
||||||
fons.set_color(white)
|
font_context.set_color(white)
|
||||||
dx = fons.draw_text(dx, dy, 'The quick ')
|
dx = font_context.draw_text(dx, dy, 'The quick ')
|
||||||
fons.set_font(state.font_normal)
|
font_context.set_font(state.font_normal)
|
||||||
fons.set_size(48.0)
|
font_context.set_size(48.0)
|
||||||
fons.set_color(brown)
|
font_context.set_color(brown)
|
||||||
dx = fons.draw_text(dx, dy, 'brown ')
|
dx = font_context.draw_text(dx, dy, 'brown ')
|
||||||
fons.set_font(state.font_normal)
|
font_context.set_font(state.font_normal)
|
||||||
fons.set_size(24.0)
|
font_context.set_size(24.0)
|
||||||
fons.set_color(white)
|
font_context.set_color(white)
|
||||||
dx = fons.draw_text(dx, dy, 'fox ')
|
dx = font_context.draw_text(dx, dy, 'fox ')
|
||||||
dx = sx
|
dx = sx
|
||||||
dy += lh * 1.2
|
dy += lh * 1.2
|
||||||
fons.set_size(20.0)
|
font_context.set_size(20.0)
|
||||||
fons.set_font(state.font_normal)
|
font_context.set_font(state.font_normal)
|
||||||
fons.set_color(blue)
|
font_context.set_color(blue)
|
||||||
fons.draw_text(dx, dy, 'Now is the time for all good men to come to the aid of the party.')
|
font_context.draw_text(dx, dy, 'Now is the time for all good men to come to the aid of the party.')
|
||||||
dx = 300
|
dx = 300
|
||||||
dy = 350
|
dy = 350
|
||||||
fons.set_align(C.FONS_ALIGN_LEFT | C.FONS_ALIGN_BASELINE)
|
font_context.set_alignment(.left | .baseline)
|
||||||
fons.set_size(60.0)
|
font_context.set_size(60.0)
|
||||||
fons.set_font(state.font_normal)
|
font_context.set_font(state.font_normal)
|
||||||
fons.set_color(white)
|
font_context.set_color(white)
|
||||||
fons.set_spacing(5.0)
|
font_context.set_spacing(5.0)
|
||||||
fons.set_blur(6.0)
|
font_context.set_blur(6.0)
|
||||||
fons.draw_text(dx, dy, 'Blurry...')
|
font_context.draw_text(dx, dy, 'Blurry...')
|
||||||
dx = 300
|
dx = 300
|
||||||
dy += 50.0
|
dy += 50.0
|
||||||
fons.set_size(28.0)
|
font_context.set_size(28.0)
|
||||||
fons.set_font(state.font_normal)
|
font_context.set_font(state.font_normal)
|
||||||
fons.set_color(white)
|
font_context.set_color(white)
|
||||||
fons.set_spacing(0.0)
|
font_context.set_spacing(0.0)
|
||||||
fons.set_blur(3.0)
|
font_context.set_blur(3.0)
|
||||||
fons.draw_text(dx, dy + 2, 'DROP SHADOW')
|
font_context.draw_text(dx, dy + 2, 'DROP SHADOW')
|
||||||
fons.set_color(black)
|
font_context.set_color(black)
|
||||||
fons.set_blur(0)
|
font_context.set_blur(0)
|
||||||
fons.draw_text(dx, dy, 'DROP SHADOW')
|
font_context.draw_text(dx, dy, 'DROP SHADOW')
|
||||||
fons.set_size(18.0)
|
font_context.set_size(18.0)
|
||||||
fons.set_font(state.font_normal)
|
font_context.set_font(state.font_normal)
|
||||||
fons.set_color(white)
|
font_context.set_color(white)
|
||||||
dx = 50
|
dx = 50
|
||||||
dy = 350
|
dy = 350
|
||||||
line(f32(dx - 10), f32(dy), f32(dx + 250), f32(dy))
|
line(f32(dx - 10), f32(dy), f32(dx + 250), f32(dy))
|
||||||
fons.set_align(C.FONS_ALIGN_LEFT | C.FONS_ALIGN_TOP)
|
font_context.set_alignment(.left | .top)
|
||||||
dx = fons.draw_text(dx, dy, 'Top')
|
dx = font_context.draw_text(dx, dy, 'Top')
|
||||||
dx += 10
|
dx += 10
|
||||||
fons.set_align(C.FONS_ALIGN_LEFT | C.FONS_ALIGN_MIDDLE)
|
font_context.set_alignment(.left | .middle)
|
||||||
dx = fons.draw_text(dx, dy, 'Middle')
|
dx = font_context.draw_text(dx, dy, 'Middle')
|
||||||
dx += 10
|
dx += 10
|
||||||
fons.set_align(C.FONS_ALIGN_LEFT | C.FONS_ALIGN_BASELINE)
|
font_context.set_alignment(.left | .baseline)
|
||||||
dx = fons.draw_text(dx, dy, 'Baseline')
|
dx = font_context.draw_text(dx, dy, 'Baseline')
|
||||||
dx += 10
|
dx += 10
|
||||||
fons.set_align(C.FONS_ALIGN_LEFT | C.FONS_ALIGN_BOTTOM)
|
font_context.set_alignment(.left | .bottom)
|
||||||
fons.draw_text(dx, dy, 'Bottom')
|
font_context.draw_text(dx, dy, 'Bottom')
|
||||||
dx = 150
|
dx = 150
|
||||||
dy = 400
|
dy = 400
|
||||||
line(f32(dx), f32(dy - 30), f32(dx), f32(dy + 80.0))
|
line(f32(dx), f32(dy - 30), f32(dx), f32(dy + 80.0))
|
||||||
fons.set_align(C.FONS_ALIGN_LEFT | C.FONS_ALIGN_BASELINE)
|
font_context.set_alignment(.left | .baseline)
|
||||||
fons.draw_text(dx, dy, 'Left')
|
font_context.draw_text(dx, dy, 'Left')
|
||||||
dy += 30
|
dy += 30
|
||||||
fons.set_align(C.FONS_ALIGN_CENTER | C.FONS_ALIGN_BASELINE)
|
font_context.set_alignment(.center | .baseline)
|
||||||
fons.draw_text(dx, dy, 'Center')
|
font_context.draw_text(dx, dy, 'Center')
|
||||||
dy += 30
|
dy += 30
|
||||||
fons.set_align(C.FONS_ALIGN_RIGHT | C.FONS_ALIGN_BASELINE)
|
font_context.set_alignment(.right | .baseline)
|
||||||
fons.draw_text(dx, dy, 'Right')
|
font_context.draw_text(dx, dy, 'Right')
|
||||||
C.sfons_flush(fons)
|
sfons.flush(font_context)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn line(sx f32, sy f32, ex f32, ey f32) {
|
fn line(sx f32, sy f32, ex f32, ey f32) {
|
||||||
|
|
|
@ -97,7 +97,7 @@ fn init(user_data voidptr) {
|
||||||
desc := sapp.create_desc()
|
desc := sapp.create_desc()
|
||||||
gfx.setup(&desc)
|
gfx.setup(&desc)
|
||||||
s := &sgl.Desc{}
|
s := &sgl.Desc{}
|
||||||
C.sgl_setup(s)
|
sgl.setup(s)
|
||||||
state.fons = sfons.create(512, 512, 1)
|
state.fons = sfons.create(512, 512, 1)
|
||||||
// or use DroidSerif-Regular.ttf
|
// or use DroidSerif-Regular.ttf
|
||||||
if bytes := os.read_bytes(os.resource_abs_path(os.join_path('..', 'assets', 'fonts',
|
if bytes := os.read_bytes(os.resource_abs_path(os.join_path('..', 'assets', 'fonts',
|
||||||
|
|
|
@ -48,7 +48,7 @@ fn audio_player_callback(buffer &f32, num_frames int, num_channels int, mut p Pl
|
||||||
p.finished = true
|
p.finished = true
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
unsafe { C.memcpy(buffer, &p.samples[p.pos], nsamples * int(sizeof(f32))) }
|
unsafe { vmemcpy(buffer, &p.samples[p.pos], nsamples * int(sizeof(f32))) }
|
||||||
p.pos += nsamples
|
p.pos += nsamples
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -178,7 +178,17 @@ pub fn (s &Context) set_blur(blur f32) {
|
||||||
// See also: clear_state
|
// See also: clear_state
|
||||||
[inline]
|
[inline]
|
||||||
pub fn (s &Context) set_align(align int) {
|
pub fn (s &Context) set_align(align int) {
|
||||||
C.fonsSetAlign(s, align)
|
C.fonsSetAlign(s, int(align))
|
||||||
|
}
|
||||||
|
|
||||||
|
// set_alignment sets the font aligning to the `align` flags.
|
||||||
|
//
|
||||||
|
// See also: push_state
|
||||||
|
// See also: pop_state
|
||||||
|
// See also: clear_state
|
||||||
|
[inline]
|
||||||
|
pub fn (s &Context) set_alignment(align Align) {
|
||||||
|
C.fonsSetAlign(s, int(align))
|
||||||
}
|
}
|
||||||
|
|
||||||
// set_font sets the font used for this render on the active state.
|
// set_font sets the font used for this render on the active state.
|
||||||
|
|
|
@ -1,23 +1,25 @@
|
||||||
module fontstash
|
module fontstash
|
||||||
|
|
||||||
pub enum FonsFlags {
|
[flag]
|
||||||
top_left = 1
|
pub enum Flags {
|
||||||
bottom_left = 2
|
top_left
|
||||||
|
bottom_left
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum FonsAlign {
|
[flag]
|
||||||
|
pub enum Align {
|
||||||
// Horizontal align
|
// Horizontal align
|
||||||
left = 1 // Default
|
left // Default
|
||||||
center = 2
|
center
|
||||||
right = 4
|
right
|
||||||
// Vertical align
|
// Vertical align
|
||||||
top = 8
|
top
|
||||||
middle = 16
|
middle
|
||||||
bottom = 32
|
bottom
|
||||||
baseline = 64 // Default
|
baseline // Default
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum FonsErrorCode {
|
pub enum ErrorCode {
|
||||||
// Font atlas is full.
|
// Font atlas is full.
|
||||||
atlas_full = 1
|
atlas_full = 1
|
||||||
// Scratch memory used to render glyphs is full, requested size reported in 'val', you may need to bump up FONS_SCRATCH_BUF_SIZE.
|
// Scratch memory used to render glyphs is full, requested size reported in 'val', you may need to bump up FONS_SCRATCH_BUF_SIZE.
|
||||||
|
|
Loading…
Reference in New Issue