… | |
… | |
155 | |
155 | |
156 | *w = rect.width; |
156 | *w = rect.width; |
157 | *h = rect.height; |
157 | *h = rect.height; |
158 | } |
158 | } |
159 | |
159 | |
|
|
160 | typedef uint16_t tileid; |
160 | typedef uint16_t mapface; |
161 | typedef uint16_t faceid; |
161 | |
162 | |
162 | typedef struct { |
163 | typedef struct { |
163 | GLint name; |
164 | int name; |
164 | int w, h; |
165 | int w, h; |
165 | float s, t; |
166 | float s, t; |
166 | uint8_t r, g, b, a; |
167 | uint8_t r, g, b, a; |
167 | } maptex; |
168 | } maptex; |
168 | |
169 | |
169 | typedef struct { |
170 | typedef struct { |
170 | uint32_t player; |
171 | uint32_t player; |
171 | mapface face[3]; |
172 | tileid tile[3]; |
172 | uint16_t darkness; |
173 | uint16_t darkness; |
173 | uint8_t stat_width, stat_hp, flags; |
174 | uint8_t stat_width, stat_hp, flags; |
174 | } mapcell; |
175 | } mapcell; |
175 | |
176 | |
176 | typedef struct { |
177 | typedef struct { |
… | |
… | |
179 | } maprow; |
180 | } maprow; |
180 | |
181 | |
181 | typedef struct map { |
182 | typedef struct map { |
182 | int x, y, w, h; |
183 | int x, y, w, h; |
183 | int ox, oy; /* offset to virtual global coordinate system */ |
184 | int ox, oy; /* offset to virtual global coordinate system */ |
184 | int faces; |
185 | int faces; tileid *face2tile; // [faceid] |
185 | mapface *face; |
186 | int texs; maptex *tex; // [tileid] |
186 | |
|
|
187 | int texs; |
|
|
188 | maptex *tex; |
|
|
189 | |
187 | |
190 | int32_t rows; |
188 | int32_t rows; |
191 | maprow *row; |
189 | maprow *row; |
192 | } *CFPlus__Map; |
190 | } *CFPlus__Map; |
193 | |
191 | |
… | |
… | |
213 | return ptr; |
211 | return ptr; |
214 | } |
212 | } |
215 | |
213 | |
216 | #define Append(type,ptr,sze,inc) (ptr) = (type *)append ((char *)ptr, (sze) * sizeof (type), (inc) * sizeof (type)) |
214 | #define Append(type,ptr,sze,inc) (ptr) = (type *)append ((char *)ptr, (sze) * sizeof (type), (inc) * sizeof (type)) |
217 | #define Prepend(type,ptr,sze,inc) (ptr) = (type *)prepend ((char *)ptr, (sze) * sizeof (type), (inc) * sizeof (type)) |
215 | #define Prepend(type,ptr,sze,inc) (ptr) = (type *)prepend ((char *)ptr, (sze) * sizeof (type), (inc) * sizeof (type)) |
|
|
216 | |
|
|
217 | static void |
|
|
218 | need_facenum (struct map *self, faceid face) |
|
|
219 | { |
|
|
220 | while (self->faces <= face) |
|
|
221 | { |
|
|
222 | Append (tileid, self->face2tile, self->faces, self->faces); |
|
|
223 | self->faces *= 2; |
|
|
224 | } |
|
|
225 | } |
|
|
226 | |
|
|
227 | static void |
|
|
228 | need_texid (struct map *self, int texid) |
|
|
229 | { |
|
|
230 | while (self->texs <= texid) |
|
|
231 | { |
|
|
232 | Append (maptex, self->tex, self->texs, self->texs); |
|
|
233 | self->texs *= 2; |
|
|
234 | } |
|
|
235 | } |
218 | |
236 | |
219 | static maprow * |
237 | static maprow * |
220 | map_get_row (CFPlus__Map self, int y) |
238 | map_get_row (CFPlus__Map self, int y) |
221 | { |
239 | { |
222 | if (0 > y) |
240 | if (0 > y) |
… | |
… | |
1240 | RETVAL->y = 0; |
1258 | RETVAL->y = 0; |
1241 | RETVAL->w = 0; |
1259 | RETVAL->w = 0; |
1242 | RETVAL->h = 0; |
1260 | RETVAL->h = 0; |
1243 | RETVAL->ox = 0; |
1261 | RETVAL->ox = 0; |
1244 | RETVAL->oy = 0; |
1262 | RETVAL->oy = 0; |
1245 | RETVAL->faces = 8192; |
1263 | RETVAL->faces = 8192; Newz (0, RETVAL->face2tile, RETVAL->faces, tileid); |
1246 | Newz (0, RETVAL->face, RETVAL->faces, mapface); |
1264 | RETVAL->texs = 8192; Newz (0, RETVAL->tex , RETVAL->texs , maptex); |
1247 | RETVAL->texs = 8192; |
|
|
1248 | Newz (0, RETVAL->tex, RETVAL->texs, maptex); |
|
|
1249 | RETVAL->rows = 0; |
1265 | RETVAL->rows = 0; |
1250 | RETVAL->row = 0; |
1266 | RETVAL->row = 0; |
1251 | OUTPUT: |
1267 | OUTPUT: |
1252 | RETVAL |
1268 | RETVAL |
1253 | |
1269 | |
1254 | void |
1270 | void |
1255 | DESTROY (CFPlus::Map self) |
1271 | DESTROY (CFPlus::Map self) |
1256 | CODE: |
1272 | CODE: |
1257 | { |
1273 | { |
1258 | map_clear (self); |
1274 | map_clear (self); |
1259 | Safefree (self->face); |
1275 | Safefree (self->face2tile); |
1260 | Safefree (self->tex); |
1276 | Safefree (self->tex); |
1261 | Safefree (self); |
1277 | Safefree (self); |
1262 | } |
1278 | } |
1263 | |
1279 | |
1264 | void |
1280 | void |
… | |
… | |
1271 | clear (CFPlus::Map self) |
1287 | clear (CFPlus::Map self) |
1272 | CODE: |
1288 | CODE: |
1273 | map_clear (self); |
1289 | map_clear (self); |
1274 | |
1290 | |
1275 | void |
1291 | void |
1276 | set_face (CFPlus::Map self, int face, int texid) |
1292 | set_tileid (CFPlus::Map self, int face, int tile) |
1277 | CODE: |
1293 | CODE: |
1278 | { |
1294 | { |
1279 | while (self->faces <= face) |
1295 | need_facenum (self, face); self->face2tile [face] = tile; |
1280 | { |
1296 | need_texid (self, tile); |
1281 | Append (mapface, self->face, self->faces, self->faces); |
|
|
1282 | self->faces *= 2; |
|
|
1283 | } |
|
|
1284 | |
|
|
1285 | self->face [face] = texid; |
|
|
1286 | } |
1297 | } |
1287 | |
1298 | |
1288 | void |
1299 | void |
1289 | set_texture (CFPlus::Map self, int texid, int name, int w, int h, float s, float t, int r, int g, int b, int a) |
1300 | set_texture (CFPlus::Map self, int texid, int name, int w, int h, float s, float t, int r, int g, int b, int a) |
1290 | CODE: |
1301 | CODE: |
1291 | { |
1302 | { |
1292 | while (self->texs <= texid) |
1303 | need_texid (self, texid); |
1293 | { |
|
|
1294 | Append (maptex, self->tex, self->texs, self->texs); |
|
|
1295 | self->texs *= 2; |
|
|
1296 | } |
|
|
1297 | |
1304 | |
1298 | { |
1305 | { |
1299 | maptex *tex = self->tex + texid; |
1306 | maptex *tex = self->tex + texid; |
1300 | |
1307 | |
1301 | tex->name = name; |
1308 | tex->name = name; |
… | |
… | |
1434 | cell->darkness = *data++ + 1; |
1441 | cell->darkness = *data++ + 1; |
1435 | } |
1442 | } |
1436 | |
1443 | |
1437 | if (flags & 4) |
1444 | if (flags & 4) |
1438 | { |
1445 | { |
1439 | cell->face [0] = self->face [(data [0] << 8) + data [1]]; data += 2; |
1446 | faceid face = (data [0] << 8) + data [1]; data += 2; |
|
|
1447 | need_facenum (self, face); |
|
|
1448 | cell->tile [0] = self->face2tile [face]; |
1440 | } |
1449 | } |
1441 | |
1450 | |
1442 | if (flags & 2) |
1451 | if (flags & 2) |
1443 | { |
1452 | { |
1444 | cell->face [1] = self->face [(data [0] << 8) + data [1]]; data += 2; |
1453 | faceid face = (data [0] << 8) + data [1]; data += 2; |
|
|
1454 | need_facenum (self, face); |
|
|
1455 | cell->tile [1] = self->face2tile [face]; |
1445 | } |
1456 | } |
1446 | |
1457 | |
1447 | if (flags & 1) |
1458 | if (flags & 1) |
1448 | { |
1459 | { |
1449 | cell->face [2] = self->face [(data [0] << 8) + data [1]]; data += 2; |
1460 | faceid face = (data [0] << 8) + data [1]; data += 2; |
|
|
1461 | need_facenum (self, face); |
|
|
1462 | cell->tile [2] = self->face2tile [face]; |
1450 | } |
1463 | } |
1451 | } |
1464 | } |
1452 | else |
1465 | else |
1453 | cell->darkness = 0; |
1466 | cell->darkness = 0; |
1454 | } |
1467 | } |
… | |
… | |
1484 | { |
1497 | { |
1485 | mapcell *cell = row->col + (x - row->c0); |
1498 | mapcell *cell = row->col + (x - row->c0); |
1486 | |
1499 | |
1487 | for (z = 0; z <= 0; z++) |
1500 | for (z = 0; z <= 0; z++) |
1488 | { |
1501 | { |
1489 | mapface face = cell->face [z]; |
|
|
1490 | |
|
|
1491 | if (face > 0 && face < self->texs) |
|
|
1492 | { |
|
|
1493 | maptex tex = self->tex [face]; |
1502 | maptex tex = self->tex [cell->tile [z]]; |
1494 | int a0 = 255 - tex.a; |
1503 | int a0 = 255 - tex.a; |
1495 | int a1 = tex.a; |
1504 | int a1 = tex.a; |
1496 | |
1505 | |
1497 | r = (r * a0 + tex.r * a1) / 255; |
1506 | r = (r * a0 + tex.r * a1) / 255; |
1498 | g = (g * a0 + tex.g * a1) / 255; |
1507 | g = (g * a0 + tex.g * a1) / 255; |
1499 | b = (b * a0 + tex.b * a1) / 255; |
1508 | b = (b * a0 + tex.b * a1) / 255; |
1500 | a = (a * a0 + tex.a * a1) / 255; |
1509 | a = (a * a0 + tex.a * a1) / 255; |
1501 | } |
|
|
1502 | } |
1510 | } |
1503 | } |
1511 | } |
1504 | |
1512 | |
1505 | *map++ = (r ) |
1513 | *map++ = (r ) |
1506 | | (g << 8) |
1514 | | (g << 8) |
… | |
… | |
1518 | draw (CFPlus::Map self, int mx, int my, int sw, int sh, int T) |
1526 | draw (CFPlus::Map self, int mx, int my, int sw, int sh, int T) |
1519 | CODE: |
1527 | CODE: |
1520 | { |
1528 | { |
1521 | int x, y, z; |
1529 | int x, y, z; |
1522 | int last_name; |
1530 | int last_name; |
1523 | mapface face; |
|
|
1524 | |
1531 | |
1525 | glColor4ub (255, 255, 255, 255); |
1532 | glColor4ub (255, 255, 255, 255); |
1526 | |
1533 | |
1527 | glEnable (GL_BLEND); |
1534 | glEnable (GL_BLEND); |
1528 | glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
1535 | glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
… | |
… | |
1544 | |
1551 | |
1545 | for (x = 0; x < sw; x++) |
1552 | for (x = 0; x < sw; x++) |
1546 | if (row->c0 <= x + mx && x + mx < row->c1) |
1553 | if (row->c0 <= x + mx && x + mx < row->c1) |
1547 | { |
1554 | { |
1548 | mapcell *cell = row->col + (x + mx - row->c0); |
1555 | mapcell *cell = row->col + (x + mx - row->c0); |
1549 | |
|
|
1550 | face = cell->face [z]; |
1556 | tileid tile = cell->tile [z]; |
1551 | |
1557 | |
1552 | if (face > 0 && face < self->texs) |
1558 | if (tile) |
1553 | { |
1559 | { |
1554 | maptex tex = self->tex [face]; |
1560 | maptex tex = self->tex [tile]; |
1555 | int px = (x + 1) * T - tex.w; |
1561 | int px = (x + 1) * T - tex.w; |
1556 | int py = (y + 1) * T - tex.h; |
1562 | int py = (y + 1) * T - tex.h; |
1557 | |
1563 | |
1558 | if (last_name != tex.name) |
1564 | if (last_name != tex.name) |
1559 | { |
1565 | { |
|
|
1566 | if (!tex.name) |
|
|
1567 | tex = self->tex [2]; /* missing, replace by noface */ |
|
|
1568 | |
1560 | glEnd (); |
1569 | glEnd (); |
1561 | glBindTexture (GL_TEXTURE_2D, last_name = tex.name); |
1570 | glBindTexture (GL_TEXTURE_2D, last_name = tex.name); |
1562 | glBegin (GL_QUADS); |
1571 | glBegin (GL_QUADS); |
1563 | } |
1572 | } |
1564 | |
1573 | |
1565 | glTexCoord2f (0 , 0 ); glVertex2f (px , py ); |
1574 | glTexCoord2f (0 , 0 ); glVertex2f (px , py ); |
1566 | glTexCoord2f (0 , tex.t); glVertex2f (px , py + tex.h); |
1575 | glTexCoord2f (0 , tex.t); glVertex2f (px , py + tex.h); |
1567 | glTexCoord2f (tex.s, tex.t); glVertex2f (px + tex.w, py + tex.h); |
1576 | glTexCoord2f (tex.s, tex.t); glVertex2f (px + tex.w, py + tex.h); |
1568 | glTexCoord2f (tex.s, 0 ); glVertex2f (px + tex.w, py ); |
1577 | glTexCoord2f (tex.s, 0 ); glVertex2f (px + tex.w, py ); |
1569 | } |
|
|
1570 | |
1578 | |
1571 | if (cell->flags && z == 2) |
1579 | if (cell->flags && z == 2) |
1572 | { |
|
|
1573 | if (cell->flags & 1) |
|
|
1574 | { |
1580 | { |
|
|
1581 | if (cell->flags & 1) |
|
|
1582 | { |
1575 | maptex tex = self->tex [1]; |
1583 | maptex tex = self->tex [1]; |
1576 | int px = (x + 1) * T - tex.w + 2; |
1584 | int px = x * T + T * 2 / 32; |
1577 | int py = (y + 1) * T - tex.h - 6; |
1585 | int py = y * T - T * 6 / 32; |
1578 | |
1586 | |
1579 | glEnd (); |
1587 | glEnd (); |
1580 | glBindTexture (GL_TEXTURE_2D, last_name = tex.name); |
1588 | glBindTexture (GL_TEXTURE_2D, last_name = tex.name); |
1581 | glBegin (GL_QUADS); |
1589 | glBegin (GL_QUADS); |
1582 | |
1590 | |
1583 | glTexCoord2f (0 , 0 ); glVertex2f (px , py ); |
1591 | glTexCoord2f (0 , 0 ); glVertex2f (px , py ); |
1584 | glTexCoord2f (0 , tex.t); glVertex2f (px , py + tex.h); |
1592 | glTexCoord2f (0 , tex.t); glVertex2f (px , py + T); |
1585 | glTexCoord2f (tex.s, tex.t); glVertex2f (px + tex.w, py + tex.h); |
1593 | glTexCoord2f (tex.s, tex.t); glVertex2f (px + T, py + T); |
1586 | glTexCoord2f (tex.s, 0 ); glVertex2f (px + tex.w, py ); |
1594 | glTexCoord2f (tex.s, 0 ); glVertex2f (px + T, py ); |
|
|
1595 | } |
1587 | } |
1596 | } |
1588 | } |
1597 | } |
1589 | } |
1598 | } |
1590 | } |
1599 | } |
1591 | |
1600 | |
… | |
… | |
1609 | int py = y * T; |
1618 | int py = y * T; |
1610 | |
1619 | |
1611 | if (cell->stat_hp) |
1620 | if (cell->stat_hp) |
1612 | { |
1621 | { |
1613 | int width = cell->stat_width * T; |
1622 | int width = cell->stat_width * T; |
1614 | int thick = sh / 28 + 1 + cell->stat_width; |
1623 | int thick = (sh * T / 32 + 27) / 28 + 1 + cell->stat_width; |
1615 | |
1624 | |
1616 | glColor3ub (0, 0, 0); |
1625 | glColor3ub (0, 0, 0); |
1617 | glRectf (px + 1, py - thick - 2, |
1626 | glRectf (px + 1, py - thick - 2, |
1618 | px + width - 1, py); |
1627 | px + width - 1, py); |
1619 | |
1628 | |
… | |
… | |
1758 | if (row && row->c0 <= x && x < row->c1) |
1767 | if (row && row->c0 <= x && x < row->c1) |
1759 | { |
1768 | { |
1760 | mapcell *cell = row->col + (x - row->c0); |
1769 | mapcell *cell = row->col + (x - row->c0); |
1761 | uint8_t flags = 0; |
1770 | uint8_t flags = 0; |
1762 | |
1771 | |
1763 | if (cell->face [0]) flags |= 1; |
1772 | if (cell->tile [0]) flags |= 1; |
1764 | if (cell->face [1]) flags |= 2; |
1773 | if (cell->tile [1]) flags |= 2; |
1765 | if (cell->face [2]) flags |= 4; |
1774 | if (cell->tile [2]) flags |= 4; |
1766 | |
1775 | |
1767 | *data++ = flags; |
1776 | *data++ = flags; |
1768 | |
1777 | |
1769 | if (flags & 1) |
1778 | if (flags & 1) |
1770 | { |
1779 | { |
|
|
1780 | tileid tile = cell->tile [0]; |
1771 | *data++ = cell->face [0] >> 8; |
1781 | *data++ = tile >> 8; |
1772 | *data++ = cell->face [0]; |
1782 | *data++ = tile; |
1773 | } |
1783 | } |
1774 | |
1784 | |
1775 | if (flags & 2) |
1785 | if (flags & 2) |
1776 | { |
1786 | { |
|
|
1787 | tileid tile = cell->tile [1]; |
1777 | *data++ = cell->face [1] >> 8; |
1788 | *data++ = tile >> 8; |
1778 | *data++ = cell->face [1]; |
1789 | *data++ = tile; |
1779 | } |
1790 | } |
1780 | |
1791 | |
1781 | if (flags & 4) |
1792 | if (flags & 4) |
1782 | { |
1793 | { |
|
|
1794 | tileid tile = cell->tile [2]; |
1783 | *data++ = cell->face [2] >> 8; |
1795 | *data++ = tile >> 8; |
1784 | *data++ = cell->face [2]; |
1796 | *data++ = tile; |
1785 | } |
1797 | } |
1786 | } |
1798 | } |
1787 | else |
1799 | else |
1788 | *data++ = 0; |
1800 | *data++ = 0; |
1789 | } |
1801 | } |
… | |
… | |
1829 | { |
1841 | { |
1830 | uint8_t flags = *data++; |
1842 | uint8_t flags = *data++; |
1831 | |
1843 | |
1832 | if (flags) |
1844 | if (flags) |
1833 | { |
1845 | { |
1834 | mapface face[3] = { 0, 0, 0 }; |
|
|
1835 | |
|
|
1836 | mapcell *cell = row_get_cell (row, x); |
1846 | mapcell *cell = row_get_cell (row, x); |
|
|
1847 | tileid tile[3] = { 0, 0, 0 }; |
1837 | |
1848 | |
1838 | if (flags & 1) { face[0] = *data++ << 8; face[0] |= *data++; } |
1849 | if (flags & 1) { tile[0] = *data++ << 8; tile[0] |= *data++; } |
1839 | if (flags & 2) { face[1] = *data++ << 8; face[1] |= *data++; } |
1850 | if (flags & 2) { tile[1] = *data++ << 8; tile[1] |= *data++; } |
1840 | if (flags & 4) { face[2] = *data++ << 8; face[2] |= *data++; } |
1851 | if (flags & 4) { tile[2] = *data++ << 8; tile[2] |= *data++; } |
1841 | |
1852 | |
1842 | if (cell->darkness == 0) |
1853 | if (cell->darkness == 0) |
1843 | { |
1854 | { |
1844 | cell->darkness = 0; |
1855 | cell->darkness = 0; |
1845 | |
1856 | |
1846 | for (z = 0; z <= 2; z++) |
1857 | for (z = 0; z <= 2; z++) |
1847 | { |
1858 | { |
1848 | cell->face[z] = face[z]; |
1859 | tileid t = tile [z]; |
1849 | |
1860 | |
1850 | if (face[z] && (face[z] >= self->texs || !self->tex[face [z]].name)) |
1861 | if (t >= self->texs || !self->tex [t].name) |
|
|
1862 | { |
1851 | XPUSHs (sv_2mortal (newSViv (face[z]))); |
1863 | XPUSHs (sv_2mortal (newSViv (t))); |
|
|
1864 | need_texid (self, t); |
|
|
1865 | } |
|
|
1866 | |
|
|
1867 | cell->tile [z] = t; |
1852 | } |
1868 | } |
1853 | } |
1869 | } |
1854 | } |
1870 | } |
1855 | } |
1871 | } |
1856 | } |
1872 | } |