ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cf.schmorp.de/Deliantra-Client/rendercache.c
Revision: 1.8
Committed: Tue Dec 25 18:58:32 2007 UTC (16 years, 6 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-0_9963
Changes since 1.7: +1 -1 lines
Log Message:
save/restore textures in a safer way, also save/restore glyph cache

File Contents

# User Rev Content
1 root 1.1 typedef struct {
2     GLenum mode;
3 root 1.3 GLenum format; // 0, GL_T2F_V3F, GL_V2F
4 root 1.7 GLuint texname;
5 root 1.1 unsigned char r, g, b, a;
6     } rc_key_t;
7    
8     typedef struct {
9     HV *hv;
10     } rc_t;
11    
12     typedef SV rc_array_t;
13    
14     static rc_t *
15 root 1.8 rc_alloc (void)
16 root 1.1 {
17     rc_t *rc = g_slice_new0 (rc_t);
18     rc->hv = newHV ();
19    
20     return rc;
21     }
22    
23     static void
24     rc_free (rc_t *rc)
25     {
26     SvREFCNT_dec (rc->hv);
27     g_slice_free (rc_t, rc);
28     }
29    
30     static void
31     rc_clear (rc_t *rc)
32     {
33     hv_clear (rc->hv);
34     }
35    
36     static rc_array_t *
37     rc_array (rc_t *rc, rc_key_t *k)
38     {
39     SV *sv = *hv_fetch (rc->hv, (char *)k, sizeof (*k), 1);
40    
41     if (SvTYPE (sv) != SVt_PV)
42     {
43     sv_upgrade (sv, SVt_PV);
44     SvPOK_only (sv);
45     }
46    
47     return sv;
48     }
49    
50     static void
51     rc_v2f (rc_array_t *arr, float x, float y)
52     {
53 root 1.2 STRLEN len = SvCUR (arr) + sizeof (float) * 2;
54 root 1.1 SvGROW (arr, len);
55    
56     ((float *)SvEND (arr))[0] = x;
57     ((float *)SvEND (arr))[1] = y;
58    
59     SvCUR_set (arr, len);
60     }
61    
62     static void
63     rc_t2f_v3f (rc_array_t *arr, float u, float v, float x, float y, float z)
64     {
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 root 1.4 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 root 1.5 c = (U8 *)SvEND (arr);
85 root 1.4
86 root 1.6 x += w;
87     y += h;
88    
89 root 1.4 *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 root 1.1 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 root 1.3
125 root 1.4 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 root 1.6 x -= w;
158     y -= h;
159    
160 root 1.4 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 root 1.3
166 root 1.4 glEnd ();
167     }
168 root 1.1 }
169    
170     glDisable (GL_TEXTURE_2D);
171     }
172    
173    
174    
175