v2 / examples / gg / raven_text_rendering.v
95 lines · 83 sloc · 3.13 KB · bbb61ab3687afe512a1fa12492c876d011626107
Raw
1module main
2
3import gg
4import os.asset
5import math
6
7const win_width = 600
8const win_height = 700
9const bg_color = gg.white
10
11const text = '
12Once upon a midnight dreary, while I pondered, weak and weary,
13Over many a quaint and curious volume of forgotten lore—
14 While I nodded, nearly napping, suddenly there came a tapping,
15As of some one gently rapping, rapping at my chamber door.
16“’Tis some visitor,” I muttered, “tapping at my chamber door—
17 Only this and nothing more.”
18
19 Ah, distinctly I remember it was in the bleak December;
20And each separate dying ember wrought its ghost upon the floor.
21 Eagerly I wished the morrow;—vainly I had sought to borrow
22 From my books surcease of sorrow—sorrow for the lost Lenore—
23For the rare and radiant maiden whom the angels name Lenore—
24 Nameless here for evermore.
25
26 And the silken, sad, uncertain rustling of each purple curtain
27Thrilled me—filled me with fantastic terrors never felt before;
28 So that now, to still the beating of my heart, I stood repeating
29 “’Tis some visitor entreating entrance at my chamber door—
30Some late visitor entreating entrance at my chamber door;—
31 This it is and nothing more.”
32
33 Presently my soul grew stronger; hesitating then no longer,
34“Sir,” said I, “or Madam, truly your forgiveness I implore;
35 But the fact is I was napping, and so gently you came rapping,
36 And so faintly you came tapping, tapping at my chamber door,
37That I scarce was sure I heard you”—here I opened wide the door;—
38 Darkness there and nothing more.
39
40Deep into that darkness peering, long I stood there wondering, fearing,
41Doubting, dreaming dreams no mortal ever dared to dream before;
42 But the silence was unbroken, and the stillness gave no token,
43 And the only word there spoken was the whispered word, “Lenore?”
44This I whispered, and an echo murmured back the word, “Lenore!”—
45 Merely this and nothing more.
46
47 Back into the chamber turning, all my soul within me burning,
48Soon again I heard a tapping somewhat louder than before.
49 “Surely,” said I, “surely that is something at my window lattice;
50 Let me see, then, what thereat is, and this mystery explore—
51Let my heart be still a moment and this mystery explore;—
52 ’Tis the wind and nothing more!”
53'
54
55const lines = text.split('\n')
56
57struct App {
58mut:
59 gg &gg.Context = unsafe { nil }
60}
61
62fn main() {
63 mut app := &App{}
64 app.gg = gg.new_context(
65 width: win_width
66 height: win_height
67 create_window: true
68 window_title: 'Raven text'
69 user_data: app
70 bg_color: bg_color
71 frame_fn: frame
72 font_path: asset.get_path('../assets', 'fonts/RobotoMono-Regular.ttf')
73 // window_user_ptr: ctx
74 // native_rendering: true
75 )
76 app.gg.run()
77}
78
79fn frame(mut app App) {
80 app.gg.begin()
81 width := gg.window_size().width
82 mut scale_factor := math.round(f32(width) / win_width)
83 if scale_factor <= 0 {
84 scale_factor = 1
85 }
86 text_cfg := gg.TextCfg{
87 size: 16 * int(scale_factor)
88 }
89 mut y := 10
90 for line in lines {
91 app.gg.draw_text(10, y, line, text_cfg)
92 y += 30
93 }
94 app.gg.end()
95}
96