ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cf.schmorp.de/Deliantra-Client/rendercache.c
(Generate patch)

Comparing cf.schmorp.de/Deliantra-Client/rendercache.c (file contents):
Revision 1.10 by root, Thu Oct 14 00:02:39 2010 UTC vs.
Revision 1.11 by root, Sun Nov 18 03:06:13 2018 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines