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.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
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{
1338 tc_restore (); 1355 tc_restore ();
1339 1356
1340DC::Layout 1357DC::Layout
1341new (SV *klass) 1358new (SV *klass)
1342 CODE: 1359 CODE:
1343 New (0, RETVAL, 1, struct cf_layout); 1360 RETVAL = new cf_layout;
1344 1361
1345 RETVAL->pl = pango_layout_new (opengl_context); 1362 RETVAL->pl = pango_layout_new (opengl_context);
1346 RETVAL->r = 1.; 1363 RETVAL->r = 1.;
1347 RETVAL->g = 1.; 1364 RETVAL->g = 1.;
1348 RETVAL->b = 1.; 1365 RETVAL->b = 1.;
1349 RETVAL->a = 1.; 1366 RETVAL->a = 1.;
1350 RETVAL->base_height = MIN_FONT_HEIGHT; 1367 RETVAL->base_height = MIN_FONT_HEIGHT;
1351 RETVAL->font = 0; 1368 RETVAL->font = 0;
1352 RETVAL->rc = rc_alloc ();
1353 1369
1354 pango_layout_set_wrap (RETVAL->pl, PANGO_WRAP_WORD_CHAR); 1370 pango_layout_set_wrap (RETVAL->pl, PANGO_WRAP_WORD_CHAR);
1355 layout_update_font (RETVAL); 1371 layout_update_font (RETVAL);
1356 OUTPUT: 1372 OUTPUT:
1357 RETVAL 1373 RETVAL
1358 1374
1359void 1375void
1360DESTROY (DC::Layout self) 1376DESTROY (DC::Layout self)
1361 CODE: 1377 CODE:
1362 g_object_unref (self->pl); 1378 g_object_unref (self->pl);
1363 rc_free (self->rc);
1364 Safefree (self); 1379 delete self;
1365 1380
1366void 1381void
1367set_text (DC::Layout self, SV *text_) 1382set_text (DC::Layout self, SV *text_)
1368 CODE: 1383 CODE:
1369{ 1384{
1623} 1638}
1624 1639
1625void 1640void
1626render (DC::Layout self, float x, float y, int flags = 0) 1641render (DC::Layout self, float x, float y, int flags = 0)
1627 CODE: 1642 CODE:
1628 rc_clear (self->rc); 1643 self->rc.clear ();
1629 pango_opengl_render_layout_subpixel ( 1644 pango_opengl_render_layout_subpixel (
1630 self->pl, 1645 self->pl,
1631 self->rc, 1646 &self->rc,
1632 x * PANGO_SCALE, y * PANGO_SCALE, 1647 x * PANGO_SCALE, y * PANGO_SCALE,
1633 self->r, self->g, self->b, self->a, 1648 self->r, self->g, self->b, self->a,
1634 flags 1649 flags
1635 ); 1650 );
1636 // we assume that context_change actually clears/frees stuff 1651 // we assume that context_change actually clears/frees stuff
1647 gl_BlendFuncSeparate (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, 1662 gl_BlendFuncSeparate (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA,
1648 GL_ONE , GL_ONE_MINUS_SRC_ALPHA); 1663 GL_ONE , GL_ONE_MINUS_SRC_ALPHA);
1649 glEnable (GL_ALPHA_TEST); 1664 glEnable (GL_ALPHA_TEST);
1650 glAlphaFunc (GL_GREATER, 7.f / 255.f); 1665 glAlphaFunc (GL_GREATER, 7.f / 255.f);
1651 1666
1652 rc_draw (self->rc); 1667 self->rc.draw ();
1653 1668
1654 glDisable (GL_ALPHA_TEST); 1669 glDisable (GL_ALPHA_TEST);
1655 glDisable (GL_BLEND); 1670 glDisable (GL_BLEND);
1656 glDisable (GL_TEXTURE_2D); 1671 glDisable (GL_TEXTURE_2D);
1657} 1672}
1871PROTOTYPES: DISABLE 1886PROTOTYPES: DISABLE
1872 1887
1873DC::Map 1888DC::Map
1874new (SV *klass) 1889new (SV *klass)
1875 CODE: 1890 CODE:
1876 New (0, RETVAL, 1, struct map); 1891 RETVAL = new mapgrid;
1877 RETVAL->x = 0; 1892 RETVAL->x = 0;
1878 RETVAL->y = 0; 1893 RETVAL->y = 0;
1879 RETVAL->w = 0; 1894 RETVAL->w = 0;
1880 RETVAL->h = 0; 1895 RETVAL->h = 0;
1881 RETVAL->ox = 0; 1896 RETVAL->ox = 0;
1882 RETVAL->oy = 0; 1897 RETVAL->oy = 0;
1883 RETVAL->faces = 8192; Newz (0, RETVAL->face2tile, RETVAL->faces, tileid);
1884 RETVAL->texs = 8192; Newz (0, RETVAL->tex , RETVAL->texs , maptex);
1885 RETVAL->rows = 0; 1898 RETVAL->rows = 0;
1886 RETVAL->row = 0; 1899 RETVAL->row = 0;
1887 OUTPUT: 1900 OUTPUT:
1888 RETVAL 1901 RETVAL
1889 1902
1890void 1903void
1891DESTROY (DC::Map self) 1904DESTROY (DC::Map self)
1892 CODE: 1905 CODE:
1893{ 1906{
1894 map_clear (self);
1895 Safefree (self->face2tile);
1896 Safefree (self->tex);
1897 Safefree (self); 1907 delete self;
1898} 1908}
1899 1909
1900void 1910void
1901resize (DC::Map self, int map_width, int map_height) 1911resize (DC::Map self, int map_width, int map_height)
1902 CODE: 1912 CODE:
1904 self->h = map_height; 1914 self->h = map_height;
1905 1915
1906void 1916void
1907clear (DC::Map self) 1917clear (DC::Map self)
1908 CODE: 1918 CODE:
1909 map_clear (self); 1919 self->clear_cells ();
1910 1920
1911void 1921void
1912set_tileid (DC::Map self, int face, int tile) 1922set_tileid (DC::Map self, int face, int tile)
1913 CODE: 1923 CODE:
1914{ 1924{
1915 need_facenum (self, face); self->face2tile [face] = tile; 1925 if (self->tile.size () <= face) self->tile.resize (face + 1);
1916 need_texid (self, tile); 1926 self->tile[face] = tile;
1927 if (self->tex.size () <= tile) self->tex .resize (tile + 1);
1917} 1928}
1918 1929
1919void 1930void
1920set_smooth (DC::Map self, int face, int smooth, int level) 1931set_smooth (DC::Map self, int face, int smooth, int level)
1921 CODE: 1932 CODE:
1922{ 1933{
1923 tileid texid;
1924 maptex *tex;
1925
1926 if (face < 0 || face >= self->faces) 1934 if (face < 0 || face >= self->tile.size ())
1927 return; 1935 return;
1928 1936
1929 if (smooth < 0 || smooth >= self->faces) 1937 if (smooth < 0 || smooth >= self->tile.size ())
1930 return; 1938 return;
1931 1939
1932 texid = self->face2tile [face]; 1940 tileid texid = self->tile[face];
1933 1941
1934 if (!texid) 1942 if (!texid)
1935 return; 1943 return;
1936 1944
1937 tex = self->tex + texid; 1945 maptex &tex = self->tex[texid];
1938 tex->smoothtile = self->face2tile [smooth]; 1946 tex.smoothtile = self->tile[smooth];
1939 tex->smoothlevel = level; 1947 tex.smoothlevel = level;
1940} 1948}
1941 1949
1942void 1950void
1943set_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)
1944 CODE: 1952 CODE:
1945{ 1953{
1946 need_texid (self, texid); 1954 if (self->tex.size () < texid) self->tex.resize (texid + 1);
1947 1955
1948 { 1956 {
1949 maptex *tex = self->tex + texid; 1957 maptex &tex = self->tex[texid];
1950 1958
1951 tex->name = name; 1959 tex.name = name;
1952 tex->w = w; 1960 tex.w = w;
1953 tex->h = h; 1961 tex.h = h;
1954 tex->s = s; 1962 tex.s = s;
1955 tex->t = t; 1963 tex.t = t;
1956 tex->r = r; 1964 tex.r = r;
1957 tex->g = g; 1965 tex.g = g;
1958 tex->b = b; 1966 tex.b = b;
1959 tex->a = a; 1967 tex.a = a;
1960 } 1968 }
1961 1969
1962 // somewhat hackish, but for textures that require it, it really 1970 // somewhat hackish, but for textures that require it, it really
1963 // improves the look, and most others don't suffer. 1971 // improves the look, and most others don't suffer.
1964 glBindTexture (GL_TEXTURE_2D, name); 1972 glBindTexture (GL_TEXTURE_2D, name);
1970} 1978}
1971 1979
1972void 1980void
1973expire_textures (DC::Map self, int texid, int count) 1981expire_textures (DC::Map self, int texid, int count)
1974 PPCODE: 1982 PPCODE:
1975 for (; texid < self->texs && count; ++texid, --count) 1983 for (; texid < self->tex.size () && count; ++texid, --count)
1976 { 1984 {
1977 maptex *tex = self->tex + texid; 1985 maptex &tex = self->tex[texid];
1978 1986
1979 if (tex->name) 1987 if (tex.name)
1980 { 1988 {
1981 if (tex->unused) 1989 if (tex.unused)
1982 { 1990 {
1983 tex->name = 0; 1991 tex.name = 0;
1984 tex->unused = 0; 1992 tex.unused = 0;
1985 XPUSHs (sv_2mortal (newSViv (texid))); 1993 XPUSHs (sv_2mortal (newSViv (texid)));
1986 } 1994 }
1987 else 1995 else
1988 tex->unused = 1; 1996 tex.unused = 1;
1989 } 1997 }
1990 } 1998 }
1991 1999
1992int 2000int
1993ox (DC::Map self) 2001ox (DC::Map self)
2104 } 2112 }
2105 2113
2106 for (z = 0; z <= 2; ++z) 2114 for (z = 0; z <= 2; ++z)
2107 if (flags & (4 >> z)) 2115 if (flags & (4 >> z))
2108 { 2116 {
2109 faceid face = (data [0] << 8) + data [1]; data += 2; 2117 faceid face = (data[0] << 8) + data[1]; data += 2;
2110 need_facenum (self, face); 2118 if (self->tile.size () <= face) self->tile.resize (face + 1);
2111 cell->tile [z] = self->face2tile [face]; 2119 cell->tile[z] = self->tile[face];
2112 2120
2113 if (cell->tile [z]) 2121 if (cell->tile[z])
2114 { 2122 {
2115 maptex *tex = self->tex + cell->tile [z]; 2123 maptex &tex = self->tex[cell->tile[z]];
2116 tex->unused = 0; 2124 tex.unused = 0;
2117 if (!tex->name) 2125 if (!tex.name)
2118 av_push (missing, newSViv (cell->tile [z])); 2126 av_push (missing, newSViv (cell->tile [z]));
2119 2127
2120 if (tex->smoothtile) 2128 if (tex.smoothtile)
2121 { 2129 {
2122 maptex *smooth = self->tex + tex->smoothtile; 2130 maptex &smooth = self->tex[tex.smoothtile];
2123 smooth->unused = 0; 2131 smooth.unused = 0;
2124 if (!smooth->name) 2132 if (!smooth.name)
2125 av_push (missing, newSViv (tex->smoothtile)); 2133 av_push (missing, newSViv (tex.smoothtile));
2126 } 2134 }
2127 } 2135 }
2128 } 2136 }
2129 } 2137 }
2130 else 2138 else
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) 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)
2194 CODE: 2202 CODE:
2195{ 2203{
2196 int x, y, z; 2204 int x, y, z;
2197 2205
2198 HV *smooth = (HV *)sv_2mortal ((SV *)newHV ());
2199 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) 2206 static uint8_t smooth_max[256][256]; // egad, fast and wasteful on memory (64k), also, static!
2201 smooth_key skey;
2202 int pl_x, pl_y; 2207 int pl_x, pl_y;
2203 maptex pl_tex; 2208 maptex pl_tex;
2204 rc_t *rc = rc_alloc (); 2209 rc_t rc;
2205 rc_t *rc_ov = rc_alloc (); 2210 rc_t rc_ov;
2206 rc_key_t key; 2211 rc_key_t key;
2207 rc_array_t *arr; 2212 rc_t::array_t *arr;
2208 2213
2209 pl_tex.name = 0; 2214 pl_tex.name = 0;
2210 2215
2211 // that's current max. sorry. 2216 // that's current max. sorry.
2212 if (sw > 255) sw = 255; 2217 if (sw > 255) sw = 255;
2213 if (sh > 255) sh = 255; 2218 if (sh > 255) sh = 255;
2214
2215 // clear key, in case of extra padding
2216 memset (&skey, 0, sizeof (skey));
2217 2219
2218 memset (&key, 0, sizeof (key)); 2220 memset (&key, 0, sizeof (key));
2219 key.r = 255; 2221 key.r = 255;
2220 key.g = 255; 2222 key.g = 255;
2221 key.b = 255; 2223 key.b = 255;
2227 my += self->y; 2229 my += self->y;
2228 2230
2229 // first pass: determine smooth_max 2231 // first pass: determine smooth_max
2230 // rather ugly, if you ask me 2232 // rather ugly, if you ask me
2231 // could also be stored inside mapcell and updated on change 2233 // could also be stored inside mapcell and updated on change
2232 memset (smooth_max, 0, sizeof (smooth_max)); 2234 memset (smooth_max, 0, sizeof (smooth_max[0]) * (sh + 1));
2233 2235
2234 for (y = 0; y < sh; y++) 2236 for (y = 0; y < sh; y++)
2235 if (0 <= y + my && y + my < self->rows) 2237 if (0 <= y + my && y + my < self->rows)
2236 { 2238 {
2237 maprow *row = self->row + (y + my); 2239 maprow *row = self->row + (y + my);
2252 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 2254 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
2253 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); 2255 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
2254 2256
2255 for (z = 0; z <= 2; z++) 2257 for (z = 0; z <= 2; z++)
2256 { 2258 {
2257 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;
2258 key.texname = -1; 2262 key.texname = -1;
2259 2263
2260 for (y = 0; y < sh; y++) 2264 for (y = 0; y < sh; y++)
2261 if (0 <= y + my && y + my < self->rows) 2265 if (0 <= y + my && y + my < self->rows)
2262 { 2266 {
2279 2283
2280 if (!tex.name) 2284 if (!tex.name)
2281 tex = self->tex [TEXID_NOFACE]; /* missing, replace by noface */ 2285 tex = self->tex [TEXID_NOFACE]; /* missing, replace by noface */
2282 2286
2283 key.texname = tex.name; 2287 key.texname = tex.name;
2284 arr = rc_array (rc, &key); 2288 arr = &rc.array (key);
2285 } 2289 }
2286 2290
2287 px = (x + 1) * Th - tex.w; 2291 px = (x + 1) * Th - tex.w;
2288 py = (y + 1) * Tw - tex.h; 2292 py = (y + 1) * Tw - tex.h;
2289 2293
2293 pl_y = py; 2297 pl_y = py;
2294 pl_tex = tex; 2298 pl_tex = tex;
2295 continue; 2299 continue;
2296 } 2300 }
2297 2301
2298 rc_t2f_v3f (arr, 0 , 0 , px , py , 0); 2302 arr->t2f_v3f (0 , 0 , px , py , 0);
2299 rc_t2f_v3f (arr, 0 , tex.t, px , py + tex.h, 0); 2303 arr->t2f_v3f (0 , tex.t, px , py + tex.h, 0);
2300 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);
2301 rc_t2f_v3f (arr, tex.s, 0 , px + tex.w, py , 0); 2305 arr->t2f_v3f (tex.s, 0 , px + tex.w, py , 0);
2302 2306
2303 // update smooth hash 2307 // update smooth hash
2304 if (tex.smoothtile) 2308 if (tex.smoothtile)
2305 { 2309 {
2306 skey.tile = tex.smoothtile; 2310 skey.tile = tex.smoothtile;
2307 skey.level = tex.smoothlevel; 2311 skey.level = tex.smoothlevel;
2308 2312
2309 smooth_level [tex.smoothlevel >> 5] |= ((uint32_t)1) << (tex.smoothlevel & 31); 2313 smooth_level[tex.smoothlevel] = 1;
2310 2314
2311 // 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
2312 // shifted +1|+1 so we always stay positive. 2316 // shifted +1|+1 so we always stay positive.
2313 2317
2314 // bits is ___n cccc CCCC bbbb 2318 // bits is ___n cccc CCCC bbbb
2322 2326
2323 // corners: 1 ┛· 2 ·┗ 4 ·· 8 ·· 2327 // corners: 1 ┛· 2 ·┗ 4 ·· 8 ··
2324 // ·· ·· ·┏ ┓· 2328 // ·· ·· ·┏ ┓·
2325 2329
2326 // full tile 2330 // full tile
2327 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);
2328 2332
2329 // borders 2333 // borders
2330 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);
2331 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);
2332 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);
2333 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);
2334 2338
2335 // corners 2339 // corners
2336 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);
2337 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);
2338 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);
2339 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);
2340 } 2344 }
2341 } 2345 }
2342 2346
2343 if (expect_false (z == 2) && expect_false (cell->flags)) 2347 if (expect_false (z == 2) && expect_false (cell->flags))
2344 { 2348 {
2345 // overlays such as the speech bubble, probably more to come 2349 // overlays such as the speech bubble, probably more to come
2346 if (cell->flags & 1) 2350 if (cell->flags & 1)
2347 { 2351 {
2348 rc_key_t key_ov = key; 2352 rc_key_t key_ov = key;
2349 maptex tex = self->tex [TEXID_SPEECH]; 2353 maptex tex = self->tex[TEXID_SPEECH];
2350 rc_array_t *arr;
2351 int px = x * Tw + Tw * 2 / 32; 2354 int px = x * Tw + Tw * 2 / 32;
2352 int py = y * Th - Th * 6 / 32; 2355 int py = y * Th - Th * 6 / 32;
2353 2356
2354 key_ov.texname = tex.name; 2357 key_ov.texname = tex.name;
2355 arr = rc_array (rc_ov, &key_ov); 2358 rc_t::array_t &arr = rc_ov.array (key_ov);
2356 2359
2357 rc_t2f_v3f (arr, 0 , 0 , px , py , 0); 2360 arr.t2f_v3f (0 , 0 , px , py , 0);
2358 rc_t2f_v3f (arr, 0 , tex.t, px , py + Th, 0); 2361 arr.t2f_v3f (0 , tex.t, px , py + Th, 0);
2359 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);
2360 rc_t2f_v3f (arr, tex.s, 0 , px + Tw, py , 0); 2363 arr.t2f_v3f (tex.s, 0 , px + Tw, py , 0);
2361 } 2364 }
2362 } 2365 }
2363 } 2366 }
2364 } 2367 }
2365 2368
2366 rc_draw (rc); 2369 rc.draw ();
2367 rc_clear (rc); 2370 rc.clear ();
2368 2371
2369 // go through all smoothlevels, lowest to highest, then draw. 2372 // go through all smoothlevels, lowest to highest, then draw.
2370 // this is basically counting sort 2373 // this is basically counting sort
2371 { 2374 {
2372 int w, b; 2375 int w, b;
2373 2376
2374 glEnable (GL_TEXTURE_2D); 2377 glEnable (GL_TEXTURE_2D);
2375 glBegin (GL_QUADS); 2378 glBegin (GL_QUADS);
2376 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)
2377 { 2382 {
2378 uint32_t smask = smooth_level [w]; 2383 smooth_key &skey = it->first;
2379 if (smask) 2384 IV bits = it->second;
2380 for (b = 0; b < 32; ++b) 2385
2381 if (smask & (((uint32_t)1) << b)) 2386 if (!(bits & 0x1000)
2387 && skey.level == level
2388 && level > smooth_max [skey.x][skey.y])
2382 { 2389 {
2383 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
2384 HE *he; 2398 if (tex.name)
2385
2386 hv_iterinit (smooth);
2387 while ((he = hv_iternext (smooth)))
2388 { 2399 {
2389 smooth_key *skey = (smooth_key *)HeKEY (he); 2400 // this time avoiding texture state changes
2390 IV bits = SvIVX (HeVAL (he)); 2401 // save gobs of state changes.
2391 2402 if (key.texname != tex.name)
2392 if (!(bits & 0x1000)
2393 && skey->level == level
2394 && level > smooth_max [skey->x][skey->y])
2395 { 2403 {
2396 maptex tex = self->tex [skey->tile];
2397 int px = (((int)skey->x) - 1) * Tw;
2398 int py = (((int)skey->y) - 1) * Th;
2399 int border = bits & 15;
2400 int corner = (bits >> 8) & ~(bits >> 4) & 15;
2401 float dx = tex.s * .0625f; // 16 images/row
2402 float dy = tex.t * .5f ; // 2 images/column
2403
2404 if (tex.name)
2405 {
2406 // this time avoiding texture state changes
2407 // save gobs of state changes.
2408 if (key.texname != tex.name)
2409 {
2410 self->tex [skey->tile].unused = 0; 2404 self->tex [skey.tile].unused = 0;
2411 2405
2412 glEnd (); 2406 glEnd ();
2413 glBindTexture (GL_TEXTURE_2D, key.texname = tex.name); 2407 glBindTexture (GL_TEXTURE_2D, key.texname = tex.name);
2414 glBegin (GL_QUADS); 2408 glBegin (GL_QUADS);
2415 } 2409 }
2416 2410
2417 if (border) 2411 if (border)
2418 { 2412 {
2419 float ox = border * dx; 2413 float ox = border * dx;
2420 2414
2421 glTexCoord2f (ox , 0.f ); glVertex2i (px , py ); 2415 glTexCoord2f (ox , 0.f ); glVertex2i (px , py );
2422 glTexCoord2f (ox , dy ); glVertex2i (px , py + Th); 2416 glTexCoord2f (ox , dy ); glVertex2i (px , py + Th);
2423 glTexCoord2f (ox + dx, dy ); glVertex2i (px + Tw, py + Th); 2417 glTexCoord2f (ox + dx, dy ); glVertex2i (px + Tw, py + Th);
2424 glTexCoord2f (ox + dx, 0.f ); glVertex2i (px + Tw, py ); 2418 glTexCoord2f (ox + dx, 0.f ); glVertex2i (px + Tw, py );
2425 } 2419 }
2426 2420
2427 if (corner) 2421 if (corner)
2428 { 2422 {
2429 float ox = corner * dx; 2423 float ox = corner * dx;
2430 2424
2431 glTexCoord2f (ox , dy ); glVertex2i (px , py ); 2425 glTexCoord2f (ox , dy ); glVertex2i (px , py );
2432 glTexCoord2f (ox , dy * 2.f); glVertex2i (px , py + Th); 2426 glTexCoord2f (ox , dy * 2.f); glVertex2i (px , py + Th);
2433 glTexCoord2f (ox + dx, dy * 2.f); glVertex2i (px + Tw, py + Th); 2427 glTexCoord2f (ox + dx, dy * 2.f); glVertex2i (px + Tw, py + Th);
2434 glTexCoord2f (ox + dx, dy ); glVertex2i (px + Tw, py ); 2428 glTexCoord2f (ox + dx, dy ); glVertex2i (px + Tw, py );
2435 }
2436 }
2437 } 2429 }
2438 } 2430 }
2439 } 2431 }
2440 } 2432 }
2441 2433
2442 glEnd (); 2434 glEnd ();
2443 glDisable (GL_TEXTURE_2D); 2435 glDisable (GL_TEXTURE_2D);
2444 key.texname = -1; 2436 key.texname = -1;
2445 } 2437 }
2446
2447 hv_clear (smooth);
2448 } 2438 }
2449 2439
2450 if (pl_tex.name) 2440 if (pl_tex.name)
2451 { 2441 {
2452 maptex tex = pl_tex; 2442 maptex tex = pl_tex;
2453 int px = pl_x + sdx; 2443 int px = pl_x + sdx;
2454 int py = pl_y + sdy; 2444 int py = pl_y + sdy;
2455 2445
2456 key.texname = tex.name; 2446 key.texname = tex.name;
2457 arr = rc_array (rc, &key); 2447 rc_t::array_t &arr = rc.array (key);
2458 2448
2459 rc_t2f_v3f (arr, 0 , 0 , px , py , 0); 2449 arr.t2f_v3f (0 , 0 , px , py , 0);
2460 rc_t2f_v3f (arr, 0 , tex.t, px , py + tex.h, 0); 2450 arr.t2f_v3f (0 , tex.t, px , py + tex.h, 0);
2461 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);
2462 rc_t2f_v3f (arr, tex.s, 0 , px + tex.w, py , 0); 2452 arr.t2f_v3f (tex.s, 0 , px + tex.w, py , 0);
2463 2453
2464 rc_draw (rc); 2454 rc.draw ();
2465 } 2455 }
2466 2456
2467 rc_draw (rc_ov); 2457 rc_ov.draw ();
2468 rc_clear (rc_ov); 2458 rc_ov.clear ();
2469 2459
2470 glDisable (GL_BLEND); 2460 glDisable (GL_BLEND);
2471 rc_free (rc);
2472 rc_free (rc_ov);
2473 2461
2474 // top layer: overlays such as the health bar 2462 // top layer: overlays such as the health bar
2475 for (y = 0; y < sh; y++) 2463 for (y = 0; y < sh; y++)
2476 if (0 <= y + my && y + my < self->rows) 2464 if (0 <= y + my && y + my < self->rows)
2477 { 2465 {
2799 2787
2800 for (z = 0; z <= 2; z++) 2788 for (z = 0; z <= 2; z++)
2801 { 2789 {
2802 tileid t = tile [z]; 2790 tileid t = tile [z];
2803 2791
2804 if (t >= self->texs || (t && !self->tex [t].name)) 2792 if (t >= self->tex.size () || (t && !self->tex[t].name))
2805 { 2793 {
2806 PUSHs (sv_2mortal (newSViv (t))); 2794 PUSHs (sv_2mortal (newSViv (t)));
2807 need_texid (self, t); 2795 if (self->tex.size () <= t) self->tex.resize (t + 1);
2808 } 2796 }
2809 2797
2810 cell->tile [z] = t; 2798 cell->tile[z] = t;
2811 } 2799 }
2812 } 2800 }
2813 } 2801 }
2814 } 2802 }
2815 } 2803 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines