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.56 by root, Thu Apr 20 09:13:31 2006 UTC vs.
Revision 1.75 by root, Mon May 8 18:28:40 2006 UTC

1#ifdef _WIN32 1#ifdef _WIN32
2# include <malloc.h> 2# include <malloc.h>
3# pragma warning(disable:4244)
3#endif 4#endif
4 5
5#include "EXTERN.h" 6#include "EXTERN.h"
6#include "perl.h" 7#include "perl.h"
7#include "XSUB.h" 8#include "XSUB.h"
33 typedef signed char int8_t; 34 typedef signed char int8_t;
34 typedef signed short int16_t; 35 typedef signed short int16_t;
35 typedef signed int int32_t; 36 typedef signed int int32_t;
36#endif 37#endif
37 38
39#include "glext.h"
40
38#define FOW_DARKNESS 32 41#define FOW_DARKNESS 32
39 42
40#define MAP_EXTEND_X 32 43#define MAP_EXTEND_X 32
41#define MAP_EXTEND_Y 512 44#define MAP_EXTEND_Y 512
42 45
46#define MIN_FONT_HEIGHT 10
47
48#define GL_CALL(type,func,args) \
49 { \
50 static int init_; \
51 static type fptr_; \
52 \
53 if (!init_) \
54 { \
55 init_ = 1; \
56 fptr_ = (type)SDL_GL_GetProcAddress (# func); \
57 } \
58 \
59 if (fptr_) \
60 fptr_ args; \
61 }
62
43typedef Mix_Chunk *CFClient__MixChunk; 63typedef Mix_Chunk *CFClient__MixChunk;
44typedef Mix_Music *CFClient__MixMusic; 64typedef Mix_Music *CFClient__MixMusic;
45 65
46static PangoContext *context; 66typedef PangoFontDescription *CFClient__Font;
47static PangoFontMap *fontmap;
48 67
49typedef struct cf_layout { 68typedef struct cf_layout {
50 PangoLayout *pl; 69 PangoLayout *pl;
51 int base_height; 70 int base_height;
71 CFClient__Font font;
52} *CFClient__Layout; 72} *CFClient__Layout;
73
74static CFClient__Font default_font;
75static PangoContext *context;
76static PangoFontMap *fontmap;
53 77
54static void 78static void
55substitute_func (FcPattern *pattern, gpointer data) 79substitute_func (FcPattern *pattern, gpointer data)
56{ 80{
57 FcPatternAddBool (pattern, FC_HINTING , 1); 81 FcPatternAddBool (pattern, FC_HINTING , 1);
58 FcPatternAddBool (pattern, FC_AUTOHINT, 0); 82 FcPatternAddBool (pattern, FC_AUTOHINT, 0);
59} 83}
60 84
61static void 85static void
62layout_update (CFClient__Layout self) 86layout_update_font (CFClient__Layout self)
63{ 87{
64 /* use a random scale factor to account for unknown descenders, 0.8 works 88 /* use a random scale factor to account for unknown descenders, 0.8 works
65 * reasonably well with bitstream vera 89 * reasonably well with bitstream vera
66 */ 90 */
67 PangoFontDescription *font = pango_context_get_font_description (context); 91 PangoFontDescription *font = self->font ? self->font : default_font;
68 92
69 int height = self->base_height * (PANGO_SCALE * 8 / 10);
70
71 if (pango_font_description_get_size (font) != height)
72 {
73 pango_font_description_set_absolute_size (font, height); 93 pango_font_description_set_absolute_size (font,
74 pango_layout_context_changed (self->pl); 94 MAX (MIN_FONT_HEIGHT, self->base_height) * (PANGO_SCALE * 8 / 10));
75 } 95
96 pango_layout_set_font_description (self->pl, font);
76} 97}
77 98
78static void 99static void
79layout_get_pixel_size (CFClient__Layout self, int *w, int *h) 100layout_get_pixel_size (CFClient__Layout self, int *w, int *h)
80{ 101{
81 layout_update (self);
82
83 pango_layout_get_pixel_size (self->pl, w, h); 102 pango_layout_get_pixel_size (self->pl, w, h);
84 103
85 *w = (*w + 3) & ~3; 104 *w = (*w + 3) & ~3;
86 if (!*w) *w = 1; 105 if (!*w) *w = 1;
87 if (!*h) *h = 1; 106 if (!*h) *h = 1;
240 } 259 }
241 } 260 }
242} 261}
243 262
244static void 263static void
245music_finished () 264music_finished (void)
246{ 265{
247 SDL_UserEvent ev; 266 SDL_UserEvent ev;
248 267
249 ev.type = SDL_USEREVENT; 268 ev.type = SDL_USEREVENT;
250 ev.code = 0; 269 ev.code = 0;
251 ev.data1 = 0; 270 ev.data1 = 0;
252 ev.data2 = 0; 271 ev.data2 = 0;
253 272
273 SDL_PushEvent ((SDL_Event *)&ev);
274}
275
276static void
277channel_finished (int channel)
278{
254 SDL_PushEvent (&ev); 279 SDL_UserEvent ev;
280
281 ev.type = SDL_USEREVENT;
282 ev.code = 1;
283 ev.data1 = (void *)(long)channel;
284 ev.data2 = 0;
285
286 SDL_PushEvent ((SDL_Event *)&ev);
255} 287}
256 288
257MODULE = CFClient PACKAGE = CFClient 289MODULE = CFClient PACKAGE = CFClient
258 290
259PROTOTYPES: ENABLE 291PROTOTYPES: ENABLE
439 471
440 while (SDL_PollEvent (&ev)) 472 while (SDL_PollEvent (&ev))
441 { 473 {
442 HV *hv = newHV (); 474 HV *hv = newHV ();
443 hv_store (hv, "type", 4, newSViv (ev.type), 0); 475 hv_store (hv, "type", 4, newSViv (ev.type), 0);
476
444 switch (ev.type) 477 switch (ev.type)
445 { 478 {
446 case SDL_KEYDOWN: 479 case SDL_KEYDOWN:
447 case SDL_KEYUP: 480 case SDL_KEYUP:
448 hv_store (hv, "state", 5, newSViv (ev.key.state), 0); 481 hv_store (hv, "state", 5, newSViv (ev.key.state), 0);
468 case SDL_MOUSEBUTTONUP: 501 case SDL_MOUSEBUTTONUP:
469 hv_store (hv, "button", 6, newSViv (ev.button.button), 0); 502 hv_store (hv, "button", 6, newSViv (ev.button.button), 0);
470 hv_store (hv, "state", 5, newSViv (ev.button.state), 0); 503 hv_store (hv, "state", 5, newSViv (ev.button.state), 0);
471 hv_store (hv, "x", 1, newSViv (ev.button.x), 0); 504 hv_store (hv, "x", 1, newSViv (ev.button.x), 0);
472 hv_store (hv, "y", 1, newSViv (ev.button.y), 0); 505 hv_store (hv, "y", 1, newSViv (ev.button.y), 0);
506 break;
507
508 case SDL_USEREVENT:
509 hv_store (hv, "code", 4, newSViv (ev.user.code), 0);
510 hv_store (hv, "data1", 5, newSViv ((IV)ev.user.data1), 0);
511 hv_store (hv, "data2", 5, newSViv ((IV)ev.user.data2), 0);
512 break;
473 } 513 }
474 514
475 XPUSHs (sv_2mortal (newRV_noinc ((SV *)hv))); 515 XPUSHs (sv_2mortal (newRV_noinc ((SV *)hv)));
476 } 516 }
477} 517}
478 518
479int 519int
480Mix_OpenAudio (int frequency = 22050, int format = MIX_DEFAULT_FORMAT, int channels = 1, int chunksize = 512) 520Mix_OpenAudio (int frequency = 48000, int format = MIX_DEFAULT_FORMAT, int channels = 1, int chunksize = 2048)
481 POSTCALL: 521 POSTCALL:
482 Mix_HookMusicFinished (music_finished); 522 Mix_HookMusicFinished (music_finished);
523 Mix_ChannelFinished (channel_finished);
483 524
484void 525void
485Mix_CloseAudio () 526Mix_CloseAudio ()
486 527
487int 528int
510 551
511void 552void
512add_font (char *file) 553add_font (char *file)
513 CODE: 554 CODE:
514 FcConfigAppFontAddFile (0, (const FcChar8 *)file); /* no idea wether this is required */ 555 FcConfigAppFontAddFile (0, (const FcChar8 *)file); /* no idea wether this is required */
515
516void
517set_font (char *file)
518 CODE:
519{
520 int count;
521 FcPattern *pattern = FcFreeTypeQuery ((const FcChar8 *)file, 0, 0, &count);
522 PangoFontDescription *font = pango_fc_font_description_from_pattern (pattern, 0);
523 FcPatternDestroy (pattern);
524 pango_context_set_font_description (context, font);
525}
526 556
527void 557void
528load_image_inline (SV *image_) 558load_image_inline (SV *image_)
529 ALIAS: 559 ALIAS:
530 load_image_file = 1 560 load_image_file = 1
612 PUSHs (sv_2mortal (newSViv (b / y))); 642 PUSHs (sv_2mortal (newSViv (b / y)));
613 PUSHs (sv_2mortal (newSViv (a / y))); 643 PUSHs (sv_2mortal (newSViv (a / y)));
614} 644}
615 645
616void 646void
647error (char *message)
648 CODE:
649#ifdef _WIN32
650 MessageBox (0, message, "Crossfire+ Error", MB_OK | MB_ICONERROR | MB_SETFOREGROUND);
651#else
652 fprintf (stderr, "ERROR: %s\n", message);
653#endif
654
655void
617fatal (char *message) 656fatal (char *message)
618 CODE: 657 CODE:
619#ifdef _WIN32 658#ifdef _WIN32
620 MessageBox (0, message, "Crossfire+ Fatal Error", MB_OK | MB_ICONERROR | MB_SETFOREGROUND); 659 MessageBox (0, message, "Crossfire+ Fatal Error", MB_OK | MB_ICONERROR | MB_SETFOREGROUND);
621#else 660#else
622 fprintf (stderr, "FATAL: %s\n", message); 661 fprintf (stderr, "FATAL: %s\n", message);
623#endif 662#endif
624 exit (1); 663 exit (1);
625 664
665MODULE = CFClient PACKAGE = CFClient::Font
666
667CFClient::Font
668new_from_file (SV *class, char *path, int id = 0)
669 CODE:
670{
671 int count;
672 FcPattern *pattern = FcFreeTypeQuery ((const FcChar8 *)path, id, 0, &count);
673 RETVAL = pango_fc_font_description_from_pattern (pattern, 0);
674 FcPatternDestroy (pattern);
675}
676 OUTPUT:
677 RETVAL
678
679void
680DESTROY (CFClient::Font self)
681 CODE:
682 pango_font_description_free (self);
683
684void
685make_default (CFClient::Font self)
686 CODE:
687 default_font = self;
688
626MODULE = CFClient PACKAGE = CFClient::Layout 689MODULE = CFClient PACKAGE = CFClient::Layout
627 690
628CFClient::Layout 691CFClient::Layout
629new (SV *class, int base_height = 10) 692new (SV *class, int base_height = MIN_FONT_HEIGHT)
630 CODE: 693 CODE:
631 New (0, RETVAL, 1, struct cf_layout); 694 New (0, RETVAL, 1, struct cf_layout);
695 RETVAL->pl = pango_layout_new (context);
632 RETVAL->base_height = base_height; 696 RETVAL->base_height = base_height;
633 RETVAL->pl = pango_layout_new (context); 697 RETVAL->font = 0;
634 pango_layout_set_wrap (RETVAL->pl, PANGO_WRAP_WORD_CHAR); 698 pango_layout_set_wrap (RETVAL->pl, PANGO_WRAP_WORD_CHAR);
699 pango_layout_set_font_description (RETVAL->pl, default_font);
635 OUTPUT: 700 OUTPUT:
636 RETVAL 701 RETVAL
637 702
638void 703void
639DESTROY (CFClient::Layout self) 704DESTROY (CFClient::Layout self)
668 SvUTF8_on (RETVAL); 733 SvUTF8_on (RETVAL);
669 OUTPUT: 734 OUTPUT:
670 RETVAL 735 RETVAL
671 736
672void 737void
738set_font (CFClient::Layout self, CFClient::Font font = 0)
739 CODE:
740 if (self->font != font)
741 {
742 self->font = font;
743 layout_update_font (self);
744 }
745
746void
673set_height (CFClient::Layout self, int base_height) 747set_height (CFClient::Layout self, int base_height)
674 CODE: 748 CODE:
749 if (self->base_height != base_height)
750 {
675 self->base_height = base_height; 751 self->base_height = base_height;
752 layout_update_font (self);
753 }
676 754
677void 755void
678set_width (CFClient::Layout self, int max_width = -1) 756set_width (CFClient::Layout self, int max_width = -1)
679 CODE: 757 CODE:
680 pango_layout_set_width (self->pl, max_width < 0 ? max_width : max_width * PANGO_SCALE); 758 pango_layout_set_width (self->pl, max_width < 0 ? max_width : max_width * PANGO_SCALE);
683size (CFClient::Layout self) 761size (CFClient::Layout self)
684 PPCODE: 762 PPCODE:
685{ 763{
686 int w, h; 764 int w, h;
687 765
688 layout_update (self);
689 layout_get_pixel_size (self, &w, &h); 766 layout_get_pixel_size (self, &w, &h);
690 767
691 EXTEND (SP, 2); 768 EXTEND (SP, 2);
692 PUSHs (sv_2mortal (newSViv (w))); 769 PUSHs (sv_2mortal (newSViv (w)));
693 PUSHs (sv_2mortal (newSViv (h))); 770 PUSHs (sv_2mortal (newSViv (h)));
696int 773int
697xy_to_index (CFClient::Layout self, int x, int y) 774xy_to_index (CFClient::Layout self, int x, int y)
698 CODE: 775 CODE:
699{ 776{
700 int index, trailing; 777 int index, trailing;
701
702 layout_update (self);
703 pango_layout_xy_to_index (self->pl, x * PANGO_SCALE, y * PANGO_SCALE, &index, &trailing); 778 pango_layout_xy_to_index (self->pl, x * PANGO_SCALE, y * PANGO_SCALE, &index, &trailing);
704
705 RETVAL = index; 779 RETVAL = index;
706} 780}
707 OUTPUT: 781 OUTPUT:
708 RETVAL 782 RETVAL
709 783
710void 784void
711cursor_pos (CFClient::Layout self, int index) 785cursor_pos (CFClient::Layout self, int index)
712 PPCODE: 786 PPCODE:
713{ 787{
714 PangoRectangle strong_pos; 788 PangoRectangle strong_pos;
715 layout_update (self);
716 pango_layout_get_cursor_pos (self->pl, index, &strong_pos, 0); 789 pango_layout_get_cursor_pos (self->pl, index, &strong_pos, 0);
717 790
718 EXTEND (SP, 3); 791 EXTEND (SP, 3);
719 PUSHs (sv_2mortal (newSViv (strong_pos.x / PANGO_SCALE))); 792 PUSHs (sv_2mortal (newSViv (strong_pos.x / PANGO_SCALE)));
720 PUSHs (sv_2mortal (newSViv (strong_pos.y / PANGO_SCALE))); 793 PUSHs (sv_2mortal (newSViv (strong_pos.y / PANGO_SCALE)));
727{ 800{
728 SV *retval; 801 SV *retval;
729 int w, h; 802 int w, h;
730 FT_Bitmap bitmap; 803 FT_Bitmap bitmap;
731 804
732 layout_update (self);
733 layout_get_pixel_size (self, &w, &h); 805 layout_get_pixel_size (self, &w, &h);
734 806
735 retval = newSV (w * h); 807 retval = newSV (w * h);
736 SvPOK_only (retval); 808 SvPOK_only (retval);
737 SvCUR_set (retval, w * h); 809 SvCUR_set (retval, w * h);
1299 } *civ, const_iv[] = { 1371 } *civ, const_iv[] = {
1300# define const_iv(name) { # name, (IV)name } 1372# define const_iv(name) { # name, (IV)name }
1301 const_iv (GL_COLOR_MATERIAL), 1373 const_iv (GL_COLOR_MATERIAL),
1302 const_iv (GL_SMOOTH), 1374 const_iv (GL_SMOOTH),
1303 const_iv (GL_FLAT), 1375 const_iv (GL_FLAT),
1376 const_iv (GL_DITHER),
1304 const_iv (GL_BLEND), 1377 const_iv (GL_BLEND),
1378 const_iv (GL_SCISSOR_TEST),
1305 const_iv (GL_AND), 1379 const_iv (GL_AND),
1380 const_iv (GL_ONE),
1381 const_iv (GL_ZERO),
1306 const_iv (GL_SRC_ALPHA), 1382 const_iv (GL_SRC_ALPHA),
1383 const_iv (GL_SRC_ALPHA_SATURATE),
1307 const_iv (GL_ONE_MINUS_SRC_ALPHA), 1384 const_iv (GL_ONE_MINUS_SRC_ALPHA),
1385 const_iv (GL_ONE_MINUS_DST_ALPHA),
1308 const_iv (GL_RGB), 1386 const_iv (GL_RGB),
1309 const_iv (GL_RGBA), 1387 const_iv (GL_RGBA),
1310 const_iv (GL_UNSIGNED_BYTE), 1388 const_iv (GL_UNSIGNED_BYTE),
1311 const_iv (GL_ALPHA4),
1312 const_iv (GL_ALPHA), 1389 const_iv (GL_ALPHA),
1313 const_iv (GL_FLOAT), 1390 const_iv (GL_FLOAT),
1314 const_iv (GL_UNSIGNED_INT_8_8_8_8_REV), 1391 const_iv (GL_UNSIGNED_INT_8_8_8_8_REV),
1315 const_iv (GL_COMPILE), 1392 const_iv (GL_COMPILE),
1316 const_iv (GL_TEXTURE_1D), 1393 const_iv (GL_TEXTURE_1D),
1323 const_iv (GL_TEXTURE_WRAP_T), 1400 const_iv (GL_TEXTURE_WRAP_T),
1324 const_iv (GL_CLAMP), 1401 const_iv (GL_CLAMP),
1325 const_iv (GL_REPEAT), 1402 const_iv (GL_REPEAT),
1326 const_iv (GL_NEAREST), 1403 const_iv (GL_NEAREST),
1327 const_iv (GL_LINEAR), 1404 const_iv (GL_LINEAR),
1405 const_iv (GL_NEAREST_MIPMAP_NEAREST),
1406 const_iv (GL_LINEAR_MIPMAP_NEAREST),
1407 const_iv (GL_NEAREST_MIPMAP_LINEAR),
1408 const_iv (GL_LINEAR_MIPMAP_LINEAR),
1409 const_iv (GL_GENERATE_MIPMAP),
1328 const_iv (GL_MODULATE), 1410 const_iv (GL_MODULATE),
1411 const_iv (GL_DECAL),
1329 const_iv (GL_REPLACE), 1412 const_iv (GL_REPLACE),
1330 const_iv (GL_COLOR_BUFFER_BIT), 1413 const_iv (GL_COLOR_BUFFER_BIT),
1331 const_iv (GL_PROJECTION), 1414 const_iv (GL_PROJECTION),
1332 const_iv (GL_MODELVIEW), 1415 const_iv (GL_MODELVIEW),
1333 const_iv (GL_COLOR_LOGIC_OP), 1416 const_iv (GL_COLOR_LOGIC_OP),
1417 const_iv (GL_SEPARABLE_2D),
1334 const_iv (GL_CONVOLUTION_2D), 1418 const_iv (GL_CONVOLUTION_2D),
1335 const_iv (GL_CONVOLUTION_BORDER_MODE), 1419 const_iv (GL_CONVOLUTION_BORDER_MODE),
1336 const_iv (GL_CONSTANT_BORDER), 1420 const_iv (GL_CONSTANT_BORDER),
1337 const_iv (GL_LINES), 1421 const_iv (GL_LINES),
1338 const_iv (GL_QUADS), 1422 const_iv (GL_QUADS),
1363 1447
1364void glBlendFunc (int sfactor, int dfactor) 1448void glBlendFunc (int sfactor, int dfactor)
1365 1449
1366void glLogicOp (int opcode) 1450void glLogicOp (int opcode)
1367 1451
1452void glColorMask (int red, int green, int blue, int alpha)
1453
1368void glMatrixMode (int mode) 1454void glMatrixMode (int mode)
1369 1455
1370void glPushMatrix () 1456void glPushMatrix ()
1371 1457
1372void glPopMatrix () 1458void glPopMatrix ()
1373 1459
1374void glLoadIdentity () 1460void glLoadIdentity ()
1375 1461
1462# near and far are due to microsofts buggy c compiler
1376void glOrtho (double left, double right, double bottom, double top, double near, double far) 1463void glOrtho (double left, double right, double bottom, double top, double near_, double far_)
1377 1464
1378void glViewport (int x, int y, int width, int height) 1465void glViewport (int x, int y, int width, int height)
1466
1467void glScissor (int x, int y, int width, int height)
1379 1468
1380void glTranslate (float x, float y, float z = 0.) 1469void glTranslate (float x, float y, float z = 0.)
1381 CODE: 1470 CODE:
1382 glTranslatef (x, y, z); 1471 glTranslatef (x, y, z);
1383 1472
1384void glScale (float x, float y, float z) 1473void glScale (float x, float y, float z = 1.)
1385 CODE: 1474 CODE:
1386 glScalef (x, y, z); 1475 glScalef (x, y, z);
1387 1476
1388void glRotate (float angle, float x, float y, float z) 1477void glRotate (float angle, float x, float y, float z)
1389 CODE: 1478 CODE:
1394void glEnd () 1483void glEnd ()
1395 1484
1396void glColor (float r, float g, float b, float a = 1.0) 1485void glColor (float r, float g, float b, float a = 1.0)
1397 PROTOTYPE: @ 1486 PROTOTYPE: @
1398 CODE: 1487 CODE:
1399 glColor4f (r, g, b, a); 1488 glColor4ub (r * 255., g * 255., b * 255., a * 255.);
1400 1489
1401void glVertex (float x, float y, float z = 0.) 1490void glVertex (float x, float y, float z = 0.)
1402 CODE: 1491 CODE:
1403 glVertex3f (x, y, z); 1492 glVertex3f (x, y, z);
1404 1493
1416 1505
1417void glBindTexture (int target, int name) 1506void glBindTexture (int target, int name)
1418 1507
1419void glConvolutionParameter (int target, int pname, float params) 1508void glConvolutionParameter (int target, int pname, float params)
1420 CODE: 1509 CODE:
1421 glConvolutionParameterf (target, pname, params); 1510 GL_CALL (PFNGLCONVOLUTIONPARAMETERFEXTPROC, glConvolutionParameterf, (target, pname, params));
1422 1511
1423void glConvolutionFilter2D (int target, int internalformat, int width, int height, int format, int type, char *data) 1512void glConvolutionFilter2D (int target, int internalformat, int width, int height, int format, int type, char *data)
1513 CODE:
1514 GL_CALL (PFNGLCONVOLUTIONFILTER2DEXTPROC, glConvolutionFilter2D,
1515 (target, internalformat, width, height, format, type, data));
1516
1517void glSeparableFilter2D (int target, int internalformat, int width, int height, int format, int type, char *row, char *column)
1518 CODE:
1519 GL_CALL (PFNGLSEPARABLEFILTER2DEXTPROC, glSeparableFilter2D,
1520 (target, internalformat, width, height, format, type, row, column));
1424 1521
1425void glTexImage2D (int target, int level, int internalformat, int width, int height, int border, int format, int type, char *data) 1522void glTexImage2D (int target, int level, int internalformat, int width, int height, int border, int format, int type, char *data)
1426 1523
1427void glCopyTexImage2D (int target, int level, int internalformat, int x, int y, int width, int height, int border) 1524void glCopyTexImage2D (int target, int level, int internalformat, int x, int y, int width, int height, int border)
1525
1526void glRasterPos (int x, int y)
1527 CODE:
1528 glRasterPos2i (x, y);
1529
1530void glCopyPixels (int x, int y, int width, int height, int type = GL_COLOR)
1428 1531
1429int glGenTexture () 1532int glGenTexture ()
1430 CODE: 1533 CODE:
1431{ 1534{
1432 GLuint name; 1535 GLuint name;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines