… | |
… | |
15 | |
15 | |
16 | #include "EXTERN.h" |
16 | #include "EXTERN.h" |
17 | #include "perl.h" |
17 | #include "perl.h" |
18 | #include "XSUB.h" |
18 | #include "XSUB.h" |
19 | |
19 | |
|
|
20 | #include "flat_hash_map.hpp" |
|
|
21 | |
20 | #ifdef _WIN32 |
22 | #ifdef _WIN32 |
21 | # undef pipe |
23 | # undef pipe |
22 | // microsoft vs. C |
24 | // microsoft vs. C |
23 | # define sqrtf(x) sqrt(x) |
25 | # define sqrtf(x) sqrt(x) |
24 | # define atan2f(x,y) atan2(x,y) |
26 | # define atan2f(x,y) atan2(x,y) |
25 | # define M_PI 3.14159265f |
27 | # define M_PI 3.14159265f |
26 | #endif |
28 | #endif |
27 | |
29 | |
28 | #include <assert.h> |
30 | #include <cassert> |
29 | #include <math.h> |
31 | #include <cmath> |
30 | #include <string.h> |
32 | #include <cstring> |
31 | #include <stdio.h> |
33 | #include <cstdio> |
32 | #include <stdlib.h> |
34 | #include <cstdlib> |
|
|
35 | |
|
|
36 | #include <utility> |
|
|
37 | #include <bitset> |
33 | |
38 | |
34 | #define USE_RWOPS 1 // for SDL_mixer:LoadMUS_RW |
39 | #define USE_RWOPS 1 // for SDL_mixer:LoadMUS_RW |
35 | |
40 | |
36 | #include <SDL.h> |
41 | #include <SDL.h> |
37 | #include <SDL_thread.h> |
42 | #include <SDL_thread.h> |
… | |
… | |
195 | } |
200 | } |
196 | |
201 | |
197 | return 0; |
202 | return 0; |
198 | } |
203 | } |
199 | |
204 | |
200 | typedef struct cf_layout { |
205 | struct cf_layout { |
201 | PangoLayout *pl; |
206 | PangoLayout *pl; |
202 | float r, g, b, a; // default color for rgba mode |
207 | float r, g, b, a; // default color for rgba mode |
203 | int base_height; |
208 | int base_height; |
204 | DC__Font font; |
209 | DC__Font font; |
205 | rc_t *rc; |
210 | rc_t rc; |
206 | } *DC__Layout; |
211 | }; |
|
|
212 | |
|
|
213 | typedef cf_layout *DC__Layout; |
207 | |
214 | |
208 | static DC__Font default_font; |
215 | static DC__Font default_font; |
209 | static PangoContext *opengl_context; |
216 | static PangoContext *opengl_context; |
210 | static PangoFontMap *opengl_fontmap; |
217 | static PangoFontMap *opengl_fontmap; |
211 | |
218 | |
… | |
… | |
252 | } |
259 | } |
253 | |
260 | |
254 | typedef uint16_t tileid; |
261 | typedef uint16_t tileid; |
255 | typedef uint16_t faceid; |
262 | typedef uint16_t faceid; |
256 | |
263 | |
257 | typedef struct { |
264 | struct maptex |
|
|
265 | { |
258 | GLuint name; |
266 | GLuint name; |
259 | int w, h; |
267 | int w, h; |
260 | float s, t; |
268 | float s, t; |
261 | uint8_t r, g, b, a; |
269 | uint8_t r, g, b, a; |
262 | tileid smoothtile; |
270 | tileid smoothtile; |
263 | uint8_t smoothlevel; |
271 | uint8_t smoothlevel; |
264 | uint8_t unused; /* set to zero on use */ |
272 | uint8_t unused; /* set to zero on use */ |
265 | } maptex; |
273 | }; |
266 | |
274 | |
267 | typedef struct { |
275 | struct mapcell |
|
|
276 | { |
268 | uint32_t player; |
277 | uint32_t player; |
269 | tileid tile[3]; |
278 | tileid tile[3]; |
270 | uint16_t darkness; |
279 | uint16_t darkness; |
271 | uint8_t stat_width, stat_hp, flags, smoothmax; |
280 | uint8_t stat_width, stat_hp, flags, smoothmax; |
272 | } mapcell; |
281 | }; |
273 | |
282 | |
274 | typedef struct { |
283 | struct maprow |
|
|
284 | { |
275 | int32_t c0, c1; |
285 | int32_t c0, c1; |
276 | mapcell *col; |
286 | mapcell *col; |
277 | } maprow; |
287 | }; |
278 | |
288 | |
279 | typedef struct map { |
289 | struct mapgrid { |
280 | int x, y, w, h; |
290 | int x, y, w, h; |
281 | int ox, oy; /* offset to virtual global coordinate system */ |
291 | int ox, oy; /* offset to virtual global coordinate system */ |
282 | int faces; tileid *face2tile; // [faceid] |
292 | int faces; tileid *face2tile; // [faceid] |
283 | int texs; maptex *tex; // [tileid] |
293 | int texs; maptex *tex; // [tileid] |
284 | |
294 | |
285 | int32_t rows; |
295 | int32_t rows; |
286 | maprow *row; |
296 | maprow *row; |
287 | } *DC__Map; |
297 | }; |
|
|
298 | |
|
|
299 | typedef mapgrid *DC__Map; |
288 | |
300 | |
289 | static char * |
301 | static char * |
290 | prepend (char *ptr, int sze, int inc) |
302 | prepend (char *ptr, int sze, int inc) |
291 | { |
303 | { |
292 | char *p; |
304 | char *p; |
… | |
… | |
310 | |
322 | |
311 | #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)) |
312 | #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)) |
313 | |
325 | |
314 | static void |
326 | static void |
315 | need_facenum (struct map *self, faceid face) |
327 | need_facenum (struct mapgrid *self, faceid face) |
316 | { |
328 | { |
317 | while (self->faces <= face) |
329 | while (self->faces <= face) |
318 | { |
330 | { |
319 | Append (tileid, self->face2tile, self->faces, self->faces); |
331 | Append (tileid, self->face2tile, self->faces, self->faces); |
320 | self->faces *= 2; |
332 | self->faces *= 2; |
321 | } |
333 | } |
322 | } |
334 | } |
323 | |
335 | |
324 | static void |
336 | static void |
325 | need_texid (struct map *self, int texid) |
337 | need_texid (struct mapgrid *self, int texid) |
326 | { |
338 | { |
327 | while (self->texs <= texid) |
339 | while (self->texs <= texid) |
328 | { |
340 | { |
329 | Append (maptex, self->tex, self->texs, self->texs); |
341 | Append (maptex, self->tex, self->texs, self->texs); |
330 | self->texs *= 2; |
342 | self->texs *= 2; |
331 | } |
343 | } |
332 | } |
344 | } |
333 | |
345 | |
334 | static maprow * |
346 | static maprow * |
335 | map_get_row (DC__Map self, int y) |
347 | map_get_row (mapgrid *self, int y) |
336 | { |
348 | { |
337 | if (0 > y) |
349 | if (0 > y) |
338 | { |
350 | { |
339 | int extend = - y + MAP_EXTEND_Y; |
351 | int extend = - y + MAP_EXTEND_Y; |
340 | Prepend (maprow, self->row, self->rows, extend); |
352 | Prepend (maprow, self->row, self->rows, extend); |
… | |
… | |
378 | |
390 | |
379 | return row->col + (x - row->c0); |
391 | return row->col + (x - row->c0); |
380 | } |
392 | } |
381 | |
393 | |
382 | static mapcell * |
394 | static mapcell * |
383 | map_get_cell (DC__Map self, int x, int y) |
395 | map_get_cell (mapgrid *self, int x, int y) |
384 | { |
396 | { |
385 | return row_get_cell (map_get_row (self, y), x); |
397 | return row_get_cell (map_get_row (self, y), x); |
386 | } |
398 | } |
387 | |
399 | |
388 | static void |
400 | static void |
389 | map_clear (DC__Map self) |
401 | map_clear (mapgrid *self) |
390 | { |
402 | { |
391 | int r; |
403 | int r; |
392 | |
404 | |
393 | for (r = 0; r < self->rows; r++) |
405 | for (r = 0; r < self->rows; r++) |
394 | Safefree (self->row[r].col); |
406 | Safefree (self->row[r].col); |
… | |
… | |
412 | (cell)->flags = 0; \ |
424 | (cell)->flags = 0; \ |
413 | (cell)->player = 0; \ |
425 | (cell)->player = 0; \ |
414 | } while (0) |
426 | } while (0) |
415 | |
427 | |
416 | static void |
428 | static void |
417 | 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) |
418 | { |
430 | { |
419 | int x, y; |
431 | int x, y; |
420 | maprow *row; |
432 | maprow *row; |
421 | mapcell *cell; |
433 | mapcell *cell; |
422 | |
434 | |
… | |
… | |
439 | CELL_CLEAR (cell); |
451 | CELL_CLEAR (cell); |
440 | } |
452 | } |
441 | } |
453 | } |
442 | } |
454 | } |
443 | |
455 | |
444 | typedef struct { |
456 | struct smooth_key |
|
|
457 | { |
445 | tileid tile; |
458 | tileid tile; |
446 | uint8_t x, y, level; |
459 | uint8_t x, y, level; |
447 | } smooth_key; |
460 | |
|
|
461 | bool operator == (const smooth_key &o) const |
|
|
462 | { |
|
|
463 | return tile == o.tile && x == o.x && y == o.y && level == o.level; |
|
|
464 | } |
|
|
465 | }; |
|
|
466 | |
|
|
467 | typedef ska::flat_hash_map<smooth_key, IV> smooth_hash; |
|
|
468 | |
|
|
469 | namespace std { |
|
|
470 | template <> |
|
|
471 | struct hash<smooth_key> |
|
|
472 | { |
|
|
473 | size_t operator () (const smooth_key &v) const |
|
|
474 | { |
|
|
475 | return v.tile + (v.x << 8) + (v.y << 16) + (v.level << 24); |
|
|
476 | } |
|
|
477 | }; |
|
|
478 | } |
448 | |
479 | |
449 | static void |
480 | static void |
450 | smooth_or_bits (HV *hv, smooth_key *key, IV bits) |
481 | smooth_or_bits (smooth_hash &h, smooth_key &key, IV bits) |
451 | { |
482 | { |
452 | SV **sv = hv_fetch (hv, (char *)key, sizeof (*key), 1); |
483 | auto &&it = h.find (key); |
453 | |
484 | |
454 | if (SvIOK (*sv)) |
485 | if (it == h.end ()) |
455 | SvIV_set (*sv, SvIVX (*sv) | bits); |
486 | h.insert (std::make_pair (key, bits)); |
456 | else |
487 | else |
457 | sv_setiv (*sv, bits); |
488 | it->second |= bits; |
458 | } |
489 | } |
459 | |
490 | |
460 | static void |
491 | static void |
461 | music_finished (void) |
492 | music_finished (void) |
462 | { |
493 | { |
… | |
… | |
1338 | tc_restore (); |
1369 | tc_restore (); |
1339 | |
1370 | |
1340 | DC::Layout |
1371 | DC::Layout |
1341 | new (SV *klass) |
1372 | new (SV *klass) |
1342 | CODE: |
1373 | CODE: |
1343 | New (0, RETVAL, 1, struct cf_layout); |
1374 | RETVAL = new cf_layout; |
1344 | |
1375 | |
1345 | RETVAL->pl = pango_layout_new (opengl_context); |
1376 | RETVAL->pl = pango_layout_new (opengl_context); |
1346 | RETVAL->r = 1.; |
1377 | RETVAL->r = 1.; |
1347 | RETVAL->g = 1.; |
1378 | RETVAL->g = 1.; |
1348 | RETVAL->b = 1.; |
1379 | RETVAL->b = 1.; |
1349 | RETVAL->a = 1.; |
1380 | RETVAL->a = 1.; |
1350 | RETVAL->base_height = MIN_FONT_HEIGHT; |
1381 | RETVAL->base_height = MIN_FONT_HEIGHT; |
1351 | RETVAL->font = 0; |
1382 | RETVAL->font = 0; |
1352 | RETVAL->rc = rc_alloc (); |
|
|
1353 | |
1383 | |
1354 | pango_layout_set_wrap (RETVAL->pl, PANGO_WRAP_WORD_CHAR); |
1384 | pango_layout_set_wrap (RETVAL->pl, PANGO_WRAP_WORD_CHAR); |
1355 | layout_update_font (RETVAL); |
1385 | layout_update_font (RETVAL); |
1356 | OUTPUT: |
1386 | OUTPUT: |
1357 | RETVAL |
1387 | RETVAL |
1358 | |
1388 | |
1359 | void |
1389 | void |
1360 | DESTROY (DC::Layout self) |
1390 | DESTROY (DC::Layout self) |
1361 | CODE: |
1391 | CODE: |
1362 | g_object_unref (self->pl); |
1392 | g_object_unref (self->pl); |
1363 | rc_free (self->rc); |
|
|
1364 | Safefree (self); |
1393 | delete self; |
1365 | |
1394 | |
1366 | void |
1395 | void |
1367 | set_text (DC::Layout self, SV *text_) |
1396 | set_text (DC::Layout self, SV *text_) |
1368 | CODE: |
1397 | CODE: |
1369 | { |
1398 | { |
… | |
… | |
1623 | } |
1652 | } |
1624 | |
1653 | |
1625 | void |
1654 | void |
1626 | render (DC::Layout self, float x, float y, int flags = 0) |
1655 | render (DC::Layout self, float x, float y, int flags = 0) |
1627 | CODE: |
1656 | CODE: |
1628 | rc_clear (self->rc); |
1657 | self->rc.clear (); |
1629 | pango_opengl_render_layout_subpixel ( |
1658 | pango_opengl_render_layout_subpixel ( |
1630 | self->pl, |
1659 | self->pl, |
1631 | self->rc, |
1660 | &self->rc, |
1632 | x * PANGO_SCALE, y * PANGO_SCALE, |
1661 | x * PANGO_SCALE, y * PANGO_SCALE, |
1633 | self->r, self->g, self->b, self->a, |
1662 | self->r, self->g, self->b, self->a, |
1634 | flags |
1663 | flags |
1635 | ); |
1664 | ); |
1636 | // we assume that context_change actually clears/frees stuff |
1665 | // we assume that context_change actually clears/frees stuff |
… | |
… | |
1647 | gl_BlendFuncSeparate (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, |
1676 | gl_BlendFuncSeparate (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, |
1648 | GL_ONE , GL_ONE_MINUS_SRC_ALPHA); |
1677 | GL_ONE , GL_ONE_MINUS_SRC_ALPHA); |
1649 | glEnable (GL_ALPHA_TEST); |
1678 | glEnable (GL_ALPHA_TEST); |
1650 | glAlphaFunc (GL_GREATER, 7.f / 255.f); |
1679 | glAlphaFunc (GL_GREATER, 7.f / 255.f); |
1651 | |
1680 | |
1652 | rc_draw (self->rc); |
1681 | self->rc.draw (); |
1653 | |
1682 | |
1654 | glDisable (GL_ALPHA_TEST); |
1683 | glDisable (GL_ALPHA_TEST); |
1655 | glDisable (GL_BLEND); |
1684 | glDisable (GL_BLEND); |
1656 | glDisable (GL_TEXTURE_2D); |
1685 | glDisable (GL_TEXTURE_2D); |
1657 | } |
1686 | } |
… | |
… | |
1871 | PROTOTYPES: DISABLE |
1900 | PROTOTYPES: DISABLE |
1872 | |
1901 | |
1873 | DC::Map |
1902 | DC::Map |
1874 | new (SV *klass) |
1903 | new (SV *klass) |
1875 | CODE: |
1904 | CODE: |
1876 | New (0, RETVAL, 1, struct map); |
1905 | New (0, RETVAL, 1, mapgrid); |
1877 | RETVAL->x = 0; |
1906 | RETVAL->x = 0; |
1878 | RETVAL->y = 0; |
1907 | RETVAL->y = 0; |
1879 | RETVAL->w = 0; |
1908 | RETVAL->w = 0; |
1880 | RETVAL->h = 0; |
1909 | RETVAL->h = 0; |
1881 | RETVAL->ox = 0; |
1910 | RETVAL->ox = 0; |
… | |
… | |
2193 | 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) |
2222 | 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) |
2194 | CODE: |
2223 | CODE: |
2195 | { |
2224 | { |
2196 | int x, y, z; |
2225 | int x, y, z; |
2197 | |
2226 | |
2198 | HV *smooth = (HV *)sv_2mortal ((SV *)newHV ()); |
|
|
2199 | uint32_t smooth_level[256 / 32]; // one bit for every possible smooth level |
|
|
2200 | static uint8_t smooth_max[256][256]; // egad, fast and wasteful on memory (64k) |
2227 | static uint8_t smooth_max[256][256]; // egad, fast and wasteful on memory (64k), also, static! |
2201 | smooth_key skey; |
|
|
2202 | int pl_x, pl_y; |
2228 | int pl_x, pl_y; |
2203 | maptex pl_tex; |
2229 | maptex pl_tex; |
2204 | rc_t *rc = rc_alloc (); |
2230 | rc_t rc; |
2205 | rc_t *rc_ov = rc_alloc (); |
2231 | rc_t rc_ov; |
2206 | rc_key_t key; |
2232 | rc_key_t key; |
2207 | rc_array_t *arr; |
2233 | rc_t::array_t *arr; |
2208 | |
2234 | |
2209 | pl_tex.name = 0; |
2235 | pl_tex.name = 0; |
2210 | |
2236 | |
2211 | // that's current max. sorry. |
2237 | // that's current max. sorry. |
2212 | if (sw > 255) sw = 255; |
2238 | if (sw > 255) sw = 255; |
2213 | if (sh > 255) sh = 255; |
2239 | if (sh > 255) sh = 255; |
2214 | |
|
|
2215 | // clear key, in case of extra padding |
|
|
2216 | memset (&skey, 0, sizeof (skey)); |
|
|
2217 | |
2240 | |
2218 | memset (&key, 0, sizeof (key)); |
2241 | memset (&key, 0, sizeof (key)); |
2219 | key.r = 255; |
2242 | key.r = 255; |
2220 | key.g = 255; |
2243 | key.g = 255; |
2221 | key.b = 255; |
2244 | key.b = 255; |
… | |
… | |
2227 | my += self->y; |
2250 | my += self->y; |
2228 | |
2251 | |
2229 | // first pass: determine smooth_max |
2252 | // first pass: determine smooth_max |
2230 | // rather ugly, if you ask me |
2253 | // rather ugly, if you ask me |
2231 | // could also be stored inside mapcell and updated on change |
2254 | // could also be stored inside mapcell and updated on change |
2232 | memset (smooth_max, 0, sizeof (smooth_max)); |
2255 | memset (smooth_max, 0, sizeof (smooth_max[0]) * (sh + 1)); |
2233 | |
2256 | |
2234 | for (y = 0; y < sh; y++) |
2257 | for (y = 0; y < sh; y++) |
2235 | if (0 <= y + my && y + my < self->rows) |
2258 | if (0 <= y + my && y + my < self->rows) |
2236 | { |
2259 | { |
2237 | maprow *row = self->row + (y + my); |
2260 | maprow *row = self->row + (y + my); |
… | |
… | |
2252 | glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
2275 | glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
2253 | glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); |
2276 | glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); |
2254 | |
2277 | |
2255 | for (z = 0; z <= 2; z++) |
2278 | for (z = 0; z <= 2; z++) |
2256 | { |
2279 | { |
2257 | memset (smooth_level, 0, sizeof (smooth_level)); |
2280 | std::bitset<256> smooth_level; // one bit for every possible smooth level |
|
|
2281 | smooth_key skey; |
|
|
2282 | smooth_hash smooth; |
2258 | key.texname = -1; |
2283 | key.texname = -1; |
2259 | |
2284 | |
2260 | for (y = 0; y < sh; y++) |
2285 | for (y = 0; y < sh; y++) |
2261 | if (0 <= y + my && y + my < self->rows) |
2286 | if (0 <= y + my && y + my < self->rows) |
2262 | { |
2287 | { |
… | |
… | |
2279 | |
2304 | |
2280 | if (!tex.name) |
2305 | if (!tex.name) |
2281 | tex = self->tex [TEXID_NOFACE]; /* missing, replace by noface */ |
2306 | tex = self->tex [TEXID_NOFACE]; /* missing, replace by noface */ |
2282 | |
2307 | |
2283 | key.texname = tex.name; |
2308 | key.texname = tex.name; |
2284 | arr = rc_array (rc, &key); |
2309 | arr = &rc.array (key); |
2285 | } |
2310 | } |
2286 | |
2311 | |
2287 | px = (x + 1) * Th - tex.w; |
2312 | px = (x + 1) * Th - tex.w; |
2288 | py = (y + 1) * Tw - tex.h; |
2313 | py = (y + 1) * Tw - tex.h; |
2289 | |
2314 | |
… | |
… | |
2293 | pl_y = py; |
2318 | pl_y = py; |
2294 | pl_tex = tex; |
2319 | pl_tex = tex; |
2295 | continue; |
2320 | continue; |
2296 | } |
2321 | } |
2297 | |
2322 | |
2298 | rc_t2f_v3f (arr, 0 , 0 , px , py , 0); |
2323 | arr->t2f_v3f (0 , 0 , px , py , 0); |
2299 | rc_t2f_v3f (arr, 0 , tex.t, px , py + tex.h, 0); |
2324 | arr->t2f_v3f (0 , tex.t, px , py + tex.h, 0); |
2300 | rc_t2f_v3f (arr, tex.s, tex.t, px + tex.w, py + tex.h, 0); |
2325 | arr->t2f_v3f (tex.s, tex.t, px + tex.w, py + tex.h, 0); |
2301 | rc_t2f_v3f (arr, tex.s, 0 , px + tex.w, py , 0); |
2326 | arr->t2f_v3f (tex.s, 0 , px + tex.w, py , 0); |
2302 | |
2327 | |
2303 | // update smooth hash |
2328 | // update smooth hash |
2304 | if (tex.smoothtile) |
2329 | if (tex.smoothtile) |
2305 | { |
2330 | { |
2306 | skey.tile = tex.smoothtile; |
2331 | skey.tile = tex.smoothtile; |
2307 | skey.level = tex.smoothlevel; |
2332 | skey.level = tex.smoothlevel; |
2308 | |
2333 | |
2309 | smooth_level [tex.smoothlevel >> 5] |= ((uint32_t)1) << (tex.smoothlevel & 31); |
2334 | smooth_level[tex.smoothlevel] = 1; |
2310 | |
2335 | |
2311 | // add bits to current tile and all neighbours. skey.x|y is |
2336 | // add bits to current tile and all neighbours. skey.x|y is |
2312 | // shifted +1|+1 so we always stay positive. |
2337 | // shifted +1|+1 so we always stay positive. |
2313 | |
2338 | |
2314 | // bits is ___n cccc CCCC bbbb |
2339 | // bits is ___n cccc CCCC bbbb |
… | |
… | |
2322 | |
2347 | |
2323 | // corners: 1 ┛· 2 ·┗ 4 ·· 8 ·· |
2348 | // corners: 1 ┛· 2 ·┗ 4 ·· 8 ·· |
2324 | // ·· ·· ·┏ ┓· |
2349 | // ·· ·· ·┏ ┓· |
2325 | |
2350 | |
2326 | // full tile |
2351 | // full tile |
2327 | skey.x = x + 1; skey.y = y + 1; smooth_or_bits (smooth, &skey, 0x1000); |
2352 | skey.x = x + 1; skey.y = y + 1; smooth_or_bits (smooth, skey, 0x1000); |
2328 | |
2353 | |
2329 | // borders |
2354 | // borders |
2330 | skey.x = x + 2; skey.y = y + 1; smooth_or_bits (smooth, &skey, 0x0091); |
2355 | skey.x = x + 2; skey.y = y + 1; smooth_or_bits (smooth, skey, 0x0091); |
2331 | skey.x = x + 1; skey.y = y + 2; smooth_or_bits (smooth, &skey, 0x0032); |
2356 | skey.x = x + 1; skey.y = y + 2; smooth_or_bits (smooth, skey, 0x0032); |
2332 | skey.x = x ; skey.y = y + 1; smooth_or_bits (smooth, &skey, 0x0064); |
2357 | skey.x = x ; skey.y = y + 1; smooth_or_bits (smooth, skey, 0x0064); |
2333 | skey.x = x + 1; skey.y = y ; smooth_or_bits (smooth, &skey, 0x00c8); |
2358 | skey.x = x + 1; skey.y = y ; smooth_or_bits (smooth, skey, 0x00c8); |
2334 | |
2359 | |
2335 | // corners |
2360 | // corners |
2336 | skey.x = x + 2; skey.y = y + 2; smooth_or_bits (smooth, &skey, 0x0100); |
2361 | skey.x = x + 2; skey.y = y + 2; smooth_or_bits (smooth, skey, 0x0100); |
2337 | skey.x = x ; skey.y = y + 2; smooth_or_bits (smooth, &skey, 0x0200); |
2362 | skey.x = x ; skey.y = y + 2; smooth_or_bits (smooth, skey, 0x0200); |
2338 | skey.x = x ; skey.y = y ; smooth_or_bits (smooth, &skey, 0x0400); |
2363 | skey.x = x ; skey.y = y ; smooth_or_bits (smooth, skey, 0x0400); |
2339 | skey.x = x + 2; skey.y = y ; smooth_or_bits (smooth, &skey, 0x0800); |
2364 | skey.x = x + 2; skey.y = y ; smooth_or_bits (smooth, skey, 0x0800); |
2340 | } |
2365 | } |
2341 | } |
2366 | } |
2342 | |
2367 | |
2343 | if (expect_false (z == 2) && expect_false (cell->flags)) |
2368 | if (expect_false (z == 2) && expect_false (cell->flags)) |
2344 | { |
2369 | { |
2345 | // overlays such as the speech bubble, probably more to come |
2370 | // overlays such as the speech bubble, probably more to come |
2346 | if (cell->flags & 1) |
2371 | if (cell->flags & 1) |
2347 | { |
2372 | { |
2348 | rc_key_t key_ov = key; |
2373 | rc_key_t key_ov = key; |
2349 | maptex tex = self->tex [TEXID_SPEECH]; |
2374 | maptex tex = self->tex[TEXID_SPEECH]; |
2350 | rc_array_t *arr; |
|
|
2351 | int px = x * Tw + Tw * 2 / 32; |
2375 | int px = x * Tw + Tw * 2 / 32; |
2352 | int py = y * Th - Th * 6 / 32; |
2376 | int py = y * Th - Th * 6 / 32; |
2353 | |
2377 | |
2354 | key_ov.texname = tex.name; |
2378 | key_ov.texname = tex.name; |
2355 | arr = rc_array (rc_ov, &key_ov); |
2379 | rc_t::array_t &arr = rc_ov.array (key_ov); |
2356 | |
2380 | |
2357 | rc_t2f_v3f (arr, 0 , 0 , px , py , 0); |
2381 | arr.t2f_v3f (0 , 0 , px , py , 0); |
2358 | rc_t2f_v3f (arr, 0 , tex.t, px , py + Th, 0); |
2382 | arr.t2f_v3f (0 , tex.t, px , py + Th, 0); |
2359 | rc_t2f_v3f (arr, tex.s, tex.t, px + Tw, py + Th, 0); |
2383 | arr.t2f_v3f (tex.s, tex.t, px + Tw, py + Th, 0); |
2360 | rc_t2f_v3f (arr, tex.s, 0 , px + Tw, py , 0); |
2384 | arr.t2f_v3f (tex.s, 0 , px + Tw, py , 0); |
2361 | } |
2385 | } |
2362 | } |
2386 | } |
2363 | } |
2387 | } |
2364 | } |
2388 | } |
2365 | |
2389 | |
2366 | rc_draw (rc); |
2390 | rc.draw (); |
2367 | rc_clear (rc); |
2391 | rc.clear (); |
2368 | |
2392 | |
2369 | // go through all smoothlevels, lowest to highest, then draw. |
2393 | // go through all smoothlevels, lowest to highest, then draw. |
2370 | // this is basically counting sort |
2394 | // this is basically counting sort |
2371 | { |
2395 | { |
2372 | int w, b; |
2396 | int w, b; |
2373 | |
2397 | |
2374 | glEnable (GL_TEXTURE_2D); |
2398 | glEnable (GL_TEXTURE_2D); |
2375 | glBegin (GL_QUADS); |
2399 | glBegin (GL_QUADS); |
2376 | for (w = 0; w < 256 / 32; ++w) |
2400 | for (int level = 0; level < smooth_level.size (); ++level) |
|
|
2401 | if (smooth_level[level]) |
|
|
2402 | for (auto &&it = smooth.begin (); it != smooth.end (); ++it) |
2377 | { |
2403 | { |
2378 | uint32_t smask = smooth_level [w]; |
2404 | smooth_key &skey = it->first; |
2379 | if (smask) |
2405 | IV bits = it->second; |
2380 | for (b = 0; b < 32; ++b) |
2406 | |
2381 | if (smask & (((uint32_t)1) << b)) |
2407 | if (!(bits & 0x1000) |
|
|
2408 | && skey.level == level |
|
|
2409 | && level > smooth_max [skey.x][skey.y]) |
2382 | { |
2410 | { |
2383 | int level = (w << 5) | b; |
2411 | maptex tex = self->tex [skey.tile]; |
|
|
2412 | int px = (((int)skey.x) - 1) * Tw; |
|
|
2413 | int py = (((int)skey.y) - 1) * Th; |
|
|
2414 | int border = bits & 15; |
|
|
2415 | int corner = (bits >> 8) & ~(bits >> 4) & 15; |
|
|
2416 | float dx = tex.s * .0625f; // 16 images/row |
|
|
2417 | float dy = tex.t * .5f ; // 2 images/column |
|
|
2418 | |
2384 | HE *he; |
2419 | if (tex.name) |
2385 | |
|
|
2386 | hv_iterinit (smooth); |
|
|
2387 | while ((he = hv_iternext (smooth))) |
|
|
2388 | { |
2420 | { |
2389 | smooth_key *skey = (smooth_key *)HeKEY (he); |
2421 | // this time avoiding texture state changes |
2390 | IV bits = SvIVX (HeVAL (he)); |
2422 | // save gobs of state changes. |
2391 | |
2423 | if (key.texname != tex.name) |
2392 | if (!(bits & 0x1000) |
|
|
2393 | && skey->level == level |
|
|
2394 | && level > smooth_max [skey->x][skey->y]) |
|
|
2395 | { |
2424 | { |
2396 | maptex tex = self->tex [skey->tile]; |
|
|
2397 | int px = (((int)skey->x) - 1) * Tw; |
|
|
2398 | int py = (((int)skey->y) - 1) * Th; |
|
|
2399 | int border = bits & 15; |
|
|
2400 | int corner = (bits >> 8) & ~(bits >> 4) & 15; |
|
|
2401 | float dx = tex.s * .0625f; // 16 images/row |
|
|
2402 | float dy = tex.t * .5f ; // 2 images/column |
|
|
2403 | |
|
|
2404 | if (tex.name) |
|
|
2405 | { |
|
|
2406 | // this time avoiding texture state changes |
|
|
2407 | // save gobs of state changes. |
|
|
2408 | if (key.texname != tex.name) |
|
|
2409 | { |
|
|
2410 | self->tex [skey->tile].unused = 0; |
2425 | self->tex [skey.tile].unused = 0; |
2411 | |
2426 | |
2412 | glEnd (); |
2427 | glEnd (); |
2413 | glBindTexture (GL_TEXTURE_2D, key.texname = tex.name); |
2428 | glBindTexture (GL_TEXTURE_2D, key.texname = tex.name); |
2414 | glBegin (GL_QUADS); |
2429 | glBegin (GL_QUADS); |
2415 | } |
2430 | } |
2416 | |
2431 | |
2417 | if (border) |
2432 | if (border) |
2418 | { |
2433 | { |
2419 | float ox = border * dx; |
2434 | float ox = border * dx; |
2420 | |
2435 | |
2421 | glTexCoord2f (ox , 0.f ); glVertex2i (px , py ); |
2436 | glTexCoord2f (ox , 0.f ); glVertex2i (px , py ); |
2422 | glTexCoord2f (ox , dy ); glVertex2i (px , py + Th); |
2437 | glTexCoord2f (ox , dy ); glVertex2i (px , py + Th); |
2423 | glTexCoord2f (ox + dx, dy ); glVertex2i (px + Tw, py + Th); |
2438 | glTexCoord2f (ox + dx, dy ); glVertex2i (px + Tw, py + Th); |
2424 | glTexCoord2f (ox + dx, 0.f ); glVertex2i (px + Tw, py ); |
2439 | glTexCoord2f (ox + dx, 0.f ); glVertex2i (px + Tw, py ); |
2425 | } |
2440 | } |
2426 | |
2441 | |
2427 | if (corner) |
2442 | if (corner) |
2428 | { |
2443 | { |
2429 | float ox = corner * dx; |
2444 | float ox = corner * dx; |
2430 | |
2445 | |
2431 | glTexCoord2f (ox , dy ); glVertex2i (px , py ); |
2446 | glTexCoord2f (ox , dy ); glVertex2i (px , py ); |
2432 | glTexCoord2f (ox , dy * 2.f); glVertex2i (px , py + Th); |
2447 | glTexCoord2f (ox , dy * 2.f); glVertex2i (px , py + Th); |
2433 | glTexCoord2f (ox + dx, dy * 2.f); glVertex2i (px + Tw, py + Th); |
2448 | glTexCoord2f (ox + dx, dy * 2.f); glVertex2i (px + Tw, py + Th); |
2434 | glTexCoord2f (ox + dx, dy ); glVertex2i (px + Tw, py ); |
2449 | glTexCoord2f (ox + dx, dy ); glVertex2i (px + Tw, py ); |
2435 | } |
|
|
2436 | } |
|
|
2437 | } |
2450 | } |
2438 | } |
2451 | } |
2439 | } |
2452 | } |
2440 | } |
2453 | } |
2441 | |
2454 | |
2442 | glEnd (); |
2455 | glEnd (); |
2443 | glDisable (GL_TEXTURE_2D); |
2456 | glDisable (GL_TEXTURE_2D); |
2444 | key.texname = -1; |
2457 | key.texname = -1; |
2445 | } |
2458 | } |
2446 | |
|
|
2447 | hv_clear (smooth); |
|
|
2448 | } |
2459 | } |
2449 | |
2460 | |
2450 | if (pl_tex.name) |
2461 | if (pl_tex.name) |
2451 | { |
2462 | { |
2452 | maptex tex = pl_tex; |
2463 | maptex tex = pl_tex; |
2453 | int px = pl_x + sdx; |
2464 | int px = pl_x + sdx; |
2454 | int py = pl_y + sdy; |
2465 | int py = pl_y + sdy; |
2455 | |
2466 | |
2456 | key.texname = tex.name; |
2467 | key.texname = tex.name; |
2457 | arr = rc_array (rc, &key); |
2468 | rc_t::array_t &arr = rc.array (key); |
2458 | |
2469 | |
2459 | rc_t2f_v3f (arr, 0 , 0 , px , py , 0); |
2470 | arr.t2f_v3f (0 , 0 , px , py , 0); |
2460 | rc_t2f_v3f (arr, 0 , tex.t, px , py + tex.h, 0); |
2471 | arr.t2f_v3f (0 , tex.t, px , py + tex.h, 0); |
2461 | rc_t2f_v3f (arr, tex.s, tex.t, px + tex.w, py + tex.h, 0); |
2472 | arr.t2f_v3f (tex.s, tex.t, px + tex.w, py + tex.h, 0); |
2462 | rc_t2f_v3f (arr, tex.s, 0 , px + tex.w, py , 0); |
2473 | arr.t2f_v3f (tex.s, 0 , px + tex.w, py , 0); |
2463 | |
2474 | |
2464 | rc_draw (rc); |
2475 | rc.draw (); |
2465 | } |
2476 | } |
2466 | |
2477 | |
2467 | rc_draw (rc_ov); |
2478 | rc_ov.draw (); |
2468 | rc_clear (rc_ov); |
2479 | rc_ov.clear (); |
2469 | |
2480 | |
2470 | glDisable (GL_BLEND); |
2481 | glDisable (GL_BLEND); |
2471 | rc_free (rc); |
|
|
2472 | rc_free (rc_ov); |
|
|
2473 | |
2482 | |
2474 | // top layer: overlays such as the health bar |
2483 | // top layer: overlays such as the health bar |
2475 | for (y = 0; y < sh; y++) |
2484 | for (y = 0; y < sh; y++) |
2476 | if (0 <= y + my && y + my < self->rows) |
2485 | if (0 <= y + my && y + my < self->rows) |
2477 | { |
2486 | { |