--- deliantra/Deliantra-Client/Client.xs 2006/04/11 13:27:48 1.16 +++ deliantra/Deliantra-Client/Client.xs 2006/04/13 01:55:38 1.29 @@ -3,8 +3,10 @@ #include "XSUB.h" #include +#include #include +#include #include #include @@ -16,6 +18,13 @@ #include #include +#include + +#define FOW_DARKNESS 32 + +#define MAP_EXTEND_X 32 +#define MAP_EXTEND_Y 512 + static PangoContext *context; static PangoFontMap *fontmap; @@ -25,10 +34,26 @@ } *CFClient__Layout; static void -layout_get_pixel_size (CFClient__Layout self, int *w, int *h) +substitute_func (FcPattern *pattern, gpointer data) +{ + FcPatternAddBool (pattern, FC_HINTING , 1); + FcPatternAddBool (pattern, FC_AUTOHINT, 1); +} + +static void +layout_update (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); - pango_font_description_set_absolute_size (font, self->base_height * PANGO_SCALE); + pango_font_description_set_absolute_size (font, self->base_height * (PANGO_SCALE * 8 / 10)); +} + +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); @@ -37,6 +62,48 @@ if (!*h) *h = 1; } +typedef struct { + int16_t darkness; + uint16_t face[3]; +} mapcell; + +typedef struct { + uint32_t c0, c1; + mapcell *col; +} maprow; + +typedef struct map { + int x, y, w, h; + int faces; + GLint *face; + + uint32_t rows; + maprow *row; +} *CFClient__Map; + +static void +map_blank (CFClient__Map self, int x0, int y0, int w, int h) +{ + int x, y; + + for (y = y0; y < y0 + h; y++) + { + if (y >= self->rows) + break; + + maprow *row = self->row + y; + + for (x = x0; x < x0 + w; x++) + if (x >= row->c0) + { + if (x >= row->c1) + break; + + row->col[x].darkness = -1; + } + } +} + MODULE = CFClient PACKAGE = CFClient PROTOTYPES: ENABLE @@ -44,6 +111,7 @@ BOOT: { 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); } @@ -82,6 +150,71 @@ pango_context_set_font_description (context, font); } +void +load_image_inline (SV *image_) + ALIAS: + load_image_file = 1 + PPCODE: +{ + STRLEN image_len; + char *image = (char *)SvPVbyte (image_, image_len); + SDL_Surface *surface, *surface2; + SDL_PixelFormat fmt; + SDL_RWops *rw = ix + ? SDL_RWFromFile (image, "r") + : SDL_RWFromConstMem (image, image_len); + + if (!rw) + croak ("load_image: unable to open file"); + + surface = IMG_Load_RW (rw, 1); + if (!surface) + croak ("load_image: unable to read file"); + + fmt.palette = NULL; + fmt.BitsPerPixel = 32; + fmt.BytesPerPixel = 4; + fmt.Rmask = 0x000000ff; + fmt.Gmask = 0x0000ff00; + fmt.Bmask = 0x00ff0000; + fmt.Amask = 0xff000000; + fmt.Rloss = 0; + fmt.Gloss = 0; + fmt.Bloss = 0; + fmt.Aloss = 0; + fmt.Rshift = 0; + fmt.Gshift = 8; + fmt.Bshift = 16; + fmt.Ashift = 24; + fmt.colorkey = 0; + fmt.alpha = 0; + + surface2 = SDL_ConvertSurface (surface, &fmt, SDL_SWSURFACE); + + EXTEND (SP, 5); + PUSHs (sv_2mortal (newSViv (surface2->w))); + PUSHs (sv_2mortal (newSViv (surface2->h))); + SDL_LockSurface (surface2); + PUSHs (sv_2mortal (newSVpvn (surface2->pixels, surface2->h * surface2->pitch))); + SDL_UnlockSurface (surface2); + PUSHs (sv_2mortal (newSViv (surface->flags & (SDL_SRCCOLORKEY | SDL_SRCALPHA) ? GL_RGBA : GL_RGB))); + PUSHs (sv_2mortal (newSViv (GL_RGBA))); + PUSHs (sv_2mortal (newSViv (GL_UNSIGNED_INT_8_8_8_8_REV))); + + SDL_FreeSurface (surface); + SDL_FreeSurface (surface2); +} + +void +fatal (char *message) + CODE: +#ifdef WIN32 + MessageBox (0, message, "Crossfire+ Fatal Error", MB_OK | MB_ICONERROR | MB_SETFOREGROUND); +#else + fprintf (stderr, "%s\n", message); +#endif + exit (1); + MODULE = CFClient PACKAGE = CFClient::Layout CFClient::Layout @@ -126,6 +259,7 @@ { int w, h; + layout_update (self); layout_get_pixel_size (self, &w, &h); EXTEND (SP, 2); @@ -133,6 +267,33 @@ PUSHs (sv_2mortal (newSViv (h))); } +int +xy_to_index (CFClient::Layout self, int x, int y) + 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: + RETVAL + +void +cursor_pos (CFClient::Layout self, int index) + PPCODE: +{ + PangoRectangle strong_pos; + layout_update (self); + pango_layout_get_cursor_pos (self->pl, index, &strong_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))); +} + void render (CFClient::Layout self) PPCODE: @@ -141,6 +302,7 @@ int w, h; FT_Bitmap bitmap; + layout_update (self); layout_get_pixel_size (self, &w, &h); retval = newSV (w * h); @@ -178,8 +340,8 @@ if (items < 5) { - w = SvNV (*hv_fetch (hv, "width", 5, 1)); - h = SvNV (*hv_fetch (hv, "height", 6, 1)); + w = SvNV (*hv_fetch (hv, "w", 1, 1)); + h = SvNV (*hv_fetch (hv, "h", 1, 1)); } glBindTexture (GL_TEXTURE_2D, name); @@ -190,3 +352,187 @@ glTexCoord2d (s, 0); glVertex2d (x + w, y ); glEnd (); } + +MODULE = CFClient PACKAGE = CFClient::Map + +CFClient::Map +new (SV *class, int map_width, int map_height) + CODE: + New (0, RETVAL, 1, struct map); + RETVAL->x = 0; + RETVAL->y = 0; + RETVAL->w = map_width; + RETVAL->h = map_height; + RETVAL->faces = 1; + Newz (0, RETVAL->face, 1, GLint); + RETVAL->rows = 0; + RETVAL->row = 0; + printf ("new map %d,%d\n", map_width, map_height);//D + OUTPUT: + RETVAL + +void +DESTROY (CFClient::Map self) + CODE: +{ + int r, c; + + printf ("detroy map\n");//D + Safefree (self->face); + + for (r = 0; r < self->rows; r++) + Safefree (self->row[r].col); + + Safefree (self->row); + Safefree (self); +} + +void +set_face (CFClient::Map self, int facenum, int face) + CODE: +{ + while (self->faces < facenum) + { + printf ("setface %d (%d) = %d\n",facenum, self->faces, face);//D + Renew (self->face, self->faces * 2, GLint); + Zero (self->face + self->faces, self->faces, GLint); + self->faces *= 2; + } + + self->face [facenum] = face; +} + +void +scroll (CFClient::Map self, int dx, int dy) + CODE: +{ + printf ("map_scroll %d,%d\n", dx, dy);//D + if (dx > 0) + map_blank (self, self->x, self->y, dx - 1, self->h); + else if (dx < 0) + map_blank (self, self->x + self->w + dx + 1, self->y, 1 - dx, self->h); + + if (dy > 0) + map_blank (self, self->x, self->y, self->w, dy - 1); + else if (dy < 0) + map_blank (self, self->x, self->y + self->h + dy + 1, self->w, 1 - dy); + + self->x += dx; + self->y += dy; + + while (self->y < 0) + { + maprow *row; + + New (0, row, self->rows + MAP_EXTEND_Y, maprow); + Move (self->row, row + MAP_EXTEND_Y, self->rows, maprow); + Zero (row, MAP_EXTEND_Y, maprow); + + self->rows += MAP_EXTEND_Y; + self->y += MAP_EXTEND_Y; + self->row = row; + } + + if (self->x < 0) + { + int y; + for (y = 0; y < self->rows; y++) + { + self->row[y].c0 += self->x; + self->row[y].c1 += self->x; + } + + self->x = 0; + } +} + +void +map1a_update (CFClient::Map self, SV *data_) + CODE: +{ + uint8_t *data = (uint8_t *)SvPVbytes_nolen (data_); + uint8_t *data_end = (uint8_t *)SvEND (data_) + 1; + + while (data < data_end) + { + int flags = (data [0] << 8) + data [1]; data += 2; + int x = ((flags >> 10) & 63) + self->x; + int y = ((flags >> 4) & 63) + self->y; + + while (y >= self->rows) + { + Renew (self->row, self->rows + MAP_EXTEND_Y, maprow); + Zero (self->row + self->rows, MAP_EXTEND_Y, maprow); + + self->rows += MAP_EXTEND_Y; + } + + assert (y < self->rows);//D + + maprow *row = self->row + y; + + if (!row->col) + { + Newz (0, row->col, MAP_EXTEND_X, mapcell); + row->c0 = self->x - MAP_EXTEND_X / 4; + row->c1 = row->c0 + MAP_EXTEND_X; + } + + if (row->c0 > x) + { + int extend = row->c0 - x + MAP_EXTEND_X; + mapcell *col; + + New (0, col, row->c1 - row->c0 + extend, mapcell); + Move (row->col, col + extend, row->c1 - row->c0, mapcell); + Zero (col, extend, mapcell); + + row->col = col; + row->c0 -= extend; + } + else if (x >= row->c1) + { + int extend = x - row->c1 + MAP_EXTEND_X; + + Renew (row->col, row->c1 - row->c0 + extend, mapcell); + Zero (row->col + row->c1 - row->c0, extend, mapcell); + + row->c1 += extend; + } + + assert (x >= row->c0);//D + assert (x < row->c1);//D + mapcell *cell = row->col + (x - row->c0); + + if (flags & 15) + { + if (cell->darkness < 0) + { + cell->darkness = 0; + cell->face [0] = 0; + cell->face [1] = 0; + cell->face [2] = 0; + } + + cell->darkness = flags & 8 ? *data++ : 255; + + if (flags & 4) + { + cell->face [0] = (data [0] << 8) + data [1]; data += 2; + } + + if (flags & 2) + { + cell->face [1] = (data [0] << 8) + data [1]; data += 2; + } + + if (flags & 1) + { + cell->face [2] = (data [0] << 8) + data [1]; data += 2; + } + } + else + cell->darkness = -1; + } +} +