ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/Client.xs
(Generate patch)

Comparing deliantra/Deliantra-Client/Client.xs (file contents):
Revision 1.220 by root, Thu Aug 9 11:02:08 2007 UTC vs.
Revision 1.227 by root, Mon Aug 13 14:15:09 2007 UTC

17#include "perl.h" 17#include "perl.h"
18#include "XSUB.h" 18#include "XSUB.h"
19 19
20#ifdef _WIN32 20#ifdef _WIN32
21# undef pipe 21# undef pipe
22// microsoft vs. C
23# define sqrtf(x) sqrt(x)
24# define roundf(x) (int)(x)
25# define atan2f(x,y) atan2(x,y)
26# define M_PI 3.14159265f
22#endif 27#endif
23 28
24#include <assert.h> 29#include <assert.h>
25#include <math.h> 30#include <math.h>
26#include <string.h> 31#include <string.h>
109 av_push (texture_av, (SV *)(size_t)name); 114 av_push (texture_av, (SV *)(size_t)name);
110 glDeleteTextures (1, &name); 115 glDeleteTextures (1, &name);
111} 116}
112 117
113#include "texcache.c" 118#include "texcache.c"
119#include "rendercache.c"
114 120
115#include "pango-font.c" 121#include "pango-font.c"
116#include "pango-fontmap.c" 122#include "pango-fontmap.c"
117#include "pango-render.c" 123#include "pango-render.c"
118 124
144typedef struct cf_layout { 150typedef struct cf_layout {
145 PangoLayout *pl; 151 PangoLayout *pl;
146 float r, g, b, a; // default color for rgba mode 152 float r, g, b, a; // default color for rgba mode
147 int base_height; 153 int base_height;
148 CFPlus__Font font; 154 CFPlus__Font font;
155 rc_t *rc;
149} *CFPlus__Layout; 156} *CFPlus__Layout;
150 157
151static CFPlus__Font default_font; 158static CFPlus__Font default_font;
152static PangoContext *opengl_context; 159static PangoContext *opengl_context;
153static PangoFontMap *opengl_fontmap; 160static PangoFontMap *opengl_fontmap;
1030 RETVAL->g = 1.; 1037 RETVAL->g = 1.;
1031 RETVAL->b = 1.; 1038 RETVAL->b = 1.;
1032 RETVAL->a = 1.; 1039 RETVAL->a = 1.;
1033 RETVAL->base_height = MIN_FONT_HEIGHT; 1040 RETVAL->base_height = MIN_FONT_HEIGHT;
1034 RETVAL->font = 0; 1041 RETVAL->font = 0;
1042 RETVAL->rc = rc_alloc ();
1035 1043
1036 pango_layout_set_wrap (RETVAL->pl, PANGO_WRAP_WORD_CHAR); 1044 pango_layout_set_wrap (RETVAL->pl, PANGO_WRAP_WORD_CHAR);
1037 layout_update_font (RETVAL); 1045 layout_update_font (RETVAL);
1038 OUTPUT: 1046 OUTPUT:
1039 RETVAL 1047 RETVAL
1040 1048
1041void 1049void
1042DESTROY (CFPlus::Layout self) 1050DESTROY (CFPlus::Layout self)
1043 CODE: 1051 CODE:
1044 g_object_unref (self->pl); 1052 g_object_unref (self->pl);
1053 rc_free (self->rc);
1045 Safefree (self); 1054 Safefree (self);
1046 1055
1047void 1056void
1048set_text (CFPlus::Layout self, SV *text_) 1057set_text (CFPlus::Layout self, SV *text_)
1049 CODE: 1058 CODE:
1301 } 1310 }
1302} 1311}
1303 1312
1304void 1313void
1305render (CFPlus::Layout self, float x, float y, int flags = 0) 1314render (CFPlus::Layout self, float x, float y, int flags = 0)
1306 PPCODE: 1315 CODE:
1316 rc_clear (self->rc);
1307 pango_opengl_render_layout_subpixel ( 1317 pango_opengl_render_layout_subpixel (
1308 self->pl, 1318 self->pl,
1319 self->rc,
1309 x * PANGO_SCALE, y * PANGO_SCALE, 1320 x * PANGO_SCALE, y * PANGO_SCALE,
1310 self->r, self->g, self->b, self->a, 1321 self->r, self->g, self->b, self->a,
1311 flags 1322 flags
1312 ); 1323 );
1324 // we assume that context_change actually clears/frees stuff
1325 // and does not do any recomputation...
1326 pango_layout_context_changed (self->pl);
1327
1328void
1329draw (CFPlus::Layout self)
1330 CODE:
1331{
1332 glEnable (GL_TEXTURE_2D);
1333 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
1334 glEnable (GL_BLEND);
1335 gl_BlendFuncSeparate (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA,
1336 GL_ONE , GL_ONE_MINUS_SRC_ALPHA);
1337 glEnable (GL_ALPHA_TEST);
1338 glAlphaFunc (GL_GREATER, 7.f / 255.f);
1339
1340 rc_draw (self->rc);
1341
1342 glDisable (GL_ALPHA_TEST);
1343 glDisable (GL_BLEND);
1344 glDisable (GL_TEXTURE_2D);
1345}
1313 1346
1314MODULE = CFPlus PACKAGE = CFPlus::Texture 1347MODULE = CFPlus PACKAGE = CFPlus::Texture
1315 1348
1316PROTOTYPES: ENABLE 1349PROTOTYPES: ENABLE
1317 1350
1550 self->rows += MAP_EXTEND_Y; 1583 self->rows += MAP_EXTEND_Y;
1551 self->y += MAP_EXTEND_Y; 1584 self->y += MAP_EXTEND_Y;
1552 } 1585 }
1553} 1586}
1554 1587
1555void 1588SV *
1556map1a_update (CFPlus::Map self, SV *data_, int extmap) 1589map1a_update (CFPlus::Map self, SV *data_, int extmap)
1557 CODE: 1590 CODE:
1558{ 1591{
1559 uint8_t *data = (uint8_t *)SvPVbyte_nolen (data_); 1592 uint8_t *data = (uint8_t *)SvPVbyte_nolen (data_);
1560 uint8_t *data_end = (uint8_t *)SvEND (data_); 1593 uint8_t *data_end = (uint8_t *)SvEND (data_);
1561 mapcell *cell; 1594 mapcell *cell;
1562 int x, y, flags; 1595 int x, y, z, flags;
1596 AV *missing = newAV ();
1597 RETVAL = newRV_noinc ((SV *)missing);
1563 1598
1564 while (data < data_end - 1) 1599 while (data < data_end - 1)
1565 { 1600 {
1566 flags = (data [0] << 8) + data [1]; data += 2; 1601 flags = (data [0] << 8) + data [1]; data += 2;
1567 1602
1618 } 1653 }
1619 else 1654 else
1620 cell->darkness = *data++ + 1; 1655 cell->darkness = *data++ + 1;
1621 } 1656 }
1622 1657
1658 for (z = 0; z <= 2; ++z)
1623 if (flags & 4) 1659 if (flags & (4 >> z))
1624 { 1660 {
1625 faceid face = (data [0] << 8) + data [1]; data += 2; 1661 faceid face = (data [0] << 8) + data [1]; data += 2;
1626 need_facenum (self, face); 1662 need_facenum (self, face);
1627 cell->tile [0] = self->face2tile [face]; 1663 cell->tile [z] = self->face2tile [face];
1664
1665 if (cell->tile [z])
1666 {
1667 maptex *tex = self->tex + cell->tile [z];
1668 if (!tex->name)
1669 av_push (missing, newSViv (cell->tile [z]));
1670
1671 if (tex->smoothtile)
1672 {
1673 maptex *smooth = self->tex + tex->smoothtile;
1674 if (!smooth->name)
1675 av_push (missing, newSViv (tex->smoothtile));
1676 }
1677 }
1628 } 1678 }
1629
1630 if (flags & 2)
1631 {
1632 faceid face = (data [0] << 8) + data [1]; data += 2;
1633 need_facenum (self, face);
1634 cell->tile [1] = self->face2tile [face];
1635 }
1636
1637 if (flags & 1)
1638 {
1639 faceid face = (data [0] << 8) + data [1]; data += 2;
1640 need_facenum (self, face);
1641 cell->tile [2] = self->face2tile [face];
1642 }
1643 } 1679 }
1644 else 1680 else
1645 cell->darkness = 0; 1681 cell->darkness = 0;
1646 } 1682 }
1647} 1683}
1684 OUTPUT:
1685 RETVAL
1648 1686
1649SV * 1687SV *
1650mapmap (CFPlus::Map self, int x0, int y0, int w, int h) 1688mapmap (CFPlus::Map self, int x0, int y0, int w, int h)
1651 CODE: 1689 CODE:
1652{ 1690{
1699 RETVAL = map_sv; 1737 RETVAL = map_sv;
1700} 1738}
1701 OUTPUT: 1739 OUTPUT:
1702 RETVAL 1740 RETVAL
1703 1741
1704SV * 1742void
1705draw (CFPlus::Map self, int mx, int my, int sw, int sh, int T) 1743draw (CFPlus::Map self, int mx, int my, int sw, int sh, int T)
1706 CODE: 1744 CODE:
1707{ 1745{
1746 int x, y, z;
1747
1708 HV *smooth = (HV *)sv_2mortal ((SV *)newHV ()); 1748 HV *smooth = (HV *)sv_2mortal ((SV *)newHV ());
1709 uint32_t smooth_level[256 / 32]; // one bit for every possible smooth level 1749 uint32_t smooth_level[256 / 32]; // one bit for every possible smooth level
1710 static uint8_t smooth_max[256][256]; // egad, fats and wasteful on memory (64k) 1750 static uint8_t smooth_max[256][256]; // egad, fast and wasteful on memory (64k)
1711 smooth_key skey; 1751 smooth_key skey;
1712 int x, y, z; 1752
1713 int last_name; 1753 rc_t *rc = rc_alloc ();
1714 AV *missing = newAV (); 1754 rc_key_t key;
1715 RETVAL = newRV_noinc ((SV *)missing); 1755 rc_array_t *arr;
1716 1756
1717 // thats current max. sorry. 1757 // thats current max. sorry.
1718 if (sw > 255) sw = 255; 1758 if (sw > 255) sw = 255;
1719 if (sh > 255) sh = 255; 1759 if (sh > 255) sh = 255;
1720 1760
1721 // clear key, in case of extra padding 1761 // clear key, in case of extra padding
1722 memset (&skey, 0, sizeof (skey)); 1762 memset (&skey, 0, sizeof (skey));
1723 1763
1724 glColor4ub (255, 255, 255, 255); 1764 memset (&key, 0, sizeof (key));
1725 1765 key.r = 255;
1726 glEnable (GL_BLEND); 1766 key.g = 255;
1727 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 1767 key.b = 255;
1728 glEnable (GL_TEXTURE_2D); 1768 key.a = 255;
1729 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); 1769 key.mode = GL_QUADS;
1730 1770 key.format = GL_T2F_V3F;
1731 glBegin (GL_QUADS);
1732
1733 last_name = -1; 1771 key.texname = -1;
1734 1772
1735 mx += self->x; 1773 mx += self->x;
1736 my += self->y; 1774 my += self->y;
1737 1775
1738 // first pass: determine smooth_max 1776 // first pass: determine smooth_max
1754 MAX (self->tex [cell->tile [0]].smoothlevel, 1792 MAX (self->tex [cell->tile [0]].smoothlevel,
1755 MAX (self->tex [cell->tile [1]].smoothlevel, 1793 MAX (self->tex [cell->tile [1]].smoothlevel,
1756 self->tex [cell->tile [2]].smoothlevel)); 1794 self->tex [cell->tile [2]].smoothlevel));
1757 } 1795 }
1758 } 1796 }
1797
1798 glEnable (GL_BLEND);
1799 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1800 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1759 1801
1760 for (z = 0; z <= 2; z++) 1802 for (z = 0; z <= 2; z++)
1761 { 1803 {
1762 memset (smooth_level, 0, sizeof (smooth_level)); 1804 memset (smooth_level, 0, sizeof (smooth_level));
1763 1805
1775 if (tile) 1817 if (tile)
1776 { 1818 {
1777 maptex tex = self->tex [tile]; 1819 maptex tex = self->tex [tile];
1778 int px, py; 1820 int px, py;
1779 1821
1780 // suppressing texture state switches here
1781 // is only moderately effective, but worth the extra effort
1782 if (last_name != tex.name) 1822 if (key.texname != tex.name)
1783 { 1823 {
1784 if (!tex.name) 1824 if (!tex.name)
1785 {
1786 av_push (missing, newSViv (tile));
1787 tex = self->tex [2]; /* missing, replace by noface */ 1825 tex = self->tex [2]; /* missing, replace by noface */
1788 }
1789 1826
1790 glEnd (); 1827 key.texname = tex.name;
1791 glBindTexture (GL_TEXTURE_2D, last_name = tex.name); 1828 arr = rc_array (rc, &key);
1792 glBegin (GL_QUADS);
1793 } 1829 }
1794 1830
1795 px = (x + 1) * T - tex.w; 1831 px = (x + 1) * T - tex.w;
1796 py = (y + 1) * T - tex.h; 1832 py = (y + 1) * T - tex.h;
1797 1833
1798 glTexCoord2f (0 , 0 ); glVertex2f (px , py ); 1834 rc_t2f_v3f (arr, 0 , 0 , px , py , 0);
1799 glTexCoord2f (0 , tex.t); glVertex2f (px , py + tex.h); 1835 rc_t2f_v3f (arr, 0 , tex.t, px , py + tex.h, 0);
1800 glTexCoord2f (tex.s, tex.t); glVertex2f (px + tex.w, py + tex.h); 1836 rc_t2f_v3f (arr, tex.s, tex.t, px + tex.w, py + tex.h, 0);
1801 glTexCoord2f (tex.s, 0 ); glVertex2f (px + tex.w, py ); 1837 rc_t2f_v3f (arr, tex.s, 0 , px + tex.w, py , 0);
1802 1838
1803 if (cell->flags && z == 2) 1839 if (cell->flags && z == 2)
1804 { 1840 {
1841 // overlays such as the speech bubble, probably more to come
1805 if (cell->flags & 1) 1842 if (cell->flags & 1)
1806 { 1843 {
1807 maptex tex = self->tex [1]; 1844 maptex tex = self->tex [1];
1808 int px = x * T + T * 2 / 32; 1845 int px = x * T + T * 2 / 32;
1809 int py = y * T - T * 6 / 32; 1846 int py = y * T - T * 6 / 32;
1810 1847
1848 if (tex.name)
1811 glEnd (); 1849 {
1812 glBindTexture (GL_TEXTURE_2D, last_name = tex.name); 1850 if (key.texname != tex.name)
1851 {
1852 key.texname = tex.name;
1853 arr = rc_array (rc, &key);
1854 }
1855
1856 rc_t2f_v3f (arr, 0 , 0 , px , py , 0);
1857 rc_t2f_v3f (arr, 0 , tex.t, px , py + T, 0);
1858 rc_t2f_v3f (arr, tex.s, tex.t, px + T, py + T, 0);
1859 rc_t2f_v3f (arr, tex.s, 0 , px + T, py , 0);
1813 glBegin (GL_QUADS); 1860 }
1814
1815 glTexCoord2f (0 , 0 ); glVertex2f (px , py );
1816 glTexCoord2f (0 , tex.t); glVertex2f (px , py + T);
1817 glTexCoord2f (tex.s, tex.t); glVertex2f (px + T, py + T);
1818 glTexCoord2f (tex.s, 0 ); glVertex2f (px + T, py );
1819 } 1861 }
1820 } 1862 }
1821 1863
1822 // update smooth hash 1864 // update smooth hash
1823 if (tex.smoothtile) 1865 if (tex.smoothtile)
1859 } 1901 }
1860 } 1902 }
1861 } 1903 }
1862 } 1904 }
1863 1905
1906 rc_draw (rc);
1907 rc_clear (rc);
1908
1864 // go through all smoothlevels, lowest to highest, then draw. 1909 // go through all smoothlevels, lowest to highest, then draw.
1865 // this is basically counting sort 1910 // this is basically counting sort
1866 { 1911 {
1867 int w, b; 1912 int w, b;
1868 1913
1914 glEnable (GL_TEXTURE_2D);
1915 glBegin (GL_QUADS);
1869 for (w = 0; w < 256 / 32; ++w) 1916 for (w = 0; w < 256 / 32; ++w)
1870 { 1917 {
1871 uint32_t smask = smooth_level [w]; 1918 uint32_t smask = smooth_level [w];
1872 if (smask) 1919 if (smask)
1873 for (b = 0; b < 32; ++b) 1920 for (b = 0; b < 32; ++b)
1892 int border = bits & 15; 1939 int border = bits & 15;
1893 int corner = (bits >> 8) & ~(bits >> 4) & 15; 1940 int corner = (bits >> 8) & ~(bits >> 4) & 15;
1894 float dx = tex.s * .0625f; // 16 images/row 1941 float dx = tex.s * .0625f; // 16 images/row
1895 float dy = tex.t * .5f ; // 2 images/column 1942 float dy = tex.t * .5f ; // 2 images/column
1896 1943
1897 // this time naively avoiding texture state changes
1898 // save gobs of state changes.
1899 if (last_name != tex.name) 1944 if (tex.name)
1900 { 1945 {
1946 // this time avoiding texture state changes
1947 // save gobs of state changes.
1901 if (!tex.name) 1948 if (key.texname != tex.name)
1902 { 1949 {
1903 av_push (missing, newSViv (skey->tile)); 1950 glEnd ();
1904 continue; // smoothing not yet available 1951 glBindTexture (GL_TEXTURE_2D, key.texname = tex.name);
1952 glBegin (GL_QUADS);
1905 } 1953 }
1906 1954
1907 glEnd ();
1908 glBindTexture (GL_TEXTURE_2D, last_name = tex.name);
1909 glBegin (GL_QUADS);
1910 }
1911
1912 if (border) 1955 if (border)
1913 { 1956 {
1914 float ox = border * dx; 1957 float ox = border * dx;
1915 1958
1916 glTexCoord2f (ox , 0.f ); glVertex2f (px , py ); 1959 glTexCoord2f (ox , 0.f ); glVertex2i (px , py );
1917 glTexCoord2f (ox , dy ); glVertex2f (px , py + T); 1960 glTexCoord2f (ox , dy ); glVertex2i (px , py + T);
1918 glTexCoord2f (ox + dx, dy ); glVertex2f (px + T, py + T); 1961 glTexCoord2f (ox + dx, dy ); glVertex2i (px + T, py + T);
1919 glTexCoord2f (ox + dx, 0.f ); glVertex2f (px + T, py ); 1962 glTexCoord2f (ox + dx, 0.f ); glVertex2i (px + T, py );
1920 } 1963 }
1921 1964
1922 if (corner) 1965 if (corner)
1923 { 1966 {
1924 float ox = corner * dx; 1967 float ox = corner * dx;
1925 1968
1926 glTexCoord2f (ox , dy ); glVertex2f (px , py ); 1969 glTexCoord2f (ox , dy ); glVertex2i (px , py );
1927 glTexCoord2f (ox , dy * 2.f); glVertex2f (px , py + T); 1970 glTexCoord2f (ox , dy * 2.f); glVertex2i (px , py + T);
1928 glTexCoord2f (ox + dx, dy * 2.f); glVertex2f (px + T, py + T); 1971 glTexCoord2f (ox + dx, dy * 2.f); glVertex2i (px + T, py + T);
1929 glTexCoord2f (ox + dx, dy ); glVertex2f (px + T, py ); 1972 glTexCoord2f (ox + dx, dy ); glVertex2i (px + T, py );
1973 }
1930 } 1974 }
1931 } 1975 }
1932 } 1976 }
1933 } 1977 }
1934 } 1978 }
1979
1980 glEnd ();
1981 glDisable (GL_TEXTURE_2D);
1982 key.texname = -1;
1935 } 1983 }
1936 1984
1937 hv_clear (smooth); 1985 hv_clear (smooth);
1938 } 1986 }
1939 1987
1940 glEnd ();
1941
1942 glDisable (GL_TEXTURE_2D);
1943 glDisable (GL_BLEND); 1988 glDisable (GL_BLEND);
1989 rc_free (rc);
1944 1990
1945 // top layer: overlays such as the health bar 1991 // top layer: overlays such as the health bar
1946 for (y = 0; y < sh; y++) 1992 for (y = 0; y < sh; y++)
1947 if (0 <= y + my && y + my < self->rows) 1993 if (0 <= y + my && y + my < self->rows)
1948 { 1994 {
1971 px + width - 2 - cell->stat_hp * (width - 4) / 255, py - 1); 2017 px + width - 2 - cell->stat_hp * (width - 4) / 255, py - 1);
1972 } 2018 }
1973 } 2019 }
1974 } 2020 }
1975} 2021}
1976 OUTPUT:
1977 RETVAL
1978 2022
1979void 2023void
1980draw_magicmap (CFPlus::Map self, int dx, int dy, int w, int h, unsigned char *data) 2024draw_magicmap (CFPlus::Map self, int dx, int dy, int w, int h, unsigned char *data)
1981 CODE: 2025 CODE:
1982{ 2026{
2199 int x, y, z; 2243 int x, y, z;
2200 int w, h; 2244 int w, h;
2201 int x1, y1; 2245 int x1, y1;
2202 2246
2203 if (*data++ != 0) 2247 if (*data++ != 0)
2204 return; /* version mismatch */ 2248 XSRETURN_EMPTY; /* version mismatch */
2205 2249
2206 w = *data++ << 8; w |= *data++; 2250 w = *data++ << 8; w |= *data++;
2207 h = *data++ << 8; h |= *data++; 2251 h = *data++ << 8; h |= *data++;
2208 2252
2209 // we need to do this 'cause we don't keep an absolute coord system for rows 2253 // we need to do this 'cause we don't keep an absolute coord system for rows

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines