--- deliantra/Deliantra-Client/Client.xs 2007/12/26 21:03:21 1.242 +++ deliantra/Deliantra-Client/Client.xs 2008/08/20 13:30:02 1.273 @@ -41,6 +41,11 @@ #include #include +/* work around os x broken headers */ +#ifdef __MACOSX__ +typedef void (APIENTRYP PFNGLBLENDFUNCSEPARATEPROC) (GLenum sfactorRGB, GLenum dfactorRGB, GLenum sfactorAlpha, GLenum dfactorAlpha); +#endif + #define PANGO_ENABLE_BACKEND #define G_DISABLE_CAST_CHECKS @@ -48,6 +53,17 @@ #include +#ifndef PANGO_VERSION_CHECK +# define PANGO_VERSION_CHECK(a,b,c) 0 +#endif + +#if !PANGO_VERSION_CHECK (1, 15, 2) +# define pango_layout_get_line_readonly pango_layout_get_line +# define pango_layout_get_lines_readonly pango_layout_get_lines +# define pango_layout_iter_get_line_readonly pango_layout_iter_get_line +# define pango_layout_iter_get_run_readonly pango_layout_iter_get_run +#endif + #ifndef _WIN32 # include # include @@ -56,6 +72,15 @@ # include #endif +#if __GNUC__ >= 4 +# define expect(expr,value) __builtin_expect ((expr),(value)) +#else +# define expect(expr,value) (expr) +#endif + +#define expect_false(expr) expect ((expr) != 0, 0) +#define expect_true(expr) expect ((expr) != 0, 1) + #define OBJ_STR "\xef\xbf\xbc" /* U+FFFC, object replacement character */ #define FOW_DARKNESS 32 @@ -65,11 +90,8 @@ #define MIN_FONT_HEIGHT 10 -#if 0 -# define PARACHUTE SDL_INIT_NOPARACHUTE -#else -# define PARACHUTE 0 -#endif +/* mask out modifiers we are not interested in */ +#define MOD_MASK (KMOD_CTRL | KMOD_SHIFT | KMOD_ALT | KMOD_META) static AV *texture_av; @@ -173,7 +195,7 @@ layout_update_font (DC__Layout self) { /* use a random scale factor to account for unknown descenders, 0.8 works - * reasonably well with bitstream vera + * reasonably well with dejavu/bistream fonts */ PangoFontDescription *font = self->font ? self->font : default_font; @@ -352,6 +374,16 @@ self->rows = 0; } +#define CELL_CLEAR(cell) \ + do { \ + if ((cell)->player) \ + (cell)->tile [2] = 0; \ + (cell)->darkness = 0; \ + (cell)->stat_hp = 0; \ + (cell)->flags = 0; \ + (cell)->player = 0; \ + } while (0) + static void map_blank (DC__Map self, int x0, int y0, int w, int h) { @@ -375,10 +407,7 @@ cell = row->col + x - row->c0; - cell->darkness = 0; - cell->stat_hp = 0; - cell->flags = 0; - cell->player = 0; + CELL_CLEAR (cell); } } } @@ -500,6 +529,45 @@ return 1; } +static void +deliantra_main () +{ + char *argv[] = { 0 }; + call_argv ("::main", G_DISCARD | G_VOID, argv); + dSP; +} + +#ifdef __MACOSX__ + /* to due surprising braindamage on the side of SDL design, we + * do some mind-boggling hack here: SDL requires a custom main() + * on OS X, so... we provide one and call the original main(), which, + * due to share dlibrary magic, calls -lSDLmain's main, not perl's main, + * and which calls our main (== SDL_main) back. + */ + extern C_LINKAGE int + main (int argc, char *argv[]) + { + deliantra_main (); + } + + #undef main + + extern C_LINKAGE int main (int argc, char *argv[]); + + static void + SDL_braino (void) + { + char *argv[] = { "deliantra client", 0 }; + (main) (1, argv); + } +#else + static void + SDL_braino (void) + { + deliantra_main (); + } +#endif + MODULE = Deliantra::Client PACKAGE = DC PROTOTYPES: ENABLE @@ -623,6 +691,35 @@ const_iv (KMOD_MODE), const_iv (MIX_DEFAULT_FORMAT), + + const_iv (SDL_INIT_TIMER), + const_iv (SDL_INIT_AUDIO), + const_iv (SDL_INIT_VIDEO), + const_iv (SDL_INIT_CDROM), + const_iv (SDL_INIT_JOYSTICK), + const_iv (SDL_INIT_EVERYTHING), + const_iv (SDL_INIT_NOPARACHUTE), + const_iv (SDL_INIT_EVENTTHREAD), + + const_iv (SDL_GL_RED_SIZE), + const_iv (SDL_GL_GREEN_SIZE), + const_iv (SDL_GL_BLUE_SIZE), + const_iv (SDL_GL_ALPHA_SIZE), + const_iv (SDL_GL_DOUBLEBUFFER), + const_iv (SDL_GL_BUFFER_SIZE), + const_iv (SDL_GL_DEPTH_SIZE), + const_iv (SDL_GL_STENCIL_SIZE), + const_iv (SDL_GL_ACCUM_RED_SIZE), + const_iv (SDL_GL_ACCUM_GREEN_SIZE), + const_iv (SDL_GL_ACCUM_BLUE_SIZE), + const_iv (SDL_GL_ACCUM_ALPHA_SIZE), + const_iv (SDL_GL_STEREO), + const_iv (SDL_GL_MULTISAMPLEBUFFERS), + const_iv (SDL_GL_MULTISAMPLESAMPLES), + const_iv (SDL_GL_ACCELERATED_VISUAL), + const_iv (SDL_GL_SWAP_CONTROL), + + const_iv (FOW_DARKNESS) # undef const_iv }; @@ -661,16 +758,33 @@ opengl_fontmap = pango_opengl_font_map_new (); pango_opengl_font_map_set_default_substitute ((PangoOpenGLFontMap *)opengl_fontmap, substitute_func, 0, 0); opengl_context = pango_opengl_font_map_create_context ((PangoOpenGLFontMap *)opengl_fontmap); + /*pango_context_set_font_description (opengl_context, default_font);*/ +#if PANGO_VERSION_CHECK (1, 15, 2) + pango_context_set_language (opengl_context, pango_language_from_string ("en")); + /*pango_context_set_base_dir (opengl_context, PANGO_DIRECTION_WEAK_LTR);*/ +#endif } -char * -SDL_GetError () +char *SDL_GetError () -int -SDL_Init (U32 flags = SDL_INIT_VIDEO | SDL_INIT_AUDIO | PARACHUTE) +void SDL_braino () -void -SDL_Quit () +int SDL_Init (U32 flags) + +int SDL_InitSubSystem (U32 flags) + +void SDL_QuitSubSystem (U32 flags) + +void SDL_Quit () + +int SDL_GL_SetAttribute (int attr, int value) + +int SDL_GL_GetAttribute (int attr) + CODE: + if (SDL_GL_GetAttribute (attr, &RETVAL)) + XSRETURN_UNDEF; + OUTPUT: + RETVAL void SDL_ListModes (int rgb, int alpha) @@ -692,17 +806,14 @@ 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 m = SDL_ListModes (0, SDL_FULLSCREEN | SDL_OPENGL); if (m && m != (SDL_Rect **)-1) while (*m) { - if ((*m)->w >= 800 && (*m)->h >= 600) + if ((*m)->w >= 400 && (*m)->h >= 300) { AV *av = newAV (); av_push (av, newSViv ((*m)->w)); @@ -754,6 +865,9 @@ int SDL_GetAppState () +int +SDL_GetModState () + void poll_events () PPCODE: @@ -772,8 +886,8 @@ case SDL_KEYUP: 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, "mod", 3, newSViv (ev.key.keysym.mod & MOD_MASK), 0); + hv_store (hv, "cmod", 4, newSViv (SDL_GetModState () & MOD_MASK), 0); /* current mode */ hv_store (hv, "unicode", 7, newSViv (ev.key.keysym.unicode), 0); break; @@ -801,7 +915,7 @@ SDL_PeepEvents (&ev, 1, SDL_GETEVENT, SDL_EVENTMASK (SDL_MOUSEMOTION)); } - hv_store (hv, "mod", 3, newSViv (SDL_GetModState ()), 0); + hv_store (hv, "mod", 3, newSViv (SDL_GetModState () & MOD_MASK), 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); @@ -812,7 +926,7 @@ case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEBUTTONUP: - hv_store (hv, "mod", 3, newSViv (SDL_GetModState ()), 0); + hv_store (hv, "mod", 3, newSViv (SDL_GetModState () & MOD_MASK), 0); hv_store (hv, "button", 6, newSViv (ev.button.button), 0); hv_store (hv, "state", 5, newSViv (ev.button.state), 0); @@ -896,10 +1010,12 @@ #endif } -void +int add_font (char *file) CODE: - FcConfigAppFontAddFile (0, (const FcChar8 *)file); + RETVAL = FcConfigAppFontAddFile (0, (const FcChar8 *)file); + OUTPUT: + RETVAL void load_image_inline (SV *image_) @@ -1167,7 +1283,7 @@ do { - PangoLayoutRun *run = pango_layout_iter_get_run (iter); + PangoLayoutRun *run = pango_layout_iter_get_run_readonly (iter); if (run && shape_attr_p (run)) { @@ -1281,7 +1397,7 @@ CODE: { PangoRectangle rect; - PangoLayoutLine *line = pango_layout_get_line (self->pl, 0); + PangoLayoutLine *line = pango_layout_get_line_readonly (self->pl, 0); pango_layout_line_get_pixel_extents (line, 0, &rect); RETVAL = PANGO_DESCENT (rect); } @@ -1303,13 +1419,13 @@ cursor_pos (DC::Layout self, int index) PPCODE: { - PangoRectangle strong_pos; - pango_layout_get_cursor_pos (self->pl, index, &strong_pos, 0); + PangoRectangle pos; + pango_layout_get_cursor_pos (self->pl, index, &pos, 0); EXTEND (SP, 3); - PUSHs (sv_2mortal (newSViv (strong_pos.x / PANGO_SCALE))); - PUSHs (sv_2mortal (newSViv (strong_pos.y / PANGO_SCALE))); - PUSHs (sv_2mortal (newSViv (strong_pos.height / PANGO_SCALE))); + PUSHs (sv_2mortal (newSViv (pos.x / PANGO_SCALE))); + PUSHs (sv_2mortal (newSViv (pos.y / PANGO_SCALE))); + PUSHs (sv_2mortal (newSViv (pos.height / PANGO_SCALE))); } void @@ -1319,10 +1435,12 @@ int line, x; pango_layout_index_to_line_x (self->pl, index, trailing, &line, &x); +#if !PANGO_VERSION_CHECK (1, 17, 3) /* pango bug: line is between 1..numlines, not 0..numlines-1 */ - + --line; +#endif EXTEND (SP, 2); - PUSHs (sv_2mortal (newSViv (line - 1))); + PUSHs (sv_2mortal (newSViv (line))); PUSHs (sv_2mortal (newSViv (x / PANGO_SCALE))); } @@ -1336,7 +1454,7 @@ if (line < 0) XSRETURN_EMPTY; - if (!(lp = pango_layout_get_line (self->pl, line))) + if (!(lp = pango_layout_get_line_readonly (self->pl, line))) XSRETURN_EMPTY; /* do better */ pango_layout_line_x_to_index (lp, x * PANGO_SCALE, &index, &trailing); @@ -1663,7 +1781,7 @@ do { ext = *data++; - cmd = ext & 0x3f; + cmd = ext & 0x7f; if (cmd < 4) cell->darkness = 255 - ext * 64 + 1; @@ -1676,8 +1794,10 @@ cell->stat_width = *data++ + 1; else if (cmd == 0x47) { - if (*data == 4) - ; // decode player count + if (*data == 1) cell->player = data [1]; + else if (*data == 2) cell->player = data [2] + (data [1] << 8); + else if (*data == 3) cell->player = data [3] + (data [2] << 8) + (data [1] << 16); + else if (*data == 4) cell->player = data [4] + (data [3] << 8) + (data [2] << 16) + (data [1] << 24); data += *data + 1; } @@ -1717,7 +1837,7 @@ } } else - cell->darkness = 0; + CELL_CLEAR (cell); } } OUTPUT: @@ -1779,7 +1899,7 @@ RETVAL void -draw (DC::Map self, int mx, int my, int sw, int sh, int T) +draw (DC::Map self, int mx, int my, int sw, int sh, int T, U32 player = 0xffffffff, int sdx = 0, int sdy = 0) CODE: { int x, y, z; @@ -1788,11 +1908,14 @@ uint32_t smooth_level[256 / 32]; // one bit for every possible smooth level static uint8_t smooth_max[256][256]; // egad, fast and wasteful on memory (64k) smooth_key skey; - + int pl_x, pl_y; + maptex pl_tex; rc_t *rc = rc_alloc (); rc_key_t key; rc_array_t *arr; + pl_tex.name = 0; + // thats current max. sorry. if (sw > 255) sw = 255; if (sh > 255) sh = 255; @@ -1856,7 +1979,8 @@ if (tile) { maptex tex = self->tex [tile]; - int px, py; + int px = (x + 1) * T - tex.w; + int py = (y + 1) * T - tex.h; if (key.texname != tex.name) { @@ -1867,15 +1991,20 @@ arr = rc_array (rc, &key); } - px = (x + 1) * T - tex.w; - py = (y + 1) * T - tex.h; + if (expect_false (cell->player == player) && expect_false (z == 2)) + { + pl_x = px; + pl_y = py; + pl_tex = tex; + continue; + } rc_t2f_v3f (arr, 0 , 0 , px , py , 0); rc_t2f_v3f (arr, 0 , tex.t, px , py + tex.h, 0); rc_t2f_v3f (arr, tex.s, tex.t, px + tex.w, py + tex.h, 0); rc_t2f_v3f (arr, tex.s, 0 , px + tex.w, py , 0); - if (cell->flags && z == 2) + if (expect_false (cell->flags) && expect_false (z == 2)) { // overlays such as the speech bubble, probably more to come if (cell->flags & 1) @@ -2024,6 +2153,23 @@ hv_clear (smooth); } + if (pl_tex.name) + { + maptex tex = pl_tex; + int px = pl_x + sdx; + int py = pl_y + sdy; + + key.texname = tex.name; + arr = rc_array (rc, &key); + + rc_t2f_v3f (arr, 0 , 0 , px , py , 0); + rc_t2f_v3f (arr, 0 , tex.t, px , py + tex.h, 0); + rc_t2f_v3f (arr, tex.s, tex.t, px + tex.w, py + tex.h, 0); + rc_t2f_v3f (arr, tex.s, 0 , px + tex.w, py , 0); + + rc_draw (rc); + } + glDisable (GL_BLEND); rc_free (rc); @@ -2138,7 +2284,7 @@ mx += self->x - 1; my += self->y - 1; - memset (darkness1, 255, sw1 * sh1); + memset (darkness1, 255 - FOW_DARKNESS, sw1 * sh1); for (y = 0; y < sh1; y++) if (0 <= y + my && y + my < self->rows) @@ -2268,20 +2414,35 @@ } } - SvPOK_only (data_sv); - SvCUR_set (data_sv, data - (uint8_t *)SvPVX (data_sv)); - RETVAL = data_sv; + /* if size is w*h + 5 then no data has been found */ + if (data - (uint8_t *)SvPVX (data_sv) != w * h + 5) + { + SvPOK_only (data_sv); + SvCUR_set (data_sv, data - (uint8_t *)SvPVX (data_sv)); + } + + RETVAL = data_sv; } OUTPUT: RETVAL void -set_rect (DC::Map self, int x0, int y0, uint8_t *data) +set_rect (DC::Map self, int x0, int y0, SV *data_sv) PPCODE: { int x, y, z; int w, h; int x1, y1; + STRLEN len; + uint8_t *data, *end; + + len = SvLEN (data_sv); + SvGROW (data_sv, len + 8); // reserve at least 7+ bytes more + data = SvPVbyte_nolen (data_sv); + end = data + len + 8; + + if (len < 5) + XSRETURN_EMPTY; if (*data++ != 0) XSRETURN_EMPTY; /* version mismatch */ @@ -2306,7 +2467,12 @@ for (x = x0; x < x1; x++) { - uint8_t flags = *data++; + uint8_t flags; + + if (data + 7 >= end) + XSRETURN_EMPTY; + + flags = *data++; if (flags) { @@ -2319,7 +2485,8 @@ if (cell->darkness == 0) { - cell->darkness = 0; + /*cell->darkness = 0;*/ + EXTEND (SP, 3); for (z = 0; z <= 2; z++) { @@ -2327,7 +2494,7 @@ if (t >= self->texs || (t && !self->tex [t].name)) { - XPUSHs (sv_2mortal (newSViv (t))); + PUSHs (sv_2mortal (newSViv (t))); need_texid (self, t); } @@ -2848,7 +3015,7 @@ if (gl.SeparableFilter2D) gl.SeparableFilter2D (target, internalformat, width, height, format, type, row, column); -void glTexImage2D (int target, int level, int internalformat, int width, int height, int border, int format, int type, char *data) +void glTexImage2D (int target, int level, int internalformat, int width, int height, int border, int format, int type, char *data = 0) void glCopyTexImage2D (int target, int level, int internalformat, int x, int y, int width, int height, int border)