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.320 by root, Sun Nov 18 01:00:10 2018 UTC vs.
Revision 1.323 by root, Sun Nov 18 01:48:39 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>
33 37
34#define USE_RWOPS 1 // for SDL_mixer:LoadMUS_RW 38#define USE_RWOPS 1 // for SDL_mixer:LoadMUS_RW
35 39
36#include <SDL.h> 40#include <SDL.h>
37#include <SDL_thread.h> 41#include <SDL_thread.h>
195 } 199 }
196 200
197 return 0; 201 return 0;
198} 202}
199 203
200typedef struct cf_layout { 204struct cf_layout {
201 PangoLayout *pl; 205 PangoLayout *pl;
202 float r, g, b, a; // default color for rgba mode 206 float r, g, b, a; // default color for rgba mode
203 int base_height; 207 int base_height;
204 DC__Font font; 208 DC__Font font;
205 rc_t *rc; 209 rc_t *rc;
206} *DC__Layout; 210};
211
212typedef cf_layout *DC__Layout;
207 213
208static DC__Font default_font; 214static DC__Font default_font;
209static PangoContext *opengl_context; 215static PangoContext *opengl_context;
210static PangoFontMap *opengl_fontmap; 216static PangoFontMap *opengl_fontmap;
211 217
252} 258}
253 259
254typedef uint16_t tileid; 260typedef uint16_t tileid;
255typedef uint16_t faceid; 261typedef uint16_t faceid;
256 262
257typedef struct { 263struct maptex
264{
258 GLuint name; 265 GLuint name;
259 int w, h; 266 int w, h;
260 float s, t; 267 float s, t;
261 uint8_t r, g, b, a; 268 uint8_t r, g, b, a;
262 tileid smoothtile; 269 tileid smoothtile;
263 uint8_t smoothlevel; 270 uint8_t smoothlevel;
264 uint8_t unused; /* set to zero on use */ 271 uint8_t unused; /* set to zero on use */
265} maptex; 272};
266 273
267typedef struct { 274struct mapcell
275{
268 uint32_t player; 276 uint32_t player;
269 tileid tile[3]; 277 tileid tile[3];
270 uint16_t darkness; 278 uint16_t darkness;
271 uint8_t stat_width, stat_hp, flags, smoothmax; 279 uint8_t stat_width, stat_hp, flags, smoothmax;
272} mapcell; 280};
273 281
274typedef struct { 282struct maprow
283{
275 int32_t c0, c1; 284 int32_t c0, c1;
276 mapcell *col; 285 mapcell *col;
277} maprow; 286};
278 287
279typedef struct map { 288struct mapgrid {
280 int x, y, w, h; 289 int x, y, w, h;
281 int ox, oy; /* offset to virtual global coordinate system */ 290 int ox, oy; /* offset to virtual global coordinate system */
282 int faces; tileid *face2tile; // [faceid] 291 int faces; tileid *face2tile; // [faceid]
283 int texs; maptex *tex; // [tileid] 292 int texs; maptex *tex; // [tileid]
284 293
285 int32_t rows; 294 int32_t rows;
286 maprow *row; 295 maprow *row;
287} *DC__Map; 296};
297
298typedef mapgrid *DC__Map;
288 299
289static char * 300static char *
290prepend (char *ptr, int sze, int inc) 301prepend (char *ptr, int sze, int inc)
291{ 302{
292 char *p; 303 char *p;
310 321
311#define Append(type,ptr,sze,inc) (ptr) = (type *)append ((char *)ptr, (sze) * sizeof (type), (inc) * sizeof (type)) 322#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)) 323#define Prepend(type,ptr,sze,inc) (ptr) = (type *)prepend ((char *)ptr, (sze) * sizeof (type), (inc) * sizeof (type))
313 324
314static void 325static void
315need_facenum (struct map *self, faceid face) 326need_facenum (struct mapgrid *self, faceid face)
316{ 327{
317 while (self->faces <= face) 328 while (self->faces <= face)
318 { 329 {
319 Append (tileid, self->face2tile, self->faces, self->faces); 330 Append (tileid, self->face2tile, self->faces, self->faces);
320 self->faces *= 2; 331 self->faces *= 2;
321 } 332 }
322} 333}
323 334
324static void 335static void
325need_texid (struct map *self, int texid) 336need_texid (struct mapgrid *self, int texid)
326{ 337{
327 while (self->texs <= texid) 338 while (self->texs <= texid)
328 { 339 {
329 Append (maptex, self->tex, self->texs, self->texs); 340 Append (maptex, self->tex, self->texs, self->texs);
330 self->texs *= 2; 341 self->texs *= 2;
331 } 342 }
332} 343}
333 344
334static maprow * 345static maprow *
335map_get_row (DC__Map self, int y) 346map_get_row (mapgrid *self, int y)
336{ 347{
337 if (0 > y) 348 if (0 > y)
338 { 349 {
339 int extend = - y + MAP_EXTEND_Y; 350 int extend = - y + MAP_EXTEND_Y;
340 Prepend (maprow, self->row, self->rows, extend); 351 Prepend (maprow, self->row, self->rows, extend);
378 389
379 return row->col + (x - row->c0); 390 return row->col + (x - row->c0);
380} 391}
381 392
382static mapcell * 393static mapcell *
383map_get_cell (DC__Map self, int x, int y) 394map_get_cell (mapgrid *self, int x, int y)
384{ 395{
385 return row_get_cell (map_get_row (self, y), x); 396 return row_get_cell (map_get_row (self, y), x);
386} 397}
387 398
388static void 399static void
389map_clear (DC__Map self) 400map_clear (mapgrid *self)
390{ 401{
391 int r; 402 int r;
392 403
393 for (r = 0; r < self->rows; r++) 404 for (r = 0; r < self->rows; r++)
394 Safefree (self->row[r].col); 405 Safefree (self->row[r].col);
412 (cell)->flags = 0; \ 423 (cell)->flags = 0; \
413 (cell)->player = 0; \ 424 (cell)->player = 0; \
414 } while (0) 425 } while (0)
415 426
416static void 427static void
417map_blank (DC__Map self, int x0, int y0, int w, int h) 428map_blank (mapgrid *self, int x0, int y0, int w, int h)
418{ 429{
419 int x, y; 430 int x, y;
420 maprow *row; 431 maprow *row;
421 mapcell *cell; 432 mapcell *cell;
422 433
439 CELL_CLEAR (cell); 450 CELL_CLEAR (cell);
440 } 451 }
441 } 452 }
442} 453}
443 454
444typedef struct { 455struct smooth_key
456{
445 tileid tile; 457 tileid tile;
446 uint8_t x, y, level; 458 uint8_t x, y, level;
447} smooth_key; 459
460 bool operator == (const smooth_key &o) const
461 {
462 return tile == o.tile && x == o.x && y == o.y && level == o.level;
463 }
464};
465
466typedef ska::flat_hash_map<smooth_key, IV> smooth_hash;
467
468namespace std {
469 template <>
470 struct hash<smooth_key>
471 {
472 size_t operator () (const smooth_key &v) const
473 {
474 return v.tile + (v.x << 8) + (v.y << 16) + (v.level << 24);
475 }
476 };
477}
448 478
449static void 479static void
450smooth_or_bits (HV *hv, smooth_key *key, IV bits) 480smooth_or_bits (smooth_hash &h, smooth_key &key, IV bits)
451{ 481{
452 SV **sv = hv_fetch (hv, (char *)key, sizeof (*key), 1); 482 auto &&it = h.find (key);
453 483
454 if (SvIOK (*sv)) 484 if (it == h.end ())
455 SvIV_set (*sv, SvIVX (*sv) | bits); 485 h.insert (std::make_pair (key, bits));
456 else 486 else
457 sv_setiv (*sv, bits); 487 it->second |= bits;
458} 488}
459 489
460static void 490static void
461music_finished (void) 491music_finished (void)
462{ 492{
481 ev.data2 = 0; 511 ev.data2 = 0;
482 512
483 SDL_PushEvent ((SDL_Event *)&ev); 513 SDL_PushEvent ((SDL_Event *)&ev);
484} 514}
485 515
516// approximately divide by 255
486static unsigned int 517static unsigned int
487div255 (unsigned int n) 518div255 (unsigned int n)
488{ 519{
489 return (n + (n >> 8)) >> 8; 520 return (n + (n >> 8)) >> 8;
490} 521}
1083 OUTPUT: 1114 OUTPUT:
1084 RETVAL 1115 RETVAL
1085 1116
1086int 1117int
1087Mix_OpenAudio (int frequency = 44100, int format = MIX_DEFAULT_FORMAT, int channels = 2, int chunksize = 4096) 1118Mix_OpenAudio (int frequency = 44100, int format = MIX_DEFAULT_FORMAT, int channels = 2, int chunksize = 4096)
1088 POSTCALL: 1119 POSTCALL:
1089 Mix_HookMusicFinished (music_finished); 1120 Mix_HookMusicFinished (music_finished);
1090 Mix_ChannelFinished (channel_finished); 1121 Mix_ChannelFinished (channel_finished);
1091 1122
1092void 1123void
1093Mix_QuerySpec () 1124Mix_QuerySpec ()
1870PROTOTYPES: DISABLE 1901PROTOTYPES: DISABLE
1871 1902
1872DC::Map 1903DC::Map
1873new (SV *klass) 1904new (SV *klass)
1874 CODE: 1905 CODE:
1875 New (0, RETVAL, 1, struct map); 1906 New (0, RETVAL, 1, mapgrid);
1876 RETVAL->x = 0; 1907 RETVAL->x = 0;
1877 RETVAL->y = 0; 1908 RETVAL->y = 0;
1878 RETVAL->w = 0; 1909 RETVAL->w = 0;
1879 RETVAL->h = 0; 1910 RETVAL->h = 0;
1880 RETVAL->ox = 0; 1911 RETVAL->ox = 0;
1917 1948
1918void 1949void
1919set_smooth (DC::Map self, int face, int smooth, int level) 1950set_smooth (DC::Map self, int face, int smooth, int level)
1920 CODE: 1951 CODE:
1921{ 1952{
1922 tileid texid; 1953 tileid texid;
1923 maptex *tex; 1954 maptex *tex;
1924 1955
1925 if (face < 0 || face >= self->faces) 1956 if (face < 0 || face >= self->faces)
1926 return; 1957 return;
1927 1958
1928 if (smooth < 0 || smooth >= self->faces) 1959 if (smooth < 0 || smooth >= self->faces)
1929 return; 1960 return;
1930 1961
1931 texid = self->face2tile [face]; 1962 texid = self->face2tile [face];
1932 1963
1933 if (!texid) 1964 if (!texid)
1934 return; 1965 return;
1935 1966
1936 tex = self->tex + texid; 1967 tex = self->tex + texid;
1969} 2000}
1970 2001
1971void 2002void
1972expire_textures (DC::Map self, int texid, int count) 2003expire_textures (DC::Map self, int texid, int count)
1973 PPCODE: 2004 PPCODE:
1974 for (; texid < self->texs && count; ++texid, --count) 2005 for (; texid < self->texs && count; ++texid, --count)
1975 { 2006 {
1976 maptex *tex = self->tex + texid; 2007 maptex *tex = self->tex + texid;
1977 2008
1978 if (tex->name) 2009 if (tex->name)
1979 { 2010 {
2181 | (b << 16) 2212 | (b << 16)
2182 | (a << 24); 2213 | (a << 24);
2183 } 2214 }
2184 } 2215 }
2185 2216
2186 RETVAL = map_sv; 2217 RETVAL = map_sv;
2187} 2218}
2188 OUTPUT: 2219 OUTPUT:
2189 RETVAL 2220 RETVAL
2190 2221
2191void 2222void
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) 2223draw (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: 2224 CODE:
2194{ 2225{
2195 int x, y, z; 2226 int x, y, z;
2196 2227
2197 HV *smooth = (HV *)sv_2mortal ((SV *)newHV ());
2198 uint32_t smooth_level[256 / 32]; // one bit for every possible smooth level 2228 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) 2229 static uint8_t smooth_max[256][256]; // egad, fast and wasteful on memory (64k)
2200 smooth_key skey; 2230 smooth_key skey;
2201 int pl_x, pl_y; 2231 int pl_x, pl_y;
2202 maptex pl_tex; 2232 maptex pl_tex;
2251 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 2281 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
2252 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); 2282 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
2253 2283
2254 for (z = 0; z <= 2; z++) 2284 for (z = 0; z <= 2; z++)
2255 { 2285 {
2286 smooth_hash smooth;
2256 memset (smooth_level, 0, sizeof (smooth_level)); 2287 memset (smooth_level, 0, sizeof (smooth_level));
2257 key.texname = -1; 2288 key.texname = -1;
2258 2289
2259 for (y = 0; y < sh; y++) 2290 for (y = 0; y < sh; y++)
2260 if (0 <= y + my && y + my < self->rows) 2291 if (0 <= y + my && y + my < self->rows)
2321 2352
2322 // corners: 1 ┛· 2 ·┗ 4 ·· 8 ·· 2353 // corners: 1 ┛· 2 ·┗ 4 ·· 8 ··
2323 // ·· ·· ·┏ ┓· 2354 // ·· ·· ·┏ ┓·
2324 2355
2325 // full tile 2356 // full tile
2326 skey.x = x + 1; skey.y = y + 1; smooth_or_bits (smooth, &skey, 0x1000); 2357 skey.x = x + 1; skey.y = y + 1; smooth_or_bits (smooth, skey, 0x1000);
2327 2358
2328 // borders 2359 // borders
2329 skey.x = x + 2; skey.y = y + 1; smooth_or_bits (smooth, &skey, 0x0091); 2360 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); 2361 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); 2362 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); 2363 skey.x = x + 1; skey.y = y ; smooth_or_bits (smooth, skey, 0x00c8);
2333 2364
2334 // corners 2365 // corners
2335 skey.x = x + 2; skey.y = y + 2; smooth_or_bits (smooth, &skey, 0x0100); 2366 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); 2367 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); 2368 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); 2369 skey.x = x + 2; skey.y = y ; smooth_or_bits (smooth, skey, 0x0800);
2339 } 2370 }
2340 } 2371 }
2341 2372
2342 if (expect_false (z == 2) && expect_false (cell->flags)) 2373 if (expect_false (z == 2) && expect_false (cell->flags))
2343 { 2374 {
2380 if (smask & (((uint32_t)1) << b)) 2411 if (smask & (((uint32_t)1) << b))
2381 { 2412 {
2382 int level = (w << 5) | b; 2413 int level = (w << 5) | b;
2383 HE *he; 2414 HE *he;
2384 2415
2385 hv_iterinit (smooth); 2416 for (auto &&it = smooth.begin (); it != smooth.end (); ++it)
2386 while ((he = hv_iternext (smooth)))
2387 { 2417 {
2388 smooth_key *skey = (smooth_key *)HeKEY (he); 2418 smooth_key &skey = it->first;
2389 IV bits = SvIVX (HeVAL (he)); 2419 IV bits = it->second;
2390 2420
2391 if (!(bits & 0x1000) 2421 if (!(bits & 0x1000)
2392 && skey->level == level 2422 && skey.level == level
2393 && level > smooth_max [skey->x][skey->y]) 2423 && level > smooth_max [skey.x][skey.y])
2394 { 2424 {
2395 maptex tex = self->tex [skey->tile]; 2425 maptex tex = self->tex [skey.tile];
2396 int px = (((int)skey->x) - 1) * Tw; 2426 int px = (((int)skey.x) - 1) * Tw;
2397 int py = (((int)skey->y) - 1) * Th; 2427 int py = (((int)skey.y) - 1) * Th;
2398 int border = bits & 15; 2428 int border = bits & 15;
2399 int corner = (bits >> 8) & ~(bits >> 4) & 15; 2429 int corner = (bits >> 8) & ~(bits >> 4) & 15;
2400 float dx = tex.s * .0625f; // 16 images/row 2430 float dx = tex.s * .0625f; // 16 images/row
2401 float dy = tex.t * .5f ; // 2 images/column 2431 float dy = tex.t * .5f ; // 2 images/column
2402 2432
2404 { 2434 {
2405 // this time avoiding texture state changes 2435 // this time avoiding texture state changes
2406 // save gobs of state changes. 2436 // save gobs of state changes.
2407 if (key.texname != tex.name) 2437 if (key.texname != tex.name)
2408 { 2438 {
2409 self->tex [skey->tile].unused = 0; 2439 self->tex [skey.tile].unused = 0;
2410 2440
2411 glEnd (); 2441 glEnd ();
2412 glBindTexture (GL_TEXTURE_2D, key.texname = tex.name); 2442 glBindTexture (GL_TEXTURE_2D, key.texname = tex.name);
2413 glBegin (GL_QUADS); 2443 glBegin (GL_QUADS);
2414 } 2444 }
2440 2470
2441 glEnd (); 2471 glEnd ();
2442 glDisable (GL_TEXTURE_2D); 2472 glDisable (GL_TEXTURE_2D);
2443 key.texname = -1; 2473 key.texname = -1;
2444 } 2474 }
2445
2446 hv_clear (smooth);
2447 } 2475 }
2448 2476
2449 if (pl_tex.name) 2477 if (pl_tex.name)
2450 { 2478 {
2451 maptex tex = pl_tex; 2479 maptex tex = pl_tex;
2720 else 2748 else
2721 *data++ = 0; 2749 *data++ = 0;
2722 } 2750 }
2723 } 2751 }
2724 2752
2725 /* if size is w*h + 5 then no data has been found */ 2753 /* if size is w*h + 5 then no data has been found */
2726 if (data - (uint8_t *)SvPVX (data_sv) != w * h + 5) 2754 if (data - (uint8_t *)SvPVX (data_sv) != w * h + 5)
2727 { 2755 {
2728 SvPOK_only (data_sv); 2756 SvPOK_only (data_sv);
2729 SvCUR_set (data_sv, data - (uint8_t *)SvPVX (data_sv)); 2757 SvCUR_set (data_sv, data - (uint8_t *)SvPVX (data_sv));
2730 } 2758 }
2731 2759
2732 RETVAL = data_sv; 2760 RETVAL = data_sv;
2733} 2761}
2734 OUTPUT: 2762 OUTPUT:
2735 RETVAL 2763 RETVAL
2736 2764
2737void 2765void
3427 3455
3428void 3456void
3429find_widget (SV *self, NV x, NV y) 3457find_widget (SV *self, NV x, NV y)
3430 PPCODE: 3458 PPCODE:
3431{ 3459{
3432 if (within_widget (self, x, y)) 3460 if (within_widget (self, x, y))
3433 XPUSHs (self); 3461 XPUSHs (self);
3434} 3462}
3435 3463
3436BOOT: 3464BOOT:
3437{ 3465{
3445 3473
3446void 3474void
3447draw (SV *self) 3475draw (SV *self)
3448 CODE: 3476 CODE:
3449{ 3477{
3450 HV *hv; 3478 HV *hv;
3451 SV **svp; 3479 SV **svp;
3452 NV x, y, w, h; 3480 NV x, y, w, h;
3453 SV *draw_x_sv = GvSV (draw_x_gv); 3481 SV *draw_x_sv = GvSV (draw_x_gv);
3454 SV *draw_y_sv = GvSV (draw_y_gv); 3482 SV *draw_y_sv = GvSV (draw_y_gv);
3455 SV *draw_w_sv = GvSV (draw_w_gv); 3483 SV *draw_w_sv = GvSV (draw_w_gv);
3456 SV *draw_h_sv = GvSV (draw_h_gv); 3484 SV *draw_h_sv = GvSV (draw_h_gv);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines