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.295 by root, Mon Dec 21 03:30:22 2009 UTC vs.
Revision 1.298 by root, Tue Dec 22 00:46:05 2009 UTC

41#include <SDL_opengl.h> 41#include <SDL_opengl.h>
42 42
43/* work around os x broken headers */ 43/* work around os x broken headers */
44#ifdef __MACOSX__ 44#ifdef __MACOSX__
45typedef void (APIENTRYP PFNGLBLENDFUNCSEPARATEPROC) (GLenum sfactorRGB, GLenum dfactorRGB, GLenum sfactorAlpha, GLenum dfactorAlpha); 45typedef void (APIENTRYP PFNGLBLENDFUNCSEPARATEPROC) (GLenum sfactorRGB, GLenum dfactorRGB, GLenum sfactorAlpha, GLenum dfactorAlpha);
46typedef void (APIENTRYP PFNGLACTIVETEXTUREPROC) (GLenum texture);
47typedef void (APIENTRYP PFNGLMULTITEXCOORD2FPROC) (GLenum target, GLfloat s, GLfloat t);
46#endif 48#endif
47 49
48#define PANGO_ENABLE_BACKEND 50#define PANGO_ENABLE_BACKEND
49#define G_DISABLE_CAST_CHECKS 51#define G_DISABLE_CAST_CHECKS
50 52
80#define expect_false(expr) expect ((expr) != 0, 0) 82#define expect_false(expr) expect ((expr) != 0, 0)
81#define expect_true(expr) expect ((expr) != 0, 1) 83#define expect_true(expr) expect ((expr) != 0, 1)
82 84
83#define OBJ_STR "\xef\xbf\xbc" /* U+FFFC, object replacement character */ 85#define OBJ_STR "\xef\xbf\xbc" /* U+FFFC, object replacement character */
84 86
87/* this is used as fow flag as well, so has to have a different value */
88/* then anything that is computed by incoming darkness */
85#define FOW_DARKNESS 64 89#define FOW_DARKNESS 50
86#define DARKNESS_ADJUST(n) ((29 * (int)(n)) >> 5) /* times 0.9 */ 90#define DARKNESS_ADJUST(n) (n)
87 91
88#define MAP_EXTEND_X 32 92#define MAP_EXTEND_X 32
89#define MAP_EXTEND_Y 512 93#define MAP_EXTEND_Y 512
90 94
91#define MIN_FONT_HEIGHT 10 95#define MIN_FONT_HEIGHT 10
95 99
96#define KMOD_LRAM 0x10000 // our extension 100#define KMOD_LRAM 0x10000 // our extension
97 101
98#define TEXID_SPEECH 1 102#define TEXID_SPEECH 1
99#define TEXID_NOFACE 2 103#define TEXID_NOFACE 2
100#define TEXID_HIDDEN 3
101 104
102static AV *texture_av; 105static AV *texture_av;
103 106
104static struct 107static struct
105{ 108{
1665 glDisable (GL_BLEND); 1668 glDisable (GL_BLEND);
1666 } 1669 }
1667} 1670}
1668 1671
1669void 1672void
1670draw_fow_texture (float intensity, int name1, float s1, float t1, float w1, float h1, float blend = 0.f, float dx = 0.f, float dy = 0.f, int name2 = 0, float s2 = 0.f, float t2 = 0.f, float w2 = 0.f, float h2 = 0.f) 1673draw_fow_texture (float intensity, int hidden_tex, int name1, uint8_t *data1, float s, float t, int w, int h, float blend = 0.f, int dx = 0, int dy = 0, int name2 = 0, uint8_t *data2 = data1)
1671 PROTOTYPE: @ 1674 PROTOTYPE: @
1672 CODE: 1675 CODE:
1673{ 1676{
1674 glEnable (GL_TEXTURE_2D); 1677 glEnable (GL_TEXTURE_2D);
1675 glEnable (GL_BLEND); 1678 glEnable (GL_BLEND);
1680 glPushMatrix (); 1683 glPushMatrix ();
1681 glScalef (1./3, 1./3, 1.); 1684 glScalef (1./3, 1./3, 1.);
1682 1685
1683 if (blend > 0.f) 1686 if (blend > 0.f)
1684 { 1687 {
1685 float S2, T2; /* 0. 0. for texture 2 */ 1688 float dx3 = dx * -3.f / w;
1686 float w = w1 > w2 ? w1 : w2; 1689 float dy3 = dy * -3.f / h;
1687 float h = h1 > h2 ? h1 : h2;
1688 GLfloat env_color[4] = { 0., 0., 0., blend }; 1690 GLfloat env_color[4] = { 0., 0., 0., blend };
1689 1691
1690 /* interpolate the two shadow textures */ 1692 /* interpolate the two shadow textures */
1691 /* stage 0 == rgb(glcolor) + alpha(t0) */ 1693 /* stage 0 == rgb(glcolor) + alpha(t0) */
1692 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); 1694 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1713 glTexEnvi (GL_TEXTURE_ENV, GL_OPERAND1_ALPHA, GL_SRC_ALPHA); 1715 glTexEnvi (GL_TEXTURE_ENV, GL_OPERAND1_ALPHA, GL_SRC_ALPHA);
1714 1716
1715 glTexEnvi (GL_TEXTURE_ENV, GL_SOURCE2_ALPHA, GL_CONSTANT); 1717 glTexEnvi (GL_TEXTURE_ENV, GL_SOURCE2_ALPHA, GL_CONSTANT);
1716 glTexEnvi (GL_TEXTURE_ENV, GL_OPERAND2_ALPHA, GL_SRC_ALPHA); 1718 glTexEnvi (GL_TEXTURE_ENV, GL_OPERAND2_ALPHA, GL_SRC_ALPHA);
1717 1719
1718 s1 *= w / w1;
1719 t1 *= h / h1;
1720
1721 dx *= -3.f / w2;
1722 dy *= -3.f / h2;
1723 dx *= w / w2;
1724 dy *= h / h2;
1725
1726 s2 *= w / w2;
1727 t2 *= h / h2;
1728
1729 glBegin (GL_QUADS); 1720 glBegin (GL_QUADS);
1730 gl.MultiTexCoord2f (GL_TEXTURE0, 0, 0); gl.MultiTexCoord2f (GL_TEXTURE1, dx , dy ); glVertex2f ( 0, 0); 1721 gl.MultiTexCoord2f (GL_TEXTURE0, 0, 0); gl.MultiTexCoord2f (GL_TEXTURE1, dx3 , dy3 ); glVertex2i (0, 0);
1731 gl.MultiTexCoord2f (GL_TEXTURE0, 0, t1); gl.MultiTexCoord2f (GL_TEXTURE1, dx , dy + t2); glVertex2f ( 0, h1); 1722 gl.MultiTexCoord2f (GL_TEXTURE0, 0, t); gl.MultiTexCoord2f (GL_TEXTURE1, dx3 , dy3 + t); glVertex2i (0, h);
1732 gl.MultiTexCoord2f (GL_TEXTURE0, s1, t1); gl.MultiTexCoord2f (GL_TEXTURE1, dx + s2, dy + t2); glVertex2f (w1, h1); 1723 gl.MultiTexCoord2f (GL_TEXTURE0, s, t); gl.MultiTexCoord2f (GL_TEXTURE1, dx3 + s, dy3 + t); glVertex2i (w, h);
1733 gl.MultiTexCoord2f (GL_TEXTURE0, s1, 0); gl.MultiTexCoord2f (GL_TEXTURE1, dx + s2, dy ); glVertex2f (w1, 0); 1724 gl.MultiTexCoord2f (GL_TEXTURE0, s, 0); gl.MultiTexCoord2f (GL_TEXTURE1, dx3 + s, dy3 ); glVertex2i (w, 0);
1734 glEnd (); 1725 glEnd ();
1735 1726
1736 glDisable (GL_TEXTURE_2D); 1727 glDisable (GL_TEXTURE_2D);
1737 gl.ActiveTexture (GL_TEXTURE0); 1728 gl.ActiveTexture (GL_TEXTURE0);
1738 } 1729 }
1740 { 1731 {
1741 /* simple blending of one texture, also opengl <1.3 path */ 1732 /* simple blending of one texture, also opengl <1.3 path */
1742 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); 1733 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1743 1734
1744 glBegin (GL_QUADS); 1735 glBegin (GL_QUADS);
1745 glTexCoord2f ( 0, 0); glVertex2f ( 0, 0); 1736 glTexCoord2f (0, 0); glVertex2f (0, 0);
1746 glTexCoord2f ( 0, t1); glVertex2f ( 0, h1); 1737 glTexCoord2f (0, t); glVertex2f (0, h);
1747 glTexCoord2f (s1, t1); glVertex2f (w1, h1); 1738 glTexCoord2f (s, t); glVertex2f (w, h);
1748 glTexCoord2f (s1, 0); glVertex2f (w1, 0); 1739 glTexCoord2f (s, 0); glVertex2f (w, 0);
1749 glEnd (); 1740 glEnd ();
1750 } 1741 }
1742
1743 /* draw ?-marks or equivalent, this is very clumsy code :/ */
1744 {
1745 int x, y;
1746 int dx3 = dx * 3;
1747 int dy3 = dy * 3;
1748
1749 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
1750 glBindTexture (GL_TEXTURE_2D, hidden_tex);
1751 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1752 glTranslatef (-1., -1., 0);
1753 glBegin (GL_QUADS);
1754
1755 for (y = 1; y < h; y += 3)
1756 {
1757 int y1 = y - dy3;
1758 int y1valid = y1 >= 0 && y1 < h;
1759
1760 for (x = 1; x < w; x += 3)
1761 {
1762 int x1 = x - dx3;
1763 uint8_t h1 = data1 [x + y * w] == DARKNESS_ADJUST (255 - FOW_DARKNESS);
1764 uint8_t h2;
1765
1766 if (y1valid && x1 >= 0 && x1 < w)
1767 h2 = data2 [x1 + y1 * w] == DARKNESS_ADJUST (255 - FOW_DARKNESS);
1768 else
1769 h2 = 1; /* out of range == invisible */
1770
1771 if (h1 || h2)
1772 {
1773 float alpha = h1 == h2 ? 1.f : h1 ? 1.f - blend : blend;
1774 glColor4f (1., 1., 1., alpha);
1775
1776 glTexCoord2f (0, 0.); glVertex2i (x , y );
1777 glTexCoord2f (0, 1.); glVertex2i (x , y + 3);
1778 glTexCoord2f (1, 1.); glVertex2i (x + 3, y + 3);
1779 glTexCoord2f (1, 0.); glVertex2i (x + 3, y );
1780 }
1781 }
1782 }
1783 }
1784
1785 glEnd ();
1751 1786
1752 glPopMatrix (); 1787 glPopMatrix ();
1753 1788
1754 glDisable (GL_TEXTURE_2D); 1789 glDisable (GL_TEXTURE_2D);
1755 glDisable (GL_BLEND); 1790 glDisable (GL_BLEND);
1976 { 2011 {
1977 ext = *data++; 2012 ext = *data++;
1978 cmd = ext & 0x7f; 2013 cmd = ext & 0x7f;
1979 2014
1980 if (cmd < 4) 2015 if (cmd < 4)
1981 cell->darkness = 255 - ext * 64 + 1; 2016 cell->darkness = 255 - ext * 64 + 1; /* make sure this doesn't collide with FOW_DARKNESS */
1982 else if (cmd == 5) // health 2017 else if (cmd == 5) // health
1983 { 2018 {
1984 cell->stat_width = 1; 2019 cell->stat_width = 1;
1985 cell->stat_hp = *data++; 2020 cell->stat_hp = *data++;
1986 } 2021 }
2107 int pl_x, pl_y; 2142 int pl_x, pl_y;
2108 maptex pl_tex; 2143 maptex pl_tex;
2109 rc_t *rc = rc_alloc (); 2144 rc_t *rc = rc_alloc ();
2110 rc_t *rc_ov = rc_alloc (); 2145 rc_t *rc_ov = rc_alloc ();
2111 rc_key_t key; 2146 rc_key_t key;
2112 rc_array_t *arr, *arr_hidden; 2147 rc_array_t *arr;
2113 2148
2114 pl_tex.name = 0; 2149 pl_tex.name = 0;
2115 2150
2116 // that's current max. sorry. 2151 // that's current max. sorry.
2117 if (sw > 255) sw = 255; 2152 if (sw > 255) sw = 255;
2154 } 2189 }
2155 2190
2156 glEnable (GL_BLEND); 2191 glEnable (GL_BLEND);
2157 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 2192 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
2158 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); 2193 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
2159
2160 key.texname = self->tex [TEXID_HIDDEN].name;
2161 arr_hidden = rc_array (rc_ov, &key);
2162 2194
2163 for (z = 0; z <= 2; z++) 2195 for (z = 0; z <= 2; z++)
2164 { 2196 {
2165 memset (smooth_level, 0, sizeof (smooth_level)); 2197 memset (smooth_level, 0, sizeof (smooth_level));
2166 key.texname = -1; 2198 key.texname = -1;
2246 skey.x = x ; skey.y = y ; smooth_or_bits (smooth, &skey, 0x0400); 2278 skey.x = x ; skey.y = y ; smooth_or_bits (smooth, &skey, 0x0400);
2247 skey.x = x + 2; skey.y = y ; smooth_or_bits (smooth, &skey, 0x0800); 2279 skey.x = x + 2; skey.y = y ; smooth_or_bits (smooth, &skey, 0x0800);
2248 } 2280 }
2249 } 2281 }
2250 2282
2251 if (expect_false (z == 2)) 2283 if (expect_false (z == 2) && expect_false (cell->flags))
2252 { 2284 {
2253 /* draw question marks on top of hidden spaces */ 2285 // overlays such as the speech bubble, probably more to come
2254 if (!cell->darkness) 2286 if (cell->flags & 1)
2255 { 2287 {
2256 maptex tex = self->tex [TEXID_HIDDEN];
2257 int px = (x + 1) * T - tex.w;
2258 int py = (y + 1) * T - tex.h;
2259
2260 rc_t2f_v3f (arr_hidden, 0 , 0 , px , py , 0);
2261 rc_t2f_v3f (arr_hidden, 0 , tex.t, px , py + tex.h, 0);
2262 rc_t2f_v3f (arr_hidden, tex.s, tex.t, px + tex.w, py + tex.h, 0);
2263 rc_t2f_v3f (arr_hidden, tex.s, 0 , px + tex.w, py , 0);
2264 }
2265
2266 if (expect_false (cell->flags))
2267 {
2268 // overlays such as the speech bubble, probably more to come
2269 if (cell->flags & 1)
2270 {
2271 rc_key_t key_ov = key; 2288 rc_key_t key_ov = key;
2272 maptex tex = self->tex [TEXID_SPEECH]; 2289 maptex tex = self->tex [TEXID_SPEECH];
2273 rc_array_t *arr; 2290 rc_array_t *arr;
2274 int px = x * T + T * 2 / 32; 2291 int px = x * T + T * 2 / 32;
2275 int py = y * T - T * 6 / 32; 2292 int py = y * T - T * 6 / 32;
2276 2293
2277 key_ov.texname = tex.name; 2294 key_ov.texname = tex.name;
2278 arr = rc_array (rc_ov, &key_ov); 2295 arr = rc_array (rc_ov, &key_ov);
2279 2296
2280 rc_t2f_v3f (arr, 0 , 0 , px , py , 0); 2297 rc_t2f_v3f (arr, 0 , 0 , px , py , 0);
2281 rc_t2f_v3f (arr, 0 , tex.t, px , py + T, 0); 2298 rc_t2f_v3f (arr, 0 , tex.t, px , py + T, 0);
2282 rc_t2f_v3f (arr, tex.s, tex.t, px + T, py + T, 0); 2299 rc_t2f_v3f (arr, tex.s, tex.t, px + T, py + T, 0);
2283 rc_t2f_v3f (arr, tex.s, 0 , px + T, py , 0); 2300 rc_t2f_v3f (arr, tex.s, 0 , px + T, py , 0);
2284 }
2285 } 2301 }
2286 } 2302 }
2287 } 2303 }
2288 } 2304 }
2289 2305
2463 int x, y; 2479 int x, y;
2464 2480
2465 glEnable (GL_TEXTURE_2D); 2481 glEnable (GL_TEXTURE_2D);
2466 /* GL_REPLACE would be correct, as we don't need to modulate alpha, 2482 /* GL_REPLACE would be correct, as we don't need to modulate alpha,
2467 * but the nvidia driver (185.18.14) mishandles alpha textures 2483 * but the nvidia driver (185.18.14) mishandles alpha textures
2468 * ansd takes the colour from god knows where instead of using 2484 * and takes the colour from god knows where instead of using
2469 * Cp. MODULATE results in the same colour, but slightly different 2485 * Cp. MODULATE results in the same colour, but slightly different
2470 * alpha, but atcually gives us the correct colour with nvidia. 2486 * alpha, but atcually gives us the correct colour with nvidia.
2471 */ 2487 */
2472 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); 2488 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
2473 glEnable (GL_BLEND); 2489 glEnable (GL_BLEND);
2502void 2518void
2503fow_texture (DC::Map self, int mx, int my, int sw, int sh) 2519fow_texture (DC::Map self, int mx, int my, int sw, int sh)
2504 PPCODE: 2520 PPCODE:
2505{ 2521{
2506 int x, y; 2522 int x, y;
2507 int sw1 = sw + 2; 2523 int sw1 = sw + 2;
2508 int sh1 = sh + 2; 2524 int sh1 = sh + 2;
2509 int sh3 = sh * 3; 2525 int sh3 = sh * 3;
2510 int sw34 = (sw * 3 + 3) & ~3; 2526 int sw3 = sw * 3;
2511 uint8_t *darkness1 = (uint8_t *)malloc (sw1 * sh1); 2527 uint8_t *darkness1 = (uint8_t *)malloc (sw1 * sh1);
2512 SV *darkness3_sv = sv_2mortal (newSV (sw34 * sh3)); 2528 SV *darkness3_sv = sv_2mortal (newSV (sw3 * sh3));
2513 uint8_t *darkness3 = (uint8_t *)SvPVX (darkness3_sv); 2529 uint8_t *darkness3 = (uint8_t *)SvPVX (darkness3_sv);
2514 2530
2515 SvPOK_only (darkness3_sv); 2531 SvPOK_only (darkness3_sv);
2516 SvCUR_set (darkness3_sv, sw34 * sh3); 2532 SvCUR_set (darkness3_sv, sw3 * sh3);
2517 2533
2518 mx += self->x - 1; 2534 mx += self->x - 1;
2519 my += self->y - 1; 2535 my += self->y - 1;
2520
2521 memset (darkness1, DARKNESS_ADJUST (255 - FOW_DARKNESS), sw1 * sh1);
2522 2536
2523 for (y = 0; y < sh1; y++) 2537 for (y = 0; y < sh1; y++)
2524 if (0 <= y + my && y + my < self->rows) 2538 if (0 <= y + my && y + my < self->rows)
2525 { 2539 {
2526 maprow *row = self->row + (y + my); 2540 maprow *row = self->row + (y + my);
2559 2573
2560 uint8_t r13 = (d13 + d23 + d12) / 3; 2574 uint8_t r13 = (d13 + d23 + d12) / 3;
2561 uint8_t r23 = d23; 2575 uint8_t r23 = d23;
2562 uint8_t r33 = (d23 + d33 + d32) / 3; 2576 uint8_t r33 = (d23 + d33 + d32) / 3;
2563 2577
2564 darkness3 [(y * 3 ) * sw34 + (x * 3 )] = MAX (d22, r11); 2578 darkness3 [(y * 3 ) * sw3 + (x * 3 )] = MAX (d22, r11);
2565 darkness3 [(y * 3 ) * sw34 + (x * 3 + 1)] = MAX (d22, r21); 2579 darkness3 [(y * 3 ) * sw3 + (x * 3 + 1)] = MAX (d22, r21);
2566 darkness3 [(y * 3 ) * sw34 + (x * 3 + 2)] = MAX (d22, r31); 2580 darkness3 [(y * 3 ) * sw3 + (x * 3 + 2)] = MAX (d22, r31);
2567 darkness3 [(y * 3 + 1) * sw34 + (x * 3 )] = MAX (d22, r12); 2581 darkness3 [(y * 3 + 1) * sw3 + (x * 3 )] = MAX (d22, r12);
2568 darkness3 [(y * 3 + 1) * sw34 + (x * 3 + 1)] = MAX (d22, r22); 2582 darkness3 [(y * 3 + 1) * sw3 + (x * 3 + 1)] = MAX (d22, r22); /* this MUST be == d22 */
2569 darkness3 [(y * 3 + 1) * sw34 + (x * 3 + 2)] = MAX (d22, r32); 2583 darkness3 [(y * 3 + 1) * sw3 + (x * 3 + 2)] = MAX (d22, r32);
2570 darkness3 [(y * 3 + 2) * sw34 + (x * 3 )] = MAX (d22, r13); 2584 darkness3 [(y * 3 + 2) * sw3 + (x * 3 )] = MAX (d22, r13);
2571 darkness3 [(y * 3 + 2) * sw34 + (x * 3 + 1)] = MAX (d22, r23); 2585 darkness3 [(y * 3 + 2) * sw3 + (x * 3 + 1)] = MAX (d22, r23);
2572 darkness3 [(y * 3 + 2) * sw34 + (x * 3 + 2)] = MAX (d22, r33); 2586 darkness3 [(y * 3 + 2) * sw3 + (x * 3 + 2)] = MAX (d22, r33);
2573 } 2587 }
2574 2588
2575 free (darkness1); 2589 free (darkness1);
2576 2590
2577 EXTEND (SP, 3); 2591 EXTEND (SP, 3);
2578 PUSHs (sv_2mortal (newSViv (sw34))); 2592 PUSHs (sv_2mortal (newSViv (sw3)));
2579 PUSHs (sv_2mortal (newSViv (sh3))); 2593 PUSHs (sv_2mortal (newSViv (sh3)));
2580 PUSHs (darkness3_sv); 2594 PUSHs (darkness3_sv);
2581} 2595}
2582 2596
2583SV * 2597SV *
3297 3311
3298void glEndList () 3312void glEndList ()
3299 3313
3300void glCallList (int list) 3314void glCallList (int list)
3301 3315
3316void c_init ()
3317 CODE:
3318 glPixelStorei (GL_PACK_ALIGNMENT , 1);
3319 glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
3320
3302MODULE = Deliantra::Client PACKAGE = DC::UI::Base 3321MODULE = Deliantra::Client PACKAGE = DC::UI::Base
3303 3322
3304PROTOTYPES: DISABLE 3323PROTOTYPES: DISABLE
3305 3324
3306void 3325void

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines