--- deliantra/Deliantra-Client/Client.xs 2006/11/07 22:41:27 1.164 +++ deliantra/Deliantra-Client/Client.xs 2007/07/21 16:07:53 1.208 @@ -1,6 +1,6 @@ #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 @@ -8,6 +8,11 @@ # pragma warning(disable:4761) #endif +//#define DEBUG 1 +#if DEBUG +# include +#endif + #include "EXTERN.h" #include "perl.h" #include "XSUB.h" @@ -16,12 +21,14 @@ # undef pipe #endif +#include #include #include #include #include #include +#include #include #include #include @@ -57,6 +64,8 @@ # define PARACHUTE 0 #endif +static AV *texture_av; + static struct { #define GL_FUNC(ptr,name) ptr name; @@ -64,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); @@ -74,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" @@ -156,20 +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; - 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 { @@ -180,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; @@ -215,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) { @@ -318,6 +372,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) { @@ -361,6 +431,50 @@ return n + 1; } +/* SDL should provide this, really. */ +#define SDLK_MODIFIER_MIN 300 +#define SDLK_MODIFIER_MAX 314 + +/******************************************************************************/ + +static int +within_widget (SV *widget, NV x, NV y) +{ + HV *self; + SV **svp; + NV wx, ww, wy, wh; + + if (!SvROK (widget)) + return 0; + + self = (HV *)SvRV (widget); + + if (SvTYPE (self) != SVt_PVHV) + return 0; + + svp = hv_fetch (self, "y", 1, 0); wy = svp ? SvNV (*svp) : 0.; + if (y < wy) + return 0; + + svp = hv_fetch (self, "h", 1, 0); wh = svp ? SvNV (*svp) : 0.; + if (y >= wy + wh) + return 0; + + svp = hv_fetch (self, "x", 1, 0); wx = svp ? SvNV (*svp) : 0.; + if (x < wx) + return 0; + + svp = hv_fetch (self, "w", 1, 0); ww = svp ? SvNV (*svp) : 0.; + if (x >= wx + ww) + return 0; + + svp = hv_fetch (self, "can_events", sizeof ("can_events") - 1, 0); + if (!svp || !SvTRUE (*svp)) + return 0; + + return 1; +} + MODULE = CFPlus PACKAGE = CFPlus PROTOTYPES: ENABLE @@ -373,6 +487,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), @@ -391,6 +508,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), @@ -455,29 +577,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: @@ -498,6 +630,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) @@ -505,22 +640,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); @@ -529,39 +664,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 @@ -571,13 +718,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); @@ -589,6 +740,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; @@ -598,13 +750,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: @@ -643,9 +813,7 @@ void lowdelay (int fd, int val = 1) CODE: -#ifndef _WIN32 - setsockopt (fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof (val)); -#endif + setsockopt (fd, IPPROTO_TCP, TCP_NODELAY, (void *)&val, sizeof (val)); void win32_proxy_info () @@ -796,8 +964,19 @@ _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: @@ -817,13 +996,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 (); @@ -1051,7 +1234,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 @@ -1070,6 +1253,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 ( @@ -1081,40 +1303,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); + 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_); - } - - sv_setiv (w_, nw); - sv_setiv (h_, nh); - } + sv_setsv (data_, result_); } } @@ -1167,8 +1378,21 @@ } } +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) CODE: @@ -1179,10 +1403,8 @@ 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: @@ -1193,7 +1415,7 @@ CODE: { map_clear (self); - Safefree (self->face); + Safefree (self->face2tile); Safefree (self->tex); Safefree (self); } @@ -1210,27 +1432,41 @@ 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; - self->face [face] = texid; + 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; @@ -1284,12 +1520,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; @@ -1351,10 +1587,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; } @@ -1373,17 +1609,23 @@ 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 @@ -1423,19 +1665,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; } } @@ -1452,12 +1689,22 @@ RETVAL void -draw (CFPlus::Map self, int mx, int my, int sw, int sh) +draw (CFPlus::Map self, int mx, int my, int sw, int sh, int T) CODE: { + 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; + + // thats current max. sorry. + if (sw > 255) sw = 255; + if (sh > 255) sh = 255; + + // clear key, in case of extra padding + memset (&skey, 0, sizeof (skey)); glColor4ub (255, 255, 255, 255); @@ -1473,58 +1720,199 @@ mx += self->x; my += self->y; - for (z = 0; z < 3; z++) - for (y = 0; y < sh; y++) - if (0 <= y + my && y + my < self->rows) - { - maprow *row = self->row + (y + my); + // 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 + mx && x + mx < row->c1) - { - mapcell *cell = row->col + (x + mx - 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 && face < self->texs) - { - maptex tex = self->tex [face]; - 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)); + } + } - if (last_name != tex.name) - { - glEnd (); - glBindTexture (GL_TEXTURE_2D, last_name = tex.name); - glBegin (GL_QUADS); - } + for (z = 0; z <= 2; z++) + { + memset (smooth_level, 0, sizeof (smooth_level)); - 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 (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); - if (cell->flags && z == 2) + // 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 (); @@ -1542,13 +1930,13 @@ { 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, @@ -1628,37 +2016,80 @@ PPCODE: { 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); - mx += self->x; - my += self->y; + mx += self->x - 1; + my += self->y - 1; - for (y = 0; y < sh; y++) + memset (darkness1, 255, sw1 * sh1); + + for (y = 0; y < sh1; y++) if (0 <= y + my && y + my < self->rows) { maprow *row = self->row + (y + my); - for (x = 0; x < sw; x++) + for (x = 0; x < sw1; x++) if (row->c0 <= x + mx && x + mx < row->c1) { 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 * @@ -1697,28 +2128,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 @@ -1768,13 +2202,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) { @@ -1782,10 +2215,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; } } } @@ -1795,6 +2233,8 @@ MODULE = CFPlus PACKAGE = CFPlus::MixChunk +PROTOTYPES: DISABLE + CFPlus::MixChunk new_from_file (SV *class, char *path) CODE: @@ -1825,11 +2265,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: @@ -1849,8 +2297,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); @@ -1859,6 +2316,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), @@ -1872,6 +2332,7 @@ const_iv (GL_RESCALE_NORMAL), const_iv (GL_FRONT), const_iv (GL_BACK), + const_iv (GL_AUX0), const_iv (GL_AND), const_iv (GL_ONE), const_iv (GL_ZERO), @@ -1895,6 +2356,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), @@ -1925,6 +2388,7 @@ const_iv (GL_CONVOLUTION_2D), const_iv (GL_CONVOLUTION_BORDER_MODE), const_iv (GL_CONSTANT_BORDER), + const_iv (GL_POINTS), const_iv (GL_LINES), const_iv (GL_LINE_STRIP), const_iv (GL_LINE_LOOP), @@ -1933,8 +2397,15 @@ const_iv (GL_TRIANGLES), const_iv (GL_TRIANGLE_STRIP), const_iv (GL_TRIANGLE_FAN), + const_iv (GL_POLYGON), const_iv (GL_PERSPECTIVE_CORRECTION_HINT), + const_iv (GL_POINT_SMOOTH_HINT), + const_iv (GL_LINE_SMOOTH_HINT), + const_iv (GL_POLYGON_SMOOTH_HINT), + const_iv (GL_GENERATE_MIPMAP_HINT), const_iv (GL_FASTEST), + const_iv (GL_DONT_CARE), + const_iv (GL_NICEST), const_iv (GL_V2F), const_iv (GL_V3F), const_iv (GL_T2F_V3F), @@ -1944,6 +2415,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 * @@ -1967,6 +2441,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 () @@ -2014,6 +2502,8 @@ # near_ and far_ are due to microsofts buggy "c" compiler void glOrtho (double left, double right, double bottom, double top, double near_, double far_) +PROTOTYPES: DISABLE + void glViewport (int x, int y, int width, int height) void glScissor (int x, int y, int width, int height) @@ -2030,12 +2520,7 @@ CODE: glRotatef (angle, x, y, z); -void glBegin (int mode) - -void glEnd () - void glColor (float r, float g, float b, float a = 1.0) - PROTOTYPE: @ ALIAS: glColor_premultiply = 1 CODE: @@ -2048,12 +2533,6 @@ // microsoft visual "c" rounds instead of truncating... glColor4f (r, g, b, a); -void glInterleavedArrays (int format, int stride, char *data) - -void glDrawElements (int mode, int count, int type, char *indices) - -# 1.2 void glDrawRangeElements (int mode, int start, int end - void glRasterPos (float x, float y, float z = 0.) CODE: glRasterPos3f (0, 0, z); @@ -2067,6 +2546,22 @@ CODE: glTexCoord2f (s, t); +PROTOTYPES: ENABLE + +void glBegin (int mode) + +void glEnd () + +void glPointSize (GLfloat size) + +void glLineWidth (GLfloat width) + +void glInterleavedArrays (int format, int stride, char *data) + +void glDrawElements (int mode, int count, int type, char *indices) + +# 1.2 void glDrawRangeElements (int mode, int start, int end + void glTexEnv (int target, int pname, float param) CODE: glTexEnvf (target, pname, param); @@ -2098,25 +2593,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); @@ -2133,3 +2623,15 @@ void glCallList (int list) +MODULE = CFPlus PACKAGE = CFPlus::UI::Base + +PROTOTYPES: DISABLE + +void +find_widget (SV *widget, NV x, NV y) + PPCODE: +{ + if (within_widget (widget, x, y)) + XPUSHs (widget); +} +