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.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{
1871PROTOTYPES: DISABLE 1901PROTOTYPES: DISABLE
1872 1902
1873DC::Map 1903DC::Map
1874new (SV *klass) 1904new (SV *klass)
1875 CODE: 1905 CODE:
1876 New (0, RETVAL, 1, struct map); 1906 New (0, RETVAL, 1, mapgrid);
1877 RETVAL->x = 0; 1907 RETVAL->x = 0;
1878 RETVAL->y = 0; 1908 RETVAL->y = 0;
1879 RETVAL->w = 0; 1909 RETVAL->w = 0;
1880 RETVAL->h = 0; 1910 RETVAL->h = 0;
1881 RETVAL->ox = 0; 1911 RETVAL->ox = 0;
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) 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)
2194 CODE: 2224 CODE:
2195{ 2225{
2196 int x, y, z; 2226 int x, y, z;
2197 2227
2198 HV *smooth = (HV *)sv_2mortal ((SV *)newHV ());
2199 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
2200 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)
2201 smooth_key skey; 2230 smooth_key skey;
2202 int pl_x, pl_y; 2231 int pl_x, pl_y;
2203 maptex pl_tex; 2232 maptex pl_tex;
2252 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 2281 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
2253 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); 2282 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
2254 2283
2255 for (z = 0; z <= 2; z++) 2284 for (z = 0; z <= 2; z++)
2256 { 2285 {
2286 smooth_hash smooth;
2257 memset (smooth_level, 0, sizeof (smooth_level)); 2287 memset (smooth_level, 0, sizeof (smooth_level));
2258 key.texname = -1; 2288 key.texname = -1;
2259 2289
2260 for (y = 0; y < sh; y++) 2290 for (y = 0; y < sh; y++)
2261 if (0 <= y + my && y + my < self->rows) 2291 if (0 <= y + my && y + my < self->rows)
2322 2352
2323 // corners: 1 ┛· 2 ·┗ 4 ·· 8 ·· 2353 // corners: 1 ┛· 2 ·┗ 4 ·· 8 ··
2324 // ·· ·· ·┏ ┓· 2354 // ·· ·· ·┏ ┓·
2325 2355
2326 // full tile 2356 // full tile
2327 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);
2328 2358
2329 // borders 2359 // borders
2330 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);
2331 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);
2332 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);
2333 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);
2334 2364
2335 // corners 2365 // corners
2336 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);
2337 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);
2338 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);
2339 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);
2340 } 2370 }
2341 } 2371 }
2342 2372
2343 if (expect_false (z == 2) && expect_false (cell->flags)) 2373 if (expect_false (z == 2) && expect_false (cell->flags))
2344 { 2374 {
2381 if (smask & (((uint32_t)1) << b)) 2411 if (smask & (((uint32_t)1) << b))
2382 { 2412 {
2383 int level = (w << 5) | b; 2413 int level = (w << 5) | b;
2384 HE *he; 2414 HE *he;
2385 2415
2386 hv_iterinit (smooth); 2416 for (auto &&it = smooth.begin (); it != smooth.end (); ++it)
2387 while ((he = hv_iternext (smooth)))
2388 { 2417 {
2389 smooth_key *skey = (smooth_key *)HeKEY (he); 2418 smooth_key &skey = it->first;
2390 IV bits = SvIVX (HeVAL (he)); 2419 IV bits = it->second;
2391 2420
2392 if (!(bits & 0x1000) 2421 if (!(bits & 0x1000)
2393 && skey->level == level 2422 && skey.level == level
2394 && level > smooth_max [skey->x][skey->y]) 2423 && level > smooth_max [skey.x][skey.y])
2395 { 2424 {
2396 maptex tex = self->tex [skey->tile]; 2425 maptex tex = self->tex [skey.tile];
2397 int px = (((int)skey->x) - 1) * Tw; 2426 int px = (((int)skey.x) - 1) * Tw;
2398 int py = (((int)skey->y) - 1) * Th; 2427 int py = (((int)skey.y) - 1) * Th;
2399 int border = bits & 15; 2428 int border = bits & 15;
2400 int corner = (bits >> 8) & ~(bits >> 4) & 15; 2429 int corner = (bits >> 8) & ~(bits >> 4) & 15;
2401 float dx = tex.s * .0625f; // 16 images/row 2430 float dx = tex.s * .0625f; // 16 images/row
2402 float dy = tex.t * .5f ; // 2 images/column 2431 float dy = tex.t * .5f ; // 2 images/column
2403 2432
2405 { 2434 {
2406 // this time avoiding texture state changes 2435 // this time avoiding texture state changes
2407 // save gobs of state changes. 2436 // save gobs of state changes.
2408 if (key.texname != tex.name) 2437 if (key.texname != tex.name)
2409 { 2438 {
2410 self->tex [skey->tile].unused = 0; 2439 self->tex [skey.tile].unused = 0;
2411 2440
2412 glEnd (); 2441 glEnd ();
2413 glBindTexture (GL_TEXTURE_2D, key.texname = tex.name); 2442 glBindTexture (GL_TEXTURE_2D, key.texname = tex.name);
2414 glBegin (GL_QUADS); 2443 glBegin (GL_QUADS);
2415 } 2444 }
2441 2470
2442 glEnd (); 2471 glEnd ();
2443 glDisable (GL_TEXTURE_2D); 2472 glDisable (GL_TEXTURE_2D);
2444 key.texname = -1; 2473 key.texname = -1;
2445 } 2474 }
2446
2447 hv_clear (smooth);
2448 } 2475 }
2449 2476
2450 if (pl_tex.name) 2477 if (pl_tex.name)
2451 { 2478 {
2452 maptex tex = pl_tex; 2479 maptex tex = pl_tex;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines