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.329 by root, Sun Nov 18 13:07:51 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>
195 } 200 }
196 201
197 return 0; 202 return 0;
198} 203}
199 204
200typedef struct cf_layout { 205struct cf_layout {
201 PangoLayout *pl; 206 PangoLayout *pl;
202 float r, g, b, a; // default color for rgba mode 207 float r, g, b, a; // default color for rgba mode
203 int base_height; 208 int base_height;
204 DC__Font font; 209 DC__Font font;
205 rc_t *rc; 210 rc_t rc;
206} *DC__Layout; 211};
212
213typedef cf_layout *DC__Layout;
207 214
208static DC__Font default_font; 215static DC__Font default_font;
209static PangoContext *opengl_context; 216static PangoContext *opengl_context;
210static PangoFontMap *opengl_fontmap; 217static PangoFontMap *opengl_fontmap;
211 218
252} 259}
253 260
254typedef uint16_t tileid; 261typedef uint16_t tileid;
255typedef uint16_t faceid; 262typedef uint16_t faceid;
256 263
257typedef struct { 264struct maptex
265{
258 GLuint name; 266 GLuint name;
259 int w, h; 267 int w, h;
260 float s, t; 268 float s, t;
261 uint8_t r, g, b, a; 269 uint8_t r, g, b, a;
262 tileid smoothtile; 270 tileid smoothtile;
263 uint8_t smoothlevel; 271 uint8_t smoothlevel;
264 uint8_t unused; /* set to zero on use */ 272 uint8_t unused; /* set to zero on use */
265} maptex; 273};
266 274
267typedef struct { 275struct mapcell
276{
268 uint32_t player; 277 uint32_t player;
269 tileid tile[3]; 278 tileid tile[3];
270 uint16_t darkness; 279 uint16_t darkness;
271 uint8_t stat_width, stat_hp, flags, smoothmax; 280 uint8_t stat_width, stat_hp, flags, smoothmax;
272} mapcell; 281};
273 282
274typedef struct { 283struct maprow
284{
275 int32_t c0, c1; 285 int32_t c0, c1;
276 mapcell *col; 286 mapcell *col;
277} maprow; 287};
278 288
279typedef struct map { 289struct mapgrid {
280 int x, y, w, h; 290 int x, y, w, h;
281 int ox, oy; /* offset to virtual global coordinate system */ 291 int ox, oy; /* offset to virtual global coordinate system */
282 int faces; tileid *face2tile; // [faceid] 292 int faces; tileid *face2tile; // [faceid]
283 int texs; maptex *tex; // [tileid] 293 int texs; maptex *tex; // [tileid]
284 294
285 int32_t rows; 295 int32_t rows;
286 maprow *row; 296 maprow *row;
287} *DC__Map; 297};
288 298
289static char * 299typedef mapgrid *DC__Map;
300
301template<typename T>
302static void
290prepend (char *ptr, int sze, int inc) 303prepend (T *&ptr, int sze, int inc)
291{ 304{
292 char *p; 305 T *p;
293 306
294 New (0, p, sze + inc, char); 307 New (0, p, sze + inc, T);
295 Zero (p, inc, char); 308 Zero (p, inc, T);
296 Move (ptr, p + inc, sze, char); 309 Move (ptr, p + inc, sze, T);
297 Safefree (ptr); 310 Safefree (ptr);
298 311
299 return p; 312 ptr = p;
300} 313}
301 314
302static char * 315template<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 316static void
317append (T *&ptr, int sze, int inc)
318{
319 Renew (ptr, sze + inc, T);
320 Zero (ptr + sze, inc, T);
321}
322
323static void
315need_facenum (struct map *self, faceid face) 324need_facenum (struct mapgrid *self, faceid face)
316{ 325{
317 while (self->faces <= face) 326 while (self->faces <= face)
318 { 327 {
319 Append (tileid, self->face2tile, self->faces, self->faces); 328 append (self->face2tile, self->faces, self->faces);
320 self->faces *= 2; 329 self->faces *= 2;
321 } 330 }
322} 331}
323 332
324static void 333static void
325need_texid (struct map *self, int texid) 334need_texid (struct mapgrid *self, int texid)
326{ 335{
327 while (self->texs <= texid) 336 while (self->texs <= texid)
328 { 337 {
329 Append (maptex, self->tex, self->texs, self->texs); 338 append (self->tex, self->texs, self->texs);
330 self->texs *= 2; 339 self->texs *= 2;
331 } 340 }
332} 341}
333 342
334static maprow * 343static maprow *
335map_get_row (DC__Map self, int y) 344map_get_row (mapgrid *self, int y)
336{ 345{
337 if (0 > y) 346 if (0 > y)
338 { 347 {
339 int extend = - y + MAP_EXTEND_Y; 348 int extend = - y + MAP_EXTEND_Y;
340 Prepend (maprow, self->row, self->rows, extend); 349 prepend (self->row, self->rows, extend);
341 350
342 self->rows += extend; 351 self->rows += extend;
343 self->y += extend; 352 self->y += extend;
344 y += extend; 353 y += extend;
345 } 354 }
346 else if (y >= self->rows) 355 else if (y >= self->rows)
347 { 356 {
348 int extend = y - self->rows + MAP_EXTEND_Y; 357 int extend = y - self->rows + MAP_EXTEND_Y;
349 Append (maprow, self->row, self->rows, extend); 358 append (self->row, self->rows, extend);
350 self->rows += extend; 359 self->rows += extend;
351 } 360 }
352 361
353 return self->row + y; 362 return self->row + y;
354} 363}
364 } 373 }
365 374
366 if (row->c0 > x) 375 if (row->c0 > x)
367 { 376 {
368 int extend = row->c0 - x + MAP_EXTEND_X; 377 int extend = row->c0 - x + MAP_EXTEND_X;
369 Prepend (mapcell, row->col, row->c1 - row->c0, extend); 378 prepend (row->col, row->c1 - row->c0, extend);
370 row->c0 -= extend; 379 row->c0 -= extend;
371 } 380 }
372 else if (x >= row->c1) 381 else if (x >= row->c1)
373 { 382 {
374 int extend = x - row->c1 + MAP_EXTEND_X; 383 int extend = x - row->c1 + MAP_EXTEND_X;
375 Append (mapcell, row->col, row->c1 - row->c0, extend); 384 append (row->col, row->c1 - row->c0, extend);
376 row->c1 += extend; 385 row->c1 += extend;
377 } 386 }
378 387
379 return row->col + (x - row->c0); 388 return row->col + (x - row->c0);
380} 389}
381 390
382static mapcell * 391static mapcell *
383map_get_cell (DC__Map self, int x, int y) 392map_get_cell (mapgrid *self, int x, int y)
384{ 393{
385 return row_get_cell (map_get_row (self, y), x); 394 return row_get_cell (map_get_row (self, y), x);
386} 395}
387 396
388static void 397static void
389map_clear (DC__Map self) 398map_clear (mapgrid *self)
390{ 399{
391 int r; 400 int r;
392 401
393 for (r = 0; r < self->rows; r++) 402 for (r = 0; r < self->rows; r++)
394 Safefree (self->row[r].col); 403 Safefree (self->row[r].col);
412 (cell)->flags = 0; \ 421 (cell)->flags = 0; \
413 (cell)->player = 0; \ 422 (cell)->player = 0; \
414 } while (0) 423 } while (0)
415 424
416static void 425static void
417map_blank (DC__Map self, int x0, int y0, int w, int h) 426map_blank (mapgrid *self, int x0, int y0, int w, int h)
418{ 427{
419 int x, y; 428 int x, y;
420 maprow *row; 429 maprow *row;
421 mapcell *cell; 430 mapcell *cell;
422 431
439 CELL_CLEAR (cell); 448 CELL_CLEAR (cell);
440 } 449 }
441 } 450 }
442} 451}
443 452
444typedef struct { 453struct smooth_key
454{
445 tileid tile; 455 tileid tile;
446 uint8_t x, y, level; 456 uint8_t x, y, level;
447} smooth_key; 457
458 bool operator == (const smooth_key &o) const
459 {
460 return tile == o.tile && x == o.x && y == o.y && level == o.level;
461 }
462};
463
464typedef ska::flat_hash_map<smooth_key, IV> smooth_hash;
465
466namespace std {
467 template <>
468 struct hash<smooth_key>
469 {
470 size_t operator () (const smooth_key &v) const
471 {
472 return v.tile + (v.x << 8) + (v.y << 16) + (v.level << 24);
473 }
474 };
475}
448 476
449static void 477static void
450smooth_or_bits (HV *hv, smooth_key *key, IV bits) 478smooth_or_bits (smooth_hash &h, smooth_key &key, IV bits)
451{ 479{
452 SV **sv = hv_fetch (hv, (char *)key, sizeof (*key), 1); 480 auto &&it = h.find (key);
453 481
454 if (SvIOK (*sv)) 482 if (it == h.end ())
455 SvIV_set (*sv, SvIVX (*sv) | bits); 483 h.insert (std::make_pair (key, bits));
456 else 484 else
457 sv_setiv (*sv, bits); 485 it->second |= bits;
458} 486}
459 487
460static void 488static void
461music_finished (void) 489music_finished (void)
462{ 490{
481 ev.data2 = 0; 509 ev.data2 = 0;
482 510
483 SDL_PushEvent ((SDL_Event *)&ev); 511 SDL_PushEvent ((SDL_Event *)&ev);
484} 512}
485 513
514// approximately divide by 255
486static unsigned int 515static unsigned int
487div255 (unsigned int n) 516div255 (unsigned int n)
488{ 517{
489 return (n + (n >> 8)) >> 8; 518 return (n + (n >> 8)) >> 8;
490} 519}
1083 OUTPUT: 1112 OUTPUT:
1084 RETVAL 1113 RETVAL
1085 1114
1086int 1115int
1087Mix_OpenAudio (int frequency = 44100, int format = MIX_DEFAULT_FORMAT, int channels = 2, int chunksize = 4096) 1116Mix_OpenAudio (int frequency = 44100, int format = MIX_DEFAULT_FORMAT, int channels = 2, int chunksize = 4096)
1088 POSTCALL: 1117 POSTCALL:
1089 Mix_HookMusicFinished (music_finished); 1118 Mix_HookMusicFinished (music_finished);
1090 Mix_ChannelFinished (channel_finished); 1119 Mix_ChannelFinished (channel_finished);
1091 1120
1092void 1121void
1093Mix_QuerySpec () 1122Mix_QuerySpec ()
1337 tc_restore (); 1366 tc_restore ();
1338 1367
1339DC::Layout 1368DC::Layout
1340new (SV *klass) 1369new (SV *klass)
1341 CODE: 1370 CODE:
1342 New (0, RETVAL, 1, struct cf_layout); 1371 RETVAL = new cf_layout;
1343 1372
1344 RETVAL->pl = pango_layout_new (opengl_context); 1373 RETVAL->pl = pango_layout_new (opengl_context);
1345 RETVAL->r = 1.; 1374 RETVAL->r = 1.;
1346 RETVAL->g = 1.; 1375 RETVAL->g = 1.;
1347 RETVAL->b = 1.; 1376 RETVAL->b = 1.;
1348 RETVAL->a = 1.; 1377 RETVAL->a = 1.;
1349 RETVAL->base_height = MIN_FONT_HEIGHT; 1378 RETVAL->base_height = MIN_FONT_HEIGHT;
1350 RETVAL->font = 0; 1379 RETVAL->font = 0;
1351 RETVAL->rc = rc_alloc ();
1352 1380
1353 pango_layout_set_wrap (RETVAL->pl, PANGO_WRAP_WORD_CHAR); 1381 pango_layout_set_wrap (RETVAL->pl, PANGO_WRAP_WORD_CHAR);
1354 layout_update_font (RETVAL); 1382 layout_update_font (RETVAL);
1355 OUTPUT: 1383 OUTPUT:
1356 RETVAL 1384 RETVAL
1357 1385
1358void 1386void
1359DESTROY (DC::Layout self) 1387DESTROY (DC::Layout self)
1360 CODE: 1388 CODE:
1361 g_object_unref (self->pl); 1389 g_object_unref (self->pl);
1362 rc_free (self->rc);
1363 Safefree (self); 1390 delete self;
1364 1391
1365void 1392void
1366set_text (DC::Layout self, SV *text_) 1393set_text (DC::Layout self, SV *text_)
1367 CODE: 1394 CODE:
1368{ 1395{
1622} 1649}
1623 1650
1624void 1651void
1625render (DC::Layout self, float x, float y, int flags = 0) 1652render (DC::Layout self, float x, float y, int flags = 0)
1626 CODE: 1653 CODE:
1627 rc_clear (self->rc); 1654 self->rc.clear ();
1628 pango_opengl_render_layout_subpixel ( 1655 pango_opengl_render_layout_subpixel (
1629 self->pl, 1656 self->pl,
1630 self->rc, 1657 &self->rc,
1631 x * PANGO_SCALE, y * PANGO_SCALE, 1658 x * PANGO_SCALE, y * PANGO_SCALE,
1632 self->r, self->g, self->b, self->a, 1659 self->r, self->g, self->b, self->a,
1633 flags 1660 flags
1634 ); 1661 );
1635 // we assume that context_change actually clears/frees stuff 1662 // we assume that context_change actually clears/frees stuff
1646 gl_BlendFuncSeparate (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, 1673 gl_BlendFuncSeparate (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA,
1647 GL_ONE , GL_ONE_MINUS_SRC_ALPHA); 1674 GL_ONE , GL_ONE_MINUS_SRC_ALPHA);
1648 glEnable (GL_ALPHA_TEST); 1675 glEnable (GL_ALPHA_TEST);
1649 glAlphaFunc (GL_GREATER, 7.f / 255.f); 1676 glAlphaFunc (GL_GREATER, 7.f / 255.f);
1650 1677
1651 rc_draw (self->rc); 1678 self->rc.draw ();
1652 1679
1653 glDisable (GL_ALPHA_TEST); 1680 glDisable (GL_ALPHA_TEST);
1654 glDisable (GL_BLEND); 1681 glDisable (GL_BLEND);
1655 glDisable (GL_TEXTURE_2D); 1682 glDisable (GL_TEXTURE_2D);
1656} 1683}
1870PROTOTYPES: DISABLE 1897PROTOTYPES: DISABLE
1871 1898
1872DC::Map 1899DC::Map
1873new (SV *klass) 1900new (SV *klass)
1874 CODE: 1901 CODE:
1875 New (0, RETVAL, 1, struct map); 1902 New (0, RETVAL, 1, mapgrid);
1876 RETVAL->x = 0; 1903 RETVAL->x = 0;
1877 RETVAL->y = 0; 1904 RETVAL->y = 0;
1878 RETVAL->w = 0; 1905 RETVAL->w = 0;
1879 RETVAL->h = 0; 1906 RETVAL->h = 0;
1880 RETVAL->ox = 0; 1907 RETVAL->ox = 0;
1917 1944
1918void 1945void
1919set_smooth (DC::Map self, int face, int smooth, int level) 1946set_smooth (DC::Map self, int face, int smooth, int level)
1920 CODE: 1947 CODE:
1921{ 1948{
1922 tileid texid; 1949 tileid texid;
1923 maptex *tex; 1950 maptex *tex;
1924 1951
1925 if (face < 0 || face >= self->faces) 1952 if (face < 0 || face >= self->faces)
1926 return; 1953 return;
1927 1954
1928 if (smooth < 0 || smooth >= self->faces) 1955 if (smooth < 0 || smooth >= self->faces)
1929 return; 1956 return;
1930 1957
1931 texid = self->face2tile [face]; 1958 texid = self->face2tile [face];
1932 1959
1933 if (!texid) 1960 if (!texid)
1934 return; 1961 return;
1935 1962
1936 tex = self->tex + texid; 1963 tex = self->tex + texid;
1969} 1996}
1970 1997
1971void 1998void
1972expire_textures (DC::Map self, int texid, int count) 1999expire_textures (DC::Map self, int texid, int count)
1973 PPCODE: 2000 PPCODE:
1974 for (; texid < self->texs && count; ++texid, --count) 2001 for (; texid < self->texs && count; ++texid, --count)
1975 { 2002 {
1976 maptex *tex = self->tex + texid; 2003 maptex *tex = self->tex + texid;
1977 2004
1978 if (tex->name) 2005 if (tex->name)
1979 { 2006 {
2026 self->ox += dx; self->x += dx; 2053 self->ox += dx; self->x += dx;
2027 self->oy += dy; self->y += dy; 2054 self->oy += dy; self->y += dy;
2028 2055
2029 while (self->y < 0) 2056 while (self->y < 0)
2030 { 2057 {
2031 Prepend (maprow, self->row, self->rows, MAP_EXTEND_Y); 2058 prepend (self->row, self->rows, MAP_EXTEND_Y);
2032 2059
2033 self->rows += MAP_EXTEND_Y; 2060 self->rows += MAP_EXTEND_Y;
2034 self->y += MAP_EXTEND_Y; 2061 self->y += MAP_EXTEND_Y;
2035 } 2062 }
2036} 2063}
2181 | (b << 16) 2208 | (b << 16)
2182 | (a << 24); 2209 | (a << 24);
2183 } 2210 }
2184 } 2211 }
2185 2212
2186 RETVAL = map_sv; 2213 RETVAL = map_sv;
2187} 2214}
2188 OUTPUT: 2215 OUTPUT:
2189 RETVAL 2216 RETVAL
2190 2217
2191void 2218void
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) 2219draw (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: 2220 CODE:
2194{ 2221{
2195 int x, y, z; 2222 int x, y, z;
2196 2223
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) 2224 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; 2225 int pl_x, pl_y;
2202 maptex pl_tex; 2226 maptex pl_tex;
2203 rc_t *rc = rc_alloc (); 2227 rc_t rc;
2204 rc_t *rc_ov = rc_alloc (); 2228 rc_t rc_ov;
2205 rc_key_t key; 2229 rc_key_t key;
2206 rc_array_t *arr; 2230 rc_t::array_t *arr;
2207 2231
2208 pl_tex.name = 0; 2232 pl_tex.name = 0;
2209 2233
2210 // that's current max. sorry. 2234 // that's current max. sorry.
2211 if (sw > 255) sw = 255; 2235 if (sw > 255) sw = 255;
2212 if (sh > 255) sh = 255; 2236 if (sh > 255) sh = 255;
2213
2214 // clear key, in case of extra padding
2215 memset (&skey, 0, sizeof (skey));
2216 2237
2217 memset (&key, 0, sizeof (key)); 2238 memset (&key, 0, sizeof (key));
2218 key.r = 255; 2239 key.r = 255;
2219 key.g = 255; 2240 key.g = 255;
2220 key.b = 255; 2241 key.b = 255;
2226 my += self->y; 2247 my += self->y;
2227 2248
2228 // first pass: determine smooth_max 2249 // first pass: determine smooth_max
2229 // rather ugly, if you ask me 2250 // rather ugly, if you ask me
2230 // could also be stored inside mapcell and updated on change 2251 // could also be stored inside mapcell and updated on change
2231 memset (smooth_max, 0, sizeof (smooth_max)); 2252 memset (smooth_max, 0, sizeof (smooth_max[0]) * (sh + 1));
2232 2253
2233 for (y = 0; y < sh; y++) 2254 for (y = 0; y < sh; y++)
2234 if (0 <= y + my && y + my < self->rows) 2255 if (0 <= y + my && y + my < self->rows)
2235 { 2256 {
2236 maprow *row = self->row + (y + my); 2257 maprow *row = self->row + (y + my);
2251 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 2272 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
2252 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); 2273 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
2253 2274
2254 for (z = 0; z <= 2; z++) 2275 for (z = 0; z <= 2; z++)
2255 { 2276 {
2256 memset (smooth_level, 0, sizeof (smooth_level)); 2277 std::bitset<256> smooth_level; // one bit for every possible smooth level
2278 smooth_key skey;
2279 smooth_hash smooth;
2257 key.texname = -1; 2280 key.texname = -1;
2258 2281
2259 for (y = 0; y < sh; y++) 2282 for (y = 0; y < sh; y++)
2260 if (0 <= y + my && y + my < self->rows) 2283 if (0 <= y + my && y + my < self->rows)
2261 { 2284 {
2278 2301
2279 if (!tex.name) 2302 if (!tex.name)
2280 tex = self->tex [TEXID_NOFACE]; /* missing, replace by noface */ 2303 tex = self->tex [TEXID_NOFACE]; /* missing, replace by noface */
2281 2304
2282 key.texname = tex.name; 2305 key.texname = tex.name;
2283 arr = rc_array (rc, &key); 2306 arr = &rc.array (key);
2284 } 2307 }
2285 2308
2286 px = (x + 1) * Th - tex.w; 2309 px = (x + 1) * Th - tex.w;
2287 py = (y + 1) * Tw - tex.h; 2310 py = (y + 1) * Tw - tex.h;
2288 2311
2292 pl_y = py; 2315 pl_y = py;
2293 pl_tex = tex; 2316 pl_tex = tex;
2294 continue; 2317 continue;
2295 } 2318 }
2296 2319
2297 rc_t2f_v3f (arr, 0 , 0 , px , py , 0); 2320 arr->t2f_v3f (0 , 0 , px , py , 0);
2298 rc_t2f_v3f (arr, 0 , tex.t, px , py + tex.h, 0); 2321 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); 2322 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); 2323 arr->t2f_v3f (tex.s, 0 , px + tex.w, py , 0);
2301 2324
2302 // update smooth hash 2325 // update smooth hash
2303 if (tex.smoothtile) 2326 if (tex.smoothtile)
2304 { 2327 {
2305 skey.tile = tex.smoothtile; 2328 skey.tile = tex.smoothtile;
2306 skey.level = tex.smoothlevel; 2329 skey.level = tex.smoothlevel;
2307 2330
2308 smooth_level [tex.smoothlevel >> 5] |= ((uint32_t)1) << (tex.smoothlevel & 31); 2331 smooth_level[tex.smoothlevel] = 1;
2309 2332
2310 // add bits to current tile and all neighbours. skey.x|y is 2333 // add bits to current tile and all neighbours. skey.x|y is
2311 // shifted +1|+1 so we always stay positive. 2334 // shifted +1|+1 so we always stay positive.
2312 2335
2313 // bits is ___n cccc CCCC bbbb 2336 // bits is ___n cccc CCCC bbbb
2321 2344
2322 // corners: 1 ┛· 2 ·┗ 4 ·· 8 ·· 2345 // corners: 1 ┛· 2 ·┗ 4 ·· 8 ··
2323 // ·· ·· ·┏ ┓· 2346 // ·· ·· ·┏ ┓·
2324 2347
2325 // full tile 2348 // full tile
2326 skey.x = x + 1; skey.y = y + 1; smooth_or_bits (smooth, &skey, 0x1000); 2349 skey.x = x + 1; skey.y = y + 1; smooth_or_bits (smooth, skey, 0x1000);
2327 2350
2328 // borders 2351 // borders
2329 skey.x = x + 2; skey.y = y + 1; smooth_or_bits (smooth, &skey, 0x0091); 2352 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); 2353 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); 2354 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); 2355 skey.x = x + 1; skey.y = y ; smooth_or_bits (smooth, skey, 0x00c8);
2333 2356
2334 // corners 2357 // corners
2335 skey.x = x + 2; skey.y = y + 2; smooth_or_bits (smooth, &skey, 0x0100); 2358 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); 2359 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); 2360 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); 2361 skey.x = x + 2; skey.y = y ; smooth_or_bits (smooth, skey, 0x0800);
2339 } 2362 }
2340 } 2363 }
2341 2364
2342 if (expect_false (z == 2) && expect_false (cell->flags)) 2365 if (expect_false (z == 2) && expect_false (cell->flags))
2343 { 2366 {
2344 // overlays such as the speech bubble, probably more to come 2367 // overlays such as the speech bubble, probably more to come
2345 if (cell->flags & 1) 2368 if (cell->flags & 1)
2346 { 2369 {
2347 rc_key_t key_ov = key; 2370 rc_key_t key_ov = key;
2348 maptex tex = self->tex [TEXID_SPEECH]; 2371 maptex tex = self->tex[TEXID_SPEECH];
2349 rc_array_t *arr;
2350 int px = x * Tw + Tw * 2 / 32; 2372 int px = x * Tw + Tw * 2 / 32;
2351 int py = y * Th - Th * 6 / 32; 2373 int py = y * Th - Th * 6 / 32;
2352 2374
2353 key_ov.texname = tex.name; 2375 key_ov.texname = tex.name;
2354 arr = rc_array (rc_ov, &key_ov); 2376 rc_t::array_t &arr = rc_ov.array (key_ov);
2355 2377
2356 rc_t2f_v3f (arr, 0 , 0 , px , py , 0); 2378 arr.t2f_v3f (0 , 0 , px , py , 0);
2357 rc_t2f_v3f (arr, 0 , tex.t, px , py + Th, 0); 2379 arr.t2f_v3f (0 , tex.t, px , py + Th, 0);
2358 rc_t2f_v3f (arr, tex.s, tex.t, px + Tw, py + Th, 0); 2380 arr.t2f_v3f (tex.s, tex.t, px + Tw, py + Th, 0);
2359 rc_t2f_v3f (arr, tex.s, 0 , px + Tw, py , 0); 2381 arr.t2f_v3f (tex.s, 0 , px + Tw, py , 0);
2360 } 2382 }
2361 } 2383 }
2362 } 2384 }
2363 } 2385 }
2364 2386
2365 rc_draw (rc); 2387 rc.draw ();
2366 rc_clear (rc); 2388 rc.clear ();
2367 2389
2368 // go through all smoothlevels, lowest to highest, then draw. 2390 // go through all smoothlevels, lowest to highest, then draw.
2369 // this is basically counting sort 2391 // this is basically counting sort
2370 { 2392 {
2371 int w, b; 2393 int w, b;
2372 2394
2373 glEnable (GL_TEXTURE_2D); 2395 glEnable (GL_TEXTURE_2D);
2374 glBegin (GL_QUADS); 2396 glBegin (GL_QUADS);
2375 for (w = 0; w < 256 / 32; ++w) 2397 for (int level = 0; level < smooth_level.size (); ++level)
2398 if (smooth_level[level])
2399 for (auto &&it = smooth.begin (); it != smooth.end (); ++it)
2376 { 2400 {
2377 uint32_t smask = smooth_level [w]; 2401 smooth_key &skey = it->first;
2378 if (smask) 2402 IV bits = it->second;
2379 for (b = 0; b < 32; ++b) 2403
2380 if (smask & (((uint32_t)1) << b)) 2404 if (!(bits & 0x1000)
2405 && skey.level == level
2406 && level > smooth_max [skey.x][skey.y])
2381 { 2407 {
2382 int level = (w << 5) | b; 2408 maptex tex = self->tex [skey.tile];
2409 int px = (((int)skey.x) - 1) * Tw;
2410 int py = (((int)skey.y) - 1) * Th;
2411 int border = bits & 15;
2412 int corner = (bits >> 8) & ~(bits >> 4) & 15;
2413 float dx = tex.s * .0625f; // 16 images/row
2414 float dy = tex.t * .5f ; // 2 images/column
2415
2383 HE *he; 2416 if (tex.name)
2384
2385 hv_iterinit (smooth);
2386 while ((he = hv_iternext (smooth)))
2387 { 2417 {
2388 smooth_key *skey = (smooth_key *)HeKEY (he); 2418 // this time avoiding texture state changes
2389 IV bits = SvIVX (HeVAL (he)); 2419 // save gobs of state changes.
2390 2420 if (key.texname != tex.name)
2391 if (!(bits & 0x1000)
2392 && skey->level == level
2393 && level > smooth_max [skey->x][skey->y])
2394 { 2421 {
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; 2422 self->tex [skey.tile].unused = 0;
2410 2423
2411 glEnd (); 2424 glEnd ();
2412 glBindTexture (GL_TEXTURE_2D, key.texname = tex.name); 2425 glBindTexture (GL_TEXTURE_2D, key.texname = tex.name);
2413 glBegin (GL_QUADS); 2426 glBegin (GL_QUADS);
2414 } 2427 }
2415 2428
2416 if (border) 2429 if (border)
2417 { 2430 {
2418 float ox = border * dx; 2431 float ox = border * dx;
2419 2432
2420 glTexCoord2f (ox , 0.f ); glVertex2i (px , py ); 2433 glTexCoord2f (ox , 0.f ); glVertex2i (px , py );
2421 glTexCoord2f (ox , dy ); glVertex2i (px , py + Th); 2434 glTexCoord2f (ox , dy ); glVertex2i (px , py + Th);
2422 glTexCoord2f (ox + dx, dy ); glVertex2i (px + Tw, py + Th); 2435 glTexCoord2f (ox + dx, dy ); glVertex2i (px + Tw, py + Th);
2423 glTexCoord2f (ox + dx, 0.f ); glVertex2i (px + Tw, py ); 2436 glTexCoord2f (ox + dx, 0.f ); glVertex2i (px + Tw, py );
2424 } 2437 }
2425 2438
2426 if (corner) 2439 if (corner)
2427 { 2440 {
2428 float ox = corner * dx; 2441 float ox = corner * dx;
2429 2442
2430 glTexCoord2f (ox , dy ); glVertex2i (px , py ); 2443 glTexCoord2f (ox , dy ); glVertex2i (px , py );
2431 glTexCoord2f (ox , dy * 2.f); glVertex2i (px , py + Th); 2444 glTexCoord2f (ox , dy * 2.f); glVertex2i (px , py + Th);
2432 glTexCoord2f (ox + dx, dy * 2.f); glVertex2i (px + Tw, py + Th); 2445 glTexCoord2f (ox + dx, dy * 2.f); glVertex2i (px + Tw, py + Th);
2433 glTexCoord2f (ox + dx, dy ); glVertex2i (px + Tw, py ); 2446 glTexCoord2f (ox + dx, dy ); glVertex2i (px + Tw, py );
2434 }
2435 }
2436 } 2447 }
2437 } 2448 }
2438 } 2449 }
2439 } 2450 }
2440 2451
2441 glEnd (); 2452 glEnd ();
2442 glDisable (GL_TEXTURE_2D); 2453 glDisable (GL_TEXTURE_2D);
2443 key.texname = -1; 2454 key.texname = -1;
2444 } 2455 }
2445
2446 hv_clear (smooth);
2447 } 2456 }
2448 2457
2449 if (pl_tex.name) 2458 if (pl_tex.name)
2450 { 2459 {
2451 maptex tex = pl_tex; 2460 maptex tex = pl_tex;
2452 int px = pl_x + sdx; 2461 int px = pl_x + sdx;
2453 int py = pl_y + sdy; 2462 int py = pl_y + sdy;
2454 2463
2455 key.texname = tex.name; 2464 key.texname = tex.name;
2456 arr = rc_array (rc, &key); 2465 rc_t::array_t &arr = rc.array (key);
2457 2466
2458 rc_t2f_v3f (arr, 0 , 0 , px , py , 0); 2467 arr.t2f_v3f (0 , 0 , px , py , 0);
2459 rc_t2f_v3f (arr, 0 , tex.t, px , py + tex.h, 0); 2468 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); 2469 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); 2470 arr.t2f_v3f (tex.s, 0 , px + tex.w, py , 0);
2462 2471
2463 rc_draw (rc); 2472 rc.draw ();
2464 } 2473 }
2465 2474
2466 rc_draw (rc_ov); 2475 rc_ov.draw ();
2467 rc_clear (rc_ov); 2476 rc_ov.clear ();
2468 2477
2469 glDisable (GL_BLEND); 2478 glDisable (GL_BLEND);
2470 rc_free (rc);
2471 rc_free (rc_ov);
2472 2479
2473 // top layer: overlays such as the health bar 2480 // top layer: overlays such as the health bar
2474 for (y = 0; y < sh; y++) 2481 for (y = 0; y < sh; y++)
2475 if (0 <= y + my && y + my < self->rows) 2482 if (0 <= y + my && y + my < self->rows)
2476 { 2483 {
2720 else 2727 else
2721 *data++ = 0; 2728 *data++ = 0;
2722 } 2729 }
2723 } 2730 }
2724 2731
2725 /* if size is w*h + 5 then no data has been found */ 2732 /* if size is w*h + 5 then no data has been found */
2726 if (data - (uint8_t *)SvPVX (data_sv) != w * h + 5) 2733 if (data - (uint8_t *)SvPVX (data_sv) != w * h + 5)
2727 { 2734 {
2728 SvPOK_only (data_sv); 2735 SvPOK_only (data_sv);
2729 SvCUR_set (data_sv, data - (uint8_t *)SvPVX (data_sv)); 2736 SvCUR_set (data_sv, data - (uint8_t *)SvPVX (data_sv));
2730 } 2737 }
2731 2738
2732 RETVAL = data_sv; 2739 RETVAL = data_sv;
2733} 2740}
2734 OUTPUT: 2741 OUTPUT:
2735 RETVAL 2742 RETVAL
2736 2743
2737void 2744void
3427 3434
3428void 3435void
3429find_widget (SV *self, NV x, NV y) 3436find_widget (SV *self, NV x, NV y)
3430 PPCODE: 3437 PPCODE:
3431{ 3438{
3432 if (within_widget (self, x, y)) 3439 if (within_widget (self, x, y))
3433 XPUSHs (self); 3440 XPUSHs (self);
3434} 3441}
3435 3442
3436BOOT: 3443BOOT:
3437{ 3444{
3445 3452
3446void 3453void
3447draw (SV *self) 3454draw (SV *self)
3448 CODE: 3455 CODE:
3449{ 3456{
3450 HV *hv; 3457 HV *hv;
3451 SV **svp; 3458 SV **svp;
3452 NV x, y, w, h; 3459 NV x, y, w, h;
3453 SV *draw_x_sv = GvSV (draw_x_gv); 3460 SV *draw_x_sv = GvSV (draw_x_gv);
3454 SV *draw_y_sv = GvSV (draw_y_gv); 3461 SV *draw_y_sv = GvSV (draw_y_gv);
3455 SV *draw_w_sv = GvSV (draw_w_gv); 3462 SV *draw_w_sv = GvSV (draw_w_gv);
3456 SV *draw_h_sv = GvSV (draw_h_gv); 3463 SV *draw_h_sv = GvSV (draw_h_gv);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines