--- deliantra/Deliantra-Client/Client.xs 2018/11/18 01:48:39 1.323 +++ deliantra/Deliantra-Client/Client.xs 2018/11/19 01:23:01 1.333 @@ -34,6 +34,7 @@ #include #include +#include #define USE_RWOPS 1 // for SDL_mixer:LoadMUS_RW @@ -77,14 +78,8 @@ # include #endif -#if __GNUC__ >= 4 -# define expect(expr,value) __builtin_expect ((expr),(value)) -#else -# define expect(expr,value) (expr) -#endif - -#define expect_false(expr) expect ((expr) != 0, 0) -#define expect_true(expr) expect ((expr) != 0, 1) +#include "ecb.h" +#include "salloc.h" #define OBJ_STR "\xef\xbf\xbc" /* U+FFFC, object replacement character */ @@ -112,7 +107,7 @@ STRLEN len = SvLEN (sv); STRLEN want = SvCUR (sv) + need; - if (expect_false (len < want)) + if (ecb_expect_false (len < want)) { do len *= 2; @@ -206,7 +201,7 @@ float r, g, b, a; // default color for rgba mode int base_height; DC__Font font; - rc_t *rc; + rc_t rc; }; typedef cf_layout *DC__Layout; @@ -257,6 +252,8 @@ *h = rect.height; } +///////////////////////////////////////////////////////////////////////////// + typedef uint16_t tileid; typedef uint16_t faceid; @@ -297,37 +294,34 @@ typedef mapgrid *DC__Map; -static char * -prepend (char *ptr, int sze, int inc) +template +static void +prepend (T *&ptr, int sze, int inc) { - char *p; + T *p; - New (0, p, sze + inc, char); - Zero (p, inc, char); - Move (ptr, p + inc, sze, char); + Newx (p, inc + sze, T); + Zero (p, inc, T); + Move (ptr, p + inc, sze, T); Safefree (ptr); - return p; + ptr = p; } -static char * -append (char *ptr, int sze, int inc) +template +static void +append (T *&ptr, int sze, int inc) { - Renew (ptr, sze + inc, char); - Zero (ptr + sze, inc, char); - - return ptr; + Renew (ptr, sze + inc, T); + Zero (ptr + sze, inc, T); } -#define Append(type,ptr,sze,inc) (ptr) = (type *)append ((char *)ptr, (sze) * sizeof (type), (inc) * sizeof (type)) -#define Prepend(type,ptr,sze,inc) (ptr) = (type *)prepend ((char *)ptr, (sze) * sizeof (type), (inc) * sizeof (type)) - static void need_facenum (struct mapgrid *self, faceid face) { while (self->faces <= face) { - Append (tileid, self->face2tile, self->faces, self->faces); + append (self->face2tile, self->faces, self->faces); self->faces *= 2; } } @@ -337,7 +331,7 @@ { while (self->texs <= texid) { - Append (maptex, self->tex, self->texs, self->texs); + append (self->tex, self->texs, self->texs); self->texs *= 2; } } @@ -348,7 +342,7 @@ if (0 > y) { int extend = - y + MAP_EXTEND_Y; - Prepend (maprow, self->row, self->rows, extend); + prepend (self->row, self->rows, extend); self->rows += extend; self->y += extend; @@ -357,7 +351,7 @@ else if (y >= self->rows) { int extend = y - self->rows + MAP_EXTEND_Y; - Append (maprow, self->row, self->rows, extend); + append (self->row, self->rows, extend); self->rows += extend; } @@ -377,13 +371,13 @@ if (row->c0 > x) { int extend = row->c0 - x + MAP_EXTEND_X; - Prepend (mapcell, row->col, row->c1 - row->c0, extend); + prepend (row->col, row->c1 - row->c0, extend); row->c0 -= extend; } else if (x >= row->c1) { int extend = x - row->c1 + MAP_EXTEND_X; - Append (mapcell, row->col, row->c1 - row->c0, extend); + append (row->col, row->c1 - row->c0, extend); row->c1 += extend; } @@ -537,17 +531,6 @@ return n + 1; } -static unsigned int -popcount (unsigned int n) -{ - n -= (n >> 1) & 0x55555555U; - n = ((n >> 2) & 0x33333333U) + (n & 0x33333333U); - n = ((n >> 4) + n) & 0x0f0f0f0fU; - n *= 0x01010101U; - - return n >> 24; -} - /* SDL should provide this, really. */ #define SDLK_MODIFIER_MIN 300 #define SDLK_MODIFIER_MAX 314 @@ -886,7 +869,17 @@ IV minpot (UV n) +UV ld32 (UV n) + CODE: + RETVAL = ecb_ld32 (n); + OUTPUT: + RETVAL + IV popcount (UV n) + CODE: + RETVAL = ecb_popcount32 (n); + OUTPUT: + RETVAL NV distance (NV dx, NV dy) CODE: @@ -1370,7 +1363,7 @@ DC::Layout new (SV *klass) CODE: - New (0, RETVAL, 1, struct cf_layout); + RETVAL = new cf_layout; RETVAL->pl = pango_layout_new (opengl_context); RETVAL->r = 1.; @@ -1379,7 +1372,6 @@ RETVAL->a = 1.; RETVAL->base_height = MIN_FONT_HEIGHT; RETVAL->font = 0; - RETVAL->rc = rc_alloc (); pango_layout_set_wrap (RETVAL->pl, PANGO_WRAP_WORD_CHAR); layout_update_font (RETVAL); @@ -1390,8 +1382,7 @@ DESTROY (DC::Layout self) CODE: g_object_unref (self->pl); - rc_free (self->rc); - Safefree (self); + delete self; void set_text (DC::Layout self, SV *text_) @@ -1655,10 +1646,10 @@ void render (DC::Layout self, float x, float y, int flags = 0) CODE: - rc_clear (self->rc); + self->rc.clear (); pango_opengl_render_layout_subpixel ( self->pl, - self->rc, + &self->rc, x * PANGO_SCALE, y * PANGO_SCALE, self->r, self->g, self->b, self->a, flags @@ -1679,7 +1670,7 @@ glEnable (GL_ALPHA_TEST); glAlphaFunc (GL_GREATER, 7.f / 255.f); - rc_draw (self->rc); + self->rc.draw (); glDisable (GL_ALPHA_TEST); glDisable (GL_BLEND); @@ -2059,7 +2050,7 @@ while (self->y < 0) { - Prepend (maprow, self->row, self->rows, MAP_EXTEND_Y); + prepend (self->row, self->rows, MAP_EXTEND_Y); self->rows += MAP_EXTEND_Y; self->y += MAP_EXTEND_Y; @@ -2225,15 +2216,13 @@ { int x, y, z; - uint32_t smooth_level[256 / 32]; // one bit for every possible smooth level - static uint8_t smooth_max[256][256]; // egad, fast and wasteful on memory (64k) - smooth_key skey; + static uint8_t smooth_max[256][256]; // egad, fast and wasteful on memory (64k), also, static! int pl_x, pl_y; maptex pl_tex; - rc_t *rc = rc_alloc (); - rc_t *rc_ov = rc_alloc (); + rc_t rc; + rc_t rc_ov; rc_key_t key; - rc_array_t *arr; + rc_t::array_t *arr; pl_tex.name = 0; @@ -2241,9 +2230,6 @@ if (sw > 255) sw = 255; if (sh > 255) sh = 255; - // clear key, in case of extra padding - memset (&skey, 0, sizeof (skey)); - memset (&key, 0, sizeof (key)); key.r = 255; key.g = 255; @@ -2258,7 +2244,7 @@ // first pass: determine smooth_max // rather ugly, if you ask me // could also be stored inside mapcell and updated on change - memset (smooth_max, 0, sizeof (smooth_max)); + memset (smooth_max, 0, sizeof (smooth_max[0]) * (sh + 1)); for (y = 0; y < sh; y++) if (0 <= y + my && y + my < self->rows) @@ -2283,8 +2269,9 @@ for (z = 0; z <= 2; z++) { + std::bitset<256> smooth_level; // one bit for every possible smooth level + smooth_key skey; smooth_hash smooth; - memset (smooth_level, 0, sizeof (smooth_level)); key.texname = -1; for (y = 0; y < sh; y++) @@ -2311,13 +2298,13 @@ tex = self->tex [TEXID_NOFACE]; /* missing, replace by noface */ key.texname = tex.name; - arr = rc_array (rc, &key); + arr = &rc.array (key); } px = (x + 1) * Th - tex.w; py = (y + 1) * Tw - tex.h; - if (expect_false (cell->player == player) && expect_false (z == 2)) + if (ecb_expect_false (cell->player == player) && ecb_expect_false (z == 2)) { pl_x = px; pl_y = py; @@ -2325,10 +2312,10 @@ continue; } - rc_t2f_v3f (arr, 0 , 0 , px , py , 0); - rc_t2f_v3f (arr, 0 , tex.t, px , py + tex.h, 0); - rc_t2f_v3f (arr, tex.s, tex.t, px + tex.w, py + tex.h, 0); - rc_t2f_v3f (arr, tex.s, 0 , px + tex.w, py , 0); + arr->t2f_v3f (0 , 0 , px , py , 0); + arr->t2f_v3f (0 , tex.t, px , py + tex.h, 0); + arr->t2f_v3f (tex.s, tex.t, px + tex.w, py + tex.h, 0); + arr->t2f_v3f (tex.s, 0 , px + tex.w, py , 0); // update smooth hash if (tex.smoothtile) @@ -2336,7 +2323,7 @@ skey.tile = tex.smoothtile; skey.level = tex.smoothlevel; - smooth_level [tex.smoothlevel >> 5] |= ((uint32_t)1) << (tex.smoothlevel & 31); + smooth_level[tex.smoothlevel] = 1; // add bits to current tile and all neighbours. skey.x|y is // shifted +1|+1 so we always stay positive. @@ -2370,31 +2357,30 @@ } } - if (expect_false (z == 2) && expect_false (cell->flags)) + if (ecb_expect_false (z == 2) && ecb_expect_false (cell->flags)) { // overlays such as the speech bubble, probably more to come if (cell->flags & 1) { rc_key_t key_ov = key; - maptex tex = self->tex [TEXID_SPEECH]; - rc_array_t *arr; + maptex tex = self->tex[TEXID_SPEECH]; int px = x * Tw + Tw * 2 / 32; int py = y * Th - Th * 6 / 32; key_ov.texname = tex.name; - arr = rc_array (rc_ov, &key_ov); + rc_t::array_t &arr = rc_ov.array (key_ov); - rc_t2f_v3f (arr, 0 , 0 , px , py , 0); - rc_t2f_v3f (arr, 0 , tex.t, px , py + Th, 0); - rc_t2f_v3f (arr, tex.s, tex.t, px + Tw, py + Th, 0); - rc_t2f_v3f (arr, tex.s, 0 , px + Tw, py , 0); + arr.t2f_v3f (0 , 0 , px , py , 0); + arr.t2f_v3f (0 , tex.t, px , py + Th, 0); + arr.t2f_v3f (tex.s, tex.t, px + Tw, py + Th, 0); + arr.t2f_v3f (tex.s, 0 , px + Tw, py , 0); } } } } - rc_draw (rc); - rc_clear (rc); + rc.draw (); + rc.clear (); // go through all smoothlevels, lowest to highest, then draw. // this is basically counting sort @@ -2403,70 +2389,60 @@ glEnable (GL_TEXTURE_2D); glBegin (GL_QUADS); - for (w = 0; w < 256 / 32; ++w) - { - uint32_t smask = smooth_level [w]; - if (smask) - for (b = 0; b < 32; ++b) - if (smask & (((uint32_t)1) << b)) + for (int level = 0; level < smooth_level.size (); ++level) + if (smooth_level[level]) + for (auto &&it = smooth.begin (); it != smooth.end (); ++it) + { + smooth_key &skey = it->first; + IV bits = it->second; + + if (!(bits & 0x1000) + && skey.level == level + && level > smooth_max [skey.x][skey.y]) { - int level = (w << 5) | b; - HE *he; + maptex tex = self->tex [skey.tile]; + int px = (((int)skey.x) - 1) * Tw; + int py = (((int)skey.y) - 1) * Th; + int border = bits & 15; + int corner = (bits >> 8) & ~(bits >> 4) & 15; + float dx = tex.s * .0625f; // 16 images/row + float dy = tex.t * .5f ; // 2 images/column - for (auto &&it = smooth.begin (); it != smooth.end (); ++it) + if (tex.name) { - smooth_key &skey = it->first; - IV bits = it->second; + // this time avoiding texture state changes + // save gobs of state changes. + if (key.texname != tex.name) + { + self->tex [skey.tile].unused = 0; - if (!(bits & 0x1000) - && skey.level == level - && level > smooth_max [skey.x][skey.y]) + glEnd (); + glBindTexture (GL_TEXTURE_2D, key.texname = tex.name); + glBegin (GL_QUADS); + } + + if (border) { - maptex tex = self->tex [skey.tile]; - int px = (((int)skey.x) - 1) * Tw; - int py = (((int)skey.y) - 1) * Th; - int border = bits & 15; - int corner = (bits >> 8) & ~(bits >> 4) & 15; - float dx = tex.s * .0625f; // 16 images/row - float dy = tex.t * .5f ; // 2 images/column - - if (tex.name) - { - // this time avoiding texture state changes - // save gobs of state changes. - if (key.texname != tex.name) - { - self->tex [skey.tile].unused = 0; - - glEnd (); - glBindTexture (GL_TEXTURE_2D, key.texname = tex.name); - glBegin (GL_QUADS); - } - - if (border) - { - float ox = border * dx; - - glTexCoord2f (ox , 0.f ); glVertex2i (px , py ); - glTexCoord2f (ox , dy ); glVertex2i (px , py + Th); - glTexCoord2f (ox + dx, dy ); glVertex2i (px + Tw, py + Th); - glTexCoord2f (ox + dx, 0.f ); glVertex2i (px + Tw, py ); - } - - if (corner) - { - float ox = corner * dx; - - glTexCoord2f (ox , dy ); glVertex2i (px , py ); - glTexCoord2f (ox , dy * 2.f); glVertex2i (px , py + Th); - glTexCoord2f (ox + dx, dy * 2.f); glVertex2i (px + Tw, py + Th); - glTexCoord2f (ox + dx, dy ); glVertex2i (px + Tw, py ); - } - } + float ox = border * dx; + + glTexCoord2f (ox , 0.f ); glVertex2i (px , py ); + glTexCoord2f (ox , dy ); glVertex2i (px , py + Th); + glTexCoord2f (ox + dx, dy ); glVertex2i (px + Tw, py + Th); + glTexCoord2f (ox + dx, 0.f ); glVertex2i (px + Tw, py ); + } + + if (corner) + { + float ox = corner * dx; + + glTexCoord2f (ox , dy ); glVertex2i (px , py ); + glTexCoord2f (ox , dy * 2.f); glVertex2i (px , py + Th); + glTexCoord2f (ox + dx, dy * 2.f); glVertex2i (px + Tw, py + Th); + glTexCoord2f (ox + dx, dy ); glVertex2i (px + Tw, py ); } } } - } + } glEnd (); glDisable (GL_TEXTURE_2D); @@ -2481,22 +2457,20 @@ int py = pl_y + sdy; key.texname = tex.name; - arr = rc_array (rc, &key); + rc_t::array_t &arr = rc.array (key); - rc_t2f_v3f (arr, 0 , 0 , px , py , 0); - rc_t2f_v3f (arr, 0 , tex.t, px , py + tex.h, 0); - rc_t2f_v3f (arr, tex.s, tex.t, px + tex.w, py + tex.h, 0); - rc_t2f_v3f (arr, tex.s, 0 , px + tex.w, py , 0); + arr.t2f_v3f (0 , 0 , px , py , 0); + arr.t2f_v3f (0 , tex.t, px , py + tex.h, 0); + arr.t2f_v3f (tex.s, tex.t, px + tex.w, py + tex.h, 0); + arr.t2f_v3f (tex.s, 0 , px + tex.w, py , 0); - rc_draw (rc); + rc.draw (); } - rc_draw (rc_ov); - rc_clear (rc_ov); + rc_ov.draw (); + rc_ov.clear (); glDisable (GL_BLEND); - rc_free (rc); - rc_free (rc_ov); // top layer: overlays such as the health bar for (y = 0; y < sh; y++) @@ -2512,7 +2486,7 @@ int px = x * Tw; int py = y * Th; - if (expect_false (cell->player == player)) + if (ecb_expect_false (cell->player == player)) { px += sdx; py += sdy;