--- deliantra/Deliantra-Client/Client.xs 2006/04/09 01:19:15 1.8 +++ deliantra/Deliantra-Client/Client.xs 2006/04/11 13:27:48 1.16 @@ -8,12 +8,38 @@ #include #include +#include #include +#include +#include +#include +#include + static PangoContext *context; static PangoFontMap *fontmap; -MODULE = Crossfire::Client PACKAGE = Crossfire::Client +typedef struct cf_layout { + PangoLayout *pl; + int base_height; +} *CFClient__Layout; + +static void +layout_get_pixel_size (CFClient__Layout self, int *w, int *h) +{ + PangoFontDescription *font = pango_context_get_font_description (context); + pango_font_description_set_absolute_size (font, self->base_height * PANGO_SCALE); + + pango_layout_get_pixel_size (self->pl, w, h); + + *w = (*w + 3) & ~3; + if (!*w) *w = 1; + if (!*h) *h = 1; +} + +MODULE = CFClient PACKAGE = CFClient + +PROTOTYPES: ENABLE BOOT: { @@ -21,6 +47,11 @@ context = pango_ft2_font_map_create_context ((PangoFT2FontMap *)fontmap); } +void +lowdelay (int fd, int val = 1) + CODE: + setsockopt (fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof (val)); + char * gl_version () CODE: @@ -38,36 +69,79 @@ void add_font (char *file) CODE: - FcConfigAppFontAddFile (0, (const FcChar8 *)file); + FcConfigAppFontAddFile (0, (const FcChar8 *)file); /* no idea wether this is required */ void -set_font (char *pango_font) - CODE: +set_font (char *file) + CODE: { - PangoFontDescription *font = pango_font_description_from_string (pango_font); + 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); } +MODULE = CFClient PACKAGE = CFClient::Layout + +CFClient::Layout +new (SV *class, int base_height = 10) + CODE: + New (0, RETVAL, 1, struct cf_layout); + RETVAL->base_height = base_height; + RETVAL->pl = pango_layout_new (context); + pango_layout_set_wrap (RETVAL->pl, PANGO_WRAP_WORD_CHAR); + OUTPUT: + RETVAL + void -font_render (SV *text_, int height = 10) - PPCODE: +DESTROY (CFClient::Layout self) + CODE: + g_object_unref (self->pl); + Safefree (self); + +void +set_markup (CFClient::Layout self, SV *text_) + CODE: { STRLEN textlen; char *text = SvPVutf8 (text_, textlen); + + pango_layout_set_markup (self->pl, text, textlen); +} + +void +set_height (CFClient::Layout self, int base_height) + CODE: + self->base_height = base_height; + +void +set_width (CFClient::Layout self, int max_width = -1) + CODE: + pango_layout_set_width (self->pl, max_width < 0 ? max_width : max_width * PANGO_SCALE); + +void +size (CFClient::Layout self) + PPCODE: +{ + int w, h; + + layout_get_pixel_size (self, &w, &h); + + EXTEND (SP, 2); + PUSHs (sv_2mortal (newSViv (w))); + PUSHs (sv_2mortal (newSViv (h))); +} + +void +render (CFClient::Layout self) + PPCODE: +{ SV *retval; int w, h; FT_Bitmap bitmap; - PangoLayout *layout; - PangoFontDescription *font = pango_context_get_font_description (context); - pango_font_description_set_absolute_size (font, height * PANGO_SCALE); - - layout = pango_layout_new (context); - pango_layout_set_markup (layout, text, textlen); - pango_layout_get_pixel_size (layout, &w, &h); - - w = (w + 3) & ~3; - if (!w) w = 1; - if (!h) h = 1; + + layout_get_pixel_size (self, &w, &h); retval = newSV (w * h); SvPOK_only (retval); @@ -82,11 +156,37 @@ memset (bitmap.buffer, 0, w * h); - pango_ft2_render_layout (&bitmap, layout, 0 * PANGO_SCALE, 0 * PANGO_SCALE); - g_object_unref (layout); + pango_ft2_render_layout (&bitmap, self->pl, 0 * PANGO_SCALE, 0 * PANGO_SCALE); EXTEND (SP, 3); PUSHs (sv_2mortal (newSViv (w))); PUSHs (sv_2mortal (newSViv (h))); PUSHs (sv_2mortal (retval)); } + +MODULE = CFClient PACKAGE = CFClient::Texture + +void +draw_quad (SV *self, double x, double y, double w = 0, double h = 0) + PROTOTYPE: $$$;$$ + CODE: +{ + HV *hv = (HV *)SvRV (self); + double s = SvNV (*hv_fetch (hv, "s", 1, 1)); + double t = SvNV (*hv_fetch (hv, "t", 1, 1)); + int name = SvIV (*hv_fetch (hv, "name", 4, 1)); + + if (items < 5) + { + w = SvNV (*hv_fetch (hv, "width", 5, 1)); + h = SvNV (*hv_fetch (hv, "height", 6, 1)); + } + + glBindTexture (GL_TEXTURE_2D, name); + glBegin (GL_QUADS); + glTexCoord2d (0, 0); glVertex2d (x , y ); + glTexCoord2d (0, t); glVertex2d (x , y + h); + glTexCoord2d (s, t); glVertex2d (x + w, y + h); + glTexCoord2d (s, 0); glVertex2d (x + w, y ); + glEnd (); +}