-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfont.sj
99 lines (89 loc) · 2.33 KB
/
font.sj
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
fontHash : hash![fontKey, weak font]()
font_load(src : 'string, size : 'f32)'heap font {
k : fontKey(
src : src
size : size)
w : fontHash[k]
h : heap w
ifValid h {
h
} elseEmpty {
result : heap font(
src : src
size : size)
fontHash[k] = weak result
result
}
}
fontKey(
src : 'string
size : 'f32
hash()'u32 {
src.hash() xor size.hash()
}
isEqual(x : 'fontKey) {
src == x.src && size == x.size
}
) { this }
font(
src : 'string
size : 'f32
--cvar--
texture_font_t* font;
texture_atlas_t* atlas;
--cvar--
getTexture()'texture {
w := 0
h := 0
id := 0u
--c--
w = _parent->atlas->width;
h = _parent->atlas->height;
id = _parent->atlas->id;
--c--
texture(size(w, h), id)
}
getTextSize(str : 'string)'size {
w := 0
h := 0
--c--
vec2 size = get_text_size(_parent->font, (char*)str->data.data);
w = (int)size.x;
h = (int)size.y;
--c--
size(w, h)
}
) {
--c--
_this->atlas = texture_atlas_new( 512, 512, 3 );
_this->font = texture_font_new_from_file(_this->atlas, _this->size, (char*)_this->src.data.data);
if (_this->font == 0) {
printf("texture_font_new_from_file Error\n");
}
glGenTextures( 1, &_this->atlas->id );
glBindTexture( GL_TEXTURE_2D, _this->atlas->id );
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, (int)_this->atlas->width, (int)_this->atlas->height, 0, GL_RGB, GL_UNSIGNED_BYTE, _this->atlas->data );
--c--
this
} copy {
--c--
_this->atlas = _from->atlas;
ptr_retain(_this->atlas);
_this->font = _from->font;
ptr_retain(_this->font);
--c--
} destroy {
--c--
if (ptr_release(_this->atlas)) {
texture_atlas_delete(_this->atlas);
}
if (ptr_release(_this->font)) {
texture_font_delete(_this->font);
}
--c--
}