--- deliantra/Deliantra-Client/Client.xs 2006/04/13 01:55:38 1.29 +++ deliantra/Deliantra-Client/Client.xs 2006/04/15 01:13:46 1.40 @@ -9,6 +9,8 @@ #include #include +#include + #include #include #include @@ -37,7 +39,7 @@ substitute_func (FcPattern *pattern, gpointer data) { FcPatternAddBool (pattern, FC_HINTING , 1); - FcPatternAddBool (pattern, FC_AUTOHINT, 1); + FcPatternAddBool (pattern, FC_AUTOHINT, 0); } static void @@ -63,45 +65,97 @@ } typedef struct { + GLint name; + int w, h; + float s, t; + uint8_t r, g, b, a; +} mapface; + +typedef struct { int16_t darkness; uint16_t face[3]; } mapcell; typedef struct { - uint32_t c0, c1; + int32_t c0, c1; mapcell *col; } maprow; typedef struct map { int x, y, w, h; + int vx, vy; int faces; - GLint *face; + 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_blank (CFClient__Map self, int x0, int y0, int w, int h) +map_clear (CFClient__Map self) { - int x, y; + int r; - for (y = y0; y < y0 + h; y++) - { - if (y >= self->rows) - break; + for (r = 0; r < self->rows; r++) + Safefree (self->row[r].col); - maprow *row = self->row + y; + Safefree (self->row); - for (x = x0; x < x0 + w; x++) - if (x >= row->c0) - { - if (x >= row->c1) - break; + self->x = 0; + self->y = 0; + self->vx = 0; + self->vy = 0; + self->row = 0; + self->rows = 0; +} - row->col[x].darkness = -1; - } - } +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 @@ -191,6 +245,8 @@ surface2 = SDL_ConvertSurface (surface, &fmt, SDL_SWSURFACE); + assert (surface2->pitch == surface2->w * 4); + EXTEND (SP, 5); PUSHs (sv_2mortal (newSViv (surface2->w))); PUSHs (sv_2mortal (newSViv (surface2->h))); @@ -206,12 +262,37 @@ } void +average (int x, int y, uint32_t *data) + PPCODE: +{ + uint32_t r = 0, g = 0, b = 0, a = 0; + + x = y = x * y; + + while (x--) + { + uint32_t p = *data++; + + r += (p ) & 255; + g += (p >> 8) & 255; + b += (p >> 16) & 255; + a += (p >> 24) & 255; + } + + EXTEND (SP, 4); + PUSHs (sv_2mortal (newSViv (r / y))); + PUSHs (sv_2mortal (newSViv (g / y))); + PUSHs (sv_2mortal (newSViv (b / y))); + PUSHs (sv_2mortal (newSViv (a / y))); +} + +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); + fprintf (stderr, "FATAL: %s\n", message); #endif exit (1); @@ -234,6 +315,16 @@ Safefree (self); void +set_text (CFClient::Layout self, SV *text_) + CODE: +{ + STRLEN textlen; + char *text = SvPVutf8 (text_, textlen); + + pango_layout_set_text (self->pl, text, textlen); +} + +void set_markup (CFClient::Layout self, SV *text_) CODE: { @@ -288,6 +379,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))); @@ -329,14 +421,15 @@ MODULE = CFClient PACKAGE = CFClient::Texture void -draw_quad (SV *self, double x, double y, double w = 0, double h = 0) +draw_quad (SV *self, float x, float y, float w = 0, float 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)); + float s = SvNV (*hv_fetch (hv, "s", 1, 1)); + float t = SvNV (*hv_fetch (hv, "t", 1, 1)); int name = SvIV (*hv_fetch (hv, "name", 4, 1)); + int wrap_mode = SvIV (*hv_fetch (hv, "wrap_mode", 9, 1)); if (items < 5) { @@ -345,11 +438,15 @@ } 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); + } 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 ); + 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 (); } @@ -363,11 +460,12 @@ RETVAL->y = 0; RETVAL->w = map_width; RETVAL->h = map_height; - RETVAL->faces = 1; - Newz (0, RETVAL->face, 1, GLint); + RETVAL->vx = 0; + RETVAL->vy = 0; + RETVAL->faces = 8192; + Newz (0, RETVAL->face, RETVAL->faces, mapface); RETVAL->rows = 0; RETVAL->row = 0; - printf ("new map %d,%d\n", map_width, map_height);//D OUTPUT: RETVAL @@ -375,38 +473,43 @@ DESTROY (CFClient::Map self) CODE: { - int r, c; - - printf ("detroy map\n");//D + map_clear (self); 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) +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, int r, int g, int b, int a) CODE: { - while (self->faces < facenum) + while (self->faces < face) { - 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); + Append (mapface, self->face, self->faces, self->faces); self->faces *= 2; } - self->face [facenum] = face; + mapface *f = self->face + face; + + f->name = name; + f->w = w; + f->h = h; + f->s = s; + f->t = t; + f->r = r; + f->g = g; + f->b = b; + f->a = a; } 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) @@ -422,27 +525,10 @@ 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); + Prepend (maprow, self->row, self->rows, MAP_EXTEND_Y); 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; } } @@ -450,25 +536,23 @@ 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; + 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) { - Renew (self->row, self->rows + MAP_EXTEND_Y, maprow); - Zero (self->row + self->rows, MAP_EXTEND_Y, maprow); + Prepend (maprow, self->row, self->rows, MAP_EXTEND_Y); self->rows += MAP_EXTEND_Y; } - assert (y < self->rows);//D - maprow *row = self->row + y; if (!row->col) @@ -481,32 +565,25 @@ 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; + 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; - - Renew (row->col, row->c1 - row->c0 + extend, mapcell); - Zero (row->col + row->c1 - row->c0, extend, mapcell); - + Append (mapcell, row->col, row->c1 - row->c0, extend); row->c1 += extend; } - assert (x >= row->c0);//D - assert (x < row->c1);//D + 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) + if (cell->darkness < 0 && x < self->w && y < self->h) { cell->darkness = 0; cell->face [0] = 0; @@ -536,3 +613,152 @@ } } +SV * +mapmap (CFClient::Map self, int w, int h) + CODE: +{ + int x0, x1, x; + int y0, y1, y; + int z; + SV *map_sv = newSV (w * h * sizeof (uint32_t)); + uint32_t *map = (uint32_t *)SvPVX (map_sv); + + 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; + + for (y = y0; y < y1; y++) + { + maprow *row = 0 <= y && y < self->rows + ? self->row + y + : 0; + + for (x = x0; x < x1; x++) + { + int r = 32, g = 32, b = 32, a = 192; + + if (row && row->c0 <= x && x < row->c1) + { + mapcell *cell = row->col + (x - row->c0); + + for (z = 0; z <= 0; z++) + { + uint16_t face = cell->face [z]; + + if (face) + { + mapface tex = self->face [face]; + int a0 = 255 - tex.a; + int a1 = tex.a; + + r = (r * a0 + tex.r * a1) / 255; + g = (g * a0 + tex.g * a1) / 255; + b = (b * a0 + tex.b * a1) / 255; + a = (a * a0 + tex.a * a1) / 255; + } + } + } + + *map++ = (r ) + | (g << 8) + | (b << 16) + | (a << 24); + } + } + + RETVAL = map_sv; +} + OUTPUT: + RETVAL + +void +draw (CFClient::Map self, int shift_x, int shift_y, int x0, int y0, int sw, int sh) + PPCODE: +{ + int sw4 = (sw + 3) & ~3; + SV *darkness_sv = sv_2mortal (newSV (sw4 * sh)); + uint8_t *darkness = (uint8_t *)SvPVX (darkness_sv); + + SvPOK_only (darkness_sv); + SvCUR_set (darkness_sv, sw4 * sh); + + int vx = self->x + (self->w - sw) / 2 - shift_x; + int vy = self->y + (self->h - sh) / 2 - shift_y; + + if (0) + { + 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); + + darkness[y * sw4 + x] = cell->darkness < 0 + ? 255 - FOW_DARKNESS + : 255 - cell->darkness; + + 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 (); + + glDisable (GL_TEXTURE_2D); + glDisable (GL_BLEND); + + EXTEND (SP, 3); + PUSHs (sv_2mortal (newSViv (sw4))); + PUSHs (sv_2mortal (newSViv (sh))); + PUSHs (darkness_sv); +} +