--- deliantra/Deliantra-Client/Client.xs 2006/07/05 03:01:01 1.128 +++ deliantra/Deliantra-Client/Client.xs 2007/07/12 17:56:50 1.194 @@ -1,14 +1,27 @@ #ifdef _WIN32 -# define _WIN32_WINNT 0x0500 // needed to get win2000 api calls +# define WIN32_LEAN_AND_MEAN +# define NTDDI_VERSION NTDDI_WIN2K // needed to get win2000 api calls # include # include +# include # pragma warning(disable:4244) +# pragma warning(disable:4761) +#endif + +//#define DEBUG 1 +#if DEBUG +# include #endif #include "EXTERN.h" #include "perl.h" #include "XSUB.h" +#ifdef _WIN32 +# undef pipe +#endif + +#include #include #include #include @@ -33,18 +46,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 -#include "glext.h" - -#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 @@ -59,6 +63,8 @@ # define PARACHUTE 0 #endif +static AV *texture_av; + static struct { #define GL_FUNC(ptr,name) ptr name; @@ -66,7 +72,8 @@ #undef GL_FUNC } gl; -static void gl_BlendFuncSeparate (GLenum sa, GLenum da, GLenum saa, GLenum daa) +static void +gl_BlendFuncSeparate (GLenum sa, GLenum da, GLenum saa, GLenum daa) { if (gl.BlendFuncSeparate) gl.BlendFuncSeparate (sa, da, saa, daa); @@ -76,16 +83,40 @@ glBlendFunc (sa, da); } +static GLuint +gen_texture () +{ + GLuint name; + + if (AvFILL (texture_av) >= 0) + name = (GLuint)(size_t)av_pop (texture_av); + else + glGenTextures (1, &name); + + return name; +} + +static void +del_texture (GLuint 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); +} + #include "texcache.c" #include "pango-font.c" #include "pango-fontmap.c" #include "pango-render.c" -typedef Mix_Chunk *CFClient__MixChunk; -typedef Mix_Music *CFClient__MixMusic; +typedef Mix_Chunk *CFPlus__MixChunk; +typedef Mix_Music *CFPlus__MixMusic; -typedef PangoFontDescription *CFClient__Font; +typedef PangoFontDescription *CFPlus__Font; static int shape_attr_p (PangoLayoutRun *run) @@ -109,10 +140,10 @@ PangoLayout *pl; float r, g, b, a; // default color for rgba mode int base_height; - CFClient__Font font; -} *CFClient__Layout; + CFPlus__Font font; +} *CFPlus__Layout; -static CFClient__Font default_font; +static CFPlus__Font default_font; static PangoContext *opengl_context; static PangoFontMap *opengl_fontmap; @@ -127,7 +158,7 @@ } static void -layout_update_font (CFClient__Layout self) +layout_update_font (CFPlus__Layout self) { /* use a random scale factor to account for unknown descenders, 0.8 works * reasonably well with bitstream vera @@ -141,28 +172,40 @@ } static void -layout_get_pixel_size (CFClient__Layout self, int *w, int *h) +layout_get_pixel_size (CFPlus__Layout self, int *w, int *h) { - pango_layout_get_pixel_size (self->pl, w, h); + PangoRectangle rect; - if (!*w) *w = 1; - if (!*h) *h = 1; + // get_pixel_* wrongly rounds down + pango_layout_get_extents (self->pl, 0, &rect); - *w = (*w + 3) & ~3; + rect.width = (rect.width + PANGO_SCALE - 1) / PANGO_SCALE; + rect.height = (rect.height + PANGO_SCALE - 1) / PANGO_SCALE; + + if (!rect.width) rect.width = 1; + if (!rect.height) rect.height = 1; + + *w = rect.width; + *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 { - int16_t darkness; - mapface face[3]; + uint32_t player; + tileid tile[3]; + uint16_t darkness; + uint8_t stat_width, stat_hp, flags, smoothmax; } mapcell; typedef struct { @@ -173,15 +216,12 @@ 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; -} *CFClient__Map; +} *CFPlus__Map; static char * prepend (char *ptr, int sze, int inc) @@ -208,8 +248,28 @@ #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 (CFClient__Map self, int y) +map_get_row (CFPlus__Map self, int y) { if (0 > y) { @@ -257,13 +317,13 @@ } static mapcell * -map_get_cell (CFClient__Map self, int x, int y) +map_get_cell (CFPlus__Map self, int x, int y) { return row_get_cell (map_get_row (self, y), x); } static void -map_clear (CFClient__Map self) +map_clear (CFPlus__Map self) { int r; @@ -281,10 +341,11 @@ } static void -map_blank (CFClient__Map self, int x0, int y0, int w, int h) +map_blank (CFPlus__Map self, int x0, int y0, int w, int h) { int x, y; maprow *row; + mapcell *cell; for (y = y0; y < y0 + h; y++) if (y >= 0) @@ -300,11 +361,32 @@ if (x >= row->c1) break; - row->col[x - row->c0].darkness = -1; + cell = row->col + x - row->c0; + + cell->darkness = 0; + cell->stat_hp = 0; + cell->flags = 0; + cell->player = 0; } } } +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) { @@ -348,18 +430,25 @@ return n + 1; } -MODULE = CFClient PACKAGE = CFClient +/* SDL should provide this, really. */ +#define SDLK_MODIFIER_MIN 300 +#define SDLK_MODIFIER_MAX 314 + +MODULE = CFPlus PACKAGE = CFPlus PROTOTYPES: ENABLE BOOT: { - HV *stash = gv_stashpv ("CFClient", 1); + HV *stash = gv_stashpv ("CFPlus", 1); static const struct { const char *name; 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), @@ -378,6 +467,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), @@ -442,29 +536,46 @@ 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: + RETVAL = PL_main_cv == Nullcv; + OUTPUT: + RETVAL + NV floor (NV x) NV ceil (NV x) @@ -504,6 +615,10 @@ SDL_GL_SetAttribute (SDL_GL_ACCUM_ALPHA_SIZE, 0); SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1); +#if SDL_VERSION_ATLEAST(1,2,10) + SDL_GL_SetAttribute (SDL_GL_ACCELERATED_VISUAL, 1); + SDL_GL_SetAttribute (SDL_GL_SWAP_CONTROL, 1); +#endif SDL_EnableUNICODE (1); SDL_EnableKeyRepeat (SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL); @@ -533,10 +648,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 @@ -565,6 +683,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; @@ -600,7 +719,7 @@ break; } - XPUSHs (sv_2mortal (newRV_noinc ((SV *)hv))); + XPUSHs (sv_2mortal (sv_bless (newRV_noinc ((SV *)hv), gv_stashpv ("CFPlus::UI::Event", 1)))); } } @@ -619,14 +738,41 @@ 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) CODE: - FcConfigAppFontAddFile (0, (const FcChar8 *)file); /* no idea wether this is required */ + FcConfigAppFontAddFile (0, (const FcChar8 *)file); void load_image_inline (SV *image_) @@ -678,15 +824,15 @@ assert (surface2->pitch == surface2->w * 4); - EXTEND (SP, 5); + SDL_LockSurface (surface2); + EXTEND (SP, 6); PUSHs (sv_2mortal (newSViv (surface2->w))); PUSHs (sv_2mortal (newSViv (surface2->h))); - SDL_LockSurface (surface2); PUSHs (sv_2mortal (newSVpvn (surface2->pixels, surface2->h * surface2->pitch))); - SDL_UnlockSurface (surface2); PUSHs (sv_2mortal (newSViv (surface->flags & (SDL_SRCCOLORKEY | SDL_SRCALPHA) ? GL_RGBA : GL_RGB))); PUSHs (sv_2mortal (newSViv (GL_RGBA))); PUSHs (sv_2mortal (newSViv (GL_UNSIGNED_BYTE))); + SDL_UnlockSurface (surface2); SDL_FreeSurface (surface); SDL_FreeSurface (surface2); @@ -735,13 +881,26 @@ _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 = CFClient PACKAGE = CFClient::Font +void +debug () + CODE: +{ +#if DEBUG + VALGRIND_DO_LEAK_CHECK; +#endif +} -CFClient::Font +MODULE = CFPlus PACKAGE = CFPlus::Font + +CFPlus::Font new_from_file (SV *class, char *path, int id = 0) CODE: { @@ -754,23 +913,23 @@ RETVAL void -DESTROY (CFClient::Font self) +DESTROY (CFPlus::Font self) CODE: pango_font_description_free (self); void -make_default (CFClient::Font self) +make_default (CFPlus::Font self) CODE: default_font = self; -MODULE = CFClient PACKAGE = CFClient::Layout +MODULE = CFPlus PACKAGE = CFPlus::Layout void -clear_font_cache () +reset_glyph_cache () CODE: tc_clear (); -CFClient::Layout +CFPlus::Layout new (SV *class) CODE: New (0, RETVAL, 1, struct cf_layout); @@ -789,13 +948,13 @@ RETVAL void -DESTROY (CFClient::Layout self) +DESTROY (CFPlus::Layout self) CODE: g_object_unref (self->pl); Safefree (self); void -set_text (CFClient::Layout self, SV *text_) +set_text (CFPlus::Layout self, SV *text_) CODE: { STRLEN textlen; @@ -805,7 +964,7 @@ } void -set_markup (CFClient::Layout self, SV *text_) +set_markup (CFPlus::Layout self, SV *text_) CODE: { STRLEN textlen; @@ -815,7 +974,7 @@ } void -set_shapes (CFClient::Layout self, ...) +set_shapes (CFPlus::Layout self, ...) CODE: { PangoAttrList *attrs = 0; @@ -860,7 +1019,7 @@ } void -get_shapes (CFClient::Layout self) +get_shapes (CFPlus::Layout self) PPCODE: { PangoLayoutIter *iter = pango_layout_get_iter (self->pl); @@ -874,6 +1033,7 @@ PangoRectangle extents; pango_layout_iter_get_run_extents (iter, 0, &extents); + EXTEND (SP, 2); PUSHs (sv_2mortal (newSViv (PANGO_PIXELS (extents.x)))); PUSHs (sv_2mortal (newSViv (PANGO_PIXELS (extents.y)))); } @@ -884,7 +1044,7 @@ } int -has_wrapped (CFClient::Layout self) +has_wrapped (CFPlus::Layout self) CODE: { int lines = 1; @@ -899,7 +1059,7 @@ RETVAL SV * -get_text (CFClient::Layout self) +get_text (CFPlus::Layout self) CODE: RETVAL = newSVpv (pango_layout_get_text (self->pl), 0); sv_utf8_decode (RETVAL); @@ -907,7 +1067,7 @@ RETVAL void -set_foreground (CFClient::Layout self, float r, float g, float b, float a = 1.) +set_foreground (CFPlus::Layout self, float r, float g, float b, float a = 1.) CODE: self->r = r; self->g = g; @@ -915,7 +1075,7 @@ self->a = a; void -set_font (CFClient::Layout self, CFClient::Font font = 0) +set_font (CFPlus::Layout self, CFPlus::Font font = 0) CODE: if (self->font != font) { @@ -924,7 +1084,7 @@ } void -set_height (CFClient::Layout self, int base_height) +set_height (CFPlus::Layout self, int base_height) CODE: if (self->base_height != base_height) { @@ -933,22 +1093,22 @@ } void -set_width (CFClient::Layout self, int max_width = -1) +set_width (CFPlus::Layout self, int max_width = -1) CODE: pango_layout_set_width (self->pl, max_width < 0 ? max_width : max_width * PANGO_SCALE); void -set_indent (CFClient::Layout self, int indent) +set_indent (CFPlus::Layout self, int indent) CODE: pango_layout_set_indent (self->pl, indent * PANGO_SCALE); void -set_spacing (CFClient::Layout self, int spacing) +set_spacing (CFPlus::Layout self, int spacing) CODE: pango_layout_set_spacing (self->pl, spacing * PANGO_SCALE); void -set_ellipsise (CFClient::Layout self, int ellipsise) +set_ellipsise (CFPlus::Layout self, int ellipsise) CODE: pango_layout_set_ellipsize (self->pl, ellipsise == 1 ? PANGO_ELLIPSIZE_START @@ -958,12 +1118,12 @@ ); void -set_single_paragraph_mode (CFClient::Layout self, int spm) +set_single_paragraph_mode (CFPlus::Layout self, int spm) CODE: pango_layout_set_single_paragraph_mode (self->pl, !!spm); void -size (CFClient::Layout self) +size (CFPlus::Layout self) PPCODE: { int w, h; @@ -976,7 +1136,7 @@ } int -descent (CFClient::Layout self) +descent (CFPlus::Layout self) CODE: { PangoRectangle rect; @@ -988,18 +1148,18 @@ RETVAL int -xy_to_index (CFClient::Layout self, int x, int y) +xy_to_index (CFPlus::Layout self, int x, int y) CODE: { 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 void -cursor_pos (CFClient::Layout self, int index) +cursor_pos (CFPlus::Layout self, int index) PPCODE: { PangoRectangle strong_pos; @@ -1012,15 +1172,55 @@ } void -render (CFClient::Layout self, float x, float y) +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 ( self->pl, x * PANGO_SCALE, y * PANGO_SCALE, - self->r, self->g, self->b, self->a + self->r, self->g, self->b, self->a, + flags ); -MODULE = CFClient PACKAGE = CFClient::Texture +MODULE = CFPlus PACKAGE = CFPlus::Texture void pad2pot (SV *data_, SV *w_, SV *h_) @@ -1108,64 +1308,82 @@ } } -MODULE = CFClient PACKAGE = CFClient::Map +MODULE = CFPlus PACKAGE = CFPlus::Map -CFClient::Map -new (SV *class, int map_width, int map_height) +CFPlus::Map +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: RETVAL void -DESTROY (CFClient::Map self) +DESTROY (CFPlus::Map self) CODE: { map_clear (self); - Safefree (self->face); + Safefree (self->face2tile); Safefree (self->tex); Safefree (self); } void -clear (CFClient::Map self) +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 (CFClient::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); +} + +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; - self->face [face] = texid; + texid = self->face2tile [face]; + + if (!texid) + return; + + tex = self->tex + texid; + tex->smoothtile = self->face2tile [smooth]; + tex->smoothlevel = level; } void -set_texture (CFClient::Map self, int texid, int name, int w, int h, float s, float t, int r, int g, int b, int a) +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; @@ -1192,7 +1410,7 @@ } int -ox (CFClient::Map self) +ox (CFPlus::Map self) ALIAS: oy = 1 x = 2 @@ -1213,18 +1431,18 @@ RETVAL void -scroll (CFClient::Map self, int dx, int dy) +scroll (CFPlus::Map self, int dx, int dy) CODE: { if (dx > 0) - map_blank (self, self->x, self->y, dx - 1, self->h); + 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, 1 - 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 - 1); + 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, 1 - 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; @@ -1239,7 +1457,7 @@ } void -map1a_update (CFClient::Map self, SV *data_) +map1a_update (CFPlus::Map self, SV *data_, int extmap) CODE: { uint8_t *data = (uint8_t *)SvPVbyte_nolen (data_); @@ -1247,7 +1465,7 @@ mapcell *cell; int x, y, flags; - while (data < data_end) + while (data < data_end - 1) { flags = (data [0] << 8) + data [1]; data += 2; @@ -1258,40 +1476,82 @@ if (flags & 15) { - if (cell->darkness < 0) + if (!cell->darkness) { - cell->darkness = 0; - cell->face [0] = 0; - cell->face [1] = 0; - cell->face [2] = 0; + memset (cell, 0, sizeof (*cell)); + cell->darkness = 256; } - cell->darkness = flags & 8 ? *data++ : 255; - //TODO: don't trust server data to be in-range(!) + if (flags & 8) + { + if (extmap) + { + uint8_t ext, cmd; + + do + { + ext = *data++; + cmd = ext & 0x3f; + + if (cmd < 4) + cell->darkness = 255 - ext * 64 + 1; + else if (cmd == 5) // health + { + cell->stat_width = 1; + cell->stat_hp = *data++; + } + else if (cmd == 6) // monster width + cell->stat_width = *data++ + 1; + else if (cmd == 0x47) + { + if (*data == 4) + ; // decode player count + + data += *data + 1; + } + else if (cmd == 8) // cell flags + cell->flags = *data++; + else if (ext & 0x40) // unknown, multibyte => skip + data += *data + 1; + else + data++; + } + while (ext & 0x80); + } + 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 - cell->darkness = -1; + cell->darkness = 0; } } SV * -mapmap (CFClient::Map self, int x0, int y0, int w, int h) +mapmap (CFPlus::Map self, int x0, int y0, int w, int h) CODE: { int x1, x; @@ -1322,19 +1582,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; } } @@ -1351,26 +1606,22 @@ RETVAL void -draw (CFClient::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); @@ -1383,50 +1634,242 @@ 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; + + // 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)); - for (x = 0; x < sw; x++) - if (row->c0 <= x + vx && x + vx < row->c1) - { - mapcell *cell = row->col + (x + vx - row->c0); + for (y = 0; y < sh; y++) + if (0 <= y + my && y + my < self->rows) + { + maprow *row = self->row + (y + my); - face = cell->face [z]; + for (x = 0; x < sw; x++) + if (row->c0 <= x + mx && x + mx < row->c1) + { + mapcell *cell = row->col + (x + mx - row->c0); - if (face) - { - maptex tex = self->tex [face]; + 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)); + } + } + + for (z = 0; z <= 2; z++) + { + memset (smooth_level, 0, sizeof (smooth_level)); - int px = (x + 1) * 32 - tex.w; - int py = (y + 1) * 32 - tex.h; + for (y = 0; y < sh; y++) + if (0 <= y + my && y + my < self->rows) + { + maprow *row = self->row + (y + my); - if (last_name != tex.name) + 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)) + { + int level = (w << 5) | b; + HE *he; + + hv_iterinit (smooth); + while ((he = hv_iternext (smooth))) { - glEnd (); - last_name = tex.name; - glBindTexture (GL_TEXTURE_2D, last_name); - glBegin (GL_QUADS); - } + smooth_key *skey = (smooth_key *)HeKEY (he); + IV bits = SvIVX (HeVAL (he)); - 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 (!(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 (); glDisable (GL_TEXTURE_2D); glDisable (GL_BLEND); + + // top layer: overlays such as the health bar + 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); + + int px = x * T; + int py = y * T; + + if (cell->stat_hp) + { + 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, + px + width - 1, py); + + glColor3ub (cell->stat_hp, 255 - cell->stat_hp, 0); + glRectf (px + 2, + py - thick - 1, + px + width - 2 - cell->stat_hp * (width - 4) / 255, py - 1); + } + } + } } void -draw_magicmap (CFClient::Map self, int dx, int dy, int w, int h, unsigned char *data) +draw_magicmap (CFPlus::Map self, int dx, int dy, int w, int h, unsigned char *data) CODE: { static float color[16][3] = { @@ -1486,10 +1929,9 @@ } void -fow_texture (CFClient::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)); @@ -1499,22 +1941,22 @@ 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 < 0 - ? 255 - FOW_DARKNESS - : 255 - cell->darkness; + darkness[y * sw4 + x] = cell->darkness + ? 255 - (cell->darkness - 1) + : 255 - FOW_DARKNESS; } } @@ -1525,7 +1967,7 @@ } SV * -get_rect (CFClient::Map self, int x0, int y0, int w, int h) +get_rect (CFPlus::Map self, int x0, int y0, int w, int h) CODE: { int x, y, x1, y1; @@ -1560,28 +2002,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 @@ -1597,7 +2042,7 @@ RETVAL void -set_rect (CFClient::Map self, int x0, int y0, uint8_t *data) +set_rect (CFPlus::Map self, int x0, int y0, uint8_t *data) PPCODE: { int x, y, z; @@ -1631,24 +2076,28 @@ 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) + if (cell->darkness == 0) { - cell->darkness = -1; + cell->darkness = 0; for (z = 0; z <= 2; z++) { - cell->face[z] = face[z]; + tileid t = tile [z]; + + if (t >= self->texs || (t && !self->tex [t].name)) + { + XPUSHs (sv_2mortal (newSViv (t))); + need_texid (self, t); + } - if (face[z] && (face[z] >= self->texs || !self->tex[face [z]].name)) - XPUSHs (sv_2mortal (newSViv (face[z]))); + cell->tile [z] = t; } } } @@ -1656,9 +2105,9 @@ } } -MODULE = CFClient PACKAGE = CFClient::MixChunk +MODULE = CFPlus PACKAGE = CFPlus::MixChunk -CFClient::MixChunk +CFPlus::MixChunk new_from_file (SV *class, char *path) CODE: RETVAL = Mix_LoadWAV (path); @@ -1666,25 +2115,25 @@ RETVAL void -DESTROY (CFClient::MixChunk self) +DESTROY (CFPlus::MixChunk self) CODE: Mix_FreeChunk (self); int -volume (CFClient::MixChunk self, int volume = -1) +volume (CFPlus::MixChunk self, int volume = -1) CODE: RETVAL = Mix_VolumeChunk (self, volume); OUTPUT: RETVAL int -play (CFClient::MixChunk self, int channel = -1, int loops = 0, int ticks = -1) +play (CFPlus::MixChunk self, int channel = -1, int loops = 0, int ticks = -1) CODE: RETVAL = Mix_PlayChannelTimed (channel, self, loops, ticks); OUTPUT: RETVAL -MODULE = CFClient PACKAGE = CFClient::MixMusic +MODULE = CFPlus PACKAGE = CFPlus::MixMusic int volume (int volume = -1) @@ -1693,7 +2142,14 @@ OUTPUT: RETVAL -CFClient::MixMusic +int +fade_out (int ms) + CODE: + RETVAL = Mix_FadeOutMusic (ms); + OUTPUT: + RETVAL + +CFPlus::MixMusic new_from_file (SV *class, char *path) CODE: RETVAL = Mix_LoadMUS (path); @@ -1701,22 +2157,22 @@ RETVAL void -DESTROY (CFClient::MixMusic self) +DESTROY (CFPlus::MixMusic self) CODE: Mix_FreeMusic (self); int -play (CFClient::MixMusic self, int loops = -1) +play (CFPlus::MixMusic self, int loops = -1) CODE: RETVAL = Mix_PlayMusic (self, loops); OUTPUT: RETVAL -MODULE = CFClient PACKAGE = CFClient::OpenGL +MODULE = CFPlus PACKAGE = CFPlus::OpenGL BOOT: { - HV *stash = gv_stashpv ("CFClient::OpenGL", 1); + HV *stash = gv_stashpv ("CFPlus::OpenGL", 1); static const struct { const char *name; IV iv; @@ -1789,6 +2245,7 @@ const_iv (GL_CONVOLUTION_BORDER_MODE), const_iv (GL_CONSTANT_BORDER), const_iv (GL_LINES), + const_iv (GL_LINE_STRIP), const_iv (GL_LINE_LOOP), const_iv (GL_QUADS), const_iv (GL_QUAD_STRIP), @@ -1806,6 +2263,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 * @@ -1908,10 +2368,7 @@ b *= a; } // microsoft visual "c" rounds instead of truncating... - glColor4ub (MIN ((int)(r * 256.f), 255), - MIN ((int)(g * 256.f), 255), - MIN ((int)(b * 256.f), 255), - MIN ((int)(a * 256.f), 255)); + glColor4f (r, g, b, a); void glInterleavedArrays (int format, int stride, char *data) @@ -1967,21 +2424,14 @@ int glGenTexture () CODE: -{ - GLuint name; - glGenTextures (1, &name); - RETVAL = name; -} + RETVAL = gen_texture (); OUTPUT: RETVAL void glDeleteTexture (int name) CODE: -{ - GLuint name_ = name; - glDeleteTextures (1, &name_); -} - + del_texture (name); + int glGenList () CODE: RETVAL = glGenLists (1); @@ -1998,4 +2448,3 @@ void glCallList (int list) -