1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
module ttf
/**********************************************************************
*
* BMP render module utility functions
*
* Copyright (c) 2021 Dario Deledda. All rights reserved.
* Use of this source code is governed by an MIT license
* that can be found in the LICENSE file.
*
* Note:
*
* TODO:
**********************************************************************/
import math
import gg
import sokol.sgl
pub struct TTF_render_Sokol {
pub mut:
bmp &BitMap // Base bitmap render
// rendering fields
sg_img C.sg_image // sokol image
scale_reduct f32 = 2.0 // scale of the cpu texture for filtering
device_dpi int = 72 // device DPI
}
/******************************************************************************
*
* Render functions
*
******************************************************************************/
pub fn (mut tf_skl TTF_render_Sokol) format_texture() {
tf_skl.bmp.format_texture()
}
pub fn (mut tf_skl TTF_render_Sokol) create_text(in_txt string, in_font_size f32) {
scale_reduct := tf_skl.scale_reduct
device_dpi := tf_skl.device_dpi
font_size := in_font_size //* scale_reduct
// Formula: (font_size * device dpi) / (72dpi * em_unit)
// scale := ((1.0 * devide_dpi )/ f32(72 * tf_skl.bmp.tf.units_per_em))* font_size
scale := f32(font_size * device_dpi) / f32(72 * tf_skl.bmp.tf.units_per_em)
// dprintln("Scale: $scale")
tf_skl.bmp.scale = scale * scale_reduct
w, h := tf_skl.bmp.get_bbox(in_txt)
tf_skl.bmp.width = int(w)
tf_skl.bmp.height = int((h + 8))
sz := tf_skl.bmp.width * tf_skl.bmp.height * tf_skl.bmp.bp
// RAM buffer
if sz > tf_skl.bmp.buf_size {
if sz > 0 {
unsafe { free(tf_skl.bmp.buf) }
}
dprintln('create_text Alloc: $sz bytes')
tf_skl.bmp.buf = unsafe { malloc_noscan(sz) }
tf_skl.bmp.buf_size = sz
}
tf_skl.bmp.init_filler()
// draw the text
mut y_base := int((tf_skl.bmp.tf.y_max - tf_skl.bmp.tf.y_min) * tf_skl.bmp.scale)
tf_skl.bmp.set_pos(0, y_base)
tf_skl.bmp.clear()
tf_skl.bmp.draw_text(in_txt)
tf_skl.format_texture()
}
pub fn (mut tf_skl TTF_render_Sokol) create_text_block(in_txt string, in_w int, in_h int, in_font_size f32) {
scale_reduct := tf_skl.scale_reduct
device_dpi := tf_skl.device_dpi
font_size := in_font_size //* scale_reduct
// Formula: (font_size * device dpi) / (72dpi * em_unit)
// scale := ((1.0 * devide_dpi )/ f32(72 * tf_skl.bmp.tf.units_per_em))* font_size
scale := f32(font_size * device_dpi) / f32(72 * tf_skl.bmp.tf.units_per_em)
// dprintln("Scale: $scale")
tf_skl.bmp.scale = scale * scale_reduct
w := in_w
h := in_h
tf_skl.bmp.width = int(w * scale_reduct + 0.5)
tf_skl.bmp.height = int((h + 2) * scale_reduct + 0.5)
sz := tf_skl.bmp.width * tf_skl.bmp.height * tf_skl.bmp.bp
// if true { return }
// RAM buffer
if sz > tf_skl.bmp.buf_size {
if sz > 0 {
unsafe { free(tf_skl.bmp.buf) }
}
dprintln('Alloc: $sz bytes')
tf_skl.bmp.buf = unsafe { malloc_noscan(sz) }
tf_skl.bmp.buf_size = sz
}
tf_skl.bmp.init_filler()
// draw the text
mut y_base := int((tf_skl.bmp.tf.y_max - tf_skl.bmp.tf.y_min) * tf_skl.bmp.scale)
tf_skl.bmp.set_pos(0, y_base)
tf_skl.bmp.clear()
tf_skl.bmp.draw_text_block(in_txt, x: 0, y: 0, w: w, h: h)
tf_skl.format_texture()
}
/******************************************************************************
*
* Sokol Render functions
*
******************************************************************************/
pub fn (mut tf_skl TTF_render_Sokol) create_texture() {
w := tf_skl.bmp.width
h := tf_skl.bmp.height
sz := tf_skl.bmp.width * tf_skl.bmp.height * tf_skl.bmp.bp
mut img_desc := C.sg_image_desc{
width: w
height: h
num_mipmaps: 0
min_filter: .linear
mag_filter: .linear
// usage: .dynamic
wrap_u: .clamp_to_edge
wrap_v: .clamp_to_edge
label: &char(0)
d3d11_texture: 0
}
// comment for dynamic
img_desc.data.subimage[0][0] = C.sg_range{
ptr: tf_skl.bmp.buf
size: size_t(sz)
}
simg := C.sg_make_image(&img_desc)
// free(tf_skl.bmp.buf) // DONT FREE IF Dynamic
tf_skl.sg_img = simg
}
pub fn (tf_skl TTF_render_Sokol) destroy_texture() {
C.sg_destroy_image(tf_skl.sg_img)
}
// Use only if usage: .dynamic
pub fn (mut tf_skl TTF_render_Sokol) update_text_texture() {
sz := tf_skl.bmp.width * tf_skl.bmp.height * tf_skl.bmp.bp
mut tmp_sbc := C.sg_image_data{}
tmp_sbc.subimage[0][0] = C.sg_range{
ptr: tf_skl.bmp.buf
size: size_t(sz)
}
C.sg_update_image(tf_skl.sg_img, &tmp_sbc)
}
pub fn (tf_skl TTF_render_Sokol) draw_text_bmp(ctx &gg.Context, x f32, y f32) {
// width := tf_skl.bmp.width >> 1
// height := tf_skl.bmp.height >> 1
sgl.push_matrix()
width := tf_skl.bmp.width / (tf_skl.scale_reduct)
height := tf_skl.bmp.height / (tf_skl.scale_reduct)
u0 := f32(0.0)
v0 := f32(0.0)
u1 := f32(1.0)
v1 := f32(1.0)
x0 := f32(0)
y0 := f32(0)
x1 := f32(width) * ctx.scale
y1 := f32(height) * ctx.scale
ca := f32(math.cos(tf_skl.bmp.angle))
sa := f32(math.sin(tf_skl.bmp.angle))
m := [
f32(ca),
-sa,
0,
0,
sa,
ca,
0,
0,
0,
0,
1,
0,
x * ctx.scale,
y * ctx.scale,
0,
1,
]
sgl.mult_matrix(m)
//
sgl.load_pipeline(ctx.timage_pip)
sgl.enable_texture()
sgl.texture(tf_skl.sg_img)
sgl.begin_quads()
sgl.c4b(255, 255, 255, 255)
sgl.v2f_t2f(x0, y0, u0, v0)
sgl.v2f_t2f(x1, y0, u1, v0)
sgl.v2f_t2f(x1, y1, u1, v1)
sgl.v2f_t2f(x0, y1, u0, v1)
sgl.end()
sgl.disable_texture()
sgl.pop_matrix()
}
|