--- deliantra/Deliantra-Client/Client.xs 2006/09/21 00:16:42 1.155 +++ deliantra/Deliantra-Client/Client.xs 2007/04/21 23:54:23 1.189 @@ -1,9 +1,11 @@ #ifdef _WIN32 # define WIN32_LEAN_AND_MEAN -# define _WIN32_WINNT 0x0500 // needed to get win2000 api calls +# define NTDDI_VERSION NTDDI_WIN2K // needed to get win2000 api calls # include # include +# include # pragma warning(disable:4244) +# pragma warning(disable:4761) #endif #include "EXTERN.h" @@ -14,6 +16,7 @@ # undef pipe #endif +#include #include #include #include @@ -38,16 +41,9 @@ # include # include # include -#else - typedef unsigned char uint8_t; - typedef unsigned short uint16_t; - typedef unsigned int uint32_t; - typedef signed char int8_t; - typedef signed short int16_t; - typedef signed int int32_t; #endif -#define OBJ_STR "\xef\xbf\xbc" /* U+FFFC, objetc replacement character */ +#define OBJ_STR "\xef\xbf\xbc" /* U+FFFC, object replacement character */ #define FOW_DARKNESS 32 @@ -62,6 +58,8 @@ # define PARACHUTE 0 #endif +static AV *texture_av; + static struct { #define GL_FUNC(ptr,name) ptr name; @@ -161,20 +159,23 @@ *h = rect.height; } -typedef uint16_t mapface; +typedef uint16_t tileid; +typedef uint16_t faceid; typedef struct { - GLint name; + int name; int w, h; float s, t; uint8_t r, g, b, a; + tileid smoothtile; + uint8_t smoothlevel; } maptex; typedef struct { uint32_t player; - mapface face[3]; + tileid tile[3]; uint16_t darkness; - uint8_t stat_width, stat_hp, flags; + uint8_t stat_width, stat_hp, flags, smoothmax; } mapcell; typedef struct { @@ -185,11 +186,8 @@ typedef struct map { int x, y, w, h; int ox, oy; /* offset to virtual global coordinate system */ - int faces; - mapface *face; - - int texs; - maptex *tex; + int faces; tileid *face2tile; // [faceid] + int texs; maptex *tex; // [tileid] int32_t rows; maprow *row; @@ -220,6 +218,26 @@ #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 map *self, faceid face) +{ + while (self->faces <= face) + { + Append (tileid, self->face2tile, self->faces, self->faces); + self->faces *= 2; + } +} + +static void +need_texid (struct map *self, int texid) +{ + while (self->texs <= texid) + { + Append (maptex, self->tex, self->texs, self->texs); + self->texs *= 2; + } +} + static maprow * map_get_row (CFPlus__Map self, int y) { @@ -323,6 +341,22 @@ } } +typedef struct { + tileid tile; + uint8_t x, y, level; +} smooth_key; + +static void +smooth_or_bits (HV *hv, smooth_key *key, IV bits) +{ + SV **sv = hv_fetch (hv, (char *)key, sizeof (*key), 1); + + if (SvIOK (*sv)) + SvIV_set (*sv, SvIVX (*sv) | bits); + else + sv_setiv (*sv, bits); +} + static void music_finished (void) { @@ -366,6 +400,10 @@ return n + 1; } +/* SDL should provide this, really. */ +#define SDLK_MODIFIER_MIN 300 +#define SDLK_MODIFIER_MAX 314 + MODULE = CFPlus PACKAGE = CFPlus PROTOTYPES: ENABLE @@ -378,6 +416,9 @@ IV iv; } *civ, const_iv[] = { # define const_iv(name) { # name, (IV)name } + const_iv (SDLK_MODIFIER_MIN), + const_iv (SDLK_MODIFIER_MAX), + const_iv (SDL_ACTIVEEVENT), const_iv (SDL_KEYDOWN), const_iv (SDL_KEYUP), @@ -396,6 +437,11 @@ const_iv (SDL_VIDEORESIZE), const_iv (SDL_VIDEOEXPOSE), const_iv (SDL_USEREVENT), + + const_iv (SDL_APPINPUTFOCUS), + const_iv (SDL_APPMOUSEFOCUS), + const_iv (SDL_APPACTIVE), + const_iv (SDLK_KP0), const_iv (SDLK_KP1), const_iv (SDLK_KP2), @@ -460,29 +506,39 @@ const_iv (SDLK_POWER), const_iv (SDLK_EURO), const_iv (SDLK_UNDO), + const_iv (KMOD_NONE), + const_iv (KMOD_SHIFT), const_iv (KMOD_LSHIFT), const_iv (KMOD_RSHIFT), + const_iv (KMOD_CTRL), const_iv (KMOD_LCTRL), const_iv (KMOD_RCTRL), + const_iv (KMOD_ALT), const_iv (KMOD_LALT), const_iv (KMOD_RALT), + const_iv (KMOD_META), const_iv (KMOD_LMETA), const_iv (KMOD_RMETA), const_iv (KMOD_NUM), const_iv (KMOD_CAPS), const_iv (KMOD_MODE), - const_iv (KMOD_CTRL), - const_iv (KMOD_SHIFT), - const_iv (KMOD_ALT), - const_iv (KMOD_META) # undef const_iv }; for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); + + assert (SDLK_MODIFIER_MIN == SDLK_NUMLOCK); + assert (SDLK_MODIFIER_MAX == SDLK_COMPOSE); } +void +weaken (SV *rv) + PROTOTYPE: $ + CODE: + sv_rvweaken (rv); + int in_destruct () CODE: @@ -562,10 +618,13 @@ ); if (RETVAL) { + av_clear (texture_av); + SDL_WM_SetCaption ("Crossfire+ Client " VERSION, "Crossfire+"); # define GL_FUNC(ptr,name) gl.name = (ptr)SDL_GL_GetProcAddress ("gl" # name); # include "glfunc.h" # undef GL_FUNC + } OUTPUT: RETVAL @@ -594,6 +653,7 @@ hv_store (hv, "state", 5, newSViv (ev.key.state), 0); hv_store (hv, "sym", 3, newSViv (ev.key.keysym.sym), 0); hv_store (hv, "mod", 3, newSViv (ev.key.keysym.mod), 0); + hv_store (hv, "cmod", 4, newSViv (SDL_GetModState ()), 0); /* current mode */ hv_store (hv, "unicode", 7, newSViv (ev.key.keysym.unicode), 0); break; @@ -648,9 +708,36 @@ void lowdelay (int fd, int val = 1) CODE: -#ifndef _WIN32 - setsockopt (fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof (val)); + setsockopt (fd, IPPROTO_TCP, TCP_NODELAY, (void *)&val, sizeof (val)); + +void +win32_proxy_info () + PPCODE: +{ +#ifdef _WIN32 + char buffer[2048]; + DWORD buflen; + + EXTEND (SP, 3); + + buflen = sizeof (buffer); + if (InternetQueryOption (0, INTERNET_OPTION_PROXY, (void *)buffer, &buflen)) + if (((INTERNET_PROXY_INFO *)buffer)->dwAccessType == INTERNET_OPEN_TYPE_PROXY) + { + PUSHs (newSVpv (((INTERNET_PROXY_INFO *)buffer)->lpszProxy, 0)); + + buflen = sizeof (buffer); + if (InternetQueryOption (0, INTERNET_OPTION_PROXY_USERNAME, (void *)buffer, &buflen)) + { + PUSHs (newSVpv (buffer, 0)); + + buflen = sizeof (buffer); + if (InternetQueryOption (0, INTERNET_OPTION_PROXY_PASSWORD, (void *)buffer, &buflen)) + PUSHs (newSVpv (buffer, 0)); + } + } #endif +} void add_font (char *file) @@ -764,9 +851,13 @@ _exit (1); void -_exit (int retval) +_exit (int retval = 0) CODE: +#ifdef WIN32 + ExitThread (retval); // unclean, please beam me up +#else _exit (retval); +#endif MODULE = CFPlus PACKAGE = CFPlus::Font @@ -1023,7 +1114,7 @@ { int index, trailing; pango_layout_xy_to_index (self->pl, x * PANGO_SCALE, y * PANGO_SCALE, &index, &trailing); - RETVAL = index; + RETVAL = index + trailing; } OUTPUT: RETVAL @@ -1042,6 +1133,45 @@ } void +index_to_line_x (CFPlus::Layout self, int index, int trailing = 0) + PPCODE: +{ + int line, x; + + pango_layout_index_to_line_x (self->pl, index, trailing, &line, &x); + /* pango bug: line is between 1..numlines, not 0..numlines-1 */ + + EXTEND (SP, 2); + PUSHs (sv_2mortal (newSViv (line - 1))); + PUSHs (sv_2mortal (newSViv (x / PANGO_SCALE))); +} + +void +line_x_to_index (CFPlus::Layout self, int line, int x) + PPCODE: +{ + PangoLayoutLine *lp; + int index, trailing; + + if (line < 0) + XSRETURN_EMPTY; + + if (!(lp = pango_layout_get_line (self->pl, line))) + XSRETURN_EMPTY; /* do better */ + + pango_layout_line_x_to_index (lp, x * PANGO_SCALE, &index, &trailing); + + EXTEND (SP, 2); + if (GIMME_V == G_SCALAR) + PUSHs (sv_2mortal (newSViv (index + trailing))); + else + { + PUSHs (sv_2mortal (newSViv (index))); + PUSHs (sv_2mortal (newSViv (trailing))); + } +} + +void render (CFPlus::Layout self, float x, float y, int flags = 0) PPCODE: pango_opengl_render_layout_subpixel ( @@ -1142,19 +1272,17 @@ MODULE = CFPlus PACKAGE = CFPlus::Map CFPlus::Map -new (SV *class, int map_width, int map_height) +new (SV *class) CODE: New (0, RETVAL, 1, struct map); RETVAL->x = 0; RETVAL->y = 0; - RETVAL->w = map_width; - RETVAL->h = map_height; + RETVAL->w = 0; + RETVAL->h = 0; RETVAL->ox = 0; RETVAL->oy = 0; - RETVAL->faces = 8192; - Newz (0, RETVAL->face, RETVAL->faces, mapface); - RETVAL->texs = 8192; - Newz (0, RETVAL->tex, RETVAL->texs, maptex); + RETVAL->faces = 8192; Newz (0, RETVAL->face2tile, RETVAL->faces, tileid); + RETVAL->texs = 8192; Newz (0, RETVAL->tex , RETVAL->texs , maptex); RETVAL->rows = 0; RETVAL->row = 0; OUTPUT: @@ -1165,38 +1293,58 @@ CODE: { map_clear (self); - Safefree (self->face); + Safefree (self->face2tile); Safefree (self->tex); Safefree (self); } void +resize (CFPlus::Map self, int map_width, int map_height) + CODE: + self->w = map_width; + self->h = map_height; + +void clear (CFPlus::Map self) CODE: map_clear (self); void -set_face (CFPlus::Map self, int face, int texid) +set_tileid (CFPlus::Map self, int face, int tile) CODE: { - while (self->faces <= face) - { - Append (mapface, self->face, self->faces, self->faces); - self->faces *= 2; - } + need_facenum (self, face); self->face2tile [face] = tile; + need_texid (self, tile); +} - self->face [face] = texid; +void +set_smooth (CFPlus::Map self, int face, int smooth, int level) + CODE: +{ + tileid texid; + maptex *tex; + + if (face < 0 || face >= self->faces) + return; + + if (smooth < 0 || smooth >= self->faces) + return; + + texid = self->face2tile [face]; + + if (!texid) + return; + + tex = self->tex + texid; + tex->smoothtile = self->face2tile [smooth]; + tex->smoothlevel = level; } void set_texture (CFPlus::Map self, int texid, int name, int w, int h, float s, float t, int r, int g, int b, int a) CODE: { - while (self->texs <= texid) - { - Append (maptex, self->tex, self->texs, self->texs); - self->texs *= 2; - } + need_texid (self, texid); { maptex *tex = self->tex + texid; @@ -1250,12 +1398,12 @@ if (dx > 0) map_blank (self, self->x, self->y, dx, self->h); else if (dx < 0) - map_blank (self, self->x + self->w + dx + 1, self->y, -dx, self->h); + map_blank (self, self->x + self->w + dx, self->y, -dx, self->h); if (dy > 0) map_blank (self, self->x, self->y, self->w, dy); else if (dy < 0) - map_blank (self, self->x, self->y + self->h + dy + 1, self->w, -dy); + map_blank (self, self->x, self->y + self->h + dy, self->w, -dy); self->ox += dx; self->x += dx; self->oy += dy; self->y += dy; @@ -1317,10 +1465,10 @@ } else if (cmd == 6) // monster width cell->stat_width = *data++ + 1; - else if (cmd == 0x47) // monster width + else if (cmd == 0x47) { if (*data == 4) - ; // decode player tag + ; // decode player count data += *data + 1; } @@ -1335,22 +1483,27 @@ } else cell->darkness = *data++ + 1; - } if (flags & 4) { - cell->face [0] = self->face [(data [0] << 8) + data [1]]; data += 2; + faceid face = (data [0] << 8) + data [1]; data += 2; + need_facenum (self, face); + cell->tile [0] = self->face2tile [face]; } if (flags & 2) { - cell->face [1] = self->face [(data [0] << 8) + data [1]]; data += 2; + faceid face = (data [0] << 8) + data [1]; data += 2; + need_facenum (self, face); + cell->tile [1] = self->face2tile [face]; } if (flags & 1) { - cell->face [2] = self->face [(data [0] << 8) + data [1]]; data += 2; + faceid face = (data [0] << 8) + data [1]; data += 2; + need_facenum (self, face); + cell->tile [2] = self->face2tile [face]; } } else @@ -1390,19 +1543,14 @@ for (z = 0; z <= 0; z++) { - mapface face = cell->face [z]; - - if (face) - { - maptex tex = self->tex [face]; - int a0 = 255 - tex.a; - int a1 = tex.a; - - r = (r * a0 + tex.r * a1) / 255; - g = (g * a0 + tex.g * a1) / 255; - b = (b * a0 + tex.b * a1) / 255; - a = (a * a0 + tex.a * a1) / 255; - } + maptex tex = self->tex [cell->tile [z]]; + int a0 = 255 - tex.a; + int a1 = tex.a; + + r = (r * a0 + tex.r * a1) / 255; + g = (g * a0 + tex.g * a1) / 255; + b = (b * a0 + tex.b * a1) / 255; + a = (a * a0 + tex.a * a1) / 255; } } @@ -1419,26 +1567,22 @@ RETVAL void -draw (CFPlus::Map self, int shift_x, int shift_y, int x0, int y0, int sw, int sh) +draw (CFPlus::Map self, int mx, int my, int sw, int sh, int T) CODE: { - int vx, vy; + HV *smooth = (HV *)sv_2mortal ((SV *)newHV ()); + uint32_t smooth_level[256 / 32]; // one bit for every possible smooth level + static uint8_t smooth_max[256][256]; // egad, fats and wasteful on memory (64k) + smooth_key skey; int x, y, z; int last_name; - mapface face; - vx = self->x + self->w / 2 - sw / 2 - shift_x; - vy = self->y + self->h / 2 - sh / 2 - shift_y; + // thats current max. sorry. + if (sw > 255) sw = 255; + if (sh > 255) sh = 255; - /* - int vx = self->vx = self->w >= sw - ? self->x + (self->w - sw) / 2 - : MIN (self->x, MAX (self->x + self->w - sw + 1, self->vx)); - - int vy = self->vy = self->h >= sh - ? self->y + (self->h - sh) / 2 - : MIN (self->y, MAX (self->y + self->h - sh + 1, self->vy)); - */ + // clear key, in case of extra padding + memset (&skey, 0, sizeof (skey)); glColor4ub (255, 255, 255, 255); @@ -1451,58 +1595,202 @@ last_name = 0; - for (z = 0; z < 3; z++) - for (y = 0; y < sh; y++) - if (0 <= y + vy && y + vy < self->rows) - { - maprow *row = self->row + (y + vy); + mx += self->x; + my += self->y; - for (x = 0; x < sw; x++) - if (row->c0 <= x + vx && x + vx < row->c1) - { - mapcell *cell = row->col + (x + vx - row->c0); + // 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)); - face = cell->face [z]; + for (y = 0; y < sh; y++) + if (0 <= y + my && y + my < self->rows) + { + maprow *row = self->row + (y + my); - if (face && face < self->texs) - { - maptex tex = self->tex [face]; - int px = (x + 1) * 32 - tex.w; - int py = (y + 1) * 32 - tex.h; + for (x = 0; x < sw; x++) + if (row->c0 <= x + mx && x + mx < row->c1) + { + mapcell *cell = row->col + (x + mx - row->c0); - if (last_name != tex.name) - { - glEnd (); - glBindTexture (GL_TEXTURE_2D, last_name = tex.name); - glBegin (GL_QUADS); - } + smooth_max[x + 1][y + 1] = + MAX (self->tex [cell->tile [0]].smoothlevel, + MAX (self->tex [cell->tile [1]].smoothlevel, + self->tex [cell->tile [2]].smoothlevel)); + } + } - glTexCoord2f (0 , 0 ); glVertex2f (px , py ); - glTexCoord2f (0 , tex.t); glVertex2f (px , py + tex.h); - glTexCoord2f (tex.s, tex.t); glVertex2f (px + tex.w, py + tex.h); - glTexCoord2f (tex.s, 0 ); glVertex2f (px + tex.w, py ); - } + for (z = 0; z <= 2; z++) + { + memset (smooth_level, 0, sizeof (smooth_level)); - if (cell->flags && z == 2) + for (y = 0; y < sh; y++) + if (0 <= y + my && y + my < self->rows) + { + maprow *row = self->row + (y + my); + + for (x = 0; x < sw; x++) + if (row->c0 <= x + mx && x + mx < row->c1) + { + mapcell *cell = row->col + (x + mx - row->c0); + tileid tile = cell->tile [z]; + + if (tile) + { + maptex tex = self->tex [tile]; + int px = (x + 1) * T - tex.w; + int py = (y + 1) * T - tex.h; + + // suppressing texture state switches here + // is only moderately effective, but worth the extra effort + if (last_name != tex.name) + { + if (!tex.name) + tex = self->tex [2]; /* missing, replace by noface */ + + glEnd (); + glBindTexture (GL_TEXTURE_2D, last_name = tex.name); + glBegin (GL_QUADS); + } + + glTexCoord2f (0 , 0 ); glVertex2f (px , py ); + glTexCoord2f (0 , tex.t); glVertex2f (px , py + tex.h); + glTexCoord2f (tex.s, tex.t); glVertex2f (px + tex.w, py + tex.h); + glTexCoord2f (tex.s, 0 ); glVertex2f (px + tex.w, py ); + + if (cell->flags && z == 2) + { + if (cell->flags & 1) + { + maptex tex = self->tex [1]; + int px = x * T + T * 2 / 32; + int py = y * T - T * 6 / 32; + + glEnd (); + glBindTexture (GL_TEXTURE_2D, last_name = tex.name); + glBegin (GL_QUADS); + + glTexCoord2f (0 , 0 ); glVertex2f (px , py ); + glTexCoord2f (0 , tex.t); glVertex2f (px , py + T); + glTexCoord2f (tex.s, tex.t); glVertex2f (px + T, py + T); + glTexCoord2f (tex.s, 0 ); glVertex2f (px + T, py ); + } + } + + // update smooth hash + if (tex.smoothtile) + { + skey.tile = tex.smoothtile; + skey.level = tex.smoothlevel; + + smooth_level [tex.smoothlevel >> 5] |= ((uint32_t)1) << (tex.smoothlevel & 31); + + // add bits to current tile and all neighbours. skey.x|y is + // shifted +1|+1 so we always stay positive. + + // bits is ___n cccc CCCC bbbb + // n do not draw borders&corners + // c draw these corners, but... + // C ... not these + // b draw these borders + + // borders: 1 ┃· 2 ━━ 4 ·┃ 8 ·· + // ┃· ·· ·┃ ━━ + + // corners: 1 ┛· 2 ·┗ 4 ·· 8 ·· + // ·· ·· ·┏ ┓· + + // full tile + skey.x = x + 1; skey.y = y + 1; smooth_or_bits (smooth, &skey, 0x1000); + + // borders + skey.x = x + 2; skey.y = y + 1; smooth_or_bits (smooth, &skey, 0x0091); + skey.x = x + 1; skey.y = y + 2; smooth_or_bits (smooth, &skey, 0x0032); + skey.x = x ; skey.y = y + 1; smooth_or_bits (smooth, &skey, 0x0064); + skey.x = x + 1; skey.y = y ; smooth_or_bits (smooth, &skey, 0x00c8); + + // corners + skey.x = x + 2; skey.y = y + 2; smooth_or_bits (smooth, &skey, 0x0100); + skey.x = x ; skey.y = y + 2; smooth_or_bits (smooth, &skey, 0x0200); + skey.x = x ; skey.y = y ; smooth_or_bits (smooth, &skey, 0x0400); + skey.x = x + 2; skey.y = y ; smooth_or_bits (smooth, &skey, 0x0800); + } + } + } + } + + // go through all smoothlevels, lowest to highest, then draw. + // this is basically counting sort + { + int w, b; + + 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)) { - if (cell->flags & 1) + int level = (w << 5) | b; + HE *he; + + hv_iterinit (smooth); + while ((he = hv_iternext (smooth))) { - maptex tex = self->tex [1]; - int px = (x + 1) * 32 - tex.w + 2; - int py = (y + 1) * 32 - tex.h - 6; - - glEnd (); - glBindTexture (GL_TEXTURE_2D, last_name = tex.name); - glBegin (GL_QUADS); - - glTexCoord2f (0 , 0 ); glVertex2f (px , py ); - glTexCoord2f (0 , tex.t); glVertex2f (px , py + tex.h); - glTexCoord2f (tex.s, tex.t); glVertex2f (px + tex.w, py + tex.h); - glTexCoord2f (tex.s, 0 ); glVertex2f (px + tex.w, py ); + smooth_key *skey = (smooth_key *)HeKEY (he); + IV bits = SvIVX (HeVAL (he)); + + if (!(bits & 0x1000) + && skey->level == level + && level >= smooth_max [skey->x][skey->y]) + { + maptex tex = self->tex [skey->tile]; + int px = (((int)skey->x) - 1) * T; + int py = (((int)skey->y) - 1) * T; + 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 + + // this time naively avoiding texture state changes + // save gobs of state changes. + if (last_name != tex.name) + { + if (!tex.name) + continue; // smoothing not yet available + + glEnd (); + glBindTexture (GL_TEXTURE_2D, last_name = tex.name); + glBegin (GL_QUADS); + } + + if (border) + { + float ox = border * dx; + + glTexCoord2f (ox , 0.f ); glVertex2f (px , py ); + glTexCoord2f (ox , dy ); glVertex2f (px , py + T); + glTexCoord2f (ox + dx, dy ); glVertex2f (px + T, py + T); + glTexCoord2f (ox + dx, 0.f ); glVertex2f (px + T, py ); + } + + if (corner) + { + float ox = corner * dx; + + glTexCoord2f (ox , dy ); glVertex2f (px , py ); + glTexCoord2f (ox , dy * 2.f); glVertex2f (px , py + T); + glTexCoord2f (ox + dx, dy * 2.f); glVertex2f (px + T, py + T); + glTexCoord2f (ox + dx, dy ); glVertex2f (px + T, py ); + } + } } } - } - } + } + } + + hv_clear (smooth); + } glEnd (); @@ -1511,22 +1799,22 @@ // top layer: overlays such as the health bar for (y = 0; y < sh; y++) - if (0 <= y + vy && y + vy < self->rows) + if (0 <= y + my && y + my < self->rows) { - maprow *row = self->row + (y + vy); + maprow *row = self->row + (y + my); for (x = 0; x < sw; x++) - if (row->c0 <= x + vx && x + vx < row->c1) + if (row->c0 <= x + mx && x + mx < row->c1) { - mapcell *cell = row->col + (x + vx - row->c0); + mapcell *cell = row->col + (x + mx - row->c0); - int px = x * 32; - int py = y * 32; + int px = x * T; + int py = y * T; if (cell->stat_hp) { - int width = cell->stat_width * 32; - int thick = sh / 28 + 1 + cell->stat_width; + int width = cell->stat_width * T; + int thick = (sh * T / 32 + 27) / 28 + 1 + cell->stat_width; glColor3ub (0, 0, 0); glRectf (px + 1, py - thick - 2, @@ -1602,10 +1890,9 @@ } void -fow_texture (CFPlus::Map self, int shift_x, int shift_y, int x0, int y0, int sw, int sh) +fow_texture (CFPlus::Map self, int mx, int my, int sw, int sh) PPCODE: { - int vx, vy; int x, y; int sw4 = (sw + 3) & ~3; SV *darkness_sv = sv_2mortal (newSV (sw4 * sh)); @@ -1615,18 +1902,18 @@ SvPOK_only (darkness_sv); SvCUR_set (darkness_sv, sw4 * sh); - vx = self->x + (self->w - sw + 1) / 2 - shift_x; - vy = self->y + (self->h - sh + 1) / 2 - shift_y; + mx += self->x; + my += self->y; for (y = 0; y < sh; y++) - if (0 <= y + vy && y + vy < self->rows) + if (0 <= y + my && y + my < self->rows) { - maprow *row = self->row + (y + vy); + maprow *row = self->row + (y + my); for (x = 0; x < sw; x++) - if (row->c0 <= x + vx && x + vx < row->c1) + if (row->c0 <= x + mx && x + mx < row->c1) { - mapcell *cell = row->col + (x + vx - row->c0); + mapcell *cell = row->col + (x + mx - row->c0); darkness[y * sw4 + x] = cell->darkness ? 255 - (cell->darkness - 1) @@ -1676,28 +1963,31 @@ mapcell *cell = row->col + (x - row->c0); uint8_t flags = 0; - if (cell->face [0]) flags |= 1; - if (cell->face [1]) flags |= 2; - if (cell->face [2]) flags |= 4; + if (cell->tile [0]) flags |= 1; + if (cell->tile [1]) flags |= 2; + if (cell->tile [2]) flags |= 4; *data++ = flags; if (flags & 1) { - *data++ = cell->face [0] >> 8; - *data++ = cell->face [0]; + tileid tile = cell->tile [0]; + *data++ = tile >> 8; + *data++ = tile; } if (flags & 2) { - *data++ = cell->face [1] >> 8; - *data++ = cell->face [1]; + tileid tile = cell->tile [1]; + *data++ = tile >> 8; + *data++ = tile; } if (flags & 4) { - *data++ = cell->face [2] >> 8; - *data++ = cell->face [2]; + tileid tile = cell->tile [2]; + *data++ = tile >> 8; + *data++ = tile; } } else @@ -1747,13 +2037,12 @@ if (flags) { - mapface face[3] = { 0, 0, 0 }; - mapcell *cell = row_get_cell (row, x); + tileid tile[3] = { 0, 0, 0 }; - if (flags & 1) { face[0] = *data++ << 8; face[0] |= *data++; } - if (flags & 2) { face[1] = *data++ << 8; face[1] |= *data++; } - if (flags & 4) { face[2] = *data++ << 8; face[2] |= *data++; } + if (flags & 1) { tile[0] = *data++ << 8; tile[0] |= *data++; } + if (flags & 2) { tile[1] = *data++ << 8; tile[1] |= *data++; } + if (flags & 4) { tile[2] = *data++ << 8; tile[2] |= *data++; } if (cell->darkness == 0) { @@ -1761,10 +2050,15 @@ for (z = 0; z <= 2; z++) { - cell->face[z] = face[z]; + tileid t = tile [z]; - if (face[z] && (face[z] >= self->texs || !self->tex[face [z]].name)) - XPUSHs (sv_2mortal (newSViv (face[z]))); + if (t >= self->texs || (t && !self->tex [t].name)) + { + XPUSHs (sv_2mortal (newSViv (t))); + need_texid (self, t); + } + + cell->tile [z] = t; } } } @@ -1923,6 +2217,9 @@ for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); + + texture_av = newAV (); + AvREAL_off (texture_av); } char * @@ -2083,7 +2380,10 @@ CODE: { GLuint name; - glGenTextures (1, &name); + if (AvFILL (texture_av) >= 0) + name = (GLuint)(size_t)av_pop (texture_av); + else + glGenTextures (1, &name); RETVAL = name; } OUTPUT: @@ -2092,8 +2392,12 @@ void glDeleteTexture (int name) CODE: { - GLuint name_ = name; - glDeleteTextures (1, &name_); + /* make a half-assed attempt at returning the memory used by the texture */ + /* textures are frequently being reused by cfplus anyway */ + glBindTexture (GL_TEXTURE_2D, name); + glTexImage2D (GL_TEXTURE_2D, 0, GL_ALPHA, 0, 0, 0, GL_ALPHA, GL_UNSIGNED_BYTE, 0); + av_push (texture_av, (SV *)(size_t)name); + /*glDeleteTextures (1, &name_);*/ } int glGenList ()