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.327 by root, Sun Nov 18 12:01:50 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
212static void 219static void
213substitute_func (FcPattern *pattern, gpointer data) 220substitute_func (FcPattern *pattern, gpointer data)
214{ 221{
215 FcPatternAddBool (pattern, FC_HINTING, 1); 222 FcPatternAddBool (pattern, FC_HINTING, 1);
216#ifdef FC_HINT_STYLE 223#ifdef FC_HINT_STYLE
217 FcPatternAddBool (pattern, FC_HINT_STYLE, FC_HINT_FULL); 224 FcPatternAddInteger (pattern, FC_HINT_STYLE, FC_HINT_FULL);
218#endif 225#endif
219 FcPatternAddBool (pattern, FC_AUTOHINT, 0); 226 FcPatternAddBool (pattern, FC_AUTOHINT, 0);
220} 227}
221 228
222static void 229static void
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 std::vector<tileid> tile;
283 int texs; maptex *tex; // [tileid] 293 std::vector<maptex> tex;
284 294
285 int32_t rows; 295 int32_t rows;
286 maprow *row; 296 maprow *row;
287} *DC__Map; 297
298 ~mapgrid ()
299 {
300 clear_cells ();
301 }
302
303 void clear_cells ();
304};
305
306typedef mapgrid *DC__Map;
288 307
289static char * 308static char *
290prepend (char *ptr, int sze, int inc) 309prepend (char *ptr, int sze, int inc)
291{ 310{
292 char *p; 311 char *p;
309} 328}
310 329
311#define Append(type,ptr,sze,inc) (ptr) = (type *)append ((char *)ptr, (sze) * sizeof (type), (inc) * sizeof (type)) 330#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)) 331#define Prepend(type,ptr,sze,inc) (ptr) = (type *)prepend ((char *)ptr, (sze) * sizeof (type), (inc) * sizeof (type))
313 332
314static void
315need_facenum (struct map *self, faceid face)
316{
317 while (self->faces <= face)
318 {
319 Append (tileid, self->face2tile, self->faces, self->faces);
320 self->faces *= 2;
321 }
322}
323
324static void
325need_texid (struct map *self, int texid)
326{
327 while (self->texs <= texid)
328 {
329 Append (maptex, self->tex, self->texs, self->texs);
330 self->texs *= 2;
331 }
332}
333
334static maprow * 333static maprow *
335map_get_row (DC__Map self, int y) 334map_get_row (mapgrid *self, int y)
336{ 335{
337 if (0 > y) 336 if (0 > y)
338 { 337 {
339 int extend = - y + MAP_EXTEND_Y; 338 int extend = - y + MAP_EXTEND_Y;
340 Prepend (maprow, self->row, self->rows, extend); 339 Prepend (maprow, self->row, self->rows, extend);
378 377
379 return row->col + (x - row->c0); 378 return row->col + (x - row->c0);
380} 379}
381 380
382static mapcell * 381static mapcell *
383map_get_cell (DC__Map self, int x, int y) 382map_get_cell (mapgrid *self, int x, int y)
384{ 383{
385 return row_get_cell (map_get_row (self, y), x); 384 return row_get_cell (map_get_row (self, y), x);
386} 385}
387 386
388static void 387void mapgrid::clear_cells ()
389map_clear (DC__Map self)
390{ 388{
391 int r; 389 int r;
392 390
393 for (r = 0; r < self->rows; r++) 391 for (r = 0; r < rows; r++)
394 Safefree (self->row[r].col); 392 Safefree (row[r].col);
395 393
396 Safefree (self->row); 394 Safefree (row);
397 395
398 self->x = 0; 396 x = 0;
399 self->y = 0; 397 y = 0;
400 self->ox = 0; 398 ox = 0;
401 self->oy = 0; 399 oy = 0;
402 self->row = 0; 400 row = 0;
403 self->rows = 0; 401 rows = 0;
404} 402}
405 403
406#define CELL_CLEAR(cell) \ 404#define CELL_CLEAR(cell) \
407 do { \ 405 do { \
408 if ((cell)->player) \ 406 if ((cell)->player) \
412 (cell)->flags = 0; \ 410 (cell)->flags = 0; \
413 (cell)->player = 0; \ 411 (cell)->player = 0; \
414 } while (0) 412 } while (0)
415 413
416static void 414static void
417map_blank (DC__Map self, int x0, int y0, int w, int h) 415map_blank (mapgrid *self, int x0, int y0, int w, int h)
418{ 416{
419 int x, y; 417 int x, y;
420 maprow *row; 418 maprow *row;
421 mapcell *cell; 419 mapcell *cell;
422 420
439 CELL_CLEAR (cell); 437 CELL_CLEAR (cell);
440 } 438 }
441 } 439 }
442} 440}
443 441
444typedef struct { 442struct smooth_key
443{
445 tileid tile; 444 tileid tile;
446 uint8_t x, y, level; 445 uint8_t x, y, level;
447} smooth_key; 446
447 bool operator == (const smooth_key &o) const
448 {
449 return tile == o.tile && x == o.x && y == o.y && level == o.level;
450 }
451};
452
453typedef ska::flat_hash_map<smooth_key, IV> smooth_hash;
454
455namespace std {
456 template <>
457 struct hash<smooth_key>
458 {
459 size_t operator () (const smooth_key &v) const
460 {
461 return v.tile + (v.x << 8) + (v.y << 16) + (v.level << 24);
462 }
463 };
464}
448 465
449static void 466static void
450smooth_or_bits (HV *hv, smooth_key *key, IV bits) 467smooth_or_bits (smooth_hash &h, smooth_key &key, IV bits)
451{ 468{
452 SV **sv = hv_fetch (hv, (char *)key, sizeof (*key), 1); 469 auto &&it = h.find (key);
453 470
454 if (SvIOK (*sv)) 471 if (it == h.end ())
455 SvIV_set (*sv, SvIVX (*sv) | bits); 472 h.insert (std::make_pair (key, bits));
456 else 473 else
457 sv_setiv (*sv, bits); 474 it->second |= bits;
458} 475}
459 476
460static void 477static void
461music_finished (void) 478music_finished (void)
462{ 479{
481 ev.data2 = 0; 498 ev.data2 = 0;
482 499
483 SDL_PushEvent ((SDL_Event *)&ev); 500 SDL_PushEvent ((SDL_Event *)&ev);
484} 501}
485 502
503// approximately divide by 255
486static unsigned int 504static unsigned int
487div255 (unsigned int n) 505div255 (unsigned int n)
488{ 506{
489 return (n + (n >> 8)) >> 8; 507 return (n + (n >> 8)) >> 8;
490} 508}
1083 OUTPUT: 1101 OUTPUT:
1084 RETVAL 1102 RETVAL
1085 1103
1086int 1104int
1087Mix_OpenAudio (int frequency = 44100, int format = MIX_DEFAULT_FORMAT, int channels = 2, int chunksize = 4096) 1105Mix_OpenAudio (int frequency = 44100, int format = MIX_DEFAULT_FORMAT, int channels = 2, int chunksize = 4096)
1088 POSTCALL: 1106 POSTCALL:
1089 Mix_HookMusicFinished (music_finished); 1107 Mix_HookMusicFinished (music_finished);
1090 Mix_ChannelFinished (channel_finished); 1108 Mix_ChannelFinished (channel_finished);
1091 1109
1092void 1110void
1093Mix_QuerySpec () 1111Mix_QuerySpec ()
1337 tc_restore (); 1355 tc_restore ();
1338 1356
1339DC::Layout 1357DC::Layout
1340new (SV *klass) 1358new (SV *klass)
1341 CODE: 1359 CODE:
1342 New (0, RETVAL, 1, struct cf_layout); 1360 RETVAL = new cf_layout;
1343 1361
1344 RETVAL->pl = pango_layout_new (opengl_context); 1362 RETVAL->pl = pango_layout_new (opengl_context);
1345 RETVAL->r = 1.; 1363 RETVAL->r = 1.;
1346 RETVAL->g = 1.; 1364 RETVAL->g = 1.;
1347 RETVAL->b = 1.; 1365 RETVAL->b = 1.;
1348 RETVAL->a = 1.; 1366 RETVAL->a = 1.;
1349 RETVAL->base_height = MIN_FONT_HEIGHT; 1367 RETVAL->base_height = MIN_FONT_HEIGHT;
1350 RETVAL->font = 0; 1368 RETVAL->font = 0;
1351 RETVAL->rc = rc_alloc ();
1352 1369
1353 pango_layout_set_wrap (RETVAL->pl, PANGO_WRAP_WORD_CHAR); 1370 pango_layout_set_wrap (RETVAL->pl, PANGO_WRAP_WORD_CHAR);
1354 layout_update_font (RETVAL); 1371 layout_update_font (RETVAL);
1355 OUTPUT: 1372 OUTPUT:
1356 RETVAL 1373 RETVAL
1357 1374
1358void 1375void
1359DESTROY (DC::Layout self) 1376DESTROY (DC::Layout self)
1360 CODE: 1377 CODE:
1361 g_object_unref (self->pl); 1378 g_object_unref (self->pl);
1362 rc_free (self->rc);
1363 Safefree (self); 1379 delete self;
1364 1380
1365void 1381void
1366set_text (DC::Layout self, SV *text_) 1382set_text (DC::Layout self, SV *text_)
1367 CODE: 1383 CODE:
1368{ 1384{
1622} 1638}
1623 1639
1624void 1640void
1625render (DC::Layout self, float x, float y, int flags = 0) 1641render (DC::Layout self, float x, float y, int flags = 0)
1626 CODE: 1642 CODE:
1627 rc_clear (self->rc); 1643 self->rc.clear ();
1628 pango_opengl_render_layout_subpixel ( 1644 pango_opengl_render_layout_subpixel (
1629 self->pl, 1645 self->pl,
1630 self->rc, 1646 &self->rc,
1631 x * PANGO_SCALE, y * PANGO_SCALE, 1647 x * PANGO_SCALE, y * PANGO_SCALE,
1632 self->r, self->g, self->b, self->a, 1648 self->r, self->g, self->b, self->a,
1633 flags 1649 flags
1634 ); 1650 );
1635 // we assume that context_change actually clears/frees stuff 1651 // we assume that context_change actually clears/frees stuff
1646 gl_BlendFuncSeparate (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, 1662 gl_BlendFuncSeparate (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA,
1647 GL_ONE , GL_ONE_MINUS_SRC_ALPHA); 1663 GL_ONE , GL_ONE_MINUS_SRC_ALPHA);
1648 glEnable (GL_ALPHA_TEST); 1664 glEnable (GL_ALPHA_TEST);
1649 glAlphaFunc (GL_GREATER, 7.f / 255.f); 1665 glAlphaFunc (GL_GREATER, 7.f / 255.f);
1650 1666
1651 rc_draw (self->rc); 1667 self->rc.draw ();
1652 1668
1653 glDisable (GL_ALPHA_TEST); 1669 glDisable (GL_ALPHA_TEST);
1654 glDisable (GL_BLEND); 1670 glDisable (GL_BLEND);
1655 glDisable (GL_TEXTURE_2D); 1671 glDisable (GL_TEXTURE_2D);
1656} 1672}
1870PROTOTYPES: DISABLE 1886PROTOTYPES: DISABLE
1871 1887
1872DC::Map 1888DC::Map
1873new (SV *klass) 1889new (SV *klass)
1874 CODE: 1890 CODE:
1875 New (0, RETVAL, 1, struct map); 1891 RETVAL = new mapgrid;
1876 RETVAL->x = 0; 1892 RETVAL->x = 0;
1877 RETVAL->y = 0; 1893 RETVAL->y = 0;
1878 RETVAL->w = 0; 1894 RETVAL->w = 0;
1879 RETVAL->h = 0; 1895 RETVAL->h = 0;
1880 RETVAL->ox = 0; 1896 RETVAL->ox = 0;
1881 RETVAL->oy = 0; 1897 RETVAL->oy = 0;
1882 RETVAL->faces = 8192; Newz (0, RETVAL->face2tile, RETVAL->faces, tileid);
1883 RETVAL->texs = 8192; Newz (0, RETVAL->tex , RETVAL->texs , maptex);
1884 RETVAL->rows = 0; 1898 RETVAL->rows = 0;
1885 RETVAL->row = 0; 1899 RETVAL->row = 0;
1886 OUTPUT: 1900 OUTPUT:
1887 RETVAL 1901 RETVAL
1888 1902
1889void 1903void
1890DESTROY (DC::Map self) 1904DESTROY (DC::Map self)
1891 CODE: 1905 CODE:
1892{ 1906{
1893 map_clear (self);
1894 Safefree (self->face2tile);
1895 Safefree (self->tex);
1896 Safefree (self); 1907 delete self;
1897} 1908}
1898 1909
1899void 1910void
1900resize (DC::Map self, int map_width, int map_height) 1911resize (DC::Map self, int map_width, int map_height)
1901 CODE: 1912 CODE:
1903 self->h = map_height; 1914 self->h = map_height;
1904 1915
1905void 1916void
1906clear (DC::Map self) 1917clear (DC::Map self)
1907 CODE: 1918 CODE:
1908 map_clear (self); 1919 self->clear_cells ();
1909 1920
1910void 1921void
1911set_tileid (DC::Map self, int face, int tile) 1922set_tileid (DC::Map self, int face, int tile)
1912 CODE: 1923 CODE:
1913{ 1924{
1914 need_facenum (self, face); self->face2tile [face] = tile; 1925 if (self->tile.size () <= face) self->tile.resize (face + 1);
1915 need_texid (self, tile); 1926 self->tile[face] = tile;
1927 if (self->tex.size () <= tile) self->tex .resize (tile + 1);
1916} 1928}
1917 1929
1918void 1930void
1919set_smooth (DC::Map self, int face, int smooth, int level) 1931set_smooth (DC::Map self, int face, int smooth, int level)
1920 CODE: 1932 CODE:
1921{ 1933{
1922 tileid texid;
1923 maptex *tex;
1924
1925 if (face < 0 || face >= self->faces) 1934 if (face < 0 || face >= self->tile.size ())
1926 return; 1935 return;
1927 1936
1928 if (smooth < 0 || smooth >= self->faces) 1937 if (smooth < 0 || smooth >= self->tile.size ())
1929 return; 1938 return;
1930 1939
1931 texid = self->face2tile [face]; 1940 tileid texid = self->tile[face];
1932 1941
1933 if (!texid) 1942 if (!texid)
1934 return; 1943 return;
1935 1944
1936 tex = self->tex + texid; 1945 maptex &tex = self->tex[texid];
1937 tex->smoothtile = self->face2tile [smooth]; 1946 tex.smoothtile = self->tile[smooth];
1938 tex->smoothlevel = level; 1947 tex.smoothlevel = level;
1939} 1948}
1940 1949
1941void 1950void
1942set_texture (DC::Map self, int texid, int name, int w, int h, float s, float t, int r, int g, int b, int a) 1951set_texture (DC::Map self, int texid, int name, int w, int h, float s, float t, int r, int g, int b, int a)
1943 CODE: 1952 CODE:
1944{ 1953{
1945 need_texid (self, texid); 1954 if (self->tex.size () < texid) self->tex.resize (texid + 1);
1946 1955
1947 { 1956 {
1948 maptex *tex = self->tex + texid; 1957 maptex &tex = self->tex[texid];
1949 1958
1950 tex->name = name; 1959 tex.name = name;
1951 tex->w = w; 1960 tex.w = w;
1952 tex->h = h; 1961 tex.h = h;
1953 tex->s = s; 1962 tex.s = s;
1954 tex->t = t; 1963 tex.t = t;
1955 tex->r = r; 1964 tex.r = r;
1956 tex->g = g; 1965 tex.g = g;
1957 tex->b = b; 1966 tex.b = b;
1958 tex->a = a; 1967 tex.a = a;
1959 } 1968 }
1960 1969
1961 // somewhat hackish, but for textures that require it, it really 1970 // somewhat hackish, but for textures that require it, it really
1962 // improves the look, and most others don't suffer. 1971 // improves the look, and most others don't suffer.
1963 glBindTexture (GL_TEXTURE_2D, name); 1972 glBindTexture (GL_TEXTURE_2D, name);
1969} 1978}
1970 1979
1971void 1980void
1972expire_textures (DC::Map self, int texid, int count) 1981expire_textures (DC::Map self, int texid, int count)
1973 PPCODE: 1982 PPCODE:
1974 for (; texid < self->texs && count; ++texid, --count) 1983 for (; texid < self->tex.size () && count; ++texid, --count)
1975 { 1984 {
1976 maptex *tex = self->tex + texid; 1985 maptex &tex = self->tex[texid];
1977 1986
1978 if (tex->name) 1987 if (tex.name)
1979 { 1988 {
1980 if (tex->unused) 1989 if (tex.unused)
1981 { 1990 {
1982 tex->name = 0; 1991 tex.name = 0;
1983 tex->unused = 0; 1992 tex.unused = 0;
1984 XPUSHs (sv_2mortal (newSViv (texid))); 1993 XPUSHs (sv_2mortal (newSViv (texid)));
1985 } 1994 }
1986 else 1995 else
1987 tex->unused = 1; 1996 tex.unused = 1;
1988 } 1997 }
1989 } 1998 }
1990 1999
1991int 2000int
1992ox (DC::Map self) 2001ox (DC::Map self)
2103 } 2112 }
2104 2113
2105 for (z = 0; z <= 2; ++z) 2114 for (z = 0; z <= 2; ++z)
2106 if (flags & (4 >> z)) 2115 if (flags & (4 >> z))
2107 { 2116 {
2108 faceid face = (data [0] << 8) + data [1]; data += 2; 2117 faceid face = (data[0] << 8) + data[1]; data += 2;
2109 need_facenum (self, face); 2118 if (self->tile.size () <= face) self->tile.resize (face + 1);
2110 cell->tile [z] = self->face2tile [face]; 2119 cell->tile[z] = self->tile[face];
2111 2120
2112 if (cell->tile [z]) 2121 if (cell->tile[z])
2113 { 2122 {
2114 maptex *tex = self->tex + cell->tile [z]; 2123 maptex &tex = self->tex[cell->tile[z]];
2115 tex->unused = 0; 2124 tex.unused = 0;
2116 if (!tex->name) 2125 if (!tex.name)
2117 av_push (missing, newSViv (cell->tile [z])); 2126 av_push (missing, newSViv (cell->tile [z]));
2118 2127
2119 if (tex->smoothtile) 2128 if (tex.smoothtile)
2120 { 2129 {
2121 maptex *smooth = self->tex + tex->smoothtile; 2130 maptex &smooth = self->tex[tex.smoothtile];
2122 smooth->unused = 0; 2131 smooth.unused = 0;
2123 if (!smooth->name) 2132 if (!smooth.name)
2124 av_push (missing, newSViv (tex->smoothtile)); 2133 av_push (missing, newSViv (tex.smoothtile));
2125 } 2134 }
2126 } 2135 }
2127 } 2136 }
2128 } 2137 }
2129 else 2138 else
2181 | (b << 16) 2190 | (b << 16)
2182 | (a << 24); 2191 | (a << 24);
2183 } 2192 }
2184 } 2193 }
2185 2194
2186 RETVAL = map_sv; 2195 RETVAL = map_sv;
2187} 2196}
2188 OUTPUT: 2197 OUTPUT:
2189 RETVAL 2198 RETVAL
2190 2199
2191void 2200void
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) 2201draw (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: 2202 CODE:
2194{ 2203{
2195 int x, y, z; 2204 int x, y, z;
2196 2205
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) 2206 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; 2207 int pl_x, pl_y;
2202 maptex pl_tex; 2208 maptex pl_tex;
2203 rc_t *rc = rc_alloc (); 2209 rc_t rc;
2204 rc_t *rc_ov = rc_alloc (); 2210 rc_t rc_ov;
2205 rc_key_t key; 2211 rc_key_t key;
2206 rc_array_t *arr; 2212 rc_t::array_t *arr;
2207 2213
2208 pl_tex.name = 0; 2214 pl_tex.name = 0;
2209 2215
2210 // that's current max. sorry. 2216 // that's current max. sorry.
2211 if (sw > 255) sw = 255; 2217 if (sw > 255) sw = 255;
2212 if (sh > 255) sh = 255; 2218 if (sh > 255) sh = 255;
2213
2214 // clear key, in case of extra padding
2215 memset (&skey, 0, sizeof (skey));
2216 2219
2217 memset (&key, 0, sizeof (key)); 2220 memset (&key, 0, sizeof (key));
2218 key.r = 255; 2221 key.r = 255;
2219 key.g = 255; 2222 key.g = 255;
2220 key.b = 255; 2223 key.b = 255;
2226 my += self->y; 2229 my += self->y;
2227 2230
2228 // first pass: determine smooth_max 2231 // first pass: determine smooth_max
2229 // rather ugly, if you ask me 2232 // rather ugly, if you ask me
2230 // could also be stored inside mapcell and updated on change 2233 // could also be stored inside mapcell and updated on change
2231 memset (smooth_max, 0, sizeof (smooth_max)); 2234 memset (smooth_max, 0, sizeof (smooth_max[0]) * (sh + 1));
2232 2235
2233 for (y = 0; y < sh; y++) 2236 for (y = 0; y < sh; y++)
2234 if (0 <= y + my && y + my < self->rows) 2237 if (0 <= y + my && y + my < self->rows)
2235 { 2238 {
2236 maprow *row = self->row + (y + my); 2239 maprow *row = self->row + (y + my);
2251 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 2254 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
2252 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); 2255 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
2253 2256
2254 for (z = 0; z <= 2; z++) 2257 for (z = 0; z <= 2; z++)
2255 { 2258 {
2256 memset (smooth_level, 0, sizeof (smooth_level)); 2259 std::bitset<256> smooth_level; // one bit for every possible smooth level
2260 smooth_key skey;
2261 smooth_hash smooth;
2257 key.texname = -1; 2262 key.texname = -1;
2258 2263
2259 for (y = 0; y < sh; y++) 2264 for (y = 0; y < sh; y++)
2260 if (0 <= y + my && y + my < self->rows) 2265 if (0 <= y + my && y + my < self->rows)
2261 { 2266 {
2278 2283
2279 if (!tex.name) 2284 if (!tex.name)
2280 tex = self->tex [TEXID_NOFACE]; /* missing, replace by noface */ 2285 tex = self->tex [TEXID_NOFACE]; /* missing, replace by noface */
2281 2286
2282 key.texname = tex.name; 2287 key.texname = tex.name;
2283 arr = rc_array (rc, &key); 2288 arr = &rc.array (key);
2284 } 2289 }
2285 2290
2286 px = (x + 1) * Th - tex.w; 2291 px = (x + 1) * Th - tex.w;
2287 py = (y + 1) * Tw - tex.h; 2292 py = (y + 1) * Tw - tex.h;
2288 2293
2292 pl_y = py; 2297 pl_y = py;
2293 pl_tex = tex; 2298 pl_tex = tex;
2294 continue; 2299 continue;
2295 } 2300 }
2296 2301
2297 rc_t2f_v3f (arr, 0 , 0 , px , py , 0); 2302 arr->t2f_v3f (0 , 0 , px , py , 0);
2298 rc_t2f_v3f (arr, 0 , tex.t, px , py + tex.h, 0); 2303 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); 2304 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); 2305 arr->t2f_v3f (tex.s, 0 , px + tex.w, py , 0);
2301 2306
2302 // update smooth hash 2307 // update smooth hash
2303 if (tex.smoothtile) 2308 if (tex.smoothtile)
2304 { 2309 {
2305 skey.tile = tex.smoothtile; 2310 skey.tile = tex.smoothtile;
2306 skey.level = tex.smoothlevel; 2311 skey.level = tex.smoothlevel;
2307 2312
2308 smooth_level [tex.smoothlevel >> 5] |= ((uint32_t)1) << (tex.smoothlevel & 31); 2313 smooth_level[tex.smoothlevel] = 1;
2309 2314
2310 // add bits to current tile and all neighbours. skey.x|y is 2315 // add bits to current tile and all neighbours. skey.x|y is
2311 // shifted +1|+1 so we always stay positive. 2316 // shifted +1|+1 so we always stay positive.
2312 2317
2313 // bits is ___n cccc CCCC bbbb 2318 // bits is ___n cccc CCCC bbbb
2321 2326
2322 // corners: 1 ┛· 2 ·┗ 4 ·· 8 ·· 2327 // corners: 1 ┛· 2 ·┗ 4 ·· 8 ··
2323 // ·· ·· ·┏ ┓· 2328 // ·· ·· ·┏ ┓·
2324 2329
2325 // full tile 2330 // full tile
2326 skey.x = x + 1; skey.y = y + 1; smooth_or_bits (smooth, &skey, 0x1000); 2331 skey.x = x + 1; skey.y = y + 1; smooth_or_bits (smooth, skey, 0x1000);
2327 2332
2328 // borders 2333 // borders
2329 skey.x = x + 2; skey.y = y + 1; smooth_or_bits (smooth, &skey, 0x0091); 2334 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); 2335 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); 2336 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); 2337 skey.x = x + 1; skey.y = y ; smooth_or_bits (smooth, skey, 0x00c8);
2333 2338
2334 // corners 2339 // corners
2335 skey.x = x + 2; skey.y = y + 2; smooth_or_bits (smooth, &skey, 0x0100); 2340 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); 2341 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); 2342 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); 2343 skey.x = x + 2; skey.y = y ; smooth_or_bits (smooth, skey, 0x0800);
2339 } 2344 }
2340 } 2345 }
2341 2346
2342 if (expect_false (z == 2) && expect_false (cell->flags)) 2347 if (expect_false (z == 2) && expect_false (cell->flags))
2343 { 2348 {
2344 // overlays such as the speech bubble, probably more to come 2349 // overlays such as the speech bubble, probably more to come
2345 if (cell->flags & 1) 2350 if (cell->flags & 1)
2346 { 2351 {
2347 rc_key_t key_ov = key; 2352 rc_key_t key_ov = key;
2348 maptex tex = self->tex [TEXID_SPEECH]; 2353 maptex tex = self->tex[TEXID_SPEECH];
2349 rc_array_t *arr;
2350 int px = x * Tw + Tw * 2 / 32; 2354 int px = x * Tw + Tw * 2 / 32;
2351 int py = y * Th - Th * 6 / 32; 2355 int py = y * Th - Th * 6 / 32;
2352 2356
2353 key_ov.texname = tex.name; 2357 key_ov.texname = tex.name;
2354 arr = rc_array (rc_ov, &key_ov); 2358 rc_t::array_t &arr = rc_ov.array (key_ov);
2355 2359
2356 rc_t2f_v3f (arr, 0 , 0 , px , py , 0); 2360 arr.t2f_v3f (0 , 0 , px , py , 0);
2357 rc_t2f_v3f (arr, 0 , tex.t, px , py + Th, 0); 2361 arr.t2f_v3f (0 , tex.t, px , py + Th, 0);
2358 rc_t2f_v3f (arr, tex.s, tex.t, px + Tw, py + Th, 0); 2362 arr.t2f_v3f (tex.s, tex.t, px + Tw, py + Th, 0);
2359 rc_t2f_v3f (arr, tex.s, 0 , px + Tw, py , 0); 2363 arr.t2f_v3f (tex.s, 0 , px + Tw, py , 0);
2360 } 2364 }
2361 } 2365 }
2362 } 2366 }
2363 } 2367 }
2364 2368
2365 rc_draw (rc); 2369 rc.draw ();
2366 rc_clear (rc); 2370 rc.clear ();
2367 2371
2368 // go through all smoothlevels, lowest to highest, then draw. 2372 // go through all smoothlevels, lowest to highest, then draw.
2369 // this is basically counting sort 2373 // this is basically counting sort
2370 { 2374 {
2371 int w, b; 2375 int w, b;
2372 2376
2373 glEnable (GL_TEXTURE_2D); 2377 glEnable (GL_TEXTURE_2D);
2374 glBegin (GL_QUADS); 2378 glBegin (GL_QUADS);
2375 for (w = 0; w < 256 / 32; ++w) 2379 for (int level = 0; level < smooth_level.size (); ++level)
2380 if (smooth_level[level])
2381 for (auto &&it = smooth.begin (); it != smooth.end (); ++it)
2376 { 2382 {
2377 uint32_t smask = smooth_level [w]; 2383 smooth_key &skey = it->first;
2378 if (smask) 2384 IV bits = it->second;
2379 for (b = 0; b < 32; ++b) 2385
2380 if (smask & (((uint32_t)1) << b)) 2386 if (!(bits & 0x1000)
2387 && skey.level == level
2388 && level > smooth_max [skey.x][skey.y])
2381 { 2389 {
2382 int level = (w << 5) | b; 2390 maptex tex = self->tex [skey.tile];
2391 int px = (((int)skey.x) - 1) * Tw;
2392 int py = (((int)skey.y) - 1) * Th;
2393 int border = bits & 15;
2394 int corner = (bits >> 8) & ~(bits >> 4) & 15;
2395 float dx = tex.s * .0625f; // 16 images/row
2396 float dy = tex.t * .5f ; // 2 images/column
2397
2383 HE *he; 2398 if (tex.name)
2384
2385 hv_iterinit (smooth);
2386 while ((he = hv_iternext (smooth)))
2387 { 2399 {
2388 smooth_key *skey = (smooth_key *)HeKEY (he); 2400 // this time avoiding texture state changes
2389 IV bits = SvIVX (HeVAL (he)); 2401 // save gobs of state changes.
2390 2402 if (key.texname != tex.name)
2391 if (!(bits & 0x1000)
2392 && skey->level == level
2393 && level > smooth_max [skey->x][skey->y])
2394 { 2403 {
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; 2404 self->tex [skey.tile].unused = 0;
2410 2405
2411 glEnd (); 2406 glEnd ();
2412 glBindTexture (GL_TEXTURE_2D, key.texname = tex.name); 2407 glBindTexture (GL_TEXTURE_2D, key.texname = tex.name);
2413 glBegin (GL_QUADS); 2408 glBegin (GL_QUADS);
2414 } 2409 }
2415 2410
2416 if (border) 2411 if (border)
2417 { 2412 {
2418 float ox = border * dx; 2413 float ox = border * dx;
2419 2414
2420 glTexCoord2f (ox , 0.f ); glVertex2i (px , py ); 2415 glTexCoord2f (ox , 0.f ); glVertex2i (px , py );
2421 glTexCoord2f (ox , dy ); glVertex2i (px , py + Th); 2416 glTexCoord2f (ox , dy ); glVertex2i (px , py + Th);
2422 glTexCoord2f (ox + dx, dy ); glVertex2i (px + Tw, py + Th); 2417 glTexCoord2f (ox + dx, dy ); glVertex2i (px + Tw, py + Th);
2423 glTexCoord2f (ox + dx, 0.f ); glVertex2i (px + Tw, py ); 2418 glTexCoord2f (ox + dx, 0.f ); glVertex2i (px + Tw, py );
2424 } 2419 }
2425 2420
2426 if (corner) 2421 if (corner)
2427 { 2422 {
2428 float ox = corner * dx; 2423 float ox = corner * dx;
2429 2424
2430 glTexCoord2f (ox , dy ); glVertex2i (px , py ); 2425 glTexCoord2f (ox , dy ); glVertex2i (px , py );
2431 glTexCoord2f (ox , dy * 2.f); glVertex2i (px , py + Th); 2426 glTexCoord2f (ox , dy * 2.f); glVertex2i (px , py + Th);
2432 glTexCoord2f (ox + dx, dy * 2.f); glVertex2i (px + Tw, py + Th); 2427 glTexCoord2f (ox + dx, dy * 2.f); glVertex2i (px + Tw, py + Th);
2433 glTexCoord2f (ox + dx, dy ); glVertex2i (px + Tw, py ); 2428 glTexCoord2f (ox + dx, dy ); glVertex2i (px + Tw, py );
2434 }
2435 }
2436 } 2429 }
2437 } 2430 }
2438 } 2431 }
2439 } 2432 }
2440 2433
2441 glEnd (); 2434 glEnd ();
2442 glDisable (GL_TEXTURE_2D); 2435 glDisable (GL_TEXTURE_2D);
2443 key.texname = -1; 2436 key.texname = -1;
2444 } 2437 }
2445
2446 hv_clear (smooth);
2447 } 2438 }
2448 2439
2449 if (pl_tex.name) 2440 if (pl_tex.name)
2450 { 2441 {
2451 maptex tex = pl_tex; 2442 maptex tex = pl_tex;
2452 int px = pl_x + sdx; 2443 int px = pl_x + sdx;
2453 int py = pl_y + sdy; 2444 int py = pl_y + sdy;
2454 2445
2455 key.texname = tex.name; 2446 key.texname = tex.name;
2456 arr = rc_array (rc, &key); 2447 rc_t::array_t &arr = rc.array (key);
2457 2448
2458 rc_t2f_v3f (arr, 0 , 0 , px , py , 0); 2449 arr.t2f_v3f (0 , 0 , px , py , 0);
2459 rc_t2f_v3f (arr, 0 , tex.t, px , py + tex.h, 0); 2450 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); 2451 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); 2452 arr.t2f_v3f (tex.s, 0 , px + tex.w, py , 0);
2462 2453
2463 rc_draw (rc); 2454 rc.draw ();
2464 } 2455 }
2465 2456
2466 rc_draw (rc_ov); 2457 rc_ov.draw ();
2467 rc_clear (rc_ov); 2458 rc_ov.clear ();
2468 2459
2469 glDisable (GL_BLEND); 2460 glDisable (GL_BLEND);
2470 rc_free (rc);
2471 rc_free (rc_ov);
2472 2461
2473 // top layer: overlays such as the health bar 2462 // top layer: overlays such as the health bar
2474 for (y = 0; y < sh; y++) 2463 for (y = 0; y < sh; y++)
2475 if (0 <= y + my && y + my < self->rows) 2464 if (0 <= y + my && y + my < self->rows)
2476 { 2465 {
2720 else 2709 else
2721 *data++ = 0; 2710 *data++ = 0;
2722 } 2711 }
2723 } 2712 }
2724 2713
2725 /* if size is w*h + 5 then no data has been found */ 2714 /* if size is w*h + 5 then no data has been found */
2726 if (data - (uint8_t *)SvPVX (data_sv) != w * h + 5) 2715 if (data - (uint8_t *)SvPVX (data_sv) != w * h + 5)
2727 { 2716 {
2728 SvPOK_only (data_sv); 2717 SvPOK_only (data_sv);
2729 SvCUR_set (data_sv, data - (uint8_t *)SvPVX (data_sv)); 2718 SvCUR_set (data_sv, data - (uint8_t *)SvPVX (data_sv));
2730 } 2719 }
2731 2720
2732 RETVAL = data_sv; 2721 RETVAL = data_sv;
2733} 2722}
2734 OUTPUT: 2723 OUTPUT:
2735 RETVAL 2724 RETVAL
2736 2725
2737void 2726void
2798 2787
2799 for (z = 0; z <= 2; z++) 2788 for (z = 0; z <= 2; z++)
2800 { 2789 {
2801 tileid t = tile [z]; 2790 tileid t = tile [z];
2802 2791
2803 if (t >= self->texs || (t && !self->tex [t].name)) 2792 if (t >= self->tex.size () || (t && !self->tex[t].name))
2804 { 2793 {
2805 PUSHs (sv_2mortal (newSViv (t))); 2794 PUSHs (sv_2mortal (newSViv (t)));
2806 need_texid (self, t); 2795 if (self->tex.size () <= t) self->tex.resize (t + 1);
2807 } 2796 }
2808 2797
2809 cell->tile [z] = t; 2798 cell->tile[z] = t;
2810 } 2799 }
2811 } 2800 }
2812 } 2801 }
2813 } 2802 }
2814 } 2803 }
3427 3416
3428void 3417void
3429find_widget (SV *self, NV x, NV y) 3418find_widget (SV *self, NV x, NV y)
3430 PPCODE: 3419 PPCODE:
3431{ 3420{
3432 if (within_widget (self, x, y)) 3421 if (within_widget (self, x, y))
3433 XPUSHs (self); 3422 XPUSHs (self);
3434} 3423}
3435 3424
3436BOOT: 3425BOOT:
3437{ 3426{
3445 3434
3446void 3435void
3447draw (SV *self) 3436draw (SV *self)
3448 CODE: 3437 CODE:
3449{ 3438{
3450 HV *hv; 3439 HV *hv;
3451 SV **svp; 3440 SV **svp;
3452 NV x, y, w, h; 3441 NV x, y, w, h;
3453 SV *draw_x_sv = GvSV (draw_x_gv); 3442 SV *draw_x_sv = GvSV (draw_x_gv);
3454 SV *draw_y_sv = GvSV (draw_y_gv); 3443 SV *draw_y_sv = GvSV (draw_y_gv);
3455 SV *draw_w_sv = GvSV (draw_w_gv); 3444 SV *draw_w_sv = GvSV (draw_w_gv);
3456 SV *draw_h_sv = GvSV (draw_h_gv); 3445 SV *draw_h_sv = GvSV (draw_h_gv);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines