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.160 by root, Mon Oct 9 01:06:36 2006 UTC vs.
Revision 1.170 by root, Sun Mar 4 19:36:08 2007 UTC

1#ifdef _WIN32 1#ifdef _WIN32
2long _ftol (double); /* defined by VC6 C libs */
3long _ftol2 (double dblSource) { return _ftol (dblSource); }
4# define WIN32_LEAN_AND_MEAN 2# define WIN32_LEAN_AND_MEAN
5# define _WIN32_WINNT 0x0500 // needed to get win2000 api calls 3# define NTDDI_VERSION NTDDI_WIN2K // needed to get win2000 api calls
6# include <malloc.h> 4# include <malloc.h>
7# include <windows.h> 5# include <windows.h>
8# include <wininet.h> 6# include <wininet.h>
9# pragma warning(disable:4244) 7# pragma warning(disable:4244)
8# pragma warning(disable:4761)
10#endif 9#endif
11 10
12#include "EXTERN.h" 11#include "EXTERN.h"
13#include "perl.h" 12#include "perl.h"
14#include "XSUB.h" 13#include "XSUB.h"
15 14
16#ifdef _WIN32 15#ifdef _WIN32
17# undef pipe 16# undef pipe
18#endif 17#endif
19 18
19#include <assert.h>
20#include <math.h> 20#include <math.h>
21#include <string.h> 21#include <string.h>
22#include <stdio.h> 22#include <stdio.h>
23#include <stdlib.h> 23#include <stdlib.h>
24 24
39# include <sys/types.h> 39# include <sys/types.h>
40# include <sys/socket.h> 40# include <sys/socket.h>
41# include <netinet/in.h> 41# include <netinet/in.h>
42# include <netinet/tcp.h> 42# include <netinet/tcp.h>
43# include <inttypes.h> 43# include <inttypes.h>
44#else
45 typedef unsigned char uint8_t;
46 typedef unsigned short uint16_t;
47 typedef unsigned int uint32_t;
48 typedef signed char int8_t;
49 typedef signed short int16_t;
50 typedef signed int int32_t;
51#endif 44#endif
52 45
53#define OBJ_STR "\xef\xbf\xbc" /* U+FFFC, objetc replacement character */ 46#define OBJ_STR "\xef\xbf\xbc" /* U+FFFC, object replacement character */
54 47
55#define FOW_DARKNESS 32 48#define FOW_DARKNESS 32
56 49
57#define MAP_EXTEND_X 32 50#define MAP_EXTEND_X 32
58#define MAP_EXTEND_Y 512 51#define MAP_EXTEND_Y 512
367 n |= n >> 16; 360 n |= n >> 16;
368 361
369 return n + 1; 362 return n + 1;
370} 363}
371 364
365/* SDL should provide this, really. */
366#define SDLK_MODIFIER_MIN 300
367#define SDLK_MODIFIER_MAX 314
368
372MODULE = CFPlus PACKAGE = CFPlus 369MODULE = CFPlus PACKAGE = CFPlus
373 370
374PROTOTYPES: ENABLE 371PROTOTYPES: ENABLE
375 372
376BOOT: 373BOOT:
379 static const struct { 376 static const struct {
380 const char *name; 377 const char *name;
381 IV iv; 378 IV iv;
382 } *civ, const_iv[] = { 379 } *civ, const_iv[] = {
383# define const_iv(name) { # name, (IV)name } 380# define const_iv(name) { # name, (IV)name }
381 const_iv (SDLK_MODIFIER_MIN),
382 const_iv (SDLK_MODIFIER_MAX),
383
384 const_iv (SDL_ACTIVEEVENT), 384 const_iv (SDL_ACTIVEEVENT),
385 const_iv (SDL_KEYDOWN), 385 const_iv (SDL_KEYDOWN),
386 const_iv (SDL_KEYUP), 386 const_iv (SDL_KEYUP),
387 const_iv (SDL_MOUSEMOTION), 387 const_iv (SDL_MOUSEMOTION),
388 const_iv (SDL_MOUSEBUTTONDOWN), 388 const_iv (SDL_MOUSEBUTTONDOWN),
397 const_iv (SDL_EVENT_RESERVEDA), 397 const_iv (SDL_EVENT_RESERVEDA),
398 const_iv (SDL_EVENT_RESERVEDB), 398 const_iv (SDL_EVENT_RESERVEDB),
399 const_iv (SDL_VIDEORESIZE), 399 const_iv (SDL_VIDEORESIZE),
400 const_iv (SDL_VIDEOEXPOSE), 400 const_iv (SDL_VIDEOEXPOSE),
401 const_iv (SDL_USEREVENT), 401 const_iv (SDL_USEREVENT),
402
403 const_iv (SDL_APPINPUTFOCUS),
404 const_iv (SDL_APPMOUSEFOCUS),
405 const_iv (SDL_APPACTIVE),
406
402 const_iv (SDLK_KP0), 407 const_iv (SDLK_KP0),
403 const_iv (SDLK_KP1), 408 const_iv (SDLK_KP1),
404 const_iv (SDLK_KP2), 409 const_iv (SDLK_KP2),
405 const_iv (SDLK_KP3), 410 const_iv (SDLK_KP3),
406 const_iv (SDLK_KP4), 411 const_iv (SDLK_KP4),
461 const_iv (SDLK_BREAK), 466 const_iv (SDLK_BREAK),
462 const_iv (SDLK_MENU), 467 const_iv (SDLK_MENU),
463 const_iv (SDLK_POWER), 468 const_iv (SDLK_POWER),
464 const_iv (SDLK_EURO), 469 const_iv (SDLK_EURO),
465 const_iv (SDLK_UNDO), 470 const_iv (SDLK_UNDO),
471
466 const_iv (KMOD_NONE), 472 const_iv (KMOD_NONE),
473 const_iv (KMOD_SHIFT),
467 const_iv (KMOD_LSHIFT), 474 const_iv (KMOD_LSHIFT),
468 const_iv (KMOD_RSHIFT), 475 const_iv (KMOD_RSHIFT),
476 const_iv (KMOD_CTRL),
469 const_iv (KMOD_LCTRL), 477 const_iv (KMOD_LCTRL),
470 const_iv (KMOD_RCTRL), 478 const_iv (KMOD_RCTRL),
479 const_iv (KMOD_ALT),
471 const_iv (KMOD_LALT), 480 const_iv (KMOD_LALT),
472 const_iv (KMOD_RALT), 481 const_iv (KMOD_RALT),
482 const_iv (KMOD_META),
473 const_iv (KMOD_LMETA), 483 const_iv (KMOD_LMETA),
474 const_iv (KMOD_RMETA), 484 const_iv (KMOD_RMETA),
475 const_iv (KMOD_NUM), 485 const_iv (KMOD_NUM),
476 const_iv (KMOD_CAPS), 486 const_iv (KMOD_CAPS),
477 const_iv (KMOD_MODE), 487 const_iv (KMOD_MODE),
478 const_iv (KMOD_CTRL),
479 const_iv (KMOD_SHIFT),
480 const_iv (KMOD_ALT),
481 const_iv (KMOD_META)
482# undef const_iv 488# undef const_iv
483 }; 489 };
484 490
485 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) 491 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; )
486 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); 492 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv));
493
494 assert (SDLK_MODIFIER_MIN == SDLK_NUMLOCK);
495 assert (SDLK_MODIFIER_MAX == SDLK_COMPOSE);
487} 496}
497
498void
499weaken (SV *rv)
500 PROTOTYPE: $
501 CODE:
502 sv_rvweaken (rv);
488 503
489int 504int
490in_destruct () 505in_destruct ()
491 CODE: 506 CODE:
492 RETVAL = PL_main_cv == Nullcv; 507 RETVAL = PL_main_cv == Nullcv;
595 case SDL_KEYDOWN: 610 case SDL_KEYDOWN:
596 case SDL_KEYUP: 611 case SDL_KEYUP:
597 hv_store (hv, "state", 5, newSViv (ev.key.state), 0); 612 hv_store (hv, "state", 5, newSViv (ev.key.state), 0);
598 hv_store (hv, "sym", 3, newSViv (ev.key.keysym.sym), 0); 613 hv_store (hv, "sym", 3, newSViv (ev.key.keysym.sym), 0);
599 hv_store (hv, "mod", 3, newSViv (ev.key.keysym.mod), 0); 614 hv_store (hv, "mod", 3, newSViv (ev.key.keysym.mod), 0);
615 hv_store (hv, "cmod", 4, newSViv (SDL_GetModState ()), 0); /* current mode */
600 hv_store (hv, "unicode", 7, newSViv (ev.key.keysym.unicode), 0); 616 hv_store (hv, "unicode", 7, newSViv (ev.key.keysym.unicode), 0);
601 break; 617 break;
602 618
603 case SDL_ACTIVEEVENT: 619 case SDL_ACTIVEEVENT:
604 hv_store (hv, "gain", 4, newSViv (ev.active.gain), 0); 620 hv_store (hv, "gain", 4, newSViv (ev.active.gain), 0);
796 _exit (1); 812 _exit (1);
797 813
798void 814void
799_exit (int retval = 0) 815_exit (int retval = 0)
800 CODE: 816 CODE:
817#ifdef WIN32
818 ExitThread (retval); // unclean, please beam me up
819#else
801 _exit (retval); 820 _exit (retval);
821#endif
802 822
803MODULE = CFPlus PACKAGE = CFPlus::Font 823MODULE = CFPlus PACKAGE = CFPlus::Font
804 824
805CFPlus::Font 825CFPlus::Font
806new_from_file (SV *class, char *path, int id = 0) 826new_from_file (SV *class, char *path, int id = 0)
1053xy_to_index (CFPlus::Layout self, int x, int y) 1073xy_to_index (CFPlus::Layout self, int x, int y)
1054 CODE: 1074 CODE:
1055{ 1075{
1056 int index, trailing; 1076 int index, trailing;
1057 pango_layout_xy_to_index (self->pl, x * PANGO_SCALE, y * PANGO_SCALE, &index, &trailing); 1077 pango_layout_xy_to_index (self->pl, x * PANGO_SCALE, y * PANGO_SCALE, &index, &trailing);
1058 RETVAL = index; 1078 RETVAL = index + trailing;
1059} 1079}
1060 OUTPUT: 1080 OUTPUT:
1061 RETVAL 1081 RETVAL
1062 1082
1063void 1083void
1069 1089
1070 EXTEND (SP, 3); 1090 EXTEND (SP, 3);
1071 PUSHs (sv_2mortal (newSViv (strong_pos.x / PANGO_SCALE))); 1091 PUSHs (sv_2mortal (newSViv (strong_pos.x / PANGO_SCALE)));
1072 PUSHs (sv_2mortal (newSViv (strong_pos.y / PANGO_SCALE))); 1092 PUSHs (sv_2mortal (newSViv (strong_pos.y / PANGO_SCALE)));
1073 PUSHs (sv_2mortal (newSViv (strong_pos.height / PANGO_SCALE))); 1093 PUSHs (sv_2mortal (newSViv (strong_pos.height / PANGO_SCALE)));
1094}
1095
1096void
1097index_to_line_x (CFPlus::Layout self, int index, int trailing = 0)
1098 PPCODE:
1099{
1100 int line, x;
1101
1102 pango_layout_index_to_line_x (self->pl, index, trailing, &line, &x);
1103 /* pango bug: line is between 1..numlines, not 0..numlines-1 */
1104
1105 EXTEND (SP, 2);
1106 PUSHs (sv_2mortal (newSViv (line - 1)));
1107 PUSHs (sv_2mortal (newSViv (x / PANGO_SCALE)));
1108}
1109
1110void
1111line_x_to_index (CFPlus::Layout self, int line, int x)
1112 PPCODE:
1113{
1114 PangoLayoutLine *lp;
1115 int index, trailing;
1116
1117 if (line < 0)
1118 XSRETURN_EMPTY;
1119
1120 if (!(lp = pango_layout_get_line (self->pl, line)))
1121 XSRETURN_EMPTY; /* do better */
1122
1123 pango_layout_line_x_to_index (lp, x * PANGO_SCALE, &index, &trailing);
1124
1125 EXTEND (SP, 2);
1126 if (GIMME_V == G_SCALAR)
1127 PUSHs (sv_2mortal (newSViv (index + trailing)));
1128 else
1129 {
1130 PUSHs (sv_2mortal (newSViv (index)));
1131 PUSHs (sv_2mortal (newSViv (trailing)));
1132 }
1074} 1133}
1075 1134
1076void 1135void
1077render (CFPlus::Layout self, float x, float y, int flags = 0) 1136render (CFPlus::Layout self, float x, float y, int flags = 0)
1078 PPCODE: 1137 PPCODE:
1172} 1231}
1173 1232
1174MODULE = CFPlus PACKAGE = CFPlus::Map 1233MODULE = CFPlus PACKAGE = CFPlus::Map
1175 1234
1176CFPlus::Map 1235CFPlus::Map
1177new (SV *class, int map_width, int map_height) 1236new (SV *class)
1178 CODE: 1237 CODE:
1179 New (0, RETVAL, 1, struct map); 1238 New (0, RETVAL, 1, struct map);
1180 RETVAL->x = 0; 1239 RETVAL->x = 0;
1181 RETVAL->y = 0; 1240 RETVAL->y = 0;
1182 RETVAL->w = map_width; 1241 RETVAL->w = 0;
1183 RETVAL->h = map_height; 1242 RETVAL->h = 0;
1184 RETVAL->ox = 0; 1243 RETVAL->ox = 0;
1185 RETVAL->oy = 0; 1244 RETVAL->oy = 0;
1186 RETVAL->faces = 8192; 1245 RETVAL->faces = 8192;
1187 Newz (0, RETVAL->face, RETVAL->faces, mapface); 1246 Newz (0, RETVAL->face, RETVAL->faces, mapface);
1188 RETVAL->texs = 8192; 1247 RETVAL->texs = 8192;
1199 map_clear (self); 1258 map_clear (self);
1200 Safefree (self->face); 1259 Safefree (self->face);
1201 Safefree (self->tex); 1260 Safefree (self->tex);
1202 Safefree (self); 1261 Safefree (self);
1203} 1262}
1263
1264void
1265resize (CFPlus::Map self, int map_width, int map_height)
1266 CODE:
1267 self->w = map_width;
1268 self->h = map_height;
1204 1269
1205void 1270void
1206clear (CFPlus::Map self) 1271clear (CFPlus::Map self)
1207 CODE: 1272 CODE:
1208 map_clear (self); 1273 map_clear (self);
1347 cell->stat_width = 1; 1412 cell->stat_width = 1;
1348 cell->stat_hp = *data++; 1413 cell->stat_hp = *data++;
1349 } 1414 }
1350 else if (cmd == 6) // monster width 1415 else if (cmd == 6) // monster width
1351 cell->stat_width = *data++ + 1; 1416 cell->stat_width = *data++ + 1;
1352 else if (cmd == 0x47) // monster width 1417 else if (cmd == 0x47)
1353 { 1418 {
1354 if (*data == 4) 1419 if (*data == 8)
1355 ; // decode player tag 1420 ; // decode player uuid
1356 1421
1357 data += *data + 1; 1422 data += *data + 1;
1358 } 1423 }
1359 else if (cmd == 8) // cell flags 1424 else if (cmd == 8) // cell flags
1360 cell->flags = *data++; 1425 cell->flags = *data++;
1448} 1513}
1449 OUTPUT: 1514 OUTPUT:
1450 RETVAL 1515 RETVAL
1451 1516
1452void 1517void
1453draw (CFPlus::Map self, int shift_x, int shift_y, int x0, int y0, int sw, int sh) 1518draw (CFPlus::Map self, int mx, int my, int sw, int sh)
1454 CODE: 1519 CODE:
1455{ 1520{
1456 int vx, vy;
1457 int x, y, z; 1521 int x, y, z;
1458 int last_name; 1522 int last_name;
1459 mapface face; 1523 mapface face;
1460
1461 vx = self->x + self->w / 2 - sw / 2 - shift_x;
1462 vy = self->y + self->h / 2 - sh / 2 - shift_y;
1463
1464 /*
1465 int vx = self->vx = self->w >= sw
1466 ? self->x + (self->w - sw) / 2
1467 : MIN (self->x, MAX (self->x + self->w - sw + 1, self->vx));
1468
1469 int vy = self->vy = self->h >= sh
1470 ? self->y + (self->h - sh) / 2
1471 : MIN (self->y, MAX (self->y + self->h - sh + 1, self->vy));
1472 */
1473 1524
1474 glColor4ub (255, 255, 255, 255); 1525 glColor4ub (255, 255, 255, 255);
1475 1526
1476 glEnable (GL_BLEND); 1527 glEnable (GL_BLEND);
1477 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 1528 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1480 1531
1481 glBegin (GL_QUADS); 1532 glBegin (GL_QUADS);
1482 1533
1483 last_name = 0; 1534 last_name = 0;
1484 1535
1536 mx += self->x;
1537 my += self->y;
1538
1485 for (z = 0; z < 3; z++) 1539 for (z = 0; z < 3; z++)
1486 for (y = 0; y < sh; y++) 1540 for (y = 0; y < sh; y++)
1487 if (0 <= y + vy && y + vy < self->rows) 1541 if (0 <= y + my && y + my < self->rows)
1488 { 1542 {
1489 maprow *row = self->row + (y + vy); 1543 maprow *row = self->row + (y + my);
1490 1544
1491 for (x = 0; x < sw; x++) 1545 for (x = 0; x < sw; x++)
1492 if (row->c0 <= x + vx && x + vx < row->c1) 1546 if (row->c0 <= x + mx && x + mx < row->c1)
1493 { 1547 {
1494 mapcell *cell = row->col + (x + vx - row->c0); 1548 mapcell *cell = row->col + (x + mx - row->c0);
1495 1549
1496 face = cell->face [z]; 1550 face = cell->face [z];
1497 1551
1498 if (face && face < self->texs) 1552 if (face && face < self->texs)
1499 { 1553 {
1540 glDisable (GL_TEXTURE_2D); 1594 glDisable (GL_TEXTURE_2D);
1541 glDisable (GL_BLEND); 1595 glDisable (GL_BLEND);
1542 1596
1543 // top layer: overlays such as the health bar 1597 // top layer: overlays such as the health bar
1544 for (y = 0; y < sh; y++) 1598 for (y = 0; y < sh; y++)
1545 if (0 <= y + vy && y + vy < self->rows) 1599 if (0 <= y + my && y + my < self->rows)
1546 { 1600 {
1547 maprow *row = self->row + (y + vy); 1601 maprow *row = self->row + (y + my);
1548 1602
1549 for (x = 0; x < sw; x++) 1603 for (x = 0; x < sw; x++)
1550 if (row->c0 <= x + vx && x + vx < row->c1) 1604 if (row->c0 <= x + mx && x + mx < row->c1)
1551 { 1605 {
1552 mapcell *cell = row->col + (x + vx - row->c0); 1606 mapcell *cell = row->col + (x + mx - row->c0);
1553 1607
1554 int px = x * 32; 1608 int px = x * 32;
1555 int py = y * 32; 1609 int py = y * 32;
1556 1610
1557 if (cell->stat_hp) 1611 if (cell->stat_hp)
1631 glDisable (GL_BLEND); 1685 glDisable (GL_BLEND);
1632 glDisable (GL_TEXTURE_2D); 1686 glDisable (GL_TEXTURE_2D);
1633} 1687}
1634 1688
1635void 1689void
1636fow_texture (CFPlus::Map self, int shift_x, int shift_y, int x0, int y0, int sw, int sh) 1690fow_texture (CFPlus::Map self, int mx, int my, int sw, int sh)
1637 PPCODE: 1691 PPCODE:
1638{ 1692{
1639 int vx, vy;
1640 int x, y; 1693 int x, y;
1641 int sw4 = (sw + 3) & ~3; 1694 int sw4 = (sw + 3) & ~3;
1642 SV *darkness_sv = sv_2mortal (newSV (sw4 * sh)); 1695 SV *darkness_sv = sv_2mortal (newSV (sw4 * sh));
1643 uint8_t *darkness = (uint8_t *)SvPVX (darkness_sv); 1696 uint8_t *darkness = (uint8_t *)SvPVX (darkness_sv);
1644 1697
1645 memset (darkness, 255, sw4 * sh); 1698 memset (darkness, 255, sw4 * sh);
1646 SvPOK_only (darkness_sv); 1699 SvPOK_only (darkness_sv);
1647 SvCUR_set (darkness_sv, sw4 * sh); 1700 SvCUR_set (darkness_sv, sw4 * sh);
1648 1701
1649 vx = self->x + (self->w - sw + 1) / 2 - shift_x; 1702 mx += self->x;
1650 vy = self->y + (self->h - sh + 1) / 2 - shift_y; 1703 my += self->y;
1651 1704
1652 for (y = 0; y < sh; y++) 1705 for (y = 0; y < sh; y++)
1653 if (0 <= y + vy && y + vy < self->rows) 1706 if (0 <= y + my && y + my < self->rows)
1654 { 1707 {
1655 maprow *row = self->row + (y + vy); 1708 maprow *row = self->row + (y + my);
1656 1709
1657 for (x = 0; x < sw; x++) 1710 for (x = 0; x < sw; x++)
1658 if (row->c0 <= x + vx && x + vx < row->c1) 1711 if (row->c0 <= x + mx && x + mx < row->c1)
1659 { 1712 {
1660 mapcell *cell = row->col + (x + vx - row->c0); 1713 mapcell *cell = row->col + (x + mx - row->c0);
1661 1714
1662 darkness[y * sw4 + x] = cell->darkness 1715 darkness[y * sw4 + x] = cell->darkness
1663 ? 255 - (cell->darkness - 1) 1716 ? 255 - (cell->darkness - 1)
1664 : 255 - FOW_DARKNESS; 1717 : 255 - FOW_DARKNESS;
1665 } 1718 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines