--- deliantra/Deliantra-Client/Client.xs 2006/05/05 19:10:16 1.72 +++ deliantra/Deliantra-Client/Client.xs 2006/05/23 18:54:37 1.93 @@ -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 @@ -65,20 +71,26 @@ 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 *context; -static PangoFontMap *fontmap; +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 @@ -100,9 +112,10 @@ { 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; @@ -260,7 +273,7 @@ } static void -music_finished () +music_finished (void) { SDL_UserEvent ev; @@ -279,7 +292,7 @@ ev.type = SDL_USEREVENT; ev.code = 1; - ev.data1 = (void *)channel; + ev.data1 = (void *)(long)channel; ev.data2 = 0; SDL_PushEvent ((SDL_Event *)&ev); @@ -400,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)); +} - 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); +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); } int @@ -423,14 +445,15 @@ SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE, 5); 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_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_ALPHA_SIZE, 0); SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1); - SDL_GL_SetAttribute (SDL_GL_BUFFER_SIZE, 15); - SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 0); SDL_EnableUNICODE (1); SDL_EnableKeyRepeat (SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL); @@ -449,6 +472,9 @@ } } +char * +SDL_GetError () + int SDL_SetVideoMode (int w, int h, int fullscreen) CODE: @@ -467,6 +493,18 @@ PPCODE: { SDL_Event ev; + int i; + static int modifier; + static int modkey[] = { + SDLK_RSHIFT, KMOD_RSHIFT, + SDLK_LSHIFT, KMOD_LSHIFT, + SDLK_RCTRL, KMOD_RCTRL, + SDLK_LCTRL, KMOD_LCTRL, + SDLK_RALT, KMOD_RALT, + SDLK_LALT, KMOD_LALT, + SDLK_RMETA, KMOD_RMETA, + SDLK_LMETA, KMOD_LMETA, + }; while (SDL_PollEvent (&ev)) { @@ -477,6 +515,17 @@ { case SDL_KEYDOWN: case SDL_KEYUP: + + for (i = 0; i < sizeof (modkey) / (sizeof (int) * 2); i++) + if (modkey [i * 2] == ev.key.keysym.sym) + { + if (ev.type == SDL_KEYDOWN) + modifier |= modkey [i * 2 + 1]; + else + modifier &= ~modkey [i * 2 + 1]; + break; + } + 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); @@ -489,6 +538,8 @@ break; case SDL_MOUSEMOTION: + hv_store (hv, "mod", 3, newSViv (modifier), 0); + 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); @@ -498,6 +549,8 @@ case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEBUTTONUP: + hv_store (hv, "mod", 3, newSViv (modifier), 0); + hv_store (hv, "button", 6, newSViv (ev.button.button), 0); hv_store (hv, "state", 5, newSViv (ev.button.state), 0); hv_store (hv, "x", 1, newSViv (ev.button.x), 0); @@ -516,7 +569,7 @@ } 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); @@ -535,6 +588,13 @@ #endif char * +gl_vendor () + CODE: + RETVAL = (char *)glGetString (GL_VENDOR); + OUTPUT: + RETVAL + +char * gl_version () CODE: RETVAL = (char *)glGetString (GL_VERSION); @@ -552,6 +612,10 @@ add_font (char *file) CODE: FcConfigAppFontAddFile (0, (const FcChar8 *)file); /* no idea wether this is required */ +#ifdef _WIN32 + // cairo... sigh... requires win2000 + AddFontResourceEx (file, FR_PRIVATE, 0); +#endif void load_image_inline (SV *image_) @@ -645,19 +709,17 @@ 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); +#ifdef _WIN32 + MessageBox (0, message, "Crossfire+ Error", MB_OK | MB_ICONERROR); #endif void fatal (char *message) CODE: -#ifdef _WIN32 - MessageBox (0, message, "Crossfire+ Fatal Error", MB_OK | MB_ICONERROR | MB_SETFOREGROUND); -#else fprintf (stderr, "FATAL: %s\n", message); +#ifdef _WIN32 + MessageBox (0, message, "Crossfire+ Fatal Error", MB_OK | MB_ICONERROR); #endif exit (1); @@ -688,14 +750,24 @@ MODULE = CFClient PACKAGE = CFClient::Layout CFClient::Layout -new (SV *class, int base_height = MIN_FONT_HEIGHT) +new (SV *class, int rgba = 0) CODE: +#if _WIN32 + rgba = 0;//D +#endif New (0, RETVAL, 1, struct cf_layout); - RETVAL->pl = pango_layout_new (context); - RETVAL->base_height = base_height; - RETVAL->font = 0; + + 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); - pango_layout_set_font_description (RETVAL->pl, default_font); + layout_update_font (RETVAL); OUTPUT: RETVAL @@ -705,6 +777,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: @@ -734,6 +813,14 @@ 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) @@ -757,6 +844,31 @@ 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: { @@ -799,29 +911,89 @@ { SV *retval; int w, h; - FT_Bitmap bitmap; 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); +#if 0 +#ifdef _WIN32 + {//D + uint8_t r = rgba >> 24; + uint8_t g = rgba >> 16; + uint8_t b = rgba >> 8; + uint8_t a = rgba >> 0; + + rgba = (rgba & 0xffffff00) | a; + } +#endif +#endif + 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 @@ -829,6 +1001,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); @@ -843,17 +1018,34 @@ 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); + 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); - } + + 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_ALPHA_TEST); + glDisable (GL_BLEND); + } } MODULE = CFClient PACKAGE = CFClient::Map @@ -1374,7 +1566,12 @@ const_iv (GL_FLAT), const_iv (GL_DITHER), const_iv (GL_BLEND), + const_iv (GL_CULL_FACE), const_iv (GL_SCISSOR_TEST), + const_iv (GL_DEPTH_TEST), + const_iv (GL_ALPHA_TEST), + const_iv (GL_NORMALIZE), + const_iv (GL_RESCALE_NORMAL), const_iv (GL_AND), const_iv (GL_ONE), const_iv (GL_ZERO), @@ -1385,7 +1582,12 @@ const_iv (GL_RGB), const_iv (GL_RGBA), const_iv (GL_UNSIGNED_BYTE), + const_iv (GL_UNSIGNED_SHORT), + const_iv (GL_UNSIGNED_INT), const_iv (GL_ALPHA), + const_iv (GL_INTENSITY), + const_iv (GL_LUMINANCE), + const_iv (GL_LUMINANCE_ALPHA), const_iv (GL_FLOAT), const_iv (GL_UNSIGNED_INT_8_8_8_8_REV), const_iv (GL_COMPILE), @@ -1409,6 +1611,7 @@ const_iv (GL_MODULATE), const_iv (GL_DECAL), const_iv (GL_REPLACE), + const_iv (GL_DEPTH_BUFFER_BIT), const_iv (GL_COLOR_BUFFER_BIT), const_iv (GL_PROJECTION), const_iv (GL_MODELVIEW), @@ -1418,10 +1621,18 @@ 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_QUADS), + const_iv (GL_QUAD_STRIP), + const_iv (GL_TRIANGLES), + const_iv (GL_TRIANGLE_STRIP), + const_iv (GL_TRIANGLE_FAN), const_iv (GL_PERSPECTIVE_CORRECTION_HINT), const_iv (GL_FASTEST), + const_iv (GL_V2F), + const_iv (GL_V3F), + const_iv (GL_T2F_V3F), + const_iv (GL_T2F_N3F_V3F), # undef const_iv }; @@ -1446,6 +1657,8 @@ void glBlendFunc (int sfactor, int dfactor) +void glDepthMask (int flag) + void glLogicOp (int opcode) void glColorMask (int red, int green, int blue, int alpha) @@ -1458,7 +1671,10 @@ void glLoadIdentity () -# near and far are due to microsofts buggy c compiler +# near_ and far_ are due to microsofts buggy "c" compiler +void glFrustum (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) @@ -1484,7 +1700,22 @@ void glColor (float r, float g, float b, float a = 1.0) PROTOTYPE: @ CODE: - glColor4ub (r * 255., g * 255., b * 255., a * 255.); + // 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)); + +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); + glBitmap (0, 0, 0, 0, x, y, 0); void glVertex (float x, float y, float z = 0.) CODE: @@ -1522,9 +1753,7 @@ 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 glDrawPixels (int width, int height, int format, int type, char *pixels) void glCopyPixels (int x, int y, int width, int height, int type = GL_COLOR) @@ -1561,3 +1790,4 @@ void glCallList (int list) +