ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cf.schmorp.de/Deliantra-Client/rendercache.c
Revision: 1.10
Committed: Thu Oct 14 00:02:39 2010 UTC (13 years, 9 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.9: +11 -19 lines
Log Message:
*** empty log message ***

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 root 1.10 sv_grow (sv, 512);
46 root 1.1 }
47    
48     return sv;
49     }
50    
51     static void
52     rc_v2f (rc_array_t *arr, float x, float y)
53     {
54 root 1.10 char *end = fast_sv_grow (arr, sizeof (float) * 2);
55 root 1.1
56 root 1.10 ((float *)end)[0] = x;
57     ((float *)end)[1] = y;
58 root 1.1 }
59    
60     static void
61     rc_t2f_v3f (rc_array_t *arr, float u, float v, float x, float y, float z)
62     {
63 root 1.10 char *end = fast_sv_grow (arr, sizeof (float) * 5);
64 root 1.1
65 root 1.10 ((float *)end)[0] = u;
66     ((float *)end)[1] = v;
67 root 1.1
68 root 1.10 ((float *)end)[2] = x;
69     ((float *)end)[3] = y;
70     ((float *)end)[4] = z;
71 root 1.1 }
72    
73     static void
74 root 1.4 rc_glyph (rc_array_t *arr, int u, int v, int w, int h, int x, int y)
75     {
76 root 1.9 if (w && h)
77     {
78 root 1.10 U8 *c = (U8 *)fast_sv_grow (arr, 4 + 4);
79 root 1.9
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 root 1.4
92 root 1.9 SvCUR_set (arr, c - (U8 *)SvPVX (arr));
93     }
94 root 1.4 }
95    
96     static void
97 root 1.1 rc_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 root 1.3
120 root 1.4 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 root 1.6 x -= w;
153     y -= h;
154    
155 root 1.4 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 root 1.3
161 root 1.4 glEnd ();
162     }
163 root 1.1 }
164    
165     glDisable (GL_TEXTURE_2D);
166     }
167    
168    
169    
170