1 | typedef struct { |
1 | #include <vector> |
|
|
2 | |
|
|
3 | #include "salloc.h" |
|
|
4 | |
|
|
5 | struct rc_key_t |
|
|
6 | { |
2 | GLenum mode; |
7 | GLenum mode; |
3 | GLenum format; // 0, GL_T2F_V3F, GL_V2F |
8 | GLenum format; // 0, GL_T2F_V3F, GL_V2F |
4 | GLuint texname; |
9 | GLuint texname; |
5 | unsigned char r, g, b, a; |
10 | unsigned char r, g, b, a; |
6 | } rc_key_t; |
|
|
7 | |
11 | |
8 | typedef struct { |
12 | bool operator == (const rc_key_t &o) const |
9 | HV *hv; |
13 | { |
10 | } rc_t; |
14 | return mode == o.mode |
|
|
15 | && format == o.format |
|
|
16 | && texname == o.texname |
|
|
17 | && r == o.r |
|
|
18 | && g == o.g |
|
|
19 | && b == o.b |
|
|
20 | && a == o.a; |
|
|
21 | } |
|
|
22 | }; |
11 | |
23 | |
12 | typedef SV rc_array_t; |
24 | namespace std { |
13 | |
25 | template <> |
14 | static rc_t * |
26 | struct hash<rc_key_t> |
15 | rc_alloc () |
27 | { |
16 | { |
28 | size_t operator () (const rc_key_t &v) const |
17 | rc_t *rc = g_slice_new0 (rc_t); |
29 | { |
18 | rc->hv = newHV (); |
30 | return v.mode + (v.format << 4) + v.texname + (v.r << 8) + (v.g << 16) + (v.b << 24) + v.a; |
19 | |
31 | } |
20 | return rc; |
32 | }; |
21 | } |
33 | } |
22 | |
34 | |
23 | static void |
35 | struct rc_t |
24 | rc_free (rc_t *rc) |
|
|
25 | { |
36 | { |
26 | SvREFCNT_dec (rc->hv); |
37 | struct glyph_data |
27 | g_slice_free (rc_t, rc); |
38 | { |
28 | } |
39 | uint8_t u, v, w, h; |
|
|
40 | uint16_t x, y; |
|
|
41 | }; |
29 | |
42 | |
30 | static void |
43 | struct array_t |
31 | rc_clear (rc_t *rc) |
44 | : std::vector<uint8_t, slice_allocator<uint8_t>> |
32 | { |
45 | { |
33 | hv_clear (rc->hv); |
46 | using std::vector<uint8_t, slice_allocator<uint8_t>>::vector; |
34 | } |
|
|
35 | |
47 | |
36 | static rc_array_t * |
48 | template<typename T> |
37 | rc_array (rc_t *rc, rc_key_t *k) |
49 | T &append () |
38 | { |
|
|
39 | SV *sv = *hv_fetch (rc->hv, (char *)k, sizeof (*k), 1); |
|
|
40 | |
|
|
41 | if (SvTYPE (sv) != SVt_PV) |
|
|
42 | { |
50 | { |
43 | sv_upgrade (sv, SVt_PV); |
51 | auto ofs = size (); |
44 | SvPOK_only (sv); |
52 | resize (ofs + sizeof (T)); |
|
|
53 | return *(T *)(data () + ofs); |
45 | } |
54 | } |
46 | |
55 | |
47 | return sv; |
56 | void v2f (float x, float y) |
48 | } |
57 | { |
|
|
58 | auto &vec = append<float[2]> (); |
|
|
59 | vec[0] = x / 2; |
|
|
60 | vec[1] = y / 2; |
|
|
61 | } |
49 | |
62 | |
50 | static void |
63 | void t2f_v3f (float u, float v, float x, float y, float z) |
51 | rc_v2f (rc_array_t *arr, float x, float y) |
64 | { |
52 | { |
65 | auto &vec = append<float[5]> (); |
53 | STRLEN len = SvCUR (arr) + sizeof (float) * 2; |
66 | vec[0] = u; |
54 | SvGROW (arr, len); |
67 | vec[1] = v; |
|
|
68 | vec[2] = x; |
|
|
69 | vec[3] = y; |
|
|
70 | vec[4] = z; |
|
|
71 | } |
55 | |
72 | |
56 | ((float *)SvEND (arr))[0] = x; |
73 | void glyph (int u, int v, int w, int h, int x, int y) |
57 | ((float *)SvEND (arr))[1] = y; |
74 | { |
|
|
75 | if (w && h) |
|
|
76 | { |
|
|
77 | auto &c = append<glyph_data> (); |
58 | |
78 | |
59 | SvCUR_set (arr, len); |
79 | c.u = u; |
60 | } |
80 | c.v = v; |
61 | |
81 | c.w = w; |
62 | static void |
82 | c.h = h; |
63 | rc_t2f_v3f (rc_array_t *arr, float u, float v, float x, float y, float z) |
83 | c.x = x + w; |
64 | { |
84 | c.y = y + h; |
65 | STRLEN len = SvCUR (arr) + sizeof (float) * 5; |
|
|
66 | SvGROW (arr, len); |
|
|
67 | |
|
|
68 | ((float *)SvEND (arr))[0] = u; |
|
|
69 | ((float *)SvEND (arr))[1] = v; |
|
|
70 | |
|
|
71 | ((float *)SvEND (arr))[2] = x; |
|
|
72 | ((float *)SvEND (arr))[3] = y; |
|
|
73 | ((float *)SvEND (arr))[4] = z; |
|
|
74 | |
|
|
75 | SvCUR_set (arr, len); |
|
|
76 | } |
|
|
77 | |
|
|
78 | static void |
|
|
79 | rc_glyph (rc_array_t *arr, int u, int v, int w, int h, int x, int y) |
|
|
80 | { |
|
|
81 | U8 *c; |
|
|
82 | STRLEN len = SvCUR (arr); |
|
|
83 | SvGROW (arr, len + 2 * 2 + 1 * 4); |
|
|
84 | c = (U8 *)SvEND (arr); |
|
|
85 | |
|
|
86 | x += w; |
|
|
87 | y += h; |
|
|
88 | |
|
|
89 | *c++ = u; |
|
|
90 | *c++ = v; |
|
|
91 | *c++ = w; |
|
|
92 | *c++ = h; |
|
|
93 | |
|
|
94 | // use ber-encoding for up to 14 bits (16k) |
|
|
95 | *c = 0x80 | (x >> 7); c += (x >> 7) ? 1 : 0; *c++ = x & 0x7f; |
|
|
96 | *c = 0x80 | (y >> 7); c += (y >> 7) ? 1 : 0; *c++ = y & 0x7f; |
|
|
97 | |
|
|
98 | SvCUR_set (arr, c - (U8 *)SvPVX (arr)); |
|
|
99 | } |
|
|
100 | |
|
|
101 | static void |
|
|
102 | rc_draw (rc_t *rc) |
|
|
103 | { |
|
|
104 | HE *he; |
|
|
105 | |
|
|
106 | hv_iterinit (rc->hv); |
|
|
107 | while ((he = hv_iternext (rc->hv))) |
|
|
108 | { |
|
|
109 | rc_key_t *key = (rc_key_t *)HeKEY (he); |
|
|
110 | rc_array_t *arr = HeVAL (he); |
|
|
111 | STRLEN len; |
|
|
112 | char *arr_pv = SvPV (arr, len); |
|
|
113 | GLsizei stride; |
|
|
114 | |
|
|
115 | if (key->texname) |
|
|
116 | { |
|
|
117 | glBindTexture (GL_TEXTURE_2D, key->texname); |
|
|
118 | glEnable (GL_TEXTURE_2D); |
|
|
119 | } |
|
|
120 | else |
|
|
121 | glDisable (GL_TEXTURE_2D); |
|
|
122 | |
|
|
123 | glColor4ub (key->r, key->g, key->b, key->a); |
|
|
124 | |
|
|
125 | if (key->format) |
|
|
126 | { |
|
|
127 | stride = key->format == GL_T2F_V3F ? sizeof (float) * 5 |
|
|
128 | : key->format == GL_V2F ? sizeof (float) * 2 |
|
|
129 | : 65536; |
|
|
130 | |
|
|
131 | glInterleavedArrays (key->format, 0, (void *)arr_pv); |
|
|
132 | //glLockArraysEXT (0, len / stride); |
|
|
133 | glDrawArrays (key->mode, 0, len / stride); |
|
|
134 | //glUnlockArraysEXT (); |
|
|
135 | } |
|
|
136 | else |
|
|
137 | { |
|
|
138 | // optimised character quad storage. slower but nice on memory. |
|
|
139 | // reduces storage requirements from 80 bytes/char to 6-8 |
|
|
140 | U8 *c = (U8 *)arr_pv; |
|
|
141 | U8 *e = c + len; |
|
|
142 | |
|
|
143 | glBegin (key->mode); // practically must be quads |
|
|
144 | |
|
|
145 | while (c < e) |
|
|
146 | { |
|
|
147 | int u, v, x, y, w, h; |
|
|
148 | |
|
|
149 | u = *c++; |
|
|
150 | v = *c++; |
|
|
151 | w = *c++; |
|
|
152 | h = *c++; |
|
|
153 | |
|
|
154 | x = *c++; if (x > 0x7f) x = ((x & 0x7f) << 7) | *c++; |
|
|
155 | y = *c++; if (y > 0x7f) y = ((y & 0x7f) << 7) | *c++; |
|
|
156 | |
|
|
157 | x -= w; |
|
|
158 | y -= h; |
|
|
159 | |
|
|
160 | glTexCoord2f ( u * (1.f / TC_WIDTH), v * (1.f / TC_HEIGHT)); glVertex2i (x , y ); |
|
|
161 | glTexCoord2f ((u + w) * (1.f / TC_WIDTH), v * (1.f / TC_HEIGHT)); glVertex2i (x + w, y ); |
|
|
162 | glTexCoord2f ((u + w) * (1.f / TC_WIDTH), (v + h) * (1.f / TC_HEIGHT)); glVertex2i (x + w, y + h); |
|
|
163 | glTexCoord2f ( u * (1.f / TC_WIDTH), (v + h) * (1.f / TC_HEIGHT)); glVertex2i (x , y + h); |
|
|
164 | } |
|
|
165 | |
|
|
166 | glEnd (); |
|
|
167 | } |
85 | } |
168 | } |
86 | } |
|
|
87 | }; |
169 | |
88 | |
170 | glDisable (GL_TEXTURE_2D); |
89 | int drawcount = 0; |
171 | } |
90 | ska::flat_hash_map<rc_key_t, array_t> h; |
172 | |
91 | |
|
|
92 | void clear () |
|
|
93 | { |
|
|
94 | drawcount = 0; |
|
|
95 | h.clear (); |
|
|
96 | } |
173 | |
97 | |
|
|
98 | array_t &array (const rc_key_t &k) |
|
|
99 | { |
|
|
100 | return h[k]; |
|
|
101 | } |
174 | |
102 | |
|
|
103 | void draw () |
|
|
104 | { |
|
|
105 | for (auto &&it = h.begin (); it != h.end (); ++it) |
|
|
106 | { |
|
|
107 | rc_key_t &key = it->first; |
|
|
108 | array_t &arr = it->second; |
|
|
109 | GLsizei stride; |
175 | |
110 | |
|
|
111 | if (key.texname) |
|
|
112 | { |
|
|
113 | glBindTexture (GL_TEXTURE_2D, key.texname); |
|
|
114 | glEnable (GL_TEXTURE_2D); |
|
|
115 | } |
|
|
116 | else |
|
|
117 | glDisable (GL_TEXTURE_2D); |
|
|
118 | |
|
|
119 | glColor4ub (key.r, key.g, key.b, key.a); |
|
|
120 | |
|
|
121 | if (key.format) |
|
|
122 | { |
|
|
123 | stride = key.format == GL_T2F_V3F ? sizeof (float) * 5 |
|
|
124 | : key.format == GL_V2F ? sizeof (float) * 2 |
|
|
125 | : 65536; |
|
|
126 | |
|
|
127 | glInterleavedArrays (key.format, 0, (void *)arr.data ()); |
|
|
128 | //glLockArraysEXT (0, len / stride); |
|
|
129 | glDrawArrays (key.mode, 0, arr.size () / stride); |
|
|
130 | //glUnlockArraysEXT (); |
|
|
131 | } |
|
|
132 | else |
|
|
133 | { |
|
|
134 | // optimised character quad storage. slower but nice on memory. |
|
|
135 | // reduces storage requirements from 80 bytes/char to 8 |
|
|
136 | auto *c = (glyph_data *) arr.data (); |
|
|
137 | auto *e = (glyph_data *)(arr.data () + arr.size ()); |
|
|
138 | |
|
|
139 | glBegin (key.mode); // practically must be quads |
|
|
140 | |
|
|
141 | while (c < e) |
|
|
142 | { |
|
|
143 | glTexCoord2f ( c->u * (1.f / TC_WIDTH), c->v * (1.f / TC_HEIGHT)); glVertex2i (c->x - c->w, c->y - c->h); |
|
|
144 | glTexCoord2f ((c->u + c->w) * (1.f / TC_WIDTH), c->v * (1.f / TC_HEIGHT)); glVertex2i (c->x , c->y - c->h); |
|
|
145 | glTexCoord2f ((c->u + c->w) * (1.f / TC_WIDTH), (c->v + c->h) * (1.f / TC_HEIGHT)); glVertex2i (c->x , c->y ); |
|
|
146 | glTexCoord2f ( c->u * (1.f / TC_WIDTH), (c->v + c->h) * (1.f / TC_HEIGHT)); glVertex2i (c->x - c->w, c->y ); |
|
|
147 | |
|
|
148 | ++c; |
|
|
149 | } |
|
|
150 | |
|
|
151 | glEnd (); |
|
|
152 | } |
|
|
153 | } |
|
|
154 | |
|
|
155 | glDisable (GL_TEXTURE_2D); |
|
|
156 | |
|
|
157 | if (ecb_expect_false (++drawcount == 16)) |
|
|
158 | for (auto &&it = h.begin (); it != h.end (); ++it) |
|
|
159 | it->second.shrink_to_fit (); |
|
|
160 | } |
|
|
161 | }; |
|
|
162 | |