… | |
… | |
32 | #include <cstring> |
32 | #include <cstring> |
33 | #include <cstdio> |
33 | #include <cstdio> |
34 | #include <cstdlib> |
34 | #include <cstdlib> |
35 | |
35 | |
36 | #include <utility> |
36 | #include <utility> |
|
|
37 | #include <bitset> |
37 | |
38 | |
38 | #define USE_RWOPS 1 // for SDL_mixer:LoadMUS_RW |
39 | #define USE_RWOPS 1 // for SDL_mixer:LoadMUS_RW |
39 | |
40 | |
40 | #include <SDL.h> |
41 | #include <SDL.h> |
41 | #include <SDL_thread.h> |
42 | #include <SDL_thread.h> |
… | |
… | |
199 | } |
200 | } |
200 | |
201 | |
201 | return 0; |
202 | return 0; |
202 | } |
203 | } |
203 | |
204 | |
204 | typedef struct cf_layout { |
205 | struct cf_layout { |
205 | PangoLayout *pl; |
206 | PangoLayout *pl; |
206 | float r, g, b, a; // default color for rgba mode |
207 | float r, g, b, a; // default color for rgba mode |
207 | int base_height; |
208 | int base_height; |
208 | DC__Font font; |
209 | DC__Font font; |
209 | rc_t *rc; |
210 | rc_t *rc; |
210 | } *DC__Layout; |
211 | }; |
|
|
212 | |
|
|
213 | typedef cf_layout *DC__Layout; |
211 | |
214 | |
212 | static DC__Font default_font; |
215 | static DC__Font default_font; |
213 | static PangoContext *opengl_context; |
216 | static PangoContext *opengl_context; |
214 | static PangoFontMap *opengl_fontmap; |
217 | static PangoFontMap *opengl_fontmap; |
215 | |
218 | |
… | |
… | |
256 | } |
259 | } |
257 | |
260 | |
258 | typedef uint16_t tileid; |
261 | typedef uint16_t tileid; |
259 | typedef uint16_t faceid; |
262 | typedef uint16_t faceid; |
260 | |
263 | |
261 | typedef struct { |
264 | struct maptex |
|
|
265 | { |
262 | GLuint name; |
266 | GLuint name; |
263 | int w, h; |
267 | int w, h; |
264 | float s, t; |
268 | float s, t; |
265 | uint8_t r, g, b, a; |
269 | uint8_t r, g, b, a; |
266 | tileid smoothtile; |
270 | tileid smoothtile; |
267 | uint8_t smoothlevel; |
271 | uint8_t smoothlevel; |
268 | uint8_t unused; /* set to zero on use */ |
272 | uint8_t unused; /* set to zero on use */ |
269 | } maptex; |
273 | }; |
270 | |
274 | |
271 | typedef struct { |
275 | struct mapcell |
|
|
276 | { |
272 | uint32_t player; |
277 | uint32_t player; |
273 | tileid tile[3]; |
278 | tileid tile[3]; |
274 | uint16_t darkness; |
279 | uint16_t darkness; |
275 | uint8_t stat_width, stat_hp, flags, smoothmax; |
280 | uint8_t stat_width, stat_hp, flags, smoothmax; |
276 | } mapcell; |
281 | }; |
277 | |
282 | |
278 | typedef struct { |
283 | struct maprow |
|
|
284 | { |
279 | int32_t c0, c1; |
285 | int32_t c0, c1; |
280 | mapcell *col; |
286 | mapcell *col; |
281 | } maprow; |
287 | }; |
282 | |
288 | |
283 | typedef struct map { |
289 | struct mapgrid { |
284 | int x, y, w, h; |
290 | int x, y, w, h; |
285 | int ox, oy; /* offset to virtual global coordinate system */ |
291 | int ox, oy; /* offset to virtual global coordinate system */ |
286 | int faces; tileid *face2tile; // [faceid] |
292 | int faces; tileid *face2tile; // [faceid] |
287 | int texs; maptex *tex; // [tileid] |
293 | int texs; maptex *tex; // [tileid] |
288 | |
294 | |
289 | int32_t rows; |
295 | int32_t rows; |
290 | maprow *row; |
296 | maprow *row; |
291 | } *DC__Map; |
297 | }; |
|
|
298 | |
|
|
299 | typedef mapgrid *DC__Map; |
292 | |
300 | |
293 | static char * |
301 | static char * |
294 | prepend (char *ptr, int sze, int inc) |
302 | prepend (char *ptr, int sze, int inc) |
295 | { |
303 | { |
296 | char *p; |
304 | char *p; |
… | |
… | |
314 | |
322 | |
315 | #define Append(type,ptr,sze,inc) (ptr) = (type *)append ((char *)ptr, (sze) * sizeof (type), (inc) * sizeof (type)) |
323 | #define Append(type,ptr,sze,inc) (ptr) = (type *)append ((char *)ptr, (sze) * sizeof (type), (inc) * sizeof (type)) |
316 | #define Prepend(type,ptr,sze,inc) (ptr) = (type *)prepend ((char *)ptr, (sze) * sizeof (type), (inc) * sizeof (type)) |
324 | #define Prepend(type,ptr,sze,inc) (ptr) = (type *)prepend ((char *)ptr, (sze) * sizeof (type), (inc) * sizeof (type)) |
317 | |
325 | |
318 | static void |
326 | static void |
319 | need_facenum (struct map *self, faceid face) |
327 | need_facenum (struct mapgrid *self, faceid face) |
320 | { |
328 | { |
321 | while (self->faces <= face) |
329 | while (self->faces <= face) |
322 | { |
330 | { |
323 | Append (tileid, self->face2tile, self->faces, self->faces); |
331 | Append (tileid, self->face2tile, self->faces, self->faces); |
324 | self->faces *= 2; |
332 | self->faces *= 2; |
325 | } |
333 | } |
326 | } |
334 | } |
327 | |
335 | |
328 | static void |
336 | static void |
329 | need_texid (struct map *self, int texid) |
337 | need_texid (struct mapgrid *self, int texid) |
330 | { |
338 | { |
331 | while (self->texs <= texid) |
339 | while (self->texs <= texid) |
332 | { |
340 | { |
333 | Append (maptex, self->tex, self->texs, self->texs); |
341 | Append (maptex, self->tex, self->texs, self->texs); |
334 | self->texs *= 2; |
342 | self->texs *= 2; |
335 | } |
343 | } |
336 | } |
344 | } |
337 | |
345 | |
338 | static maprow * |
346 | static maprow * |
339 | map_get_row (DC__Map self, int y) |
347 | map_get_row (mapgrid *self, int y) |
340 | { |
348 | { |
341 | if (0 > y) |
349 | if (0 > y) |
342 | { |
350 | { |
343 | int extend = - y + MAP_EXTEND_Y; |
351 | int extend = - y + MAP_EXTEND_Y; |
344 | Prepend (maprow, self->row, self->rows, extend); |
352 | Prepend (maprow, self->row, self->rows, extend); |
… | |
… | |
382 | |
390 | |
383 | return row->col + (x - row->c0); |
391 | return row->col + (x - row->c0); |
384 | } |
392 | } |
385 | |
393 | |
386 | static mapcell * |
394 | static mapcell * |
387 | map_get_cell (DC__Map self, int x, int y) |
395 | map_get_cell (mapgrid *self, int x, int y) |
388 | { |
396 | { |
389 | return row_get_cell (map_get_row (self, y), x); |
397 | return row_get_cell (map_get_row (self, y), x); |
390 | } |
398 | } |
391 | |
399 | |
392 | static void |
400 | static void |
393 | map_clear (DC__Map self) |
401 | map_clear (mapgrid *self) |
394 | { |
402 | { |
395 | int r; |
403 | int r; |
396 | |
404 | |
397 | for (r = 0; r < self->rows; r++) |
405 | for (r = 0; r < self->rows; r++) |
398 | Safefree (self->row[r].col); |
406 | Safefree (self->row[r].col); |
… | |
… | |
416 | (cell)->flags = 0; \ |
424 | (cell)->flags = 0; \ |
417 | (cell)->player = 0; \ |
425 | (cell)->player = 0; \ |
418 | } while (0) |
426 | } while (0) |
419 | |
427 | |
420 | static void |
428 | static void |
421 | map_blank (DC__Map self, int x0, int y0, int w, int h) |
429 | map_blank (mapgrid *self, int x0, int y0, int w, int h) |
422 | { |
430 | { |
423 | int x, y; |
431 | int x, y; |
424 | maprow *row; |
432 | maprow *row; |
425 | mapcell *cell; |
433 | mapcell *cell; |
426 | |
434 | |
… | |
… | |
1894 | PROTOTYPES: DISABLE |
1902 | PROTOTYPES: DISABLE |
1895 | |
1903 | |
1896 | DC::Map |
1904 | DC::Map |
1897 | new (SV *klass) |
1905 | new (SV *klass) |
1898 | CODE: |
1906 | CODE: |
1899 | New (0, RETVAL, 1, struct map); |
1907 | New (0, RETVAL, 1, mapgrid); |
1900 | RETVAL->x = 0; |
1908 | RETVAL->x = 0; |
1901 | RETVAL->y = 0; |
1909 | RETVAL->y = 0; |
1902 | RETVAL->w = 0; |
1910 | RETVAL->w = 0; |
1903 | RETVAL->h = 0; |
1911 | RETVAL->h = 0; |
1904 | RETVAL->ox = 0; |
1912 | RETVAL->ox = 0; |
… | |
… | |
2216 | draw (DC::Map self, int mx, int my, int sw, int sh, int Tw, int Th, U32 player = 0xffffffff, int sdx = 0, int sdy = 0) |
2224 | draw (DC::Map self, int mx, int my, int sw, int sh, int Tw, int Th, U32 player = 0xffffffff, int sdx = 0, int sdy = 0) |
2217 | CODE: |
2225 | CODE: |
2218 | { |
2226 | { |
2219 | int x, y, z; |
2227 | int x, y, z; |
2220 | |
2228 | |
2221 | uint32_t smooth_level[256 / 32]; // one bit for every possible smooth level |
|
|
2222 | static uint8_t smooth_max[256][256]; // egad, fast and wasteful on memory (64k) |
2229 | static uint8_t smooth_max[256][256]; // egad, fast and wasteful on memory (64k), also, static! |
2223 | smooth_key skey; |
|
|
2224 | int pl_x, pl_y; |
2230 | int pl_x, pl_y; |
2225 | maptex pl_tex; |
2231 | maptex pl_tex; |
2226 | rc_t *rc = rc_alloc (); |
2232 | rc_t *rc = rc_alloc (); |
2227 | rc_t *rc_ov = rc_alloc (); |
2233 | rc_t *rc_ov = rc_alloc (); |
2228 | rc_key_t key; |
2234 | rc_key_t key; |
… | |
… | |
2231 | pl_tex.name = 0; |
2237 | pl_tex.name = 0; |
2232 | |
2238 | |
2233 | // that's current max. sorry. |
2239 | // that's current max. sorry. |
2234 | if (sw > 255) sw = 255; |
2240 | if (sw > 255) sw = 255; |
2235 | if (sh > 255) sh = 255; |
2241 | if (sh > 255) sh = 255; |
2236 | |
|
|
2237 | // clear key, in case of extra padding |
|
|
2238 | memset (&skey, 0, sizeof (skey)); |
|
|
2239 | |
2242 | |
2240 | memset (&key, 0, sizeof (key)); |
2243 | memset (&key, 0, sizeof (key)); |
2241 | key.r = 255; |
2244 | key.r = 255; |
2242 | key.g = 255; |
2245 | key.g = 255; |
2243 | key.b = 255; |
2246 | key.b = 255; |
… | |
… | |
2249 | my += self->y; |
2252 | my += self->y; |
2250 | |
2253 | |
2251 | // first pass: determine smooth_max |
2254 | // first pass: determine smooth_max |
2252 | // rather ugly, if you ask me |
2255 | // rather ugly, if you ask me |
2253 | // could also be stored inside mapcell and updated on change |
2256 | // could also be stored inside mapcell and updated on change |
2254 | memset (smooth_max, 0, sizeof (smooth_max)); |
2257 | memset (smooth_max, 0, sizeof (smooth_max[0]) * (sh + 1)); |
2255 | |
2258 | |
2256 | for (y = 0; y < sh; y++) |
2259 | for (y = 0; y < sh; y++) |
2257 | if (0 <= y + my && y + my < self->rows) |
2260 | if (0 <= y + my && y + my < self->rows) |
2258 | { |
2261 | { |
2259 | maprow *row = self->row + (y + my); |
2262 | maprow *row = self->row + (y + my); |
… | |
… | |
2274 | glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
2277 | glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
2275 | glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); |
2278 | glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); |
2276 | |
2279 | |
2277 | for (z = 0; z <= 2; z++) |
2280 | for (z = 0; z <= 2; z++) |
2278 | { |
2281 | { |
|
|
2282 | std::bitset<256> smooth_level; // one bit for every possible smooth level |
|
|
2283 | smooth_key skey; |
2279 | smooth_hash smooth; |
2284 | smooth_hash smooth; |
2280 | memset (smooth_level, 0, sizeof (smooth_level)); |
|
|
2281 | key.texname = -1; |
2285 | key.texname = -1; |
2282 | |
2286 | |
2283 | for (y = 0; y < sh; y++) |
2287 | for (y = 0; y < sh; y++) |
2284 | if (0 <= y + my && y + my < self->rows) |
2288 | if (0 <= y + my && y + my < self->rows) |
2285 | { |
2289 | { |
… | |
… | |
2327 | if (tex.smoothtile) |
2331 | if (tex.smoothtile) |
2328 | { |
2332 | { |
2329 | skey.tile = tex.smoothtile; |
2333 | skey.tile = tex.smoothtile; |
2330 | skey.level = tex.smoothlevel; |
2334 | skey.level = tex.smoothlevel; |
2331 | |
2335 | |
2332 | smooth_level [tex.smoothlevel >> 5] |= ((uint32_t)1) << (tex.smoothlevel & 31); |
2336 | smooth_level[tex.smoothlevel] = 1; |
2333 | |
2337 | |
2334 | // add bits to current tile and all neighbours. skey.x|y is |
2338 | // add bits to current tile and all neighbours. skey.x|y is |
2335 | // shifted +1|+1 so we always stay positive. |
2339 | // shifted +1|+1 so we always stay positive. |
2336 | |
2340 | |
2337 | // bits is ___n cccc CCCC bbbb |
2341 | // bits is ___n cccc CCCC bbbb |
… | |
… | |
2394 | { |
2398 | { |
2395 | int w, b; |
2399 | int w, b; |
2396 | |
2400 | |
2397 | glEnable (GL_TEXTURE_2D); |
2401 | glEnable (GL_TEXTURE_2D); |
2398 | glBegin (GL_QUADS); |
2402 | glBegin (GL_QUADS); |
2399 | for (w = 0; w < 256 / 32; ++w) |
2403 | for (int level = 0; level < smooth_level.size (); ++level) |
|
|
2404 | if (smooth_level[level]) |
|
|
2405 | for (auto &&it = smooth.begin (); it != smooth.end (); ++it) |
2400 | { |
2406 | { |
2401 | uint32_t smask = smooth_level [w]; |
2407 | smooth_key &skey = it->first; |
2402 | if (smask) |
2408 | IV bits = it->second; |
2403 | for (b = 0; b < 32; ++b) |
2409 | |
2404 | if (smask & (((uint32_t)1) << b)) |
2410 | if (!(bits & 0x1000) |
|
|
2411 | && skey.level == level |
|
|
2412 | && level > smooth_max [skey.x][skey.y]) |
2405 | { |
2413 | { |
2406 | int level = (w << 5) | b; |
2414 | maptex tex = self->tex [skey.tile]; |
|
|
2415 | int px = (((int)skey.x) - 1) * Tw; |
|
|
2416 | int py = (((int)skey.y) - 1) * Th; |
|
|
2417 | int border = bits & 15; |
|
|
2418 | int corner = (bits >> 8) & ~(bits >> 4) & 15; |
|
|
2419 | float dx = tex.s * .0625f; // 16 images/row |
|
|
2420 | float dy = tex.t * .5f ; // 2 images/column |
|
|
2421 | |
2407 | HE *he; |
2422 | if (tex.name) |
2408 | |
|
|
2409 | for (auto &&it = smooth.begin (); it != smooth.end (); ++it) |
|
|
2410 | { |
2423 | { |
2411 | smooth_key &skey = it->first; |
2424 | // this time avoiding texture state changes |
2412 | IV bits = it->second; |
2425 | // save gobs of state changes. |
2413 | |
2426 | if (key.texname != tex.name) |
2414 | if (!(bits & 0x1000) |
|
|
2415 | && skey.level == level |
|
|
2416 | && level > smooth_max [skey.x][skey.y]) |
|
|
2417 | { |
2427 | { |
2418 | maptex tex = self->tex [skey.tile]; |
|
|
2419 | int px = (((int)skey.x) - 1) * Tw; |
|
|
2420 | int py = (((int)skey.y) - 1) * Th; |
|
|
2421 | int border = bits & 15; |
|
|
2422 | int corner = (bits >> 8) & ~(bits >> 4) & 15; |
|
|
2423 | float dx = tex.s * .0625f; // 16 images/row |
|
|
2424 | float dy = tex.t * .5f ; // 2 images/column |
|
|
2425 | |
|
|
2426 | if (tex.name) |
|
|
2427 | { |
|
|
2428 | // this time avoiding texture state changes |
|
|
2429 | // save gobs of state changes. |
|
|
2430 | if (key.texname != tex.name) |
|
|
2431 | { |
|
|
2432 | self->tex [skey.tile].unused = 0; |
2428 | self->tex [skey.tile].unused = 0; |
2433 | |
2429 | |
2434 | glEnd (); |
2430 | glEnd (); |
2435 | glBindTexture (GL_TEXTURE_2D, key.texname = tex.name); |
2431 | glBindTexture (GL_TEXTURE_2D, key.texname = tex.name); |
2436 | glBegin (GL_QUADS); |
2432 | glBegin (GL_QUADS); |
2437 | } |
2433 | } |
2438 | |
2434 | |
2439 | if (border) |
2435 | if (border) |
2440 | { |
2436 | { |
2441 | float ox = border * dx; |
2437 | float ox = border * dx; |
2442 | |
2438 | |
2443 | glTexCoord2f (ox , 0.f ); glVertex2i (px , py ); |
2439 | glTexCoord2f (ox , 0.f ); glVertex2i (px , py ); |
2444 | glTexCoord2f (ox , dy ); glVertex2i (px , py + Th); |
2440 | glTexCoord2f (ox , dy ); glVertex2i (px , py + Th); |
2445 | glTexCoord2f (ox + dx, dy ); glVertex2i (px + Tw, py + Th); |
2441 | glTexCoord2f (ox + dx, dy ); glVertex2i (px + Tw, py + Th); |
2446 | glTexCoord2f (ox + dx, 0.f ); glVertex2i (px + Tw, py ); |
2442 | glTexCoord2f (ox + dx, 0.f ); glVertex2i (px + Tw, py ); |
2447 | } |
2443 | } |
2448 | |
2444 | |
2449 | if (corner) |
2445 | if (corner) |
2450 | { |
2446 | { |
2451 | float ox = corner * dx; |
2447 | float ox = corner * dx; |
2452 | |
2448 | |
2453 | glTexCoord2f (ox , dy ); glVertex2i (px , py ); |
2449 | glTexCoord2f (ox , dy ); glVertex2i (px , py ); |
2454 | glTexCoord2f (ox , dy * 2.f); glVertex2i (px , py + Th); |
2450 | glTexCoord2f (ox , dy * 2.f); glVertex2i (px , py + Th); |
2455 | glTexCoord2f (ox + dx, dy * 2.f); glVertex2i (px + Tw, py + Th); |
2451 | glTexCoord2f (ox + dx, dy * 2.f); glVertex2i (px + Tw, py + Th); |
2456 | glTexCoord2f (ox + dx, dy ); glVertex2i (px + Tw, py ); |
2452 | glTexCoord2f (ox + dx, dy ); glVertex2i (px + Tw, py ); |
2457 | } |
|
|
2458 | } |
|
|
2459 | } |
2453 | } |
2460 | } |
2454 | } |
2461 | } |
2455 | } |
2462 | } |
2456 | } |
2463 | |
2457 | |
2464 | glEnd (); |
2458 | glEnd (); |
2465 | glDisable (GL_TEXTURE_2D); |
2459 | glDisable (GL_TEXTURE_2D); |
2466 | key.texname = -1; |
2460 | key.texname = -1; |
2467 | } |
2461 | } |