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.319 by root, Sun Nov 18 00:52:22 2018 UTC vs.
Revision 1.333 by root, Mon Nov 19 01:23:01 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)) 82#include "salloc.h"
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 83
85#define OBJ_STR "\xef\xbf\xbc" /* U+FFFC, object replacement character */ 84#define OBJ_STR "\xef\xbf\xbc" /* U+FFFC, object replacement character */
86 85
87/* this is used as fow flag as well, so has to have a different value */ 86/* this is used as fow flag as well, so has to have a different value */
88/* then anything that is computed by incoming darkness */ 87/* then anything that is computed by incoming darkness */
106fast_sv_grow (SV *sv, STRLEN need) 105fast_sv_grow (SV *sv, STRLEN need)
107{ 106{
108 STRLEN len = SvLEN (sv); 107 STRLEN len = SvLEN (sv);
109 STRLEN want = SvCUR (sv) + need; 108 STRLEN want = SvCUR (sv) + need;
110 109
111 if (expect_false (len < want)) 110 if (ecb_expect_false (len < want))
112 { 111 {
113 do 112 do
114 len *= 2; 113 len *= 2;
115 while (len < want); 114 while (len < want);
116 115
195 } 194 }
196 195
197 return 0; 196 return 0;
198} 197}
199 198
200typedef struct cf_layout { 199struct cf_layout {
201 PangoLayout *pl; 200 PangoLayout *pl;
202 float r, g, b, a; // default color for rgba mode 201 float r, g, b, a; // default color for rgba mode
203 int base_height; 202 int base_height;
204 DC__Font font; 203 DC__Font font;
205 rc_t *rc; 204 rc_t rc;
206} *DC__Layout; 205};
206
207typedef cf_layout *DC__Layout;
207 208
208static DC__Font default_font; 209static DC__Font default_font;
209static PangoContext *opengl_context; 210static PangoContext *opengl_context;
210static PangoFontMap *opengl_fontmap; 211static PangoFontMap *opengl_fontmap;
211 212
212static void 213static void
213substitute_func (FcPattern *pattern, gpointer data) 214substitute_func (FcPattern *pattern, gpointer data)
214{ 215{
215 FcPatternAddBool (pattern, FC_HINTING, 1); 216 FcPatternAddBool (pattern, FC_HINTING, 1);
216#ifdef FC_HINT_STYLE 217#ifdef FC_HINT_STYLE
217 FcPatternAddBool (pattern, FC_HINT_STYLE, FC_HINT_FULL); 218 FcPatternAddInteger (pattern, FC_HINT_STYLE, FC_HINT_FULL);
218#endif 219#endif
219 FcPatternAddBool (pattern, FC_AUTOHINT, 0); 220 FcPatternAddBool (pattern, FC_AUTOHINT, 0);
220} 221}
221 222
222static void 223static void
249 250
250 *w = rect.width; 251 *w = rect.width;
251 *h = rect.height; 252 *h = rect.height;
252} 253}
253 254
255/////////////////////////////////////////////////////////////////////////////
256
254typedef uint16_t tileid; 257typedef uint16_t tileid;
255typedef uint16_t faceid; 258typedef uint16_t faceid;
256 259
257typedef struct { 260struct maptex
261{
258 GLuint name; 262 GLuint name;
259 int w, h; 263 int w, h;
260 float s, t; 264 float s, t;
261 uint8_t r, g, b, a; 265 uint8_t r, g, b, a;
262 tileid smoothtile; 266 tileid smoothtile;
263 uint8_t smoothlevel; 267 uint8_t smoothlevel;
264 uint8_t unused; /* set to zero on use */ 268 uint8_t unused; /* set to zero on use */
265} maptex; 269};
266 270
267typedef struct { 271struct mapcell
272{
268 uint32_t player; 273 uint32_t player;
269 tileid tile[3]; 274 tileid tile[3];
270 uint16_t darkness; 275 uint16_t darkness;
271 uint8_t stat_width, stat_hp, flags, smoothmax; 276 uint8_t stat_width, stat_hp, flags, smoothmax;
272} mapcell; 277};
273 278
274typedef struct { 279struct maprow
280{
275 int32_t c0, c1; 281 int32_t c0, c1;
276 mapcell *col; 282 mapcell *col;
277} maprow; 283};
278 284
279typedef struct map { 285struct mapgrid {
280 int x, y, w, h; 286 int x, y, w, h;
281 int ox, oy; /* offset to virtual global coordinate system */ 287 int ox, oy; /* offset to virtual global coordinate system */
282 int faces; tileid *face2tile; // [faceid] 288 int faces; tileid *face2tile; // [faceid]
283 int texs; maptex *tex; // [tileid] 289 int texs; maptex *tex; // [tileid]
284 290
285 int32_t rows; 291 int32_t rows;
286 maprow *row; 292 maprow *row;
287} *DC__Map; 293};
288 294
289static char * 295typedef mapgrid *DC__Map;
296
297template<typename T>
298static void
290prepend (char *ptr, int sze, int inc) 299prepend (T *&ptr, int sze, int inc)
291{ 300{
292 char *p; 301 T *p;
293 302
294 New (0, p, sze + inc, char); 303 Newx (p, inc + sze, T);
295 Zero (p, inc, char); 304 Zero (p, inc, T);
296 Move (ptr, p + inc, sze, char); 305 Move (ptr, p + inc, sze, T);
297 Safefree (ptr); 306 Safefree (ptr);
298 307
299 return p; 308 ptr = p;
300} 309}
301 310
302static char * 311template<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 312static void
313append (T *&ptr, int sze, int inc)
314{
315 Renew (ptr, sze + inc, T);
316 Zero (ptr + sze, inc, T);
317}
318
319static void
315need_facenum (struct map *self, faceid face) 320need_facenum (struct mapgrid *self, faceid face)
316{ 321{
317 while (self->faces <= face) 322 while (self->faces <= face)
318 { 323 {
319 Append (tileid, self->face2tile, self->faces, self->faces); 324 append (self->face2tile, self->faces, self->faces);
320 self->faces *= 2; 325 self->faces *= 2;
321 } 326 }
322} 327}
323 328
324static void 329static void
325need_texid (struct map *self, int texid) 330need_texid (struct mapgrid *self, int texid)
326{ 331{
327 while (self->texs <= texid) 332 while (self->texs <= texid)
328 { 333 {
329 Append (maptex, self->tex, self->texs, self->texs); 334 append (self->tex, self->texs, self->texs);
330 self->texs *= 2; 335 self->texs *= 2;
331 } 336 }
332} 337}
333 338
334static maprow * 339static maprow *
335map_get_row (DC__Map self, int y) 340map_get_row (mapgrid *self, int y)
336{ 341{
337 if (0 > y) 342 if (0 > y)
338 { 343 {
339 int extend = - y + MAP_EXTEND_Y; 344 int extend = - y + MAP_EXTEND_Y;
340 Prepend (maprow, self->row, self->rows, extend); 345 prepend (self->row, self->rows, extend);
341 346
342 self->rows += extend; 347 self->rows += extend;
343 self->y += extend; 348 self->y += extend;
344 y += extend; 349 y += extend;
345 } 350 }
346 else if (y >= self->rows) 351 else if (y >= self->rows)
347 { 352 {
348 int extend = y - self->rows + MAP_EXTEND_Y; 353 int extend = y - self->rows + MAP_EXTEND_Y;
349 Append (maprow, self->row, self->rows, extend); 354 append (self->row, self->rows, extend);
350 self->rows += extend; 355 self->rows += extend;
351 } 356 }
352 357
353 return self->row + y; 358 return self->row + y;
354} 359}
364 } 369 }
365 370
366 if (row->c0 > x) 371 if (row->c0 > x)
367 { 372 {
368 int extend = row->c0 - x + MAP_EXTEND_X; 373 int extend = row->c0 - x + MAP_EXTEND_X;
369 Prepend (mapcell, row->col, row->c1 - row->c0, extend); 374 prepend (row->col, row->c1 - row->c0, extend);
370 row->c0 -= extend; 375 row->c0 -= extend;
371 } 376 }
372 else if (x >= row->c1) 377 else if (x >= row->c1)
373 { 378 {
374 int extend = x - row->c1 + MAP_EXTEND_X; 379 int extend = x - row->c1 + MAP_EXTEND_X;
375 Append (mapcell, row->col, row->c1 - row->c0, extend); 380 append (row->col, row->c1 - row->c0, extend);
376 row->c1 += extend; 381 row->c1 += extend;
377 } 382 }
378 383
379 return row->col + (x - row->c0); 384 return row->col + (x - row->c0);
380} 385}
381 386
382static mapcell * 387static mapcell *
383map_get_cell (DC__Map self, int x, int y) 388map_get_cell (mapgrid *self, int x, int y)
384{ 389{
385 return row_get_cell (map_get_row (self, y), x); 390 return row_get_cell (map_get_row (self, y), x);
386} 391}
387 392
388static void 393static void
389map_clear (DC__Map self) 394map_clear (mapgrid *self)
390{ 395{
391 int r; 396 int r;
392 397
393 for (r = 0; r < self->rows; r++) 398 for (r = 0; r < self->rows; r++)
394 Safefree (self->row[r].col); 399 Safefree (self->row[r].col);
412 (cell)->flags = 0; \ 417 (cell)->flags = 0; \
413 (cell)->player = 0; \ 418 (cell)->player = 0; \
414 } while (0) 419 } while (0)
415 420
416static void 421static void
417map_blank (DC__Map self, int x0, int y0, int w, int h) 422map_blank (mapgrid *self, int x0, int y0, int w, int h)
418{ 423{
419 int x, y; 424 int x, y;
420 maprow *row; 425 maprow *row;
421 mapcell *cell; 426 mapcell *cell;
422 427
439 CELL_CLEAR (cell); 444 CELL_CLEAR (cell);
440 } 445 }
441 } 446 }
442} 447}
443 448
444typedef struct { 449struct smooth_key
450{
445 tileid tile; 451 tileid tile;
446 uint8_t x, y, level; 452 uint8_t x, y, level;
447} smooth_key; 453
454 bool operator == (const smooth_key &o) const
455 {
456 return tile == o.tile && x == o.x && y == o.y && level == o.level;
457 }
458};
459
460typedef ska::flat_hash_map<smooth_key, IV> smooth_hash;
461
462namespace std {
463 template <>
464 struct hash<smooth_key>
465 {
466 size_t operator () (const smooth_key &v) const
467 {
468 return v.tile + (v.x << 8) + (v.y << 16) + (v.level << 24);
469 }
470 };
471}
448 472
449static void 473static void
450smooth_or_bits (HV *hv, smooth_key *key, IV bits) 474smooth_or_bits (smooth_hash &h, smooth_key &key, IV bits)
451{ 475{
452 SV **sv = hv_fetch (hv, (char *)key, sizeof (*key), 1); 476 auto &&it = h.find (key);
453 477
454 if (SvIOK (*sv)) 478 if (it == h.end ())
455 SvIV_set (*sv, SvIVX (*sv) | bits); 479 h.insert (std::make_pair (key, bits));
456 else 480 else
457 sv_setiv (*sv, bits); 481 it->second |= bits;
458} 482}
459 483
460static void 484static void
461music_finished (void) 485music_finished (void)
462{ 486{
481 ev.data2 = 0; 505 ev.data2 = 0;
482 506
483 SDL_PushEvent ((SDL_Event *)&ev); 507 SDL_PushEvent ((SDL_Event *)&ev);
484} 508}
485 509
510// approximately divide by 255
486static unsigned int 511static unsigned int
487div255 (unsigned int n) 512div255 (unsigned int n)
488{ 513{
489 return (n + (n >> 8)) >> 8; 514 return (n + (n >> 8)) >> 8;
490} 515}
502 n |= n >> 4; 527 n |= n >> 4;
503 n |= n >> 8; 528 n |= n >> 8;
504 n |= n >> 16; 529 n |= n >> 16;
505 530
506 return n + 1; 531 return n + 1;
507}
508
509static unsigned int
510popcount (unsigned int n)
511{
512 n -= (n >> 1) & 0x55555555U;
513 n = ((n >> 2) & 0x33333333U) + (n & 0x33333333U);
514 n = ((n >> 4) + n) & 0x0f0f0f0fU;
515 n *= 0x01010101U;
516
517 return n >> 24;
518} 532}
519 533
520/* SDL should provide this, really. */ 534/* SDL should provide this, really. */
521#define SDLK_MODIFIER_MIN 300 535#define SDLK_MODIFIER_MIN 300
522#define SDLK_MODIFIER_MAX 314 536#define SDLK_MODIFIER_MAX 314
853 867
854NV ceil (NV x) 868NV ceil (NV x)
855 869
856IV minpot (UV n) 870IV minpot (UV n)
857 871
872UV ld32 (UV n)
873 CODE:
874 RETVAL = ecb_ld32 (n);
875 OUTPUT:
876 RETVAL
877
858IV popcount (UV n) 878IV popcount (UV n)
879 CODE:
880 RETVAL = ecb_popcount32 (n);
881 OUTPUT:
882 RETVAL
859 883
860NV distance (NV dx, NV dy) 884NV distance (NV dx, NV dy)
861 CODE: 885 CODE:
862 RETVAL = pow (dx * dx + dy * dy, 0.5); 886 RETVAL = pow (dx * dx + dy * dy, 0.5);
863 OUTPUT: 887 OUTPUT:
1083 OUTPUT: 1107 OUTPUT:
1084 RETVAL 1108 RETVAL
1085 1109
1086int 1110int
1087Mix_OpenAudio (int frequency = 44100, int format = MIX_DEFAULT_FORMAT, int channels = 2, int chunksize = 4096) 1111Mix_OpenAudio (int frequency = 44100, int format = MIX_DEFAULT_FORMAT, int channels = 2, int chunksize = 4096)
1088 POSTCALL: 1112 POSTCALL:
1089 Mix_HookMusicFinished (music_finished); 1113 Mix_HookMusicFinished (music_finished);
1090 Mix_ChannelFinished (channel_finished); 1114 Mix_ChannelFinished (channel_finished);
1091 1115
1092void 1116void
1093Mix_QuerySpec () 1117Mix_QuerySpec ()
1337 tc_restore (); 1361 tc_restore ();
1338 1362
1339DC::Layout 1363DC::Layout
1340new (SV *klass) 1364new (SV *klass)
1341 CODE: 1365 CODE:
1342 New (0, RETVAL, 1, struct cf_layout); 1366 RETVAL = new cf_layout;
1343 1367
1344 RETVAL->pl = pango_layout_new (opengl_context); 1368 RETVAL->pl = pango_layout_new (opengl_context);
1345 RETVAL->r = 1.; 1369 RETVAL->r = 1.;
1346 RETVAL->g = 1.; 1370 RETVAL->g = 1.;
1347 RETVAL->b = 1.; 1371 RETVAL->b = 1.;
1348 RETVAL->a = 1.; 1372 RETVAL->a = 1.;
1349 RETVAL->base_height = MIN_FONT_HEIGHT; 1373 RETVAL->base_height = MIN_FONT_HEIGHT;
1350 RETVAL->font = 0; 1374 RETVAL->font = 0;
1351 RETVAL->rc = rc_alloc ();
1352 1375
1353 pango_layout_set_wrap (RETVAL->pl, PANGO_WRAP_WORD_CHAR); 1376 pango_layout_set_wrap (RETVAL->pl, PANGO_WRAP_WORD_CHAR);
1354 layout_update_font (RETVAL); 1377 layout_update_font (RETVAL);
1355 OUTPUT: 1378 OUTPUT:
1356 RETVAL 1379 RETVAL
1357 1380
1358void 1381void
1359DESTROY (DC::Layout self) 1382DESTROY (DC::Layout self)
1360 CODE: 1383 CODE:
1361 g_object_unref (self->pl); 1384 g_object_unref (self->pl);
1362 rc_free (self->rc);
1363 Safefree (self); 1385 delete self;
1364 1386
1365void 1387void
1366set_text (DC::Layout self, SV *text_) 1388set_text (DC::Layout self, SV *text_)
1367 CODE: 1389 CODE:
1368{ 1390{
1622} 1644}
1623 1645
1624void 1646void
1625render (DC::Layout self, float x, float y, int flags = 0) 1647render (DC::Layout self, float x, float y, int flags = 0)
1626 CODE: 1648 CODE:
1627 rc_clear (self->rc); 1649 self->rc.clear ();
1628 pango_opengl_render_layout_subpixel ( 1650 pango_opengl_render_layout_subpixel (
1629 self->pl, 1651 self->pl,
1630 self->rc, 1652 &self->rc,
1631 x * PANGO_SCALE, y * PANGO_SCALE, 1653 x * PANGO_SCALE, y * PANGO_SCALE,
1632 self->r, self->g, self->b, self->a, 1654 self->r, self->g, self->b, self->a,
1633 flags 1655 flags
1634 ); 1656 );
1635 // we assume that context_change actually clears/frees stuff 1657 // we assume that context_change actually clears/frees stuff
1646 gl_BlendFuncSeparate (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, 1668 gl_BlendFuncSeparate (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA,
1647 GL_ONE , GL_ONE_MINUS_SRC_ALPHA); 1669 GL_ONE , GL_ONE_MINUS_SRC_ALPHA);
1648 glEnable (GL_ALPHA_TEST); 1670 glEnable (GL_ALPHA_TEST);
1649 glAlphaFunc (GL_GREATER, 7.f / 255.f); 1671 glAlphaFunc (GL_GREATER, 7.f / 255.f);
1650 1672
1651 rc_draw (self->rc); 1673 self->rc.draw ();
1652 1674
1653 glDisable (GL_ALPHA_TEST); 1675 glDisable (GL_ALPHA_TEST);
1654 glDisable (GL_BLEND); 1676 glDisable (GL_BLEND);
1655 glDisable (GL_TEXTURE_2D); 1677 glDisable (GL_TEXTURE_2D);
1656} 1678}
1870PROTOTYPES: DISABLE 1892PROTOTYPES: DISABLE
1871 1893
1872DC::Map 1894DC::Map
1873new (SV *klass) 1895new (SV *klass)
1874 CODE: 1896 CODE:
1875 New (0, RETVAL, 1, struct map); 1897 New (0, RETVAL, 1, mapgrid);
1876 RETVAL->x = 0; 1898 RETVAL->x = 0;
1877 RETVAL->y = 0; 1899 RETVAL->y = 0;
1878 RETVAL->w = 0; 1900 RETVAL->w = 0;
1879 RETVAL->h = 0; 1901 RETVAL->h = 0;
1880 RETVAL->ox = 0; 1902 RETVAL->ox = 0;
1917 1939
1918void 1940void
1919set_smooth (DC::Map self, int face, int smooth, int level) 1941set_smooth (DC::Map self, int face, int smooth, int level)
1920 CODE: 1942 CODE:
1921{ 1943{
1922 tileid texid; 1944 tileid texid;
1923 maptex *tex; 1945 maptex *tex;
1924 1946
1925 if (face < 0 || face >= self->faces) 1947 if (face < 0 || face >= self->faces)
1926 return; 1948 return;
1927 1949
1928 if (smooth < 0 || smooth >= self->faces) 1950 if (smooth < 0 || smooth >= self->faces)
1929 return; 1951 return;
1930 1952
1931 texid = self->face2tile [face]; 1953 texid = self->face2tile [face];
1932 1954
1933 if (!texid) 1955 if (!texid)
1934 return; 1956 return;
1935 1957
1936 tex = self->tex + texid; 1958 tex = self->tex + texid;
1969} 1991}
1970 1992
1971void 1993void
1972expire_textures (DC::Map self, int texid, int count) 1994expire_textures (DC::Map self, int texid, int count)
1973 PPCODE: 1995 PPCODE:
1974 for (; texid < self->texs && count; ++texid, --count) 1996 for (; texid < self->texs && count; ++texid, --count)
1975 { 1997 {
1976 maptex *tex = self->tex + texid; 1998 maptex *tex = self->tex + texid;
1977 1999
1978 if (tex->name) 2000 if (tex->name)
1979 { 2001 {
2026 self->ox += dx; self->x += dx; 2048 self->ox += dx; self->x += dx;
2027 self->oy += dy; self->y += dy; 2049 self->oy += dy; self->y += dy;
2028 2050
2029 while (self->y < 0) 2051 while (self->y < 0)
2030 { 2052 {
2031 Prepend (maprow, self->row, self->rows, MAP_EXTEND_Y); 2053 prepend (self->row, self->rows, MAP_EXTEND_Y);
2032 2054
2033 self->rows += MAP_EXTEND_Y; 2055 self->rows += MAP_EXTEND_Y;
2034 self->y += MAP_EXTEND_Y; 2056 self->y += MAP_EXTEND_Y;
2035 } 2057 }
2036} 2058}
2181 | (b << 16) 2203 | (b << 16)
2182 | (a << 24); 2204 | (a << 24);
2183 } 2205 }
2184 } 2206 }
2185 2207
2186 RETVAL = map_sv; 2208 RETVAL = map_sv;
2187} 2209}
2188 OUTPUT: 2210 OUTPUT:
2189 RETVAL 2211 RETVAL
2190 2212
2191void 2213void
2192draw (DC::Map self, int mx, int my, int sw, int sh, int Tw, int Th, U32 player = 0xffffffff, int sdx = 0, int sdy = 0) 2214draw (DC::Map self, int mx, int my, int sw, int sh, int Tw, int Th, U32 player = 0xffffffff, int sdx = 0, int sdy = 0)
2193 CODE: 2215 CODE:
2194{ 2216{
2195 int x, y, z; 2217 int x, y, z;
2196 2218
2197 HV *smooth = (HV *)sv_2mortal ((SV *)newHV ());
2198 uint32_t smooth_level[256 / 32]; // one bit for every possible smooth level
2199 static uint8_t smooth_max[256][256]; // egad, fast and wasteful on memory (64k) 2219 static uint8_t smooth_max[256][256]; // egad, fast and wasteful on memory (64k), also, static!
2200 smooth_key skey;
2201 int pl_x, pl_y; 2220 int pl_x, pl_y;
2202 maptex pl_tex; 2221 maptex pl_tex;
2203 rc_t *rc = rc_alloc (); 2222 rc_t rc;
2204 rc_t *rc_ov = rc_alloc (); 2223 rc_t rc_ov;
2205 rc_key_t key; 2224 rc_key_t key;
2206 rc_array_t *arr; 2225 rc_t::array_t *arr;
2207 2226
2208 pl_tex.name = 0; 2227 pl_tex.name = 0;
2209 2228
2210 // that's current max. sorry. 2229 // that's current max. sorry.
2211 if (sw > 255) sw = 255; 2230 if (sw > 255) sw = 255;
2212 if (sh > 255) sh = 255; 2231 if (sh > 255) sh = 255;
2213
2214 // clear key, in case of extra padding
2215 memset (&skey, 0, sizeof (skey));
2216 2232
2217 memset (&key, 0, sizeof (key)); 2233 memset (&key, 0, sizeof (key));
2218 key.r = 255; 2234 key.r = 255;
2219 key.g = 255; 2235 key.g = 255;
2220 key.b = 255; 2236 key.b = 255;
2226 my += self->y; 2242 my += self->y;
2227 2243
2228 // first pass: determine smooth_max 2244 // first pass: determine smooth_max
2229 // rather ugly, if you ask me 2245 // rather ugly, if you ask me
2230 // could also be stored inside mapcell and updated on change 2246 // could also be stored inside mapcell and updated on change
2231 memset (smooth_max, 0, sizeof (smooth_max)); 2247 memset (smooth_max, 0, sizeof (smooth_max[0]) * (sh + 1));
2232 2248
2233 for (y = 0; y < sh; y++) 2249 for (y = 0; y < sh; y++)
2234 if (0 <= y + my && y + my < self->rows) 2250 if (0 <= y + my && y + my < self->rows)
2235 { 2251 {
2236 maprow *row = self->row + (y + my); 2252 maprow *row = self->row + (y + my);
2251 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 2267 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
2252 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); 2268 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
2253 2269
2254 for (z = 0; z <= 2; z++) 2270 for (z = 0; z <= 2; z++)
2255 { 2271 {
2256 memset (smooth_level, 0, sizeof (smooth_level)); 2272 std::bitset<256> smooth_level; // one bit for every possible smooth level
2273 smooth_key skey;
2274 smooth_hash smooth;
2257 key.texname = -1; 2275 key.texname = -1;
2258 2276
2259 for (y = 0; y < sh; y++) 2277 for (y = 0; y < sh; y++)
2260 if (0 <= y + my && y + my < self->rows) 2278 if (0 <= y + my && y + my < self->rows)
2261 { 2279 {
2278 2296
2279 if (!tex.name) 2297 if (!tex.name)
2280 tex = self->tex [TEXID_NOFACE]; /* missing, replace by noface */ 2298 tex = self->tex [TEXID_NOFACE]; /* missing, replace by noface */
2281 2299
2282 key.texname = tex.name; 2300 key.texname = tex.name;
2283 arr = rc_array (rc, &key); 2301 arr = &rc.array (key);
2284 } 2302 }
2285 2303
2286 px = (x + 1) * Th - tex.w; 2304 px = (x + 1) * Th - tex.w;
2287 py = (y + 1) * Tw - tex.h; 2305 py = (y + 1) * Tw - tex.h;
2288 2306
2289 if (expect_false (cell->player == player) && expect_false (z == 2)) 2307 if (ecb_expect_false (cell->player == player) && ecb_expect_false (z == 2))
2290 { 2308 {
2291 pl_x = px; 2309 pl_x = px;
2292 pl_y = py; 2310 pl_y = py;
2293 pl_tex = tex; 2311 pl_tex = tex;
2294 continue; 2312 continue;
2295 } 2313 }
2296 2314
2297 rc_t2f_v3f (arr, 0 , 0 , px , py , 0); 2315 arr->t2f_v3f (0 , 0 , px , py , 0);
2298 rc_t2f_v3f (arr, 0 , tex.t, px , py + tex.h, 0); 2316 arr->t2f_v3f (0 , tex.t, px , py + tex.h, 0);
2299 rc_t2f_v3f (arr, tex.s, tex.t, px + tex.w, py + tex.h, 0); 2317 arr->t2f_v3f (tex.s, tex.t, px + tex.w, py + tex.h, 0);
2300 rc_t2f_v3f (arr, tex.s, 0 , px + tex.w, py , 0); 2318 arr->t2f_v3f (tex.s, 0 , px + tex.w, py , 0);
2301 2319
2302 // update smooth hash 2320 // update smooth hash
2303 if (tex.smoothtile) 2321 if (tex.smoothtile)
2304 { 2322 {
2305 skey.tile = tex.smoothtile; 2323 skey.tile = tex.smoothtile;
2306 skey.level = tex.smoothlevel; 2324 skey.level = tex.smoothlevel;
2307 2325
2308 smooth_level [tex.smoothlevel >> 5] |= ((uint32_t)1) << (tex.smoothlevel & 31); 2326 smooth_level[tex.smoothlevel] = 1;
2309 2327
2310 // add bits to current tile and all neighbours. skey.x|y is 2328 // add bits to current tile and all neighbours. skey.x|y is
2311 // shifted +1|+1 so we always stay positive. 2329 // shifted +1|+1 so we always stay positive.
2312 2330
2313 // bits is ___n cccc CCCC bbbb 2331 // bits is ___n cccc CCCC bbbb
2321 2339
2322 // corners: 1 ┛· 2 ·┗ 4 ·· 8 ·· 2340 // corners: 1 ┛· 2 ·┗ 4 ·· 8 ··
2323 // ·· ·· ·┏ ┓· 2341 // ·· ·· ·┏ ┓·
2324 2342
2325 // full tile 2343 // full tile
2326 skey.x = x + 1; skey.y = y + 1; smooth_or_bits (smooth, &skey, 0x1000); 2344 skey.x = x + 1; skey.y = y + 1; smooth_or_bits (smooth, skey, 0x1000);
2327 2345
2328 // borders 2346 // borders
2329 skey.x = x + 2; skey.y = y + 1; smooth_or_bits (smooth, &skey, 0x0091); 2347 skey.x = x + 2; skey.y = y + 1; smooth_or_bits (smooth, skey, 0x0091);
2330 skey.x = x + 1; skey.y = y + 2; smooth_or_bits (smooth, &skey, 0x0032); 2348 skey.x = x + 1; skey.y = y + 2; smooth_or_bits (smooth, skey, 0x0032);
2331 skey.x = x ; skey.y = y + 1; smooth_or_bits (smooth, &skey, 0x0064); 2349 skey.x = x ; skey.y = y + 1; smooth_or_bits (smooth, skey, 0x0064);
2332 skey.x = x + 1; skey.y = y ; smooth_or_bits (smooth, &skey, 0x00c8); 2350 skey.x = x + 1; skey.y = y ; smooth_or_bits (smooth, skey, 0x00c8);
2333 2351
2334 // corners 2352 // corners
2335 skey.x = x + 2; skey.y = y + 2; smooth_or_bits (smooth, &skey, 0x0100); 2353 skey.x = x + 2; skey.y = y + 2; smooth_or_bits (smooth, skey, 0x0100);
2336 skey.x = x ; skey.y = y + 2; smooth_or_bits (smooth, &skey, 0x0200); 2354 skey.x = x ; skey.y = y + 2; smooth_or_bits (smooth, skey, 0x0200);
2337 skey.x = x ; skey.y = y ; smooth_or_bits (smooth, &skey, 0x0400); 2355 skey.x = x ; skey.y = y ; smooth_or_bits (smooth, skey, 0x0400);
2338 skey.x = x + 2; skey.y = y ; smooth_or_bits (smooth, &skey, 0x0800); 2356 skey.x = x + 2; skey.y = y ; smooth_or_bits (smooth, skey, 0x0800);
2339 } 2357 }
2340 } 2358 }
2341 2359
2342 if (expect_false (z == 2) && expect_false (cell->flags)) 2360 if (ecb_expect_false (z == 2) && ecb_expect_false (cell->flags))
2343 { 2361 {
2344 // overlays such as the speech bubble, probably more to come 2362 // overlays such as the speech bubble, probably more to come
2345 if (cell->flags & 1) 2363 if (cell->flags & 1)
2346 { 2364 {
2347 rc_key_t key_ov = key; 2365 rc_key_t key_ov = key;
2348 maptex tex = self->tex [TEXID_SPEECH]; 2366 maptex tex = self->tex[TEXID_SPEECH];
2349 rc_array_t *arr;
2350 int px = x * Tw + Tw * 2 / 32; 2367 int px = x * Tw + Tw * 2 / 32;
2351 int py = y * Th - Th * 6 / 32; 2368 int py = y * Th - Th * 6 / 32;
2352 2369
2353 key_ov.texname = tex.name; 2370 key_ov.texname = tex.name;
2354 arr = rc_array (rc_ov, &key_ov); 2371 rc_t::array_t &arr = rc_ov.array (key_ov);
2355 2372
2356 rc_t2f_v3f (arr, 0 , 0 , px , py , 0); 2373 arr.t2f_v3f (0 , 0 , px , py , 0);
2357 rc_t2f_v3f (arr, 0 , tex.t, px , py + Th, 0); 2374 arr.t2f_v3f (0 , tex.t, px , py + Th, 0);
2358 rc_t2f_v3f (arr, tex.s, tex.t, px + Tw, py + Th, 0); 2375 arr.t2f_v3f (tex.s, tex.t, px + Tw, py + Th, 0);
2359 rc_t2f_v3f (arr, tex.s, 0 , px + Tw, py , 0); 2376 arr.t2f_v3f (tex.s, 0 , px + Tw, py , 0);
2360 } 2377 }
2361 } 2378 }
2362 } 2379 }
2363 } 2380 }
2364 2381
2365 rc_draw (rc); 2382 rc.draw ();
2366 rc_clear (rc); 2383 rc.clear ();
2367 2384
2368 // go through all smoothlevels, lowest to highest, then draw. 2385 // go through all smoothlevels, lowest to highest, then draw.
2369 // this is basically counting sort 2386 // this is basically counting sort
2370 { 2387 {
2371 int w, b; 2388 int w, b;
2372 2389
2373 glEnable (GL_TEXTURE_2D); 2390 glEnable (GL_TEXTURE_2D);
2374 glBegin (GL_QUADS); 2391 glBegin (GL_QUADS);
2375 for (w = 0; w < 256 / 32; ++w) 2392 for (int level = 0; level < smooth_level.size (); ++level)
2393 if (smooth_level[level])
2394 for (auto &&it = smooth.begin (); it != smooth.end (); ++it)
2376 { 2395 {
2377 uint32_t smask = smooth_level [w]; 2396 smooth_key &skey = it->first;
2378 if (smask) 2397 IV bits = it->second;
2379 for (b = 0; b < 32; ++b) 2398
2380 if (smask & (((uint32_t)1) << b)) 2399 if (!(bits & 0x1000)
2400 && skey.level == level
2401 && level > smooth_max [skey.x][skey.y])
2381 { 2402 {
2382 int level = (w << 5) | b; 2403 maptex tex = self->tex [skey.tile];
2404 int px = (((int)skey.x) - 1) * Tw;
2405 int py = (((int)skey.y) - 1) * Th;
2406 int border = bits & 15;
2407 int corner = (bits >> 8) & ~(bits >> 4) & 15;
2408 float dx = tex.s * .0625f; // 16 images/row
2409 float dy = tex.t * .5f ; // 2 images/column
2410
2383 HE *he; 2411 if (tex.name)
2384
2385 hv_iterinit (smooth);
2386 while ((he = hv_iternext (smooth)))
2387 { 2412 {
2388 smooth_key *skey = (smooth_key *)HeKEY (he); 2413 // this time avoiding texture state changes
2389 IV bits = SvIVX (HeVAL (he)); 2414 // save gobs of state changes.
2390 2415 if (key.texname != tex.name)
2391 if (!(bits & 0x1000)
2392 && skey->level == level
2393 && level > smooth_max [skey->x][skey->y])
2394 { 2416 {
2395 maptex tex = self->tex [skey->tile];
2396 int px = (((int)skey->x) - 1) * Tw;
2397 int py = (((int)skey->y) - 1) * Th;
2398 int border = bits & 15;
2399 int corner = (bits >> 8) & ~(bits >> 4) & 15;
2400 float dx = tex.s * .0625f; // 16 images/row
2401 float dy = tex.t * .5f ; // 2 images/column
2402
2403 if (tex.name)
2404 {
2405 // this time avoiding texture state changes
2406 // save gobs of state changes.
2407 if (key.texname != tex.name)
2408 {
2409 self->tex [skey->tile].unused = 0; 2417 self->tex [skey.tile].unused = 0;
2410 2418
2411 glEnd (); 2419 glEnd ();
2412 glBindTexture (GL_TEXTURE_2D, key.texname = tex.name); 2420 glBindTexture (GL_TEXTURE_2D, key.texname = tex.name);
2413 glBegin (GL_QUADS); 2421 glBegin (GL_QUADS);
2414 } 2422 }
2415 2423
2416 if (border) 2424 if (border)
2417 { 2425 {
2418 float ox = border * dx; 2426 float ox = border * dx;
2419 2427
2420 glTexCoord2f (ox , 0.f ); glVertex2i (px , py ); 2428 glTexCoord2f (ox , 0.f ); glVertex2i (px , py );
2421 glTexCoord2f (ox , dy ); glVertex2i (px , py + Th); 2429 glTexCoord2f (ox , dy ); glVertex2i (px , py + Th);
2422 glTexCoord2f (ox + dx, dy ); glVertex2i (px + Tw, py + Th); 2430 glTexCoord2f (ox + dx, dy ); glVertex2i (px + Tw, py + Th);
2423 glTexCoord2f (ox + dx, 0.f ); glVertex2i (px + Tw, py ); 2431 glTexCoord2f (ox + dx, 0.f ); glVertex2i (px + Tw, py );
2424 } 2432 }
2425 2433
2426 if (corner) 2434 if (corner)
2427 { 2435 {
2428 float ox = corner * dx; 2436 float ox = corner * dx;
2429 2437
2430 glTexCoord2f (ox , dy ); glVertex2i (px , py ); 2438 glTexCoord2f (ox , dy ); glVertex2i (px , py );
2431 glTexCoord2f (ox , dy * 2.f); glVertex2i (px , py + Th); 2439 glTexCoord2f (ox , dy * 2.f); glVertex2i (px , py + Th);
2432 glTexCoord2f (ox + dx, dy * 2.f); glVertex2i (px + Tw, py + Th); 2440 glTexCoord2f (ox + dx, dy * 2.f); glVertex2i (px + Tw, py + Th);
2433 glTexCoord2f (ox + dx, dy ); glVertex2i (px + Tw, py ); 2441 glTexCoord2f (ox + dx, dy ); glVertex2i (px + Tw, py );
2434 }
2435 }
2436 } 2442 }
2437 } 2443 }
2438 } 2444 }
2439 } 2445 }
2440 2446
2441 glEnd (); 2447 glEnd ();
2442 glDisable (GL_TEXTURE_2D); 2448 glDisable (GL_TEXTURE_2D);
2443 key.texname = -1; 2449 key.texname = -1;
2444 } 2450 }
2445
2446 hv_clear (smooth);
2447 } 2451 }
2448 2452
2449 if (pl_tex.name) 2453 if (pl_tex.name)
2450 { 2454 {
2451 maptex tex = pl_tex; 2455 maptex tex = pl_tex;
2452 int px = pl_x + sdx; 2456 int px = pl_x + sdx;
2453 int py = pl_y + sdy; 2457 int py = pl_y + sdy;
2454 2458
2455 key.texname = tex.name; 2459 key.texname = tex.name;
2456 arr = rc_array (rc, &key); 2460 rc_t::array_t &arr = rc.array (key);
2457 2461
2458 rc_t2f_v3f (arr, 0 , 0 , px , py , 0); 2462 arr.t2f_v3f (0 , 0 , px , py , 0);
2459 rc_t2f_v3f (arr, 0 , tex.t, px , py + tex.h, 0); 2463 arr.t2f_v3f (0 , tex.t, px , py + tex.h, 0);
2460 rc_t2f_v3f (arr, tex.s, tex.t, px + tex.w, py + tex.h, 0); 2464 arr.t2f_v3f (tex.s, tex.t, px + tex.w, py + tex.h, 0);
2461 rc_t2f_v3f (arr, tex.s, 0 , px + tex.w, py , 0); 2465 arr.t2f_v3f (tex.s, 0 , px + tex.w, py , 0);
2462 2466
2463 rc_draw (rc); 2467 rc.draw ();
2464 } 2468 }
2465 2469
2466 rc_draw (rc_ov); 2470 rc_ov.draw ();
2467 rc_clear (rc_ov); 2471 rc_ov.clear ();
2468 2472
2469 glDisable (GL_BLEND); 2473 glDisable (GL_BLEND);
2470 rc_free (rc);
2471 rc_free (rc_ov);
2472 2474
2473 // top layer: overlays such as the health bar 2475 // top layer: overlays such as the health bar
2474 for (y = 0; y < sh; y++) 2476 for (y = 0; y < sh; y++)
2475 if (0 <= y + my && y + my < self->rows) 2477 if (0 <= y + my && y + my < self->rows)
2476 { 2478 {
2482 mapcell *cell = row->col + (x + mx - row->c0); 2484 mapcell *cell = row->col + (x + mx - row->c0);
2483 2485
2484 int px = x * Tw; 2486 int px = x * Tw;
2485 int py = y * Th; 2487 int py = y * Th;
2486 2488
2487 if (expect_false (cell->player == player)) 2489 if (ecb_expect_false (cell->player == player))
2488 { 2490 {
2489 px += sdx; 2491 px += sdx;
2490 py += sdy; 2492 py += sdy;
2491 } 2493 }
2492 2494
2720 else 2722 else
2721 *data++ = 0; 2723 *data++ = 0;
2722 } 2724 }
2723 } 2725 }
2724 2726
2725 /* if size is w*h + 5 then no data has been found */ 2727 /* if size is w*h + 5 then no data has been found */
2726 if (data - (uint8_t *)SvPVX (data_sv) != w * h + 5) 2728 if (data - (uint8_t *)SvPVX (data_sv) != w * h + 5)
2727 { 2729 {
2728 SvPOK_only (data_sv); 2730 SvPOK_only (data_sv);
2729 SvCUR_set (data_sv, data - (uint8_t *)SvPVX (data_sv)); 2731 SvCUR_set (data_sv, data - (uint8_t *)SvPVX (data_sv));
2730 } 2732 }
2731 2733
2732 RETVAL = data_sv; 2734 RETVAL = data_sv;
2733} 2735}
2734 OUTPUT: 2736 OUTPUT:
2735 RETVAL 2737 RETVAL
2736 2738
2737void 2739void
3427 3429
3428void 3430void
3429find_widget (SV *self, NV x, NV y) 3431find_widget (SV *self, NV x, NV y)
3430 PPCODE: 3432 PPCODE:
3431{ 3433{
3432 if (within_widget (self, x, y)) 3434 if (within_widget (self, x, y))
3433 XPUSHs (self); 3435 XPUSHs (self);
3434} 3436}
3435 3437
3436BOOT: 3438BOOT:
3437{ 3439{
3445 3447
3446void 3448void
3447draw (SV *self) 3449draw (SV *self)
3448 CODE: 3450 CODE:
3449{ 3451{
3450 HV *hv; 3452 HV *hv;
3451 SV **svp; 3453 SV **svp;
3452 NV x, y, w, h; 3454 NV x, y, w, h;
3453 SV *draw_x_sv = GvSV (draw_x_gv); 3455 SV *draw_x_sv = GvSV (draw_x_gv);
3454 SV *draw_y_sv = GvSV (draw_y_gv); 3456 SV *draw_y_sv = GvSV (draw_y_gv);
3455 SV *draw_w_sv = GvSV (draw_w_gv); 3457 SV *draw_w_sv = GvSV (draw_w_gv);
3456 SV *draw_h_sv = GvSV (draw_h_gv); 3458 SV *draw_h_sv = GvSV (draw_h_gv);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines