--- deliantra/Deliantra-Client/Client.xs 2006/04/12 20:42:52 1.23 +++ deliantra/Deliantra-Client/Client.xs 2006/04/13 23:11:11 1.31 @@ -3,24 +3,30 @@ #include "XSUB.h" #include +#include #include #include #include +#include + #include #include #include -#include -#include -#include - #include #include #include #include +#include + +#define FOW_DARKNESS 32 + +#define MAP_EXTEND_X 32 +#define MAP_EXTEND_Y 512 + static PangoContext *context; static PangoFontMap *fontmap; @@ -58,6 +64,99 @@ if (!*h) *h = 1; } +typedef struct { + GLint name; + int w, h; + float s, t; +} mapface; + +typedef struct { + int16_t darkness; + uint16_t face[3]; +} mapcell; + +typedef struct { + int32_t c0, c1; + mapcell *col; +} maprow; + +typedef struct map { + int x, y, w, h; + int vx, vy; + int faces; + mapface *face; + + uint32_t rows; + maprow *row; +} *CFClient__Map; + +static char * +prepend (char *ptr, int sze, int inc) +{ + char *p; + + New (0, p, sze + inc, char); + Zero (p, inc, char); + Move (ptr, p + inc, sze, char); + Safefree (ptr); + + return p; +} + +static char * +append (char *ptr, int sze, int inc) +{ + Renew (ptr, sze + inc, char); + Zero (ptr + sze, inc, char); + + return ptr; +} + +#define Append(type,ptr,sze,inc) (ptr) = (type *)append ((char *)ptr, (sze) * sizeof (type), (inc) * sizeof (type)) +#define Prepend(type,ptr,sze,inc) (ptr) = (type *)prepend ((char *)ptr, (sze) * sizeof (type), (inc) * sizeof (type)) + +static void +map_clear (CFClient__Map self) +{ + int r; + + for (r = 0; r < self->rows; r++) + Safefree (self->row[r].col); + + Safefree (self->row); + + self->x = 0; + self->y = 0; + self->vx = 0; + self->vy = 0; + self->row = 0; + self->rows = 0; +} + +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 >= 0) + { + 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 - row->c0].darkness = -1; + } + } +} + MODULE = CFClient PACKAGE = CFClient PROTOTYPES: ENABLE @@ -151,7 +250,7 @@ SDL_LockSurface (surface2); PUSHs (sv_2mortal (newSVpvn (surface2->pixels, surface2->h * surface2->pitch))); SDL_UnlockSurface (surface2); - PUSHs (sv_2mortal (newSViv (surface->format->Amask ? GL_RGBA : GL_RGB))); + 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))); @@ -159,6 +258,16 @@ 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 @@ -232,6 +341,7 @@ 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))); @@ -296,3 +406,224 @@ 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->vx = 0; + RETVAL->vy = 0; + RETVAL->faces = 8192; + Newz (0, RETVAL->face, RETVAL->faces, mapface); + RETVAL->rows = 0; + RETVAL->row = 0; + OUTPUT: + RETVAL + +void +DESTROY (CFClient::Map self) + CODE: +{ + map_clear (self); + Safefree (self->face); + Safefree (self); +} + +void +clear (CFClient::Map self) + CODE: + map_clear (self); + +void +set_texture (CFClient::Map self, int face, int name, int w, int h, float s, float t) + CODE: +{ + while (self->faces < face) + { + Append (mapface, self->face, self->faces, self->faces); + self->faces *= 2; + } + + self->face [face].name = name; + self->face [face].w = w; + self->face [face].h = h; + self->face [face].s = s; + self->face [face].t = t; +} + +void +scroll (CFClient::Map self, int dx, int dy) + CODE: +{ + 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) + { + Prepend (maprow, self->row, self->rows, MAP_EXTEND_Y); + + self->rows += MAP_EXTEND_Y; + self->y += MAP_EXTEND_Y; + } +} + +void +map1a_update (CFClient::Map self, SV *data_) + CODE: +{ + uint8_t *data = (uint8_t *)SvPVbyte_nolen (data_); + uint8_t *data_end = (uint8_t *)SvEND (data_); + + 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) + { + Prepend (maprow, self->row, self->rows, MAP_EXTEND_Y); + + self->rows += MAP_EXTEND_Y; + } + + 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; + Prepend (mapcell, row->col, row->c1 - row->c0, extend); + row->c0 -= extend; + } + else if (x >= row->c1) + { + int extend = x - row->c1 + MAP_EXTEND_X; + Append (mapcell, row->col, row->c1 - row->c0, extend); + row->c1 += extend; + } + + assert (y >= 0); + assert (y < self->rows); + assert (x >= row->c0); + assert (x < row->c1); + mapcell *cell = row->col + (x - row->c0); + + if (flags & 15) + { + if (cell->darkness < 0 && x < self->w && y < self->h) + { + 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; + } +} + +void +draw (CFClient::Map self, int x0, int y0, int sw, int sh) + CODE: +{ + int vx = self->vx = self->w >= sw + ? self->x + (self->w - sw) / 2 + : MIN (self->x, MAX (self->x + self->w - sw + 1, self->vx)); + + int vy = self->vy = self->h >= sh + ? self->y + (self->h - sh) / 2 + : MIN (self->y, MAX (self->y + self->h - sh + 1, self->vy)); + + glColor4ub (255, 255, 255, 255); + + glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + glEnable (GL_BLEND); + glEnable (GL_TEXTURE_2D); + glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); + + int x, y, z; + + int last_name = 0; + + glBegin (GL_QUADS); + + for (z = 0; z < 3; z++) + 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); + uint16_t face = cell->face [z]; + + if (face) + { + mapface tex = self->face [face]; + + int px = (x + 1) * 32 - tex.w; + int py = (y + 1) * 32 - tex.h; + + if (last_name != tex.name) + { + glEnd (); + last_name = tex.name; + glBindTexture (GL_TEXTURE_2D, last_name); + glBegin (GL_QUADS); + } + + glTexCoord2f (0 , 0 ); glVertex2f (px , py ); + glTexCoord2f (0 , tex.t); glVertex2f (px , py + tex.h); + glTexCoord2f (tex.s, tex.t); glVertex2f (px + tex.w, py + tex.h); + glTexCoord2f (tex.s, 0 ); glVertex2f (px + tex.w, py ); + } + } + } + + glEnd (); +} +