ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/Client.xs
(Generate patch)

Comparing deliantra/Deliantra-Client/Client.xs (file contents):
Revision 1.321 by root, Sun Nov 18 01:15:04 2018 UTC vs.
Revision 1.332 by root, Mon Nov 19 00:56:08 2018 UTC

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>
71# include <netinet/in.h> 76# include <netinet/in.h>
72# include <netinet/tcp.h> 77# include <netinet/tcp.h>
73# include <inttypes.h> 78# include <inttypes.h>
74#endif 79#endif
75 80
76#if __GNUC__ >= 4 81#include "ecb.h"
77# define expect(expr,value) __builtin_expect ((expr),(value))
78#else
79# define expect(expr,value) (expr)
80#endif
81
82#define expect_false(expr) expect ((expr) != 0, 0)
83#define expect_true(expr) expect ((expr) != 0, 1)
84 82
85#define OBJ_STR "\xef\xbf\xbc" /* U+FFFC, object replacement character */ 83#define OBJ_STR "\xef\xbf\xbc" /* U+FFFC, object replacement character */
86 84
87/* this is used as fow flag as well, so has to have a different value */ 85/* this is used as fow flag as well, so has to have a different value */
88/* then anything that is computed by incoming darkness */ 86/* then anything that is computed by incoming darkness */
106fast_sv_grow (SV *sv, STRLEN need) 104fast_sv_grow (SV *sv, STRLEN need)
107{ 105{
108 STRLEN len = SvLEN (sv); 106 STRLEN len = SvLEN (sv);
109 STRLEN want = SvCUR (sv) + need; 107 STRLEN want = SvCUR (sv) + need;
110 108
111 if (expect_false (len < want)) 109 if (ecb_expect_false (len < want))
112 { 110 {
113 do 111 do
114 len *= 2; 112 len *= 2;
115 while (len < want); 113 while (len < want);
116 114
195 } 193 }
196 194
197 return 0; 195 return 0;
198} 196}
199 197
200typedef struct cf_layout { 198struct cf_layout {
201 PangoLayout *pl; 199 PangoLayout *pl;
202 float r, g, b, a; // default color for rgba mode 200 float r, g, b, a; // default color for rgba mode
203 int base_height; 201 int base_height;
204 DC__Font font; 202 DC__Font font;
205 rc_t *rc; 203 rc_t rc;
206} *DC__Layout; 204};
205
206typedef cf_layout *DC__Layout;
207 207
208static DC__Font default_font; 208static DC__Font default_font;
209static PangoContext *opengl_context; 209static PangoContext *opengl_context;
210static PangoFontMap *opengl_fontmap; 210static PangoFontMap *opengl_fontmap;
211 211
249 249
250 *w = rect.width; 250 *w = rect.width;
251 *h = rect.height; 251 *h = rect.height;
252} 252}
253 253
254/////////////////////////////////////////////////////////////////////////////
255
254typedef uint16_t tileid; 256typedef uint16_t tileid;
255typedef uint16_t faceid; 257typedef uint16_t faceid;
256 258
257typedef struct { 259struct maptex
260{
258 GLuint name; 261 GLuint name;
259 int w, h; 262 int w, h;
260 float s, t; 263 float s, t;
261 uint8_t r, g, b, a; 264 uint8_t r, g, b, a;
262 tileid smoothtile; 265 tileid smoothtile;
263 uint8_t smoothlevel; 266 uint8_t smoothlevel;
264 uint8_t unused; /* set to zero on use */ 267 uint8_t unused; /* set to zero on use */
265} maptex; 268};
266 269
267typedef struct { 270struct mapcell
271{
268 uint32_t player; 272 uint32_t player;
269 tileid tile[3]; 273 tileid tile[3];
270 uint16_t darkness; 274 uint16_t darkness;
271 uint8_t stat_width, stat_hp, flags, smoothmax; 275 uint8_t stat_width, stat_hp, flags, smoothmax;
272} mapcell; 276};
273 277
274typedef struct { 278struct maprow
279{
275 int32_t c0, c1; 280 int32_t c0, c1;
276 mapcell *col; 281 mapcell *col;
277} maprow; 282};
278 283
279typedef struct map { 284struct mapgrid {
280 int x, y, w, h; 285 int x, y, w, h;
281 int ox, oy; /* offset to virtual global coordinate system */ 286 int ox, oy; /* offset to virtual global coordinate system */
282 int faces; tileid *face2tile; // [faceid] 287 int faces; tileid *face2tile; // [faceid]
283 int texs; maptex *tex; // [tileid] 288 int texs; maptex *tex; // [tileid]
284 289
285 int32_t rows; 290 int32_t rows;
286 maprow *row; 291 maprow *row;
287} *DC__Map; 292};
288 293
289static char * 294typedef mapgrid *DC__Map;
295
296template<typename T>
297static void
290prepend (char *ptr, int sze, int inc) 298prepend (T *&ptr, int sze, int inc)
291{ 299{
292 char *p; 300 T *p;
293 301
294 New (0, p, sze + inc, char); 302 Newx (p, inc + sze, T);
295 Zero (p, inc, char); 303 Zero (p, inc, T);
296 Move (ptr, p + inc, sze, char); 304 Move (ptr, p + inc, sze, T);
297 Safefree (ptr); 305 Safefree (ptr);
298 306
299 return p; 307 ptr = p;
300} 308}
301 309
302static char * 310template<typename T>
303append (char *ptr, int sze, int inc)
304{
305 Renew (ptr, sze + inc, char);
306 Zero (ptr + sze, inc, char);
307
308 return ptr;
309}
310
311#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))
313
314static void 311static void
312append (T *&ptr, int sze, int inc)
313{
314 Renew (ptr, sze + inc, T);
315 Zero (ptr + sze, inc, T);
316}
317
318static void
315need_facenum (struct map *self, faceid face) 319need_facenum (struct mapgrid *self, faceid face)
316{ 320{
317 while (self->faces <= face) 321 while (self->faces <= face)
318 { 322 {
319 Append (tileid, self->face2tile, self->faces, self->faces); 323 append (self->face2tile, self->faces, self->faces);
320 self->faces *= 2; 324 self->faces *= 2;
321 } 325 }
322} 326}
323 327
324static void 328static void
325need_texid (struct map *self, int texid) 329need_texid (struct mapgrid *self, int texid)
326{ 330{
327 while (self->texs <= texid) 331 while (self->texs <= texid)
328 { 332 {
329 Append (maptex, self->tex, self->texs, self->texs); 333 append (self->tex, self->texs, self->texs);
330 self->texs *= 2; 334 self->texs *= 2;
331 } 335 }
332} 336}
333 337
334static maprow * 338static maprow *
335map_get_row (DC__Map self, int y) 339map_get_row (mapgrid *self, int y)
336{ 340{
337 if (0 > y) 341 if (0 > y)
338 { 342 {
339 int extend = - y + MAP_EXTEND_Y; 343 int extend = - y + MAP_EXTEND_Y;
340 Prepend (maprow, self->row, self->rows, extend); 344 prepend (self->row, self->rows, extend);
341 345
342 self->rows += extend; 346 self->rows += extend;
343 self->y += extend; 347 self->y += extend;
344 y += extend; 348 y += extend;
345 } 349 }
346 else if (y >= self->rows) 350 else if (y >= self->rows)
347 { 351 {
348 int extend = y - self->rows + MAP_EXTEND_Y; 352 int extend = y - self->rows + MAP_EXTEND_Y;
349 Append (maprow, self->row, self->rows, extend); 353 append (self->row, self->rows, extend);
350 self->rows += extend; 354 self->rows += extend;
351 } 355 }
352 356
353 return self->row + y; 357 return self->row + y;
354} 358}
364 } 368 }
365 369
366 if (row->c0 > x) 370 if (row->c0 > x)
367 { 371 {
368 int extend = row->c0 - x + MAP_EXTEND_X; 372 int extend = row->c0 - x + MAP_EXTEND_X;
369 Prepend (mapcell, row->col, row->c1 - row->c0, extend); 373 prepend (row->col, row->c1 - row->c0, extend);
370 row->c0 -= extend; 374 row->c0 -= extend;
371 } 375 }
372 else if (x >= row->c1) 376 else if (x >= row->c1)
373 { 377 {
374 int extend = x - row->c1 + MAP_EXTEND_X; 378 int extend = x - row->c1 + MAP_EXTEND_X;
375 Append (mapcell, row->col, row->c1 - row->c0, extend); 379 append (row->col, row->c1 - row->c0, extend);
376 row->c1 += extend; 380 row->c1 += extend;
377 } 381 }
378 382
379 return row->col + (x - row->c0); 383 return row->col + (x - row->c0);
380} 384}
381 385
382static mapcell * 386static mapcell *
383map_get_cell (DC__Map self, int x, int y) 387map_get_cell (mapgrid *self, int x, int y)
384{ 388{
385 return row_get_cell (map_get_row (self, y), x); 389 return row_get_cell (map_get_row (self, y), x);
386} 390}
387 391
388static void 392static void
389map_clear (DC__Map self) 393map_clear (mapgrid *self)
390{ 394{
391 int r; 395 int r;
392 396
393 for (r = 0; r < self->rows; r++) 397 for (r = 0; r < self->rows; r++)
394 Safefree (self->row[r].col); 398 Safefree (self->row[r].col);
412 (cell)->flags = 0; \ 416 (cell)->flags = 0; \
413 (cell)->player = 0; \ 417 (cell)->player = 0; \
414 } while (0) 418 } while (0)
415 419
416static void 420static void
417map_blank (DC__Map self, int x0, int y0, int w, int h) 421map_blank (mapgrid *self, int x0, int y0, int w, int h)
418{ 422{
419 int x, y; 423 int x, y;
420 maprow *row; 424 maprow *row;
421 mapcell *cell; 425 mapcell *cell;
422 426
439 CELL_CLEAR (cell); 443 CELL_CLEAR (cell);
440 } 444 }
441 } 445 }
442} 446}
443 447
444typedef struct { 448struct smooth_key
449{
445 tileid tile; 450 tileid tile;
446 uint8_t x, y, level; 451 uint8_t x, y, level;
447} smooth_key; 452
453 bool operator == (const smooth_key &o) const
454 {
455 return tile == o.tile && x == o.x && y == o.y && level == o.level;
456 }
457};
458
459typedef ska::flat_hash_map<smooth_key, IV> smooth_hash;
460
461namespace std {
462 template <>
463 struct hash<smooth_key>
464 {
465 size_t operator () (const smooth_key &v) const
466 {
467 return v.tile + (v.x << 8) + (v.y << 16) + (v.level << 24);
468 }
469 };
470}
448 471
449static void 472static void
450smooth_or_bits (HV *hv, smooth_key *key, IV bits) 473smooth_or_bits (smooth_hash &h, smooth_key &key, IV bits)
451{ 474{
452 SV **sv = hv_fetch (hv, (char *)key, sizeof (*key), 1); 475 auto &&it = h.find (key);
453 476
454 if (SvIOK (*sv)) 477 if (it == h.end ())
455 SvIV_set (*sv, SvIVX (*sv) | bits); 478 h.insert (std::make_pair (key, bits));
456 else 479 else
457 sv_setiv (*sv, bits); 480 it->second |= bits;
458} 481}
459 482
460static void 483static void
461music_finished (void) 484music_finished (void)
462{ 485{
503 n |= n >> 4; 526 n |= n >> 4;
504 n |= n >> 8; 527 n |= n >> 8;
505 n |= n >> 16; 528 n |= n >> 16;
506 529
507 return n + 1; 530 return n + 1;
508}
509
510static unsigned int
511popcount (unsigned int n)
512{
513 n -= (n >> 1) & 0x55555555U;
514 n = ((n >> 2) & 0x33333333U) + (n & 0x33333333U);
515 n = ((n >> 4) + n) & 0x0f0f0f0fU;
516 n *= 0x01010101U;
517
518 return n >> 24;
519} 531}
520 532
521/* SDL should provide this, really. */ 533/* SDL should provide this, really. */
522#define SDLK_MODIFIER_MIN 300 534#define SDLK_MODIFIER_MIN 300
523#define SDLK_MODIFIER_MAX 314 535#define SDLK_MODIFIER_MAX 314
854 866
855NV ceil (NV x) 867NV ceil (NV x)
856 868
857IV minpot (UV n) 869IV minpot (UV n)
858 870
871UV ld32 (UV n)
872 CODE:
873 RETVAL = ecb_ld32 (n);
874 OUTPUT:
875 RETVAL
876
859IV popcount (UV n) 877IV popcount (UV n)
878 CODE:
879 RETVAL = ecb_popcount32 (n);
880 OUTPUT:
881 RETVAL
860 882
861NV distance (NV dx, NV dy) 883NV distance (NV dx, NV dy)
862 CODE: 884 CODE:
863 RETVAL = pow (dx * dx + dy * dy, 0.5); 885 RETVAL = pow (dx * dx + dy * dy, 0.5);
864 OUTPUT: 886 OUTPUT:
1338 tc_restore (); 1360 tc_restore ();
1339 1361
1340DC::Layout 1362DC::Layout
1341new (SV *klass) 1363new (SV *klass)
1342 CODE: 1364 CODE:
1343 New (0, RETVAL, 1, struct cf_layout); 1365 RETVAL = new cf_layout;
1344 1366
1345 RETVAL->pl = pango_layout_new (opengl_context); 1367 RETVAL->pl = pango_layout_new (opengl_context);
1346 RETVAL->r = 1.; 1368 RETVAL->r = 1.;
1347 RETVAL->g = 1.; 1369 RETVAL->g = 1.;
1348 RETVAL->b = 1.; 1370 RETVAL->b = 1.;
1349 RETVAL->a = 1.; 1371 RETVAL->a = 1.;
1350 RETVAL->base_height = MIN_FONT_HEIGHT; 1372 RETVAL->base_height = MIN_FONT_HEIGHT;
1351 RETVAL->font = 0; 1373 RETVAL->font = 0;
1352 RETVAL->rc = rc_alloc ();
1353 1374
1354 pango_layout_set_wrap (RETVAL->pl, PANGO_WRAP_WORD_CHAR); 1375 pango_layout_set_wrap (RETVAL->pl, PANGO_WRAP_WORD_CHAR);
1355 layout_update_font (RETVAL); 1376 layout_update_font (RETVAL);
1356 OUTPUT: 1377 OUTPUT:
1357 RETVAL 1378 RETVAL
1358 1379
1359void 1380void
1360DESTROY (DC::Layout self) 1381DESTROY (DC::Layout self)
1361 CODE: 1382 CODE:
1362 g_object_unref (self->pl); 1383 g_object_unref (self->pl);
1363 rc_free (self->rc);
1364 Safefree (self); 1384 delete self;
1365 1385
1366void 1386void
1367set_text (DC::Layout self, SV *text_) 1387set_text (DC::Layout self, SV *text_)
1368 CODE: 1388 CODE:
1369{ 1389{
1623} 1643}
1624 1644
1625void 1645void
1626render (DC::Layout self, float x, float y, int flags = 0) 1646render (DC::Layout self, float x, float y, int flags = 0)
1627 CODE: 1647 CODE:
1628 rc_clear (self->rc); 1648 self->rc.clear ();
1629 pango_opengl_render_layout_subpixel ( 1649 pango_opengl_render_layout_subpixel (
1630 self->pl, 1650 self->pl,
1631 self->rc, 1651 &self->rc,
1632 x * PANGO_SCALE, y * PANGO_SCALE, 1652 x * PANGO_SCALE, y * PANGO_SCALE,
1633 self->r, self->g, self->b, self->a, 1653 self->r, self->g, self->b, self->a,
1634 flags 1654 flags
1635 ); 1655 );
1636 // we assume that context_change actually clears/frees stuff 1656 // we assume that context_change actually clears/frees stuff
1647 gl_BlendFuncSeparate (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, 1667 gl_BlendFuncSeparate (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA,
1648 GL_ONE , GL_ONE_MINUS_SRC_ALPHA); 1668 GL_ONE , GL_ONE_MINUS_SRC_ALPHA);
1649 glEnable (GL_ALPHA_TEST); 1669 glEnable (GL_ALPHA_TEST);
1650 glAlphaFunc (GL_GREATER, 7.f / 255.f); 1670 glAlphaFunc (GL_GREATER, 7.f / 255.f);
1651 1671
1652 rc_draw (self->rc); 1672 self->rc.draw ();
1653 1673
1654 glDisable (GL_ALPHA_TEST); 1674 glDisable (GL_ALPHA_TEST);
1655 glDisable (GL_BLEND); 1675 glDisable (GL_BLEND);
1656 glDisable (GL_TEXTURE_2D); 1676 glDisable (GL_TEXTURE_2D);
1657} 1677}
1871PROTOTYPES: DISABLE 1891PROTOTYPES: DISABLE
1872 1892
1873DC::Map 1893DC::Map
1874new (SV *klass) 1894new (SV *klass)
1875 CODE: 1895 CODE:
1876 New (0, RETVAL, 1, struct map); 1896 New (0, RETVAL, 1, mapgrid);
1877 RETVAL->x = 0; 1897 RETVAL->x = 0;
1878 RETVAL->y = 0; 1898 RETVAL->y = 0;
1879 RETVAL->w = 0; 1899 RETVAL->w = 0;
1880 RETVAL->h = 0; 1900 RETVAL->h = 0;
1881 RETVAL->ox = 0; 1901 RETVAL->ox = 0;
2027 self->ox += dx; self->x += dx; 2047 self->ox += dx; self->x += dx;
2028 self->oy += dy; self->y += dy; 2048 self->oy += dy; self->y += dy;
2029 2049
2030 while (self->y < 0) 2050 while (self->y < 0)
2031 { 2051 {
2032 Prepend (maprow, self->row, self->rows, MAP_EXTEND_Y); 2052 prepend (self->row, self->rows, MAP_EXTEND_Y);
2033 2053
2034 self->rows += MAP_EXTEND_Y; 2054 self->rows += MAP_EXTEND_Y;
2035 self->y += MAP_EXTEND_Y; 2055 self->y += MAP_EXTEND_Y;
2036 } 2056 }
2037} 2057}
2193draw (DC::Map self, int mx, int my, int sw, int sh, int Tw, int Th, U32 player = 0xffffffff, int sdx = 0, int sdy = 0) 2213draw (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: 2214 CODE:
2195{ 2215{
2196 int x, y, z; 2216 int x, y, z;
2197 2217
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) 2218 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; 2219 int pl_x, pl_y;
2203 maptex pl_tex; 2220 maptex pl_tex;
2204 rc_t *rc = rc_alloc (); 2221 rc_t rc;
2205 rc_t *rc_ov = rc_alloc (); 2222 rc_t rc_ov;
2206 rc_key_t key; 2223 rc_key_t key;
2207 rc_array_t *arr; 2224 rc_t::array_t *arr;
2208 2225
2209 pl_tex.name = 0; 2226 pl_tex.name = 0;
2210 2227
2211 // that's current max. sorry. 2228 // that's current max. sorry.
2212 if (sw > 255) sw = 255; 2229 if (sw > 255) sw = 255;
2213 if (sh > 255) sh = 255; 2230 if (sh > 255) sh = 255;
2214
2215 // clear key, in case of extra padding
2216 memset (&skey, 0, sizeof (skey));
2217 2231
2218 memset (&key, 0, sizeof (key)); 2232 memset (&key, 0, sizeof (key));
2219 key.r = 255; 2233 key.r = 255;
2220 key.g = 255; 2234 key.g = 255;
2221 key.b = 255; 2235 key.b = 255;
2227 my += self->y; 2241 my += self->y;
2228 2242
2229 // first pass: determine smooth_max 2243 // first pass: determine smooth_max
2230 // rather ugly, if you ask me 2244 // rather ugly, if you ask me
2231 // could also be stored inside mapcell and updated on change 2245 // could also be stored inside mapcell and updated on change
2232 memset (smooth_max, 0, sizeof (smooth_max)); 2246 memset (smooth_max, 0, sizeof (smooth_max[0]) * (sh + 1));
2233 2247
2234 for (y = 0; y < sh; y++) 2248 for (y = 0; y < sh; y++)
2235 if (0 <= y + my && y + my < self->rows) 2249 if (0 <= y + my && y + my < self->rows)
2236 { 2250 {
2237 maprow *row = self->row + (y + my); 2251 maprow *row = self->row + (y + my);
2252 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 2266 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
2253 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); 2267 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
2254 2268
2255 for (z = 0; z <= 2; z++) 2269 for (z = 0; z <= 2; z++)
2256 { 2270 {
2257 memset (smooth_level, 0, sizeof (smooth_level)); 2271 std::bitset<256> smooth_level; // one bit for every possible smooth level
2272 smooth_key skey;
2273 smooth_hash smooth;
2258 key.texname = -1; 2274 key.texname = -1;
2259 2275
2260 for (y = 0; y < sh; y++) 2276 for (y = 0; y < sh; y++)
2261 if (0 <= y + my && y + my < self->rows) 2277 if (0 <= y + my && y + my < self->rows)
2262 { 2278 {
2279 2295
2280 if (!tex.name) 2296 if (!tex.name)
2281 tex = self->tex [TEXID_NOFACE]; /* missing, replace by noface */ 2297 tex = self->tex [TEXID_NOFACE]; /* missing, replace by noface */
2282 2298
2283 key.texname = tex.name; 2299 key.texname = tex.name;
2284 arr = rc_array (rc, &key); 2300 arr = &rc.array (key);
2285 } 2301 }
2286 2302
2287 px = (x + 1) * Th - tex.w; 2303 px = (x + 1) * Th - tex.w;
2288 py = (y + 1) * Tw - tex.h; 2304 py = (y + 1) * Tw - tex.h;
2289 2305
2290 if (expect_false (cell->player == player) && expect_false (z == 2)) 2306 if (ecb_expect_false (cell->player == player) && ecb_expect_false (z == 2))
2291 { 2307 {
2292 pl_x = px; 2308 pl_x = px;
2293 pl_y = py; 2309 pl_y = py;
2294 pl_tex = tex; 2310 pl_tex = tex;
2295 continue; 2311 continue;
2296 } 2312 }
2297 2313
2298 rc_t2f_v3f (arr, 0 , 0 , px , py , 0); 2314 arr->t2f_v3f (0 , 0 , px , py , 0);
2299 rc_t2f_v3f (arr, 0 , tex.t, px , py + tex.h, 0); 2315 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); 2316 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); 2317 arr->t2f_v3f (tex.s, 0 , px + tex.w, py , 0);
2302 2318
2303 // update smooth hash 2319 // update smooth hash
2304 if (tex.smoothtile) 2320 if (tex.smoothtile)
2305 { 2321 {
2306 skey.tile = tex.smoothtile; 2322 skey.tile = tex.smoothtile;
2307 skey.level = tex.smoothlevel; 2323 skey.level = tex.smoothlevel;
2308 2324
2309 smooth_level [tex.smoothlevel >> 5] |= ((uint32_t)1) << (tex.smoothlevel & 31); 2325 smooth_level[tex.smoothlevel] = 1;
2310 2326
2311 // add bits to current tile and all neighbours. skey.x|y is 2327 // add bits to current tile and all neighbours. skey.x|y is
2312 // shifted +1|+1 so we always stay positive. 2328 // shifted +1|+1 so we always stay positive.
2313 2329
2314 // bits is ___n cccc CCCC bbbb 2330 // bits is ___n cccc CCCC bbbb
2322 2338
2323 // corners: 1 ┛· 2 ·┗ 4 ·· 8 ·· 2339 // corners: 1 ┛· 2 ·┗ 4 ·· 8 ··
2324 // ·· ·· ·┏ ┓· 2340 // ·· ·· ·┏ ┓·
2325 2341
2326 // full tile 2342 // full tile
2327 skey.x = x + 1; skey.y = y + 1; smooth_or_bits (smooth, &skey, 0x1000); 2343 skey.x = x + 1; skey.y = y + 1; smooth_or_bits (smooth, skey, 0x1000);
2328 2344
2329 // borders 2345 // borders
2330 skey.x = x + 2; skey.y = y + 1; smooth_or_bits (smooth, &skey, 0x0091); 2346 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); 2347 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); 2348 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); 2349 skey.x = x + 1; skey.y = y ; smooth_or_bits (smooth, skey, 0x00c8);
2334 2350
2335 // corners 2351 // corners
2336 skey.x = x + 2; skey.y = y + 2; smooth_or_bits (smooth, &skey, 0x0100); 2352 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); 2353 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); 2354 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); 2355 skey.x = x + 2; skey.y = y ; smooth_or_bits (smooth, skey, 0x0800);
2340 } 2356 }
2341 } 2357 }
2342 2358
2343 if (expect_false (z == 2) && expect_false (cell->flags)) 2359 if (ecb_expect_false (z == 2) && ecb_expect_false (cell->flags))
2344 { 2360 {
2345 // overlays such as the speech bubble, probably more to come 2361 // overlays such as the speech bubble, probably more to come
2346 if (cell->flags & 1) 2362 if (cell->flags & 1)
2347 { 2363 {
2348 rc_key_t key_ov = key; 2364 rc_key_t key_ov = key;
2349 maptex tex = self->tex [TEXID_SPEECH]; 2365 maptex tex = self->tex[TEXID_SPEECH];
2350 rc_array_t *arr;
2351 int px = x * Tw + Tw * 2 / 32; 2366 int px = x * Tw + Tw * 2 / 32;
2352 int py = y * Th - Th * 6 / 32; 2367 int py = y * Th - Th * 6 / 32;
2353 2368
2354 key_ov.texname = tex.name; 2369 key_ov.texname = tex.name;
2355 arr = rc_array (rc_ov, &key_ov); 2370 rc_t::array_t &arr = rc_ov.array (key_ov);
2356 2371
2357 rc_t2f_v3f (arr, 0 , 0 , px , py , 0); 2372 arr.t2f_v3f (0 , 0 , px , py , 0);
2358 rc_t2f_v3f (arr, 0 , tex.t, px , py + Th, 0); 2373 arr.t2f_v3f (0 , tex.t, px , py + Th, 0);
2359 rc_t2f_v3f (arr, tex.s, tex.t, px + Tw, py + Th, 0); 2374 arr.t2f_v3f (tex.s, tex.t, px + Tw, py + Th, 0);
2360 rc_t2f_v3f (arr, tex.s, 0 , px + Tw, py , 0); 2375 arr.t2f_v3f (tex.s, 0 , px + Tw, py , 0);
2361 } 2376 }
2362 } 2377 }
2363 } 2378 }
2364 } 2379 }
2365 2380
2366 rc_draw (rc); 2381 rc.draw ();
2367 rc_clear (rc); 2382 rc.clear ();
2368 2383
2369 // go through all smoothlevels, lowest to highest, then draw. 2384 // go through all smoothlevels, lowest to highest, then draw.
2370 // this is basically counting sort 2385 // this is basically counting sort
2371 { 2386 {
2372 int w, b; 2387 int w, b;
2373 2388
2374 glEnable (GL_TEXTURE_2D); 2389 glEnable (GL_TEXTURE_2D);
2375 glBegin (GL_QUADS); 2390 glBegin (GL_QUADS);
2376 for (w = 0; w < 256 / 32; ++w) 2391 for (int level = 0; level < smooth_level.size (); ++level)
2392 if (smooth_level[level])
2393 for (auto &&it = smooth.begin (); it != smooth.end (); ++it)
2377 { 2394 {
2378 uint32_t smask = smooth_level [w]; 2395 smooth_key &skey = it->first;
2379 if (smask) 2396 IV bits = it->second;
2380 for (b = 0; b < 32; ++b) 2397
2381 if (smask & (((uint32_t)1) << b)) 2398 if (!(bits & 0x1000)
2399 && skey.level == level
2400 && level > smooth_max [skey.x][skey.y])
2382 { 2401 {
2383 int level = (w << 5) | b; 2402 maptex tex = self->tex [skey.tile];
2403 int px = (((int)skey.x) - 1) * Tw;
2404 int py = (((int)skey.y) - 1) * Th;
2405 int border = bits & 15;
2406 int corner = (bits >> 8) & ~(bits >> 4) & 15;
2407 float dx = tex.s * .0625f; // 16 images/row
2408 float dy = tex.t * .5f ; // 2 images/column
2409
2384 HE *he; 2410 if (tex.name)
2385
2386 hv_iterinit (smooth);
2387 while ((he = hv_iternext (smooth)))
2388 { 2411 {
2389 smooth_key *skey = (smooth_key *)HeKEY (he); 2412 // this time avoiding texture state changes
2390 IV bits = SvIVX (HeVAL (he)); 2413 // save gobs of state changes.
2391 2414 if (key.texname != tex.name)
2392 if (!(bits & 0x1000)
2393 && skey->level == level
2394 && level > smooth_max [skey->x][skey->y])
2395 { 2415 {
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; 2416 self->tex [skey.tile].unused = 0;
2411 2417
2412 glEnd (); 2418 glEnd ();
2413 glBindTexture (GL_TEXTURE_2D, key.texname = tex.name); 2419 glBindTexture (GL_TEXTURE_2D, key.texname = tex.name);
2414 glBegin (GL_QUADS); 2420 glBegin (GL_QUADS);
2415 } 2421 }
2416 2422
2417 if (border) 2423 if (border)
2418 { 2424 {
2419 float ox = border * dx; 2425 float ox = border * dx;
2420 2426
2421 glTexCoord2f (ox , 0.f ); glVertex2i (px , py ); 2427 glTexCoord2f (ox , 0.f ); glVertex2i (px , py );
2422 glTexCoord2f (ox , dy ); glVertex2i (px , py + Th); 2428 glTexCoord2f (ox , dy ); glVertex2i (px , py + Th);
2423 glTexCoord2f (ox + dx, dy ); glVertex2i (px + Tw, py + Th); 2429 glTexCoord2f (ox + dx, dy ); glVertex2i (px + Tw, py + Th);
2424 glTexCoord2f (ox + dx, 0.f ); glVertex2i (px + Tw, py ); 2430 glTexCoord2f (ox + dx, 0.f ); glVertex2i (px + Tw, py );
2425 } 2431 }
2426 2432
2427 if (corner) 2433 if (corner)
2428 { 2434 {
2429 float ox = corner * dx; 2435 float ox = corner * dx;
2430 2436
2431 glTexCoord2f (ox , dy ); glVertex2i (px , py ); 2437 glTexCoord2f (ox , dy ); glVertex2i (px , py );
2432 glTexCoord2f (ox , dy * 2.f); glVertex2i (px , py + Th); 2438 glTexCoord2f (ox , dy * 2.f); glVertex2i (px , py + Th);
2433 glTexCoord2f (ox + dx, dy * 2.f); glVertex2i (px + Tw, py + Th); 2439 glTexCoord2f (ox + dx, dy * 2.f); glVertex2i (px + Tw, py + Th);
2434 glTexCoord2f (ox + dx, dy ); glVertex2i (px + Tw, py ); 2440 glTexCoord2f (ox + dx, dy ); glVertex2i (px + Tw, py );
2435 }
2436 }
2437 } 2441 }
2438 } 2442 }
2439 } 2443 }
2440 } 2444 }
2441 2445
2442 glEnd (); 2446 glEnd ();
2443 glDisable (GL_TEXTURE_2D); 2447 glDisable (GL_TEXTURE_2D);
2444 key.texname = -1; 2448 key.texname = -1;
2445 } 2449 }
2446
2447 hv_clear (smooth);
2448 } 2450 }
2449 2451
2450 if (pl_tex.name) 2452 if (pl_tex.name)
2451 { 2453 {
2452 maptex tex = pl_tex; 2454 maptex tex = pl_tex;
2453 int px = pl_x + sdx; 2455 int px = pl_x + sdx;
2454 int py = pl_y + sdy; 2456 int py = pl_y + sdy;
2455 2457
2456 key.texname = tex.name; 2458 key.texname = tex.name;
2457 arr = rc_array (rc, &key); 2459 rc_t::array_t &arr = rc.array (key);
2458 2460
2459 rc_t2f_v3f (arr, 0 , 0 , px , py , 0); 2461 arr.t2f_v3f (0 , 0 , px , py , 0);
2460 rc_t2f_v3f (arr, 0 , tex.t, px , py + tex.h, 0); 2462 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); 2463 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); 2464 arr.t2f_v3f (tex.s, 0 , px + tex.w, py , 0);
2463 2465
2464 rc_draw (rc); 2466 rc.draw ();
2465 } 2467 }
2466 2468
2467 rc_draw (rc_ov); 2469 rc_ov.draw ();
2468 rc_clear (rc_ov); 2470 rc_ov.clear ();
2469 2471
2470 glDisable (GL_BLEND); 2472 glDisable (GL_BLEND);
2471 rc_free (rc);
2472 rc_free (rc_ov);
2473 2473
2474 // top layer: overlays such as the health bar 2474 // top layer: overlays such as the health bar
2475 for (y = 0; y < sh; y++) 2475 for (y = 0; y < sh; y++)
2476 if (0 <= y + my && y + my < self->rows) 2476 if (0 <= y + my && y + my < self->rows)
2477 { 2477 {
2483 mapcell *cell = row->col + (x + mx - row->c0); 2483 mapcell *cell = row->col + (x + mx - row->c0);
2484 2484
2485 int px = x * Tw; 2485 int px = x * Tw;
2486 int py = y * Th; 2486 int py = y * Th;
2487 2487
2488 if (expect_false (cell->player == player)) 2488 if (ecb_expect_false (cell->player == player))
2489 { 2489 {
2490 px += sdx; 2490 px += sdx;
2491 py += sdy; 2491 py += sdy;
2492 } 2492 }
2493 2493

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines