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.325 by root, Sun Nov 18 02:04:48 2018 UTC vs.
Revision 1.335 by root, Mon Nov 19 01:56:11 2018 UTC

76# include <netinet/in.h> 76# include <netinet/in.h>
77# include <netinet/tcp.h> 77# include <netinet/tcp.h>
78# include <inttypes.h> 78# include <inttypes.h>
79#endif 79#endif
80 80
81#if __GNUC__ >= 4 81#include "ecb.h"
82# define expect(expr,value) __builtin_expect ((expr),(value)) 82#include "salloc.h"
83#else
84# define expect(expr,value) (expr)
85#endif
86
87#define expect_false(expr) expect ((expr) != 0, 0)
88#define expect_true(expr) expect ((expr) != 0, 1)
89 83
90#define OBJ_STR "\xef\xbf\xbc" /* U+FFFC, object replacement character */ 84#define OBJ_STR "\xef\xbf\xbc" /* U+FFFC, object replacement character */
91 85
92/* 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 */
93/* then anything that is computed by incoming darkness */ 87/* then anything that is computed by incoming darkness */
105#define KMOD_LRAM 0x10000 // our extension 99#define KMOD_LRAM 0x10000 // our extension
106 100
107#define TEXID_SPEECH 1 101#define TEXID_SPEECH 1
108#define TEXID_NOFACE 2 102#define TEXID_NOFACE 2
109 103
104// approximately divide by 255
105static unsigned int
106div255 (unsigned int n)
107{
108 return (n + (n >> 8)) >> 8;
109}
110
111static unsigned int
112minpot (unsigned int n)
113{
114 if (!n)
115 return 0;
116
117 --n;
118
119 n |= n >> 1;
120 n |= n >> 2;
121 n |= n >> 4;
122 n |= n >> 8;
123 n |= n >> 16;
124
125 return n + 1;
126}
127
110static char * 128static char *
111fast_sv_grow (SV *sv, STRLEN need) 129fast_sv_grow (SV *sv, STRLEN need)
112{ 130{
113 STRLEN len = SvLEN (sv); 131 STRLEN len = SvLEN (sv);
114 STRLEN want = SvCUR (sv) + need; 132 STRLEN want = SvCUR (sv) + need;
115 133
116 if (expect_false (len < want)) 134 if (ecb_expect_false (len < want))
117 { 135 {
118 do 136 do
119 len *= 2; 137 len *= 2;
120 while (len < want); 138 while (len < want);
121 139
205struct cf_layout { 223struct cf_layout {
206 PangoLayout *pl; 224 PangoLayout *pl;
207 float r, g, b, a; // default color for rgba mode 225 float r, g, b, a; // default color for rgba mode
208 int base_height; 226 int base_height;
209 DC__Font font; 227 DC__Font font;
210 rc_t *rc; 228 rc_t rc;
211}; 229};
212 230
213typedef cf_layout *DC__Layout; 231typedef cf_layout *DC__Layout;
214 232
215static DC__Font default_font; 233static DC__Font default_font;
255 if (!rect.height) rect.height = 1; 273 if (!rect.height) rect.height = 1;
256 274
257 *w = rect.width; 275 *w = rect.width;
258 *h = rect.height; 276 *h = rect.height;
259} 277}
278
279/////////////////////////////////////////////////////////////////////////////
260 280
261typedef uint16_t tileid; 281typedef uint16_t tileid;
262typedef uint16_t faceid; 282typedef uint16_t faceid;
263 283
264struct maptex 284struct maptex
296 maprow *row; 316 maprow *row;
297}; 317};
298 318
299typedef mapgrid *DC__Map; 319typedef mapgrid *DC__Map;
300 320
301static char * 321template<typename T>
322ecb_cold static void
302prepend (char *ptr, int sze, int inc) 323prepend (T *&ptr, int sze, int inc)
303{ 324{
304 char *p; 325 T *p;
305 326
306 New (0, p, sze + inc, char); 327 Newx (p, inc + sze, T);
307 Zero (p, inc, char); 328 Zero (p, inc, T);
308 Move (ptr, p + inc, sze, char); 329 Move (ptr, p + inc, sze, T);
309 Safefree (ptr); 330 Safefree (ptr);
310 331
311 return p; 332 ptr = p;
312} 333}
313 334
314static char * 335template<typename T>
336ecb_cold static void
315append (char *ptr, int sze, int inc) 337append (T *&ptr, int sze, int inc)
316{ 338{
317 Renew (ptr, sze + inc, char); 339 Renew (ptr, sze + inc, T);
318 Zero (ptr + sze, inc, char); 340 Zero (ptr + sze, inc, T);
319
320 return ptr;
321} 341}
322
323#define Append(type,ptr,sze,inc) (ptr) = (type *)append ((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))
325 342
326static void 343static void
327need_facenum (struct mapgrid *self, faceid face) 344need_facenum (struct mapgrid *self, faceid face)
328{ 345{
329 while (self->faces <= face) 346 if (ecb_expect_true (self->faces > face))
330 { 347 return;
348
349 size_t newsize = minpot (face + 1);
331 Append (tileid, self->face2tile, self->faces, self->faces); 350 append (self->face2tile, self->faces, newsize - self->faces);
332 self->faces *= 2; 351 self->faces = newsize;
333 }
334} 352}
335 353
336static void 354static void
337need_texid (struct mapgrid *self, int texid) 355need_texid (struct mapgrid *self, int texid)
338{ 356{
339 while (self->texs <= texid) 357 if (ecb_expect_true (self->texs > texid))
340 { 358 return;
359
360 size_t newsize = minpot (texid + 1);
341 Append (maptex, self->tex, self->texs, self->texs); 361 append (self->tex, self->texs, newsize - self->texs);
342 self->texs *= 2; 362 self->texs = newsize;
343 }
344} 363}
345 364
346static maprow * 365static maprow *
347map_get_row (mapgrid *self, int y) 366map_get_row (mapgrid *self, int y)
348{ 367{
349 if (0 > y) 368 if (0 > y)
350 { 369 {
351 int extend = - y + MAP_EXTEND_Y; 370 int extend = - y + MAP_EXTEND_Y;
352 Prepend (maprow, self->row, self->rows, extend); 371 prepend (self->row, self->rows, extend);
353 372
354 self->rows += extend; 373 self->rows += extend;
355 self->y += extend; 374 self->y += extend;
356 y += extend; 375 y += extend;
357 } 376 }
358 else if (y >= self->rows) 377 else if (y >= self->rows)
359 { 378 {
360 int extend = y - self->rows + MAP_EXTEND_Y; 379 int extend = y - self->rows + MAP_EXTEND_Y;
361 Append (maprow, self->row, self->rows, extend); 380 append (self->row, self->rows, extend);
362 self->rows += extend; 381 self->rows += extend;
363 } 382 }
364 383
365 return self->row + y; 384 return self->row + y;
366} 385}
376 } 395 }
377 396
378 if (row->c0 > x) 397 if (row->c0 > x)
379 { 398 {
380 int extend = row->c0 - x + MAP_EXTEND_X; 399 int extend = row->c0 - x + MAP_EXTEND_X;
381 Prepend (mapcell, row->col, row->c1 - row->c0, extend); 400 prepend (row->col, row->c1 - row->c0, extend);
382 row->c0 -= extend; 401 row->c0 -= extend;
383 } 402 }
384 else if (x >= row->c1) 403 else if (x >= row->c1)
385 { 404 {
386 int extend = x - row->c1 + MAP_EXTEND_X; 405 int extend = x - row->c1 + MAP_EXTEND_X;
387 Append (mapcell, row->col, row->c1 - row->c0, extend); 406 append (row->col, row->c1 - row->c0, extend);
388 row->c1 += extend; 407 row->c1 += extend;
389 } 408 }
390 409
391 return row->col + (x - row->c0); 410 return row->col + (x - row->c0);
392} 411}
462 { 481 {
463 return tile == o.tile && x == o.x && y == o.y && level == o.level; 482 return tile == o.tile && x == o.x && y == o.y && level == o.level;
464 } 483 }
465}; 484};
466 485
467typedef ska::flat_hash_map<smooth_key, IV> smooth_hash; 486typedef ska::flat_hash_map<smooth_key, IV, std::hash<smooth_key>, std::equal_to<smooth_key>, slice_allocator<smooth_key>> smooth_hash;
468 487
469namespace std { 488namespace std {
470 template <> 489 template <>
471 struct hash<smooth_key> 490 struct hash<smooth_key>
472 { 491 {
510 ev.code = 1; 529 ev.code = 1;
511 ev.data1 = (void *)(long)channel; 530 ev.data1 = (void *)(long)channel;
512 ev.data2 = 0; 531 ev.data2 = 0;
513 532
514 SDL_PushEvent ((SDL_Event *)&ev); 533 SDL_PushEvent ((SDL_Event *)&ev);
515}
516
517// approximately divide by 255
518static unsigned int
519div255 (unsigned int n)
520{
521 return (n + (n >> 8)) >> 8;
522}
523
524static unsigned int
525minpot (unsigned int n)
526{
527 if (!n)
528 return 0;
529
530 --n;
531
532 n |= n >> 1;
533 n |= n >> 2;
534 n |= n >> 4;
535 n |= n >> 8;
536 n |= n >> 16;
537
538 return n + 1;
539}
540
541static unsigned int
542popcount (unsigned int n)
543{
544 n -= (n >> 1) & 0x55555555U;
545 n = ((n >> 2) & 0x33333333U) + (n & 0x33333333U);
546 n = ((n >> 4) + n) & 0x0f0f0f0fU;
547 n *= 0x01010101U;
548
549 return n >> 24;
550} 534}
551 535
552/* SDL should provide this, really. */ 536/* SDL should provide this, really. */
553#define SDLK_MODIFIER_MIN 300 537#define SDLK_MODIFIER_MIN 300
554#define SDLK_MODIFIER_MAX 314 538#define SDLK_MODIFIER_MAX 314
885 869
886NV ceil (NV x) 870NV ceil (NV x)
887 871
888IV minpot (UV n) 872IV minpot (UV n)
889 873
874UV ld32 (UV n)
875 CODE:
876 RETVAL = ecb_ld32 (n);
877 OUTPUT:
878 RETVAL
879
890IV popcount (UV n) 880IV popcount (UV n)
881 CODE:
882 RETVAL = ecb_popcount32 (n);
883 OUTPUT:
884 RETVAL
891 885
892NV distance (NV dx, NV dy) 886NV distance (NV dx, NV dy)
893 CODE: 887 CODE:
894 RETVAL = pow (dx * dx + dy * dy, 0.5); 888 RETVAL = pow (dx * dx + dy * dy, 0.5);
895 OUTPUT: 889 OUTPUT:
1369 tc_restore (); 1363 tc_restore ();
1370 1364
1371DC::Layout 1365DC::Layout
1372new (SV *klass) 1366new (SV *klass)
1373 CODE: 1367 CODE:
1374 New (0, RETVAL, 1, struct cf_layout); 1368 RETVAL = new cf_layout;
1375 1369
1376 RETVAL->pl = pango_layout_new (opengl_context); 1370 RETVAL->pl = pango_layout_new (opengl_context);
1377 RETVAL->r = 1.; 1371 RETVAL->r = 1.;
1378 RETVAL->g = 1.; 1372 RETVAL->g = 1.;
1379 RETVAL->b = 1.; 1373 RETVAL->b = 1.;
1380 RETVAL->a = 1.; 1374 RETVAL->a = 1.;
1381 RETVAL->base_height = MIN_FONT_HEIGHT; 1375 RETVAL->base_height = MIN_FONT_HEIGHT;
1382 RETVAL->font = 0; 1376 RETVAL->font = 0;
1383 RETVAL->rc = rc_alloc ();
1384 1377
1385 pango_layout_set_wrap (RETVAL->pl, PANGO_WRAP_WORD_CHAR); 1378 pango_layout_set_wrap (RETVAL->pl, PANGO_WRAP_WORD_CHAR);
1386 layout_update_font (RETVAL); 1379 layout_update_font (RETVAL);
1387 OUTPUT: 1380 OUTPUT:
1388 RETVAL 1381 RETVAL
1389 1382
1390void 1383void
1391DESTROY (DC::Layout self) 1384DESTROY (DC::Layout self)
1392 CODE: 1385 CODE:
1393 g_object_unref (self->pl); 1386 g_object_unref (self->pl);
1394 rc_free (self->rc);
1395 Safefree (self); 1387 delete self;
1396 1388
1397void 1389void
1398set_text (DC::Layout self, SV *text_) 1390set_text (DC::Layout self, SV *text_)
1399 CODE: 1391 CODE:
1400{ 1392{
1654} 1646}
1655 1647
1656void 1648void
1657render (DC::Layout self, float x, float y, int flags = 0) 1649render (DC::Layout self, float x, float y, int flags = 0)
1658 CODE: 1650 CODE:
1659 rc_clear (self->rc); 1651 self->rc.clear ();
1660 pango_opengl_render_layout_subpixel ( 1652 pango_opengl_render_layout_subpixel (
1661 self->pl, 1653 self->pl,
1662 self->rc, 1654 &self->rc,
1663 x * PANGO_SCALE, y * PANGO_SCALE, 1655 x * PANGO_SCALE, y * PANGO_SCALE,
1664 self->r, self->g, self->b, self->a, 1656 self->r, self->g, self->b, self->a,
1665 flags 1657 flags
1666 ); 1658 );
1667 // we assume that context_change actually clears/frees stuff 1659 // we assume that context_change actually clears/frees stuff
1678 gl_BlendFuncSeparate (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, 1670 gl_BlendFuncSeparate (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA,
1679 GL_ONE , GL_ONE_MINUS_SRC_ALPHA); 1671 GL_ONE , GL_ONE_MINUS_SRC_ALPHA);
1680 glEnable (GL_ALPHA_TEST); 1672 glEnable (GL_ALPHA_TEST);
1681 glAlphaFunc (GL_GREATER, 7.f / 255.f); 1673 glAlphaFunc (GL_GREATER, 7.f / 255.f);
1682 1674
1683 rc_draw (self->rc); 1675 self->rc.draw ();
1684 1676
1685 glDisable (GL_ALPHA_TEST); 1677 glDisable (GL_ALPHA_TEST);
1686 glDisable (GL_BLEND); 1678 glDisable (GL_BLEND);
1687 glDisable (GL_TEXTURE_2D); 1679 glDisable (GL_TEXTURE_2D);
1688} 1680}
2058 self->ox += dx; self->x += dx; 2050 self->ox += dx; self->x += dx;
2059 self->oy += dy; self->y += dy; 2051 self->oy += dy; self->y += dy;
2060 2052
2061 while (self->y < 0) 2053 while (self->y < 0)
2062 { 2054 {
2063 Prepend (maprow, self->row, self->rows, MAP_EXTEND_Y); 2055 prepend (self->row, self->rows, MAP_EXTEND_Y);
2064 2056
2065 self->rows += MAP_EXTEND_Y; 2057 self->rows += MAP_EXTEND_Y;
2066 self->y += MAP_EXTEND_Y; 2058 self->y += MAP_EXTEND_Y;
2067 } 2059 }
2068} 2060}
2227 int x, y, z; 2219 int x, y, z;
2228 2220
2229 static uint8_t smooth_max[256][256]; // egad, fast and wasteful on memory (64k), also, static! 2221 static uint8_t smooth_max[256][256]; // egad, fast and wasteful on memory (64k), also, static!
2230 int pl_x, pl_y; 2222 int pl_x, pl_y;
2231 maptex pl_tex; 2223 maptex pl_tex;
2232 rc_t *rc = rc_alloc (); 2224 rc_t rc;
2233 rc_t *rc_ov = rc_alloc (); 2225 rc_t rc_ov;
2234 rc_key_t key; 2226 rc_key_t key;
2235 rc_array_t *arr; 2227 rc_t::array_t *arr;
2236 2228
2237 pl_tex.name = 0; 2229 pl_tex.name = 0;
2238 2230
2239 // that's current max. sorry. 2231 // that's current max. sorry.
2240 if (sw > 255) sw = 255; 2232 if (sw > 255) sw = 255;
2306 2298
2307 if (!tex.name) 2299 if (!tex.name)
2308 tex = self->tex [TEXID_NOFACE]; /* missing, replace by noface */ 2300 tex = self->tex [TEXID_NOFACE]; /* missing, replace by noface */
2309 2301
2310 key.texname = tex.name; 2302 key.texname = tex.name;
2311 arr = rc_array (rc, &key); 2303 arr = &rc.array (key);
2312 } 2304 }
2313 2305
2314 px = (x + 1) * Th - tex.w; 2306 px = (x + 1) * Th - tex.w;
2315 py = (y + 1) * Tw - tex.h; 2307 py = (y + 1) * Tw - tex.h;
2316 2308
2317 if (expect_false (cell->player == player) && expect_false (z == 2)) 2309 if (ecb_expect_false (cell->player == player) && ecb_expect_false (z == 2))
2318 { 2310 {
2319 pl_x = px; 2311 pl_x = px;
2320 pl_y = py; 2312 pl_y = py;
2321 pl_tex = tex; 2313 pl_tex = tex;
2322 continue; 2314 continue;
2323 } 2315 }
2324 2316
2325 rc_t2f_v3f (arr, 0 , 0 , px , py , 0); 2317 arr->t2f_v3f (0 , 0 , px , py , 0);
2326 rc_t2f_v3f (arr, 0 , tex.t, px , py + tex.h, 0); 2318 arr->t2f_v3f (0 , tex.t, px , py + tex.h, 0);
2327 rc_t2f_v3f (arr, tex.s, tex.t, px + tex.w, py + tex.h, 0); 2319 arr->t2f_v3f (tex.s, tex.t, px + tex.w, py + tex.h, 0);
2328 rc_t2f_v3f (arr, tex.s, 0 , px + tex.w, py , 0); 2320 arr->t2f_v3f (tex.s, 0 , px + tex.w, py , 0);
2329 2321
2330 // update smooth hash 2322 // update smooth hash
2331 if (tex.smoothtile) 2323 if (tex.smoothtile)
2332 { 2324 {
2333 skey.tile = tex.smoothtile; 2325 skey.tile = tex.smoothtile;
2365 skey.x = x ; skey.y = y ; smooth_or_bits (smooth, skey, 0x0400); 2357 skey.x = x ; skey.y = y ; smooth_or_bits (smooth, skey, 0x0400);
2366 skey.x = x + 2; skey.y = y ; smooth_or_bits (smooth, skey, 0x0800); 2358 skey.x = x + 2; skey.y = y ; smooth_or_bits (smooth, skey, 0x0800);
2367 } 2359 }
2368 } 2360 }
2369 2361
2370 if (expect_false (z == 2) && expect_false (cell->flags)) 2362 if (ecb_expect_false (z == 2) && ecb_expect_false (cell->flags))
2371 { 2363 {
2372 // overlays such as the speech bubble, probably more to come 2364 // overlays such as the speech bubble, probably more to come
2373 if (cell->flags & 1) 2365 if (cell->flags & 1)
2374 { 2366 {
2375 rc_key_t key_ov = key; 2367 rc_key_t key_ov = key;
2376 maptex tex = self->tex [TEXID_SPEECH]; 2368 maptex tex = self->tex[TEXID_SPEECH];
2377 rc_array_t *arr;
2378 int px = x * Tw + Tw * 2 / 32; 2369 int px = x * Tw + Tw * 2 / 32;
2379 int py = y * Th - Th * 6 / 32; 2370 int py = y * Th - Th * 6 / 32;
2380 2371
2381 key_ov.texname = tex.name; 2372 key_ov.texname = tex.name;
2382 arr = rc_array (rc_ov, &key_ov); 2373 rc_t::array_t &arr = rc_ov.array (key_ov);
2383 2374
2384 rc_t2f_v3f (arr, 0 , 0 , px , py , 0); 2375 arr.t2f_v3f (0 , 0 , px , py , 0);
2385 rc_t2f_v3f (arr, 0 , tex.t, px , py + Th, 0); 2376 arr.t2f_v3f (0 , tex.t, px , py + Th, 0);
2386 rc_t2f_v3f (arr, tex.s, tex.t, px + Tw, py + Th, 0); 2377 arr.t2f_v3f (tex.s, tex.t, px + Tw, py + Th, 0);
2387 rc_t2f_v3f (arr, tex.s, 0 , px + Tw, py , 0); 2378 arr.t2f_v3f (tex.s, 0 , px + Tw, py , 0);
2388 } 2379 }
2389 } 2380 }
2390 } 2381 }
2391 } 2382 }
2392 2383
2393 rc_draw (rc); 2384 rc.draw ();
2394 rc_clear (rc); 2385 rc.clear ();
2395 2386
2396 // go through all smoothlevels, lowest to highest, then draw. 2387 // go through all smoothlevels, lowest to highest, then draw.
2397 // this is basically counting sort 2388 // this is basically counting sort
2398 { 2389 {
2399 int w, b; 2390 int w, b;
2466 maptex tex = pl_tex; 2457 maptex tex = pl_tex;
2467 int px = pl_x + sdx; 2458 int px = pl_x + sdx;
2468 int py = pl_y + sdy; 2459 int py = pl_y + sdy;
2469 2460
2470 key.texname = tex.name; 2461 key.texname = tex.name;
2471 arr = rc_array (rc, &key); 2462 rc_t::array_t &arr = rc.array (key);
2472 2463
2473 rc_t2f_v3f (arr, 0 , 0 , px , py , 0); 2464 arr.t2f_v3f (0 , 0 , px , py , 0);
2474 rc_t2f_v3f (arr, 0 , tex.t, px , py + tex.h, 0); 2465 arr.t2f_v3f (0 , tex.t, px , py + tex.h, 0);
2475 rc_t2f_v3f (arr, tex.s, tex.t, px + tex.w, py + tex.h, 0); 2466 arr.t2f_v3f (tex.s, tex.t, px + tex.w, py + tex.h, 0);
2476 rc_t2f_v3f (arr, tex.s, 0 , px + tex.w, py , 0); 2467 arr.t2f_v3f (tex.s, 0 , px + tex.w, py , 0);
2477 2468
2478 rc_draw (rc); 2469 rc.draw ();
2479 } 2470 }
2480 2471
2481 rc_draw (rc_ov); 2472 rc_ov.draw ();
2482 rc_clear (rc_ov); 2473 rc_ov.clear ();
2483 2474
2484 glDisable (GL_BLEND); 2475 glDisable (GL_BLEND);
2485 rc_free (rc);
2486 rc_free (rc_ov);
2487 2476
2488 // top layer: overlays such as the health bar 2477 // top layer: overlays such as the health bar
2489 for (y = 0; y < sh; y++) 2478 for (y = 0; y < sh; y++)
2490 if (0 <= y + my && y + my < self->rows) 2479 if (0 <= y + my && y + my < self->rows)
2491 { 2480 {
2497 mapcell *cell = row->col + (x + mx - row->c0); 2486 mapcell *cell = row->col + (x + mx - row->c0);
2498 2487
2499 int px = x * Tw; 2488 int px = x * Tw;
2500 int py = y * Th; 2489 int py = y * Th;
2501 2490
2502 if (expect_false (cell->player == player)) 2491 if (ecb_expect_false (cell->player == player))
2503 { 2492 {
2504 px += sdx; 2493 px += sdx;
2505 py += sdy; 2494 py += sdy;
2506 } 2495 }
2507 2496

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines