2020-05-30 13:52:23 +03:00
|
|
|
|
import sokol
|
|
|
|
|
import sokol.sapp
|
|
|
|
|
import sokol.gfx
|
|
|
|
|
import sokol.sgl
|
|
|
|
|
import sokol.sfons
|
|
|
|
|
import os
|
|
|
|
|
import time
|
|
|
|
|
|
|
|
|
|
const (
|
2021-04-29 02:17:37 +03:00
|
|
|
|
text = '
|
2020-05-30 13:52:23 +03:00
|
|
|
|
Once upon a midnight dreary, while I pondered, weak and weary,
|
|
|
|
|
Over many a quaint and curious volume of forgotten lore—
|
|
|
|
|
While I nodded, nearly napping, suddenly there came a tapping,
|
|
|
|
|
As of some one gently rapping, rapping at my chamber door.
|
|
|
|
|
“’Tis some visitor,” I muttered, “tapping at my chamber door—
|
|
|
|
|
Only this and nothing more.”
|
|
|
|
|
|
|
|
|
|
Ah, distinctly I remember it was in the bleak December;
|
|
|
|
|
And each separate dying ember wrought its ghost upon the floor.
|
|
|
|
|
Eagerly I wished the morrow;—vainly I had sought to borrow
|
|
|
|
|
From my books surcease of sorrow—sorrow for the lost Lenore—
|
|
|
|
|
For the rare and radiant maiden whom the angels name Lenore—
|
|
|
|
|
Nameless here for evermore.
|
|
|
|
|
|
|
|
|
|
And the silken, sad, uncertain rustling of each purple curtain
|
|
|
|
|
Thrilled me—filled me with fantastic terrors never felt before;
|
|
|
|
|
So that now, to still the beating of my heart, I stood repeating
|
|
|
|
|
“’Tis some visitor entreating entrance at my chamber door—
|
|
|
|
|
Some late visitor entreating entrance at my chamber door;—
|
|
|
|
|
This it is and nothing more.”
|
|
|
|
|
|
|
|
|
|
Presently my soul grew stronger; hesitating then no longer,
|
|
|
|
|
“Sir,” said I, “or Madam, truly your forgiveness I implore;
|
|
|
|
|
But the fact is I was napping, and so gently you came rapping,
|
|
|
|
|
And so faintly you came tapping, tapping at my chamber door,
|
|
|
|
|
That I scarce was sure I heard you”—here I opened wide the door;—
|
|
|
|
|
Darkness there and nothing more.
|
|
|
|
|
|
|
|
|
|
Deep into that darkness peering, long I stood there wondering, fearing,
|
|
|
|
|
Doubting, dreaming dreams no mortal ever dared to dream before;
|
|
|
|
|
But the silence was unbroken, and the stillness gave no token,
|
|
|
|
|
And the only word there spoken was the whispered word, “Lenore?”
|
|
|
|
|
This I whispered, and an echo murmured back the word, “Lenore!”—
|
|
|
|
|
Merely this and nothing more.
|
|
|
|
|
|
|
|
|
|
Back into the chamber turning, all my soul within me burning,
|
|
|
|
|
Soon again I heard a tapping somewhat louder than before.
|
|
|
|
|
“Surely,” said I, “surely that is something at my window lattice;
|
|
|
|
|
Let me see, then, what thereat is, and this mystery explore—
|
|
|
|
|
Let my heart be still a moment and this mystery explore;—
|
|
|
|
|
’Tis the wind and nothing more!”
|
|
|
|
|
'
|
2021-04-20 17:16:35 +03:00
|
|
|
|
lines = text.split('\n')
|
2020-05-30 13:52:23 +03:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
struct AppState {
|
|
|
|
|
mut:
|
|
|
|
|
pass_action C.sg_pass_action
|
|
|
|
|
fons &C.FONScontext
|
|
|
|
|
font_normal int
|
2021-04-20 17:16:35 +03:00
|
|
|
|
inited bool
|
2020-05-30 13:52:23 +03:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-06 10:53:46 +03:00
|
|
|
|
[console]
|
2020-05-30 13:52:23 +03:00
|
|
|
|
fn main() {
|
|
|
|
|
mut color_action := C.sg_color_attachment_action{
|
2021-01-08 19:41:52 +03:00
|
|
|
|
action: gfx.Action(C.SG_ACTION_CLEAR)
|
2021-04-07 21:39:23 +03:00
|
|
|
|
value: C.sg_color{
|
|
|
|
|
r: 1.0
|
|
|
|
|
g: 1.0
|
|
|
|
|
b: 1.0
|
|
|
|
|
a: 1.0
|
|
|
|
|
}
|
2020-05-30 13:52:23 +03:00
|
|
|
|
}
|
|
|
|
|
mut pass_action := C.sg_pass_action{}
|
|
|
|
|
pass_action.colors[0] = color_action
|
|
|
|
|
state := &AppState{
|
|
|
|
|
pass_action: pass_action
|
|
|
|
|
fons: &C.FONScontext(0)
|
|
|
|
|
}
|
|
|
|
|
title := 'V Metal/GL Text Rendering'
|
|
|
|
|
desc := C.sapp_desc{
|
|
|
|
|
user_data: state
|
|
|
|
|
init_userdata_cb: init
|
|
|
|
|
frame_userdata_cb: frame
|
|
|
|
|
window_title: title.str
|
|
|
|
|
html5_canvas_name: title.str
|
|
|
|
|
width: 600
|
|
|
|
|
height: 700
|
|
|
|
|
high_dpi: true
|
|
|
|
|
}
|
|
|
|
|
sapp.run(&desc)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn init(user_data voidptr) {
|
|
|
|
|
mut state := &AppState(user_data)
|
2020-08-23 08:47:50 +03:00
|
|
|
|
desc := sapp.create_desc()
|
|
|
|
|
gfx.setup(&desc)
|
2020-05-30 13:52:23 +03:00
|
|
|
|
s := &C.sgl_desc_t{}
|
|
|
|
|
C.sgl_setup(s)
|
|
|
|
|
state.fons = sfons.create(512, 512, 1)
|
|
|
|
|
// 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-05-30 13:52:23 +03:00
|
|
|
|
println('loaded font: $bytes.len')
|
2021-05-20 09:17:44 +03:00
|
|
|
|
state.font_normal = C.fonsAddFontMem(state.fons, c'sans', bytes.data, bytes.len,
|
2021-04-20 17:16:35 +03:00
|
|
|
|
false)
|
2020-05-30 13:52:23 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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()
|
2021-04-20 17:16:35 +03:00
|
|
|
|
println(time.ticks() - t)
|
2020-05-30 13:52:23 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const (
|
2021-04-20 17:16:35 +03:00
|
|
|
|
black = C.sfons_rgba(0, 0, 0, 255)
|
2020-05-30 13:52:23 +03:00
|
|
|
|
)
|
|
|
|
|
|
2020-07-24 13:29:47 +03:00
|
|
|
|
fn (mut state AppState) render_font() {
|
2020-05-30 13:52:23 +03:00
|
|
|
|
lh := 30
|
|
|
|
|
mut dy := lh
|
|
|
|
|
if !state.inited {
|
|
|
|
|
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)
|
|
|
|
|
state.inited = true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for line in lines {
|
|
|
|
|
C.fonsDrawText(state.fons, 40, dy, line.str, C.NULL)
|
|
|
|
|
dy += lh
|
|
|
|
|
}
|
|
|
|
|
C.sfons_flush(state.fons)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn line(sx f32, sy f32, ex f32, ey f32) {
|
|
|
|
|
sgl.begin_lines()
|
|
|
|
|
sgl.c4b(255, 255, 0, 128)
|
|
|
|
|
sgl.v2f(sx, sy)
|
|
|
|
|
sgl.v2f(ex, ey)
|
|
|
|
|
sgl.end()
|
|
|
|
|
}
|