--- deliantra/Deliantra-Client/Client.xs 2006/04/13 01:55:38 1.29 +++ deliantra/Deliantra-Client/Client.xs 2006/04/13 23:07:56 1.30 @@ -9,6 +9,8 @@ #include #include +#include + #include #include #include @@ -63,45 +65,98 @@ } typedef struct { + GLint name; + int w, h; + float s, t; +} 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_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 >= self->rows) - break; + 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; - 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; - } - } + mapcell *cell = row->col + (x - row->c0); + cell->darkness = -1; + cell->face [2] = 0; + } + } } MODULE = CFClient PACKAGE = CFClient @@ -288,6 +343,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))); @@ -363,11 +419,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 +432,37 @@ 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) 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; + 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: { - 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 +478,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 +489,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 +518,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; @@ -532,7 +562,73 @@ } } else - cell->darkness = -1; + { + cell->darkness = -1; + cell->face [2] = 0; + } } } +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 (); +} +