--- deliantra/Deliantra-Client/Client.xs 2006/08/19 18:18:36 1.152 +++ deliantra/Deliantra-Client/Client.xs 2007/07/19 22:38:52 1.205 @@ -1,9 +1,16 @@ #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 + +//#define DEBUG 1 +#if DEBUG +# include #endif #include "EXTERN.h" @@ -14,12 +21,14 @@ # undef pipe #endif +#include #include #include #include #include #include +#include #include #include #include @@ -38,16 +47,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 +64,8 @@ # define PARACHUTE 0 #endif +static AV *texture_av; + static struct { #define GL_FUNC(ptr,name) ptr name; @@ -69,7 +73,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); @@ -79,6 +84,30 @@ 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" @@ -161,19 +190,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; + tileid tile[3]; uint16_t darkness; - mapface face[3]; - uint8_t stat_width, stat_hp; + uint8_t stat_width, stat_hp, flags, smoothmax; } mapcell; typedef struct { @@ -184,11 +217,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; @@ -219,6 +249,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) { @@ -316,10 +366,28 @@ 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) { @@ -363,6 +431,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 @@ -375,6 +447,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), @@ -393,6 +468,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), @@ -457,29 +537,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: @@ -500,6 +590,9 @@ opengl_context = pango_opengl_font_map_create_context ((PangoOpenGLFontMap *)opengl_fontmap); } +char * +SDL_GetError () + int SDL_Init (U32 flags = SDL_INIT_VIDEO | SDL_INIT_AUDIO | PARACHUTE) @@ -507,22 +600,22 @@ SDL_Quit () void -SDL_ListModes () +SDL_ListModes (int rgb, int alpha) PPCODE: { SDL_Rect **m; - SDL_GL_SetAttribute (SDL_GL_RED_SIZE, 5); - SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, 5); - SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE, 5); - SDL_GL_SetAttribute (SDL_GL_ALPHA_SIZE, 1); + SDL_GL_SetAttribute (SDL_GL_RED_SIZE , rgb); + SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, rgb); + SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE , rgb); + SDL_GL_SetAttribute (SDL_GL_ALPHA_SIZE, alpha); SDL_GL_SetAttribute (SDL_GL_BUFFER_SIZE, 15); - SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 0); + SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE , 0); - SDL_GL_SetAttribute (SDL_GL_ACCUM_RED_SIZE, 0); + SDL_GL_SetAttribute (SDL_GL_ACCUM_RED_SIZE , 0); SDL_GL_SetAttribute (SDL_GL_ACCUM_GREEN_SIZE, 0); - SDL_GL_SetAttribute (SDL_GL_ACCUM_BLUE_SIZE, 0); + SDL_GL_SetAttribute (SDL_GL_ACCUM_BLUE_SIZE , 0); SDL_GL_SetAttribute (SDL_GL_ACCUM_ALPHA_SIZE, 0); SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1); @@ -531,39 +624,51 @@ SDL_GL_SetAttribute (SDL_GL_SWAP_CONTROL, 1); #endif - SDL_EnableUNICODE (1); - SDL_EnableKeyRepeat (SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL); - m = SDL_ListModes (0, SDL_FULLSCREEN | SDL_OPENGL); if (m && m != (SDL_Rect **)-1) while (*m) { - AV *av = newAV (); - av_push (av, newSViv ((*m)->w)); - av_push (av, newSViv ((*m)->h)); - XPUSHs (sv_2mortal (newRV_noinc ((SV *)av))); + if ((*m)->w >= 640 && (*m)->h >= 480) + { + AV *av = newAV (); + av_push (av, newSViv ((*m)->w)); + av_push (av, newSViv ((*m)->h)); + av_push (av, newSViv (rgb)); + av_push (av, newSViv (alpha)); + XPUSHs (sv_2mortal (newRV_noinc ((SV *)av))); + } ++m; } } -char * -SDL_GetError () - int -SDL_SetVideoMode (int w, int h, int fullscreen) +SDL_SetVideoMode (int w, int h, int rgb, int alpha, int fullscreen) CODE: +{ + SDL_EnableUNICODE (1); + SDL_EnableKeyRepeat (SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL); + + SDL_GL_SetAttribute (SDL_GL_RED_SIZE , rgb); + SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, rgb); + SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE , rgb); + SDL_GL_SetAttribute (SDL_GL_ALPHA_SIZE, alpha); + RETVAL = !!SDL_SetVideoMode ( w, h, 0, SDL_OPENGL | (fullscreen ? SDL_FULLSCREEN : 0) ); + 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 +#define GL_FUNC(ptr,name) gl.name = (ptr)SDL_GL_GetProcAddress ("gl" # name); +#include "glfunc.h" +#undef GL_FUNC } +} OUTPUT: RETVAL @@ -573,13 +678,17 @@ char * SDL_GetKeyName (int sym) +int +SDL_GetAppState () + void -SDL_PollEvent () +poll_events () PPCODE: { SDL_Event ev; - while (SDL_PollEvent (&ev)) + SDL_PumpEvents (); + while (SDL_PeepEvents (&ev, 1, SDL_GETEVENT, SDL_ALLEVENTS) > 0) { HV *hv = newHV (); hv_store (hv, "type", 4, newSViv (ev.type), 0); @@ -591,6 +700,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,13 +710,31 @@ break; case SDL_MOUSEMOTION: - hv_store (hv, "mod", 3, newSViv (SDL_GetModState ()), 0); + { + int state = ev.motion.state; + int x = ev.motion.x; + int y = ev.motion.y; + int xrel = ev.motion.xrel; + int yrel = ev.motion.yrel; + + /* do simplistic event compression */ + while (SDL_PeepEvents (&ev, 1, SDL_PEEKEVENT, SDL_EVENTMASK (SDL_MOUSEMOTION)) > 0 + && state == ev.motion.state) + { + xrel += ev.motion.xrel; + yrel += ev.motion.yrel; + x = ev.motion.x; + y = ev.motion.y; + SDL_PeepEvents (&ev, 1, SDL_GETEVENT, SDL_EVENTMASK (SDL_MOUSEMOTION)); + } - hv_store (hv, "state", 5, newSViv (ev.motion.state), 0); - hv_store (hv, "x", 1, newSViv (ev.motion.x), 0); - hv_store (hv, "y", 1, newSViv (ev.motion.y), 0); - hv_store (hv, "xrel", 4, newSViv (ev.motion.xrel), 0); - hv_store (hv, "yrel", 4, newSViv (ev.motion.yrel), 0); + hv_store (hv, "mod", 3, newSViv (SDL_GetModState ()), 0); + hv_store (hv, "state", 5, newSViv (state), 0); + hv_store (hv, "x", 1, newSViv (x), 0); + hv_store (hv, "y", 1, newSViv (y), 0); + hv_store (hv, "xrel", 4, newSViv (xrel), 0); + hv_store (hv, "yrel", 4, newSViv (yrel), 0); + } break; case SDL_MOUSEBUTTONDOWN: @@ -645,9 +773,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) @@ -761,12 +916,27 @@ _exit (1); void -_exit (int retval) +_exit (int retval = 0) CODE: +#ifdef WIN32 + ExitThread (retval); // unclean, please beam me up +#else _exit (retval); +#endif + +void +debug () + CODE: +{ +#if DEBUG + VALGRIND_DO_LEAK_CHECK; +#endif +} MODULE = CFPlus PACKAGE = CFPlus::Font +PROTOTYPES: DISABLE + CFPlus::Font new_from_file (SV *class, char *path, int id = 0) CODE: @@ -786,13 +956,17 @@ void make_default (CFPlus::Font self) + PROTOTYPE: $ CODE: default_font = self; MODULE = CFPlus PACKAGE = CFPlus::Layout +PROTOTYPES: DISABLE + void reset_glyph_cache () + PROTOTYPE: CODE: tc_clear (); @@ -1020,7 +1194,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 @@ -1039,6 +1213,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 ( @@ -1050,40 +1263,29 @@ MODULE = CFPlus PACKAGE = CFPlus::Texture +PROTOTYPES: ENABLE + +int minpot (int n) + void -pad2pot (SV *data_, SV *w_, SV *h_) +pad (SV *data_, int ow, int oh, int nw, int nh) CODE: { - int ow = SvIV (w_); - int oh = SvIV (h_); - - if (ow && oh) + if ((nw != ow || nh != oh) && SvOK (data_)) { - int nw = minpot (ow); - int nh = minpot (oh); - - if (nw != ow || nh != oh) - { - if (SvOK (data_)) - { - STRLEN datalen; - char *data = SvPVbyte (data_, datalen); - int bpp = datalen / (ow * oh); - SV *result_ = sv_2mortal (newSV (nw * nh * bpp)); - - SvPOK_only (result_); - SvCUR_set (result_, nw * nh * bpp); - - memset (SvPVX (result_), 0, nw * nh * bpp); - while (oh--) - memcpy (SvPVX (result_) + oh * nw * bpp, data + oh * ow * bpp, ow * bpp); - - sv_setsv (data_, result_); - } + STRLEN datalen; + char *data = SvPVbyte (data_, datalen); + int bpp = datalen / (ow * oh); + SV *result_ = sv_2mortal (newSV (nw * nh * bpp)); + + SvPOK_only (result_); + SvCUR_set (result_, nw * nh * bpp); + + memset (SvPVX (result_), 0, nw * nh * bpp); + while (oh--) + memcpy (SvPVX (result_) + oh * nw * bpp, data + oh * ow * bpp, ow * bpp); - sv_setiv (w_, nw); - sv_setiv (h_, nh); - } + sv_setsv (data_, result_); } } @@ -1136,22 +1338,33 @@ } } +IV texture_valid_2d (GLint internalformat, GLsizei w, GLsizei h, GLenum format, GLenum type) + CODE: +{ + GLint width; + glTexImage2D (GL_PROXY_TEXTURE_2D, 0, internalformat, w, h, 0, format, type, 0); + glGetTexLevelParameteriv (GL_PROXY_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &width); + RETVAL = width > 0; +} + OUTPUT: + RETVAL + MODULE = CFPlus PACKAGE = CFPlus::Map +PROTOTYPES: DISABLE + 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: @@ -1162,38 +1375,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); +} + +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 (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; @@ -1245,14 +1478,14 @@ 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; @@ -1288,10 +1521,8 @@ { if (!cell->darkness) { + memset (cell, 0, sizeof (*cell)); cell->darkness = 256; - cell->face [0] = 0; - cell->face [1] = 0; - cell->face [2] = 0; } //TODO: don't trust server data to be in-range(!) @@ -1316,6 +1547,15 @@ } 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 @@ -1329,24 +1569,27 @@ 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 = 0; - cell->stat_hp = 0; - } + cell->darkness = 0; } } @@ -1382,19 +1625,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; } } @@ -1411,26 +1649,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); @@ -1443,41 +1677,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]; + 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 + 1) * 32 - tex.w; - int py = (y + 1) * 32 - tex.h; + 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)); + + 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 ); + } + } - if (last_name != tex.name) + // 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 (); @@ -1486,22 +1881,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, @@ -1577,42 +1972,84 @@ } 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)); - uint8_t *darkness = (uint8_t *)SvPVX (darkness_sv); - - memset (darkness, 255, sw4 * sh); - SvPOK_only (darkness_sv); - SvCUR_set (darkness_sv, sw4 * sh); + int sw1 = sw + 2; + int sh1 = sh + 2; + int sh3 = sh * 3; + int sw34 = (sw * 3 + 3) & ~3; + uint8_t *darkness1 = (uint8_t *)malloc (sw1 * sh1); + SV *darkness3_sv = sv_2mortal (newSV (sw34 * sh3)); + uint8_t *darkness3 = (uint8_t *)SvPVX (darkness3_sv); + + SvPOK_only (darkness3_sv); + SvCUR_set (darkness3_sv, sw34 * sh3); - vx = self->x + (self->w - sw + 1) / 2 - shift_x; - vy = self->y + (self->h - sh + 1) / 2 - shift_y; + mx += self->x - 1; + my += self->y - 1; - for (y = 0; y < sh; y++) - if (0 <= y + vy && y + vy < self->rows) + memset (darkness1, 255, sw1 * sh1); + + for (y = 0; y < sh1; y++) + 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) + for (x = 0; x < sw1; x++) + 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 + darkness1 [y * sw1 + x] = cell->darkness ? 255 - (cell->darkness - 1) : 255 - FOW_DARKNESS; } } + for (y = 0; y < sh; ++y) + for (x = 0; x < sw; ++x) + { + uint8_t d11 = darkness1 [(y ) * sw1 + x ]; + uint8_t d21 = darkness1 [(y ) * sw1 + x + 1]; + uint8_t d31 = darkness1 [(y ) * sw1 + x + 2]; + uint8_t d12 = darkness1 [(y + 1) * sw1 + x ]; + uint8_t d22 = darkness1 [(y + 1) * sw1 + x + 1]; + uint8_t d32 = darkness1 [(y + 1) * sw1 + x + 2]; + uint8_t d13 = darkness1 [(y + 2) * sw1 + x ]; + uint8_t d23 = darkness1 [(y + 2) * sw1 + x + 1]; + uint8_t d33 = darkness1 [(y + 2) * sw1 + x + 2]; + + uint8_t r11 = (d11 + d21 + d12) / 3; + uint8_t r21 = d21; + uint8_t r31 = (d21 + d31 + d32) / 3; + + uint8_t r12 = d12; + uint8_t r22 = d22; + uint8_t r32 = d32; + + uint8_t r13 = (d13 + d23 + d12) / 3; + uint8_t r23 = d23; + uint8_t r33 = (d23 + d33 + d32) / 3; + + darkness3 [(y * 3 ) * sw34 + (x * 3 )] = MAX (d22, r11); + darkness3 [(y * 3 ) * sw34 + (x * 3 + 1)] = MAX (d22, r21); + darkness3 [(y * 3 ) * sw34 + (x * 3 + 2)] = MAX (d22, r31); + darkness3 [(y * 3 + 1) * sw34 + (x * 3 )] = MAX (d22, r12); + darkness3 [(y * 3 + 1) * sw34 + (x * 3 + 1)] = MAX (d22, r22); + darkness3 [(y * 3 + 1) * sw34 + (x * 3 + 2)] = MAX (d22, r32); + darkness3 [(y * 3 + 2) * sw34 + (x * 3 )] = MAX (d22, r13); + darkness3 [(y * 3 + 2) * sw34 + (x * 3 + 1)] = MAX (d22, r23); + darkness3 [(y * 3 + 2) * sw34 + (x * 3 + 2)] = MAX (d22, r33); + } + + free (darkness1); + EXTEND (SP, 3); - PUSHs (sv_2mortal (newSViv (sw4))); - PUSHs (sv_2mortal (newSViv (sh))); - PUSHs (darkness_sv); + PUSHs (sv_2mortal (newSViv (sw34))); + PUSHs (sv_2mortal (newSViv (sh3))); + PUSHs (darkness3_sv); } SV * @@ -1651,28 +2088,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 @@ -1722,13 +2162,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) { @@ -1736,10 +2175,15 @@ 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; } } } @@ -1749,6 +2193,8 @@ MODULE = CFPlus PACKAGE = CFPlus::MixChunk +PROTOTYPES: DISABLE + CFPlus::MixChunk new_from_file (SV *class, char *path) CODE: @@ -1779,11 +2225,19 @@ int volume (int volume = -1) + PROTOTYPE: ;$ CODE: RETVAL = Mix_VolumeMusic (volume); OUTPUT: RETVAL +int +fade_out (int ms) + CODE: + RETVAL = Mix_FadeOutMusic (ms); + OUTPUT: + RETVAL + CFPlus::MixMusic new_from_file (SV *class, char *path) CODE: @@ -1803,8 +2257,17 @@ OUTPUT: RETVAL +int +fade_in_pos (CFPlus::MixMusic self, int loops, int ms, double position) + CODE: + RETVAL = Mix_FadeInMusicPos (self, loops, ms, position); + OUTPUT: + RETVAL + MODULE = CFPlus PACKAGE = CFPlus::OpenGL +PROTOTYPES: ENABLE + BOOT: { HV *stash = gv_stashpv ("CFPlus::OpenGL", 1); @@ -1813,6 +2276,9 @@ IV iv; } *civ, const_iv[] = { # define const_iv(name) { # name, (IV)name } + const_iv (GL_VENDOR), + const_iv (GL_VERSION), + const_iv (GL_EXTENSIONS), const_iv (GL_COLOR_MATERIAL), const_iv (GL_SMOOTH), const_iv (GL_FLAT), @@ -1849,6 +2315,8 @@ const_iv (GL_FLOAT), const_iv (GL_UNSIGNED_INT_8_8_8_8_REV), const_iv (GL_COMPILE), + const_iv (GL_PROXY_TEXTURE_1D), + const_iv (GL_PROXY_TEXTURE_2D), const_iv (GL_TEXTURE_1D), const_iv (GL_TEXTURE_2D), const_iv (GL_TEXTURE_ENV), @@ -1898,6 +2366,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 * @@ -1921,6 +2392,20 @@ OUTPUT: RETVAL +const char *glGetString (GLenum pname) + +GLint glGetInteger (GLenum pname) + CODE: + glGetIntegerv (pname, &RETVAL); + OUTPUT: + RETVAL + +GLdouble glGetDouble (GLenum pname) + CODE: + glGetDoublev (pname, &RETVAL); + OUTPUT: + RETVAL + int glGetError () void glFinish () @@ -2052,25 +2537,20 @@ void glDrawPixels (int width, int height, int format, int type, char *pixels) +void glPixelZoom (float x, float y) + void glCopyPixels (int x, int y, int width, int height, int type = GL_COLOR) 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);