--- deliantra/Deliantra-Client/Client.xs 2006/04/19 23:37:48 1.54 +++ deliantra/Deliantra-Client/Client.xs 2006/05/17 15:18:57 1.84 @@ -1,15 +1,20 @@ #ifdef _WIN32 +# define _WIN32_WINNT 0x0500 // needed to get win2000 api calls # include +# include +# pragma warning(disable:4244) #endif #include "EXTERN.h" #include "perl.h" #include "XSUB.h" +#include #include #include #include +#include #include #include #include @@ -19,6 +24,7 @@ #include #include #include +#include #ifndef _WIN32 # include @@ -35,56 +41,81 @@ typedef signed int int32_t; #endif +#include "glext.h" + #define FOW_DARKNESS 32 #define MAP_EXTEND_X 32 #define MAP_EXTEND_Y 512 +#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; \ + } + typedef Mix_Chunk *CFClient__MixChunk; typedef Mix_Music *CFClient__MixMusic; -static PangoContext *context; -static PangoFontMap *fontmap; +typedef PangoFontDescription *CFClient__Font; typedef struct cf_layout { - PangoLayout *pl; + PangoLayout *pl; // either derived from a cairo or ft2 context + int rgba; // wether we use rgba (cairo) or grayscale (ft2) + float r, g, b, a; // default color for rgba mode int base_height; + CFClient__Font font; } *CFClient__Layout; +static CFClient__Font default_font; +static PangoContext *ft2_context, *cairo_context; +static PangoFontMap *ft2_fontmap, *cairo_fontmap; + static void substitute_func (FcPattern *pattern, gpointer data) { FcPatternAddBool (pattern, FC_HINTING , 1); +#ifdef _WIN32 + FcPatternAddBool (pattern, FC_AUTOHINT, 1); +#else FcPatternAddBool (pattern, FC_AUTOHINT, 0); +#endif } static void -layout_update (CFClient__Layout self) +layout_update_font (CFClient__Layout self) { /* use a random scale factor to account for unknown descenders, 0.8 works * reasonably well with bitstream vera */ - PangoFontDescription *font = pango_context_get_font_description (context); + PangoFontDescription *font = self->font ? self->font : default_font; - int height = self->base_height * (PANGO_SCALE * 8 / 10); + pango_font_description_set_absolute_size (font, + MAX (MIN_FONT_HEIGHT, self->base_height) * (PANGO_SCALE * 8 / 10)); - if (pango_font_description_get_size (font) != height) - { - pango_font_description_set_absolute_size (font, height); - pango_layout_context_changed (self->pl); - } + pango_layout_set_font_description (self->pl, font); } static void layout_get_pixel_size (CFClient__Layout self, int *w, int *h) { - layout_update (self); - pango_layout_get_pixel_size (self->pl, w, h); - *w = (*w + 3) & ~3; if (!*w) *w = 1; if (!*h) *h = 1; + + *w = (*w + 3) & ~3; } typedef uint16_t mapface; @@ -241,6 +272,32 @@ } } +static void +music_finished (void) +{ + SDL_UserEvent ev; + + ev.type = SDL_USEREVENT; + ev.code = 0; + ev.data1 = 0; + ev.data2 = 0; + + SDL_PushEvent ((SDL_Event *)&ev); +} + +static void +channel_finished (int channel) +{ + SDL_UserEvent ev; + + ev.type = SDL_USEREVENT; + ev.code = 1; + ev.data1 = (void *)(long)channel; + ev.data2 = 0; + + SDL_PushEvent ((SDL_Event *)&ev); +} + MODULE = CFClient PACKAGE = CFClient PROTOTYPES: ENABLE @@ -356,10 +413,19 @@ for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); +} + +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); - fontmap = pango_ft2_font_map_new (); - pango_ft2_font_map_set_default_substitute ((PangoFT2FontMap *)fontmap, substitute_func, 0, 0); - context = pango_ft2_font_map_create_context ((PangoFT2FontMap *)fontmap); + cairo_fontmap = pango_cairo_font_map_get_default (); + cairo_context = pango_cairo_font_map_create_context ((PangoCairoFontMap *)cairo_fontmap); } int @@ -428,6 +494,7 @@ { HV *hv = newHV (); hv_store (hv, "type", 4, newSViv (ev.type), 0); + switch (ev.type) { case SDL_KEYDOWN: @@ -457,6 +524,13 @@ hv_store (hv, "state", 5, newSViv (ev.button.state), 0); hv_store (hv, "x", 1, newSViv (ev.button.x), 0); hv_store (hv, "y", 1, newSViv (ev.button.y), 0); + break; + + case SDL_USEREVENT: + hv_store (hv, "code", 4, newSViv (ev.user.code), 0); + hv_store (hv, "data1", 5, newSViv ((IV)ev.user.data1), 0); + hv_store (hv, "data2", 5, newSViv ((IV)ev.user.data2), 0); + break; } XPUSHs (sv_2mortal (newRV_noinc ((SV *)hv))); @@ -464,7 +538,10 @@ } int -Mix_OpenAudio (int frequency = 22050, int format = MIX_DEFAULT_FORMAT, int channels = 1, int chunksize = 512) +Mix_OpenAudio (int frequency = 48000, int format = MIX_DEFAULT_FORMAT, int channels = 1, int chunksize = 2048) + POSTCALL: + Mix_HookMusicFinished (music_finished); + Mix_ChannelFinished (channel_finished); void Mix_CloseAudio () @@ -497,17 +574,10 @@ add_font (char *file) CODE: FcConfigAppFontAddFile (0, (const FcChar8 *)file); /* no idea wether this is required */ - -void -set_font (char *file) - CODE: -{ - int count; - FcPattern *pattern = FcFreeTypeQuery ((const FcChar8 *)file, 0, 0, &count); - PangoFontDescription *font = pango_fc_font_description_from_pattern (pattern, 0); - FcPatternDestroy (pattern); - pango_context_set_font_description (context, font); -} +#ifdef _WIN32 + // cairo... sigh... requires win2000 + AddFontResourceEx (file, FR_PRIVATE, 0); +#endif void load_image_inline (SV *image_) @@ -599,6 +669,15 @@ } void +error (char *message) + CODE: +#ifdef _WIN32 + MessageBox (0, message, "Crossfire+ Error", MB_OK | MB_ICONERROR | MB_SETFOREGROUND); +#else + fprintf (stderr, "ERROR: %s\n", message); +#endif + +void fatal (char *message) CODE: #ifdef _WIN32 @@ -608,15 +687,48 @@ #endif exit (1); +MODULE = CFClient PACKAGE = CFClient::Font + +CFClient::Font +new_from_file (SV *class, char *path, int id = 0) + CODE: +{ + int count; + FcPattern *pattern = FcFreeTypeQuery ((const FcChar8 *)path, id, 0, &count); + RETVAL = pango_fc_font_description_from_pattern (pattern, 0); + FcPatternDestroy (pattern); +} + OUTPUT: + RETVAL + +void +DESTROY (CFClient::Font self) + CODE: + pango_font_description_free (self); + +void +make_default (CFClient::Font self) + CODE: + default_font = self; + MODULE = CFClient PACKAGE = CFClient::Layout CFClient::Layout -new (SV *class, int base_height = 10) +new (SV *class, int rgba = 0) CODE: New (0, RETVAL, 1, struct cf_layout); - RETVAL->base_height = base_height; - RETVAL->pl = pango_layout_new (context); + + RETVAL->pl = pango_layout_new (rgba ? cairo_context : ft2_context); + RETVAL->rgba = rgba; + RETVAL->r = 1.; + RETVAL->g = 1.; + RETVAL->b = 1.; + RETVAL->a = 1.; + RETVAL->base_height = MIN_FONT_HEIGHT; + RETVAL->font = 0; + pango_layout_set_wrap (RETVAL->pl, PANGO_WRAP_WORD_CHAR); + layout_update_font (RETVAL); OUTPUT: RETVAL @@ -626,6 +738,13 @@ g_object_unref (self->pl); Safefree (self); +int +is_rgba (CFClient::Layout self) + CODE: + RETVAL = self->rgba; + OUTPUT: + RETVAL + void set_text (CFClient::Layout self, SV *text_) CODE: @@ -655,9 +774,30 @@ RETVAL void +set_foreground (CFClient::Layout self, float r, float g, float b, float a = 1.) + CODE: + self->r = r; + self->g = g; + self->b = b; + self->a = a; + +void +set_font (CFClient::Layout self, CFClient::Font font = 0) + CODE: + if (self->font != font) + { + self->font = font; + layout_update_font (self); + } + +void set_height (CFClient::Layout self, int base_height) CODE: - self->base_height = base_height; + if (self->base_height != base_height) + { + self->base_height = base_height; + layout_update_font (self); + } void set_width (CFClient::Layout self, int max_width = -1) @@ -665,12 +805,36 @@ pango_layout_set_width (self->pl, max_width < 0 ? max_width : max_width * PANGO_SCALE); void +set_indent (CFClient::Layout self, int indent) + CODE: + pango_layout_set_indent (self->pl, indent * PANGO_SCALE); + +void +set_spacing (CFClient::Layout self, int spacing) + CODE: + pango_layout_set_spacing (self->pl, spacing * PANGO_SCALE); + +void +set_ellipsise (CFClient::Layout self, int ellipsise) + CODE: + pango_layout_set_ellipsize (self->pl, + ellipsise == 1 ? PANGO_ELLIPSIZE_START + : ellipsise == 2 ? PANGO_ELLIPSIZE_MIDDLE + : ellipsise == 3 ? PANGO_ELLIPSIZE_END + : PANGO_ELLIPSIZE_NONE + ); + +void +set_single_paragraph_mode (CFClient::Layout self, int spm) + CODE: + pango_layout_set_single_paragraph_mode (self->pl, !!spm); + +void size (CFClient::Layout self) PPCODE: { int w, h; - layout_update (self); layout_get_pixel_size (self, &w, &h); EXTEND (SP, 2); @@ -683,10 +847,7 @@ CODE: { int index, trailing; - - layout_update (self); pango_layout_xy_to_index (self->pl, x * PANGO_SCALE, y * PANGO_SCALE, &index, &trailing); - RETVAL = index; } OUTPUT: @@ -697,7 +858,6 @@ PPCODE: { PangoRectangle strong_pos; - layout_update (self); pango_layout_get_cursor_pos (self->pl, index, &strong_pos, 0); EXTEND (SP, 3); @@ -712,30 +872,77 @@ { SV *retval; int w, h; - FT_Bitmap bitmap; - layout_update (self); layout_get_pixel_size (self, &w, &h); - retval = newSV (w * h); - SvPOK_only (retval); - SvCUR_set (retval, w * h); - - bitmap.rows = h; - bitmap.width = w; - bitmap.pitch = w; - bitmap.buffer = (unsigned char*)SvPVX (retval); - bitmap.num_grays = 256; - bitmap.pixel_mode = FT_PIXEL_MODE_GRAY; + if (self->rgba) + { + cairo_surface_t *surface; + cairo_t *cairo; - memset (bitmap.buffer, 0, w * h); - - pango_ft2_render_layout (&bitmap, self->pl, 0 * PANGO_SCALE, 0 * PANGO_SCALE); + retval = newSV (w * h * 4); + SvPOK_only (retval); + SvCUR_set (retval, w * h * 4); - EXTEND (SP, 3); - PUSHs (sv_2mortal (newSViv (w))); - PUSHs (sv_2mortal (newSViv (h))); - PUSHs (sv_2mortal (retval)); + memset (SvPVX (retval), 0, w * h * 4); + + surface = cairo_image_surface_create_for_data ( + (void*)SvPVX (retval), CAIRO_FORMAT_ARGB32, w, h, w * 4); + cairo = cairo_create (surface); + cairo_set_source_rgba (cairo, self->r, self->g, self->b, self->a); + + pango_cairo_show_layout (cairo, self->pl); + + cairo_destroy (cairo); + cairo_surface_destroy (surface); + + // what a mess, and its premultiplied, too :( + { + uint32_t *p = (uint32_t *)SvPVX (retval); + uint32_t *e = p + w * h; + + while (p < e) + { + uint32_t rgba = *p; + rgba = (rgba >> 24) | (rgba << 8); + rgba = SDL_SwapBE32 (rgba); + *p++ = rgba; + } + } + + EXTEND (SP, 5); + PUSHs (sv_2mortal (newSViv (w))); + PUSHs (sv_2mortal (newSViv (h))); + PUSHs (sv_2mortal (retval)); + PUSHs (sv_2mortal (newSViv (GL_RGBA))); + PUSHs (sv_2mortal (newSViv (GL_RGBA))); + } + else + { + FT_Bitmap bitmap; + + retval = newSV (w * h); + SvPOK_only (retval); + SvCUR_set (retval, w * h); + + bitmap.rows = h; + bitmap.width = w; + bitmap.pitch = w; + bitmap.buffer = (unsigned char*)SvPVX (retval); + bitmap.num_grays = 256; + bitmap.pixel_mode = FT_PIXEL_MODE_GRAY; + + memset (bitmap.buffer, 0, w * h); + + pango_ft2_render_layout (&bitmap, self->pl, 0 * PANGO_SCALE, 0 * PANGO_SCALE); + + EXTEND (SP, 5); + PUSHs (sv_2mortal (newSViv (w))); + PUSHs (sv_2mortal (newSViv (h))); + PUSHs (sv_2mortal (retval)); + PUSHs (sv_2mortal (newSViv (GL_ALPHA))); + PUSHs (sv_2mortal (newSViv (GL_ALPHA))); + } } MODULE = CFClient PACKAGE = CFClient::Texture @@ -743,6 +950,9 @@ void draw_quad (SV *self, float x, float y, float w = 0, float h = 0) PROTOTYPE: $$$;$$ + ALIAS: + draw_quad_alpha = 1 + draw_quad_alpha_premultiplied = 2 CODE: { HV *hv = (HV *)SvRV (self); @@ -757,17 +967,29 @@ h = SvNV (*hv_fetch (hv, "h", 1, 1)); } + if (ix) + { + glEnable (GL_BLEND); + glBlendFunc (ix == 1 ? GL_SRC_ALPHA : GL_ONE, GL_ONE_MINUS_SRC_ALPHA); + } + 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); - } + + 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); glTexCoord2f (s, t); glVertex2f (x + w, y + h); glTexCoord2f (s, 0); glVertex2f (x + w, y ); glEnd (); + + if (ix) + glDisable (GL_BLEND); } MODULE = CFClient PACKAGE = CFClient::Map @@ -935,11 +1157,11 @@ } SV * -mapmap (CFClient::Map self, int w, int h) +mapmap (CFClient::Map self, int x0, int y0, int w, int h) CODE: { - int x0, x1, x; - int y0, y1, y; + int x1, x; + int y1, y; int z; SV *map_sv = newSV (w * h * sizeof (uint32_t)); uint32_t *map = (uint32_t *)SvPVX (map_sv); @@ -947,8 +1169,8 @@ SvPOK_only (map_sv); SvCUR_set (map_sv, w * h * sizeof (uint32_t)); - x0 = self->x - w / 2; x1 = x0 + w; - y0 = self->y - h / 2; y1 = y0 + h; + x0 += self->x; x1 = x0 + w; + y0 += self->y; y1 = y0 + h; for (y = y0; y < y1; y++) { @@ -1098,7 +1320,7 @@ *data++ = h >> 8; *data++ = h; // we need to do this 'cause we don't keep an absolute coord system for rows - // TODO: treat rows as we treat + // TODO: treat rows as we treat columns map_get_row (self, y0 + self->y - self->oy);//D map_get_row (self, y0 + self->y - self->oy + h - 1);//D @@ -1172,7 +1394,7 @@ h = *data++ << 8; h |= *data++; // we need to do this 'cause we don't keep an absolute coord system for rows - // TODO: treat rows as we treat + // TODO: treat rows as we treat columns map_get_row (self, y0 + self->y - self->oy);//D map_get_row (self, y0 + self->y - self->oy + h - 1);//D @@ -1286,15 +1508,21 @@ const_iv (GL_COLOR_MATERIAL), const_iv (GL_SMOOTH), const_iv (GL_FLAT), + const_iv (GL_DITHER), const_iv (GL_BLEND), + const_iv (GL_SCISSOR_TEST), const_iv (GL_AND), + const_iv (GL_ONE), + const_iv (GL_ZERO), const_iv (GL_SRC_ALPHA), + const_iv (GL_SRC_ALPHA_SATURATE), const_iv (GL_ONE_MINUS_SRC_ALPHA), + const_iv (GL_ONE_MINUS_DST_ALPHA), const_iv (GL_RGB), const_iv (GL_RGBA), const_iv (GL_UNSIGNED_BYTE), - const_iv (GL_ALPHA4), const_iv (GL_ALPHA), + const_iv (GL_LUMINANCE), const_iv (GL_FLOAT), const_iv (GL_UNSIGNED_INT_8_8_8_8_REV), const_iv (GL_COMPILE), @@ -1310,17 +1538,25 @@ const_iv (GL_REPEAT), const_iv (GL_NEAREST), const_iv (GL_LINEAR), + const_iv (GL_NEAREST_MIPMAP_NEAREST), + const_iv (GL_LINEAR_MIPMAP_NEAREST), + const_iv (GL_NEAREST_MIPMAP_LINEAR), + const_iv (GL_LINEAR_MIPMAP_LINEAR), + const_iv (GL_GENERATE_MIPMAP), const_iv (GL_MODULATE), + const_iv (GL_DECAL), const_iv (GL_REPLACE), const_iv (GL_COLOR_BUFFER_BIT), const_iv (GL_PROJECTION), const_iv (GL_MODELVIEW), const_iv (GL_COLOR_LOGIC_OP), + const_iv (GL_SEPARABLE_2D), const_iv (GL_CONVOLUTION_2D), const_iv (GL_CONVOLUTION_BORDER_MODE), const_iv (GL_CONSTANT_BORDER), const_iv (GL_LINES), const_iv (GL_QUADS), + const_iv (GL_LINE_LOOP), const_iv (GL_PERSPECTIVE_CORRECTION_HINT), const_iv (GL_FASTEST), # undef const_iv @@ -1349,6 +1585,8 @@ void glLogicOp (int opcode) +void glColorMask (int red, int green, int blue, int alpha) + void glMatrixMode (int mode) void glPushMatrix () @@ -1357,15 +1595,18 @@ void glLoadIdentity () -void glOrtho (double left, double right, double bottom, double top, double near, double far) +# near and far are due to microsofts buggy c compiler +void glOrtho (double left, double right, double bottom, double top, double near_, double far_) void glViewport (int x, int y, int width, int height) +void glScissor (int x, int y, int width, int height) + void glTranslate (float x, float y, float z = 0.) CODE: glTranslatef (x, y, z); -void glScale (float x, float y, float z) +void glScale (float x, float y, float z = 1.) CODE: glScalef (x, y, z); @@ -1380,7 +1621,10 @@ void glColor (float r, float g, float b, float a = 1.0) PROTOTYPE: @ CODE: - glColor4f (r, g, b, a); + 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)); void glVertex (float x, float y, float z = 0.) CODE: @@ -1402,14 +1646,28 @@ void glConvolutionParameter (int target, int pname, float params) CODE: - glConvolutionParameterf (target, pname, params); + GL_CALL (PFNGLCONVOLUTIONPARAMETERFEXTPROC, glConvolutionParameterf, (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)); + +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)); void glTexImage2D (int target, int level, int internalformat, int width, int height, int border, int format, int type, char *data) void glCopyTexImage2D (int target, int level, int internalformat, int x, int y, int width, int height, int border) +void glRasterPos (int x, int y) + CODE: + glRasterPos2i (x, y); + +void glCopyPixels (int x, int y, int width, int height, int type = GL_COLOR) + int glGenTexture () CODE: {