ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/rendercache.c
Revision: 1.12
Committed: Sun Nov 18 03:10:35 2018 UTC (5 years, 5 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.11: +2 -2 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 #include <vector>
2
3 struct rc_key_t
4 {
5 GLenum mode;
6 GLenum format; // 0, GL_T2F_V3F, GL_V2F
7 GLuint texname;
8 unsigned char r, g, b, a;
9
10 bool operator == (const rc_key_t &o) const
11 {
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 };
21
22 namespace std {
23 template <>
24 struct hash<rc_key_t>
25 {
26 size_t operator () (const rc_key_t &v) const
27 {
28 return v.mode + (v.format << 4) + v.texname + (v.r << 8) + (v.g << 16) + (v.b << 24) + v.a;
29 }
30 };
31 }
32
33 struct rc_t
34 {
35 struct glyph_data
36 {
37 uint8_t u, v, w, h;
38 uint16_t x, y;
39 };
40
41 struct array_t
42 : std::vector<uint8_t>
43 {
44 using std::vector<uint8_t>::vector;
45
46 template<typename T>
47 T &append ()
48 {
49 auto ofs = size ();
50 resize (ofs + sizeof (T));
51 return *(T *)(data () + ofs);
52 }
53
54 void v2f (float x, float y)
55 {
56 auto &vec = append<float[2]> ();
57 vec[0] = x / 2;
58 vec[1] = y / 2;
59 }
60
61 void t2f_v3f (float u, float v, float x, float y, float z)
62 {
63 auto &vec = append<float[5]> ();
64 vec[0] = u;
65 vec[1] = v;
66 vec[2] = x;
67 vec[3] = y;
68 vec[4] = z;
69 }
70
71 void glyph (int u, int v, int w, int h, int x, int y)
72 {
73 if (w && h)
74 {
75 auto &c = append<glyph_data> ();
76
77 c.u = u;
78 c.v = v;
79 c.w = w;
80 c.h = h;
81 c.x = x + w;
82 c.y = y + h;
83 }
84 }
85 };
86
87 ska::flat_hash_map<rc_key_t, array_t> h;
88
89 void clear ()
90 {
91 h.clear ();
92 }
93
94 array_t &array (const rc_key_t &k)
95 {
96 return h[k];
97 }
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;
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