ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/rendercache.c
Revision: 1.11
Committed: Sun Nov 18 03:06:13 2018 UTC (5 years, 5 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.10: +129 -145 lines
Log Message:
clumsy c++ification of rendercache

File Contents

# User Rev Content
1 root 1.11 #include <vector>
2    
3     struct rc_key_t
4     {
5 root 1.1 GLenum mode;
6 root 1.3 GLenum format; // 0, GL_T2F_V3F, GL_V2F
7 root 1.7 GLuint texname;
8 root 1.1 unsigned char r, g, b, a;
9    
10 root 1.11 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 root 1.1 }
32    
33 root 1.11 struct rc_t
34 root 1.1 {
35 root 1.11 struct glyph_data
36     {
37     uint8_t u, v, w, h;
38     uint16_t x, y;
39     };
40 root 1.1
41 root 1.11 struct array_t
42     : std::vector<uint8_t>
43     {
44     using std::vector<uint8_t>::vector;
45 root 1.1
46 root 1.11 template<typename T>
47     T &append ()
48 root 1.1 {
49 root 1.11 auto ofs = size ();
50     resize (ofs + sizeof (T));
51     return *(T *)(data () + ofs);
52 root 1.1 }
53    
54 root 1.11 void v2f (float x, float y)
55     {
56     auto &vec = append<float[2]> ();
57     vec[0] = x;
58     vec[1] = y;
59     }
60 root 1.1
61 root 1.11 void t2f_v3f (float u, float v, float x, float y, float z)
62 root 1.9 {
63 root 1.11 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 root 1.9 }
70 root 1.1
71 root 1.11 void glyph (int u, int v, int w, int h, int x, int y)
72 root 1.1 {
73 root 1.11 if (w && h)
74 root 1.4 {
75 root 1.11 auto &c = append<glyph_data> ();
76 root 1.3
77 root 1.11 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 root 1.4 }
84 root 1.1 }
85 root 1.11 };
86 root 1.1
87 root 1.11 ska::flat_hash_map<rc_key_t, array_t> h;
88 root 1.1
89 root 1.11 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 root 1.1