--- deliantra/Deliantra-Client/Client.xs 2006/05/26 18:28:23 1.97 +++ deliantra/Deliantra-Client/Client.xs 2006/06/15 15:33:57 1.117 @@ -12,6 +12,7 @@ #include #include #include +#include #include #include @@ -50,20 +51,22 @@ #define MIN_FONT_HEIGHT 10 -#define GL_CALL(type,func,args) \ - { \ - static int init_; \ - static type fptr_; \ - \ - if (!init_) \ - { \ - init_ = 1; \ - fptr_ = (type)SDL_GL_GetProcAddress (# func); \ - } \ - \ - if (fptr_) \ - fptr_ args; \ - } +static struct +{ +#define GL_FUNC(ptr,name) ptr name; +#include "glfunc.h" +#undef GL_FUNC +} gl; + +static void gl_BlendFuncSeparate (GLenum sa, GLenum da, GLenum saa, GLenum daa) +{ + if (gl.BlendFuncSeparate) + gl.BlendFuncSeparate (sa, da, saa, daa); + else if (gl.BlendFuncSeparateEXT) + gl.BlendFuncSeparateEXT (sa, da, saa, daa); + else + glBlendFunc (sa, da); +} typedef Mix_Chunk *CFClient__MixChunk; typedef Mix_Music *CFClient__MixMusic; @@ -85,7 +88,10 @@ static void substitute_func (FcPattern *pattern, gpointer data) { - FcPatternAddBool (pattern, FC_HINTING , 1); + FcPatternAddBool (pattern, FC_HINTING, 1); +#ifdef FC_HINT_STYLE + FcPatternAddBool (pattern, FC_HINT_STYLE, FC_HINT_FULL); +#endif #ifdef _WIN32 FcPatternAddBool (pattern, FC_AUTOHINT, 1); #else @@ -298,6 +304,23 @@ SDL_PushEvent ((SDL_Event *)&ev); } +static unsigned int +minpot (unsigned int n) +{ + if (!n) + return 0; + + --n; + + n |= n >> 1; + n |= n >> 2; + n |= n >> 4; + n |= n >> 8; + n |= n >> 16; + + return n + 1; +} + MODULE = CFClient PACKAGE = CFClient PROTOTYPES: ENABLE @@ -415,17 +438,35 @@ newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); } +NV floor (NV x) + +NV ceil (NV x) + void pango_init () CODE: -{ // delayed, so it can pick up new fonts added by AddFontResourceEx - ft2_fontmap = pango_ft2_font_map_new (); - pango_ft2_font_map_set_default_substitute ((PangoFT2FontMap *)ft2_fontmap, substitute_func, 0, 0); - ft2_context = pango_ft2_font_map_create_context ((PangoFT2FontMap *)ft2_fontmap); - - cairo_fontmap = pango_cairo_font_map_get_default (); - cairo_context = pango_cairo_font_map_create_context ((PangoCairoFontMap *)cairo_fontmap); +{ + { + ft2_fontmap = pango_ft2_font_map_new (); + pango_ft2_font_map_set_default_substitute ((PangoFT2FontMap *)ft2_fontmap, substitute_func, 0, 0); + ft2_context = pango_ft2_font_map_create_context ((PangoFT2FontMap *)ft2_fontmap); + } + { + cairo_font_options_t *fopt = cairo_font_options_create (); + cairo_fontmap = pango_cairo_font_map_get_default (); + cairo_context = pango_cairo_font_map_create_context ((PangoCairoFontMap *)cairo_fontmap); +#ifdef _WIN32 + // cairo looks like shit eaten twice on windows + cairo_font_options_set_antialias (fopt, CAIRO_ANTIALIAS_NONE); +#else + cairo_font_options_set_antialias (fopt, CAIRO_ANTIALIAS_GRAY); +#endif + cairo_font_options_set_hint_style (fopt, CAIRO_HINT_STYLE_FULL); + cairo_font_options_set_hint_metrics (fopt, CAIRO_HINT_METRICS_ON); + pango_cairo_context_set_font_options (cairo_context, fopt); + cairo_font_options_destroy (fopt); + } } int @@ -446,7 +487,7 @@ SDL_GL_SetAttribute (SDL_GL_ALPHA_SIZE, 1); SDL_GL_SetAttribute (SDL_GL_BUFFER_SIZE, 15); - SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 16); + SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 0); SDL_GL_SetAttribute (SDL_GL_ACCUM_RED_SIZE, 0); SDL_GL_SetAttribute (SDL_GL_ACCUM_GREEN_SIZE, 0); @@ -481,7 +522,13 @@ RETVAL = !!SDL_SetVideoMode ( w, h, 0, SDL_OPENGL | (fullscreen ? SDL_FULLSCREEN : 0) ); - SDL_WM_SetCaption ("Crossfire+ Client " VERSION, "Crossfire+"); + if (RETVAL) + { + 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 @@ -680,7 +727,12 @@ #ifdef _WIN32 MessageBox (0, message, "Crossfire+ Fatal Error", MB_OK | MB_ICONERROR); #endif - exit (1); + _exit (1); + +void +_exit (int retval) + CODE: + _exit (retval); MODULE = CFClient PACKAGE = CFClient::Font @@ -711,9 +763,6 @@ CFClient::Layout new (SV *class, int rgba = 0) CODE: -#if _WIN32 - //rgba = 0;//D make stext nicer, breaks TextView -#endif New (0, RETVAL, 1, struct cf_layout); RETVAL->pl = pango_layout_new (rgba ? cairo_context : ft2_context); @@ -767,7 +816,7 @@ get_text (CFClient::Layout self) CODE: RETVAL = newSVpv (pango_layout_get_text (self->pl), 0); - SvUTF8_on (RETVAL); + sv_utf8_decode (RETVAL); OUTPUT: RETVAL @@ -958,7 +1007,44 @@ MODULE = CFClient PACKAGE = CFClient::Texture void -draw_quad (SV *self, float x, float y, float w = 0, float h = 0) +pad2pot (SV *data_, SV *w_, SV *h_) + CODE: +{ + int ow = SvIV (w_); + int oh = SvIV (h_); + + if (ow && oh) + { + 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_); + } + + sv_setiv (w_, nw); + sv_setiv (h_, nh); + } + } +} + +void +draw_quad (SV *self, float x, float y, float w = 0., float h = 0.) PROTOTYPE: $$$;$$ ALIAS: draw_quad_alpha = 1 @@ -969,7 +1055,6 @@ float s = SvNV (*hv_fetch (hv, "s", 1, 1)); float t = SvNV (*hv_fetch (hv, "t", 1, 1)); int name = SvIV (*hv_fetch (hv, "name", 4, 1)); - int wrap_mode = SvIV (*hv_fetch (hv, "wrap_mode", 9, 1)); if (items < 5) { @@ -980,19 +1065,19 @@ if (ix) { glEnable (GL_BLEND); - glBlendFunc (ix == 1 ? GL_SRC_ALPHA : GL_ONE, GL_ONE_MINUS_SRC_ALPHA); + + if (ix == 2) + glBlendFunc (GL_ONE, GL_ONE_MINUS_SRC_ALPHA); + else + gl_BlendFuncSeparate (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, + GL_ONE , GL_ONE_MINUS_SRC_ALPHA); + glEnable (GL_ALPHA_TEST); glAlphaFunc (GL_GREATER, 0.01f); } glBindTexture (GL_TEXTURE_2D, name); - if (wrap_mode) - { - glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); - glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); - } - glBegin (GL_QUADS); glTexCoord2f (0, 0); glVertex2f (x , y ); glTexCoord2f (0, t); glVertex2f (x , y + h); @@ -1034,6 +1119,7 @@ { map_clear (self); Safefree (self->face); + Safefree (self->tex); Safefree (self); } @@ -1082,19 +1168,30 @@ // somewhat hackish, but for textures that require it, it really // improves the look, and most others don't suffer. glBindTexture (GL_TEXTURE_2D, name); - glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); - glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); + //glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + //glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + // use uglier nearest interpolation because linear suffers + // from transparent color bleeding and ugly wrapping effects. + glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); } int ox (CFClient::Map self) ALIAS: oy = 1 + x = 2 + y = 3 + w = 4 + h = 5 CODE: switch (ix) { case 0: RETVAL = self->ox; break; case 1: RETVAL = self->oy; break; + case 2: RETVAL = self->x; break; + case 3: RETVAL = self->y; break; + case 4: RETVAL = self->w; break; + case 5: RETVAL = self->h; break; } OUTPUT: RETVAL @@ -1239,22 +1336,15 @@ void draw (CFClient::Map self, int shift_x, int shift_y, int x0, int y0, int sw, int sh) - PPCODE: + CODE: { int vx, vy; int x, y, z; int last_name; mapface face; - 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); - vx = self->x + (self->w - sw) / 2 - shift_x; - vy = self->y + (self->h - sh) / 2 - shift_y; + vx = self->x + (self->w - sw + 1) / 2 - shift_x; + vy = self->y + (self->h - sh + 1) / 2 - shift_y; /* int vx = self->vx = self->w >= sw @@ -1268,8 +1358,8 @@ glColor4ub (255, 255, 255, 255); - glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glEnable (GL_BLEND); + glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glEnable (GL_TEXTURE_2D); glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); @@ -1288,10 +1378,6 @@ { mapcell *cell = row->col + (x + vx - row->c0); - darkness[y * sw4 + x] = cell->darkness < 0 - ? 255 - FOW_DARKNESS - : 255 - cell->darkness; - face = cell->face [z]; if (face) @@ -1321,6 +1407,97 @@ glDisable (GL_TEXTURE_2D); glDisable (GL_BLEND); +} + +void +draw_magicmap (CFClient::Map self, int dx, int dy, int w, int h, unsigned char *data) + CODE: +{ + static float color[16][3] = { + { 0.00, 0.00, 0.00 }, + { 1.00, 1.00, 1.00 }, + { 0.00, 0.00, 0.55 }, + { 1.00, 0.00, 0.00 }, + + { 1.00, 0.54, 0.00 }, + { 0.11, 0.56, 1.00 }, + { 0.93, 0.46, 0.00 }, + { 0.18, 0.54, 0.34 }, + + { 0.56, 0.73, 0.56 }, + { 0.80, 0.80, 0.80 }, + { 0.55, 0.41, 0.13 }, + { 0.99, 0.77, 0.26 }, + + { 0.74, 0.65, 0.41 }, + + { 0.00, 1.00, 1.00 }, + { 1.00, 0.00, 1.00 }, + { 1.00, 1.00, 0.00 }, + }; + + int x, y; + + glEnable (GL_TEXTURE_2D); + glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); + glEnable (GL_BLEND); + glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + glBegin (GL_QUADS); + + for (y = 0; y < h; y++) + for (x = 0; x < w; x++) + { + unsigned char m = data [x + y * w]; + + float *c = color [m & 15]; + + float tx1 = m & 0x40 ? 0.5 : m & 0x80 ? 0.25 : 0.; + float tx2 = tx1 + 0.25; + + glColor4f (c[0], c[1], c[2], 0.75); + glTexCoord2f (tx1, 0.); glVertex2i (x , y ); + glTexCoord2f (tx1, 1.); glVertex2i (x , y + 1); + glTexCoord2f (tx2, 1.); glVertex2i (x + 1, y + 1); + glTexCoord2f (tx2, 0.); glVertex2i (x + 1, y ); + } + + glEnd (); + glDisable (GL_BLEND); + glDisable (GL_TEXTURE_2D); +} + +void +fow_texture (CFClient::Map self, int shift_x, int shift_y, int x0, int y0, 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); + + vx = self->x + (self->w - sw + 1) / 2 - shift_x; + vy = self->y + (self->h - sh + 1) / 2 - shift_y; + + for (y = 0; y < sh; y++) + if (0 <= y + vy && y + vy < self->rows) + { + maprow *row = self->row + (y + vy); + + for (x = 0; x < sw; x++) + if (row->c0 <= x + vx && x + vx < row->c1) + { + mapcell *cell = row->col + (x + vx - row->c0); + + darkness[y * sw4 + x] = cell->darkness < 0 + ? 255 - FOW_DARKNESS + : 255 - cell->darkness; + } + } EXTEND (SP, 3); PUSHs (sv_2mortal (newSViv (sw4))); @@ -1541,11 +1718,15 @@ const_iv (GL_ONE), const_iv (GL_ZERO), const_iv (GL_SRC_ALPHA), - const_iv (GL_SRC_ALPHA_SATURATE), + const_iv (GL_DST_ALPHA), const_iv (GL_ONE_MINUS_SRC_ALPHA), const_iv (GL_ONE_MINUS_DST_ALPHA), + const_iv (GL_SRC_ALPHA_SATURATE), const_iv (GL_RGB), const_iv (GL_RGBA), + const_iv (GL_RGBA4), + const_iv (GL_RGBA8), + const_iv (GL_RGB5_A1), const_iv (GL_UNSIGNED_BYTE), const_iv (GL_UNSIGNED_SHORT), const_iv (GL_UNSIGNED_INT), @@ -1564,8 +1745,9 @@ const_iv (GL_TEXTURE_ENV_MODE), const_iv (GL_TEXTURE_WRAP_S), const_iv (GL_TEXTURE_WRAP_T), - const_iv (GL_CLAMP), const_iv (GL_REPEAT), + const_iv (GL_CLAMP), + const_iv (GL_CLAMP_TO_EDGE), const_iv (GL_NEAREST), const_iv (GL_LINEAR), const_iv (GL_NEAREST_MIPMAP_NEAREST), @@ -1628,6 +1810,8 @@ int glGetError () +void glFinish () + void glClear (int mask) void glClearColor (float r, float g, float b, float a = 1.0) @@ -1643,6 +1827,10 @@ void glBlendFunc (int sfactor, int dfactor) +void glBlendFuncSeparate (int sa, int da, int saa, int daa) + CODE: + gl_BlendFuncSeparate (sa, da, saa, daa); + void glDepthMask (int flag) void glLogicOp (int opcode) @@ -1685,12 +1873,20 @@ void glColor (float r, float g, float b, float a = 1.0) PROTOTYPE: @ + ALIAS: + glColor_premultiply = 1 CODE: + if (ix) + { + r *= a; + g *= a; + b *= a; + } // microsoft visual "c" rounds instead of truncating... - glColor4ub (MIN ((int)(r * 255.f), 255), - MIN ((int)(g * 255.f), 255), - MIN ((int)(b * 255.f), 255), - MIN ((int)(a * 255.f), 255)); + 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)); void glInterleavedArrays (int format, int stride, char *data) @@ -1723,17 +1919,18 @@ void glConvolutionParameter (int target, int pname, float params) CODE: - GL_CALL (PFNGLCONVOLUTIONPARAMETERFEXTPROC, glConvolutionParameterf, (target, pname, params)); + if (gl.ConvolutionParameterf) + gl.ConvolutionParameterf (target, pname, params); void glConvolutionFilter2D (int target, int internalformat, int width, int height, int format, int type, char *data) CODE: - GL_CALL (PFNGLCONVOLUTIONFILTER2DEXTPROC, glConvolutionFilter2D, - (target, internalformat, width, height, format, type, data)); + if (gl.ConvolutionFilter2D) + gl.ConvolutionFilter2D (target, internalformat, width, height, format, type, data); void glSeparableFilter2D (int target, int internalformat, int width, int height, int format, int type, char *row, char *column) CODE: - GL_CALL (PFNGLSEPARABLEFILTER2DEXTPROC, glSeparableFilter2D, - (target, internalformat, width, height, format, type, row, column)); + 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)