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.69 by root, Sun Apr 30 08:09:44 2006 UTC vs.
Revision 1.80 by root, Sun May 14 22:58:24 2006 UTC

1#ifdef _WIN32 1#ifdef _WIN32
2# define _WIN32_WINNT 0x0500 // needed to get win2000 api calls
2# include <malloc.h> 3# include <malloc.h>
4# include <windows.h>
5# pragma warning(disable:4244)
3#endif 6#endif
4 7
5#include "EXTERN.h" 8#include "EXTERN.h"
6#include "perl.h" 9#include "perl.h"
7#include "XSUB.h" 10#include "XSUB.h"
8 11
12#include <math.h>
9#include <string.h> 13#include <string.h>
10#include <stdio.h> 14#include <stdio.h>
11 15
12#include <SDL.h> 16#include <SDL.h>
17#include <SDL_endian.h>
13#include <SDL_image.h> 18#include <SDL_image.h>
14#include <SDL_mixer.h> 19#include <SDL_mixer.h>
15#include <SDL_opengl.h> 20#include <SDL_opengl.h>
16 21
17#include <glib/gmacros.h> 22#include <glib/gmacros.h>
18 23
19#include <pango/pango.h> 24#include <pango/pango.h>
20#include <pango/pangofc-fontmap.h> 25#include <pango/pangofc-fontmap.h>
21#include <pango/pangoft2.h> 26#include <pango/pangoft2.h>
27#include <pango/pangocairo.h>
22 28
23#ifndef _WIN32 29#ifndef _WIN32
24# include <sys/types.h> 30# include <sys/types.h>
25# include <sys/socket.h> 31# include <sys/socket.h>
26# include <netinet/in.h> 32# include <netinet/in.h>
63typedef Mix_Music *CFClient__MixMusic; 69typedef Mix_Music *CFClient__MixMusic;
64 70
65typedef PangoFontDescription *CFClient__Font; 71typedef PangoFontDescription *CFClient__Font;
66 72
67typedef struct cf_layout { 73typedef struct cf_layout {
68 PangoLayout *pl; 74 PangoLayout *pl; // either derived from a cairo or ft2 context
75 int rgba; // wether we use rgba (cairo) or grayscale (ft2)
76 float r, g, b, a; // default color for rgba mode
69 int base_height; 77 int base_height;
70 CFClient__Font font; 78 CFClient__Font font;
71} *CFClient__Layout; 79} *CFClient__Layout;
72 80
73static CFClient__Font default_font; 81static CFClient__Font default_font;
74static PangoContext *context; 82static PangoContext *ft2_context, *cairo_context;
75static PangoFontMap *fontmap; 83static PangoFontMap *ft2_fontmap, *cairo_fontmap;
76 84
77static void 85static void
78substitute_func (FcPattern *pattern, gpointer data) 86substitute_func (FcPattern *pattern, gpointer data)
79{ 87{
80 FcPatternAddBool (pattern, FC_HINTING , 1); 88 FcPatternAddBool (pattern, FC_HINTING , 1);
81 FcPatternAddBool (pattern, FC_AUTOHINT, 0); 89 FcPatternAddBool (pattern, FC_AUTOHINT, 1);
82} 90}
83 91
84static void 92static void
85layout_update_font (CFClient__Layout self) 93layout_update_font (CFClient__Layout self)
86{ 94{
98static void 106static void
99layout_get_pixel_size (CFClient__Layout self, int *w, int *h) 107layout_get_pixel_size (CFClient__Layout self, int *w, int *h)
100{ 108{
101 pango_layout_get_pixel_size (self->pl, w, h); 109 pango_layout_get_pixel_size (self->pl, w, h);
102 110
103 *w = (*w + 3) & ~3;
104 if (!*w) *w = 1; 111 if (!*w) *w = 1;
105 if (!*h) *h = 1; 112 if (!*h) *h = 1;
113
114 *w = (*w + 3) & ~3;
106} 115}
107 116
108typedef uint16_t mapface; 117typedef uint16_t mapface;
109 118
110typedef struct { 119typedef struct {
258 } 267 }
259 } 268 }
260} 269}
261 270
262static void 271static void
263music_finished () 272music_finished (void)
264{ 273{
265 SDL_UserEvent ev; 274 SDL_UserEvent ev;
266 275
267 ev.type = SDL_USEREVENT; 276 ev.type = SDL_USEREVENT;
268 ev.code = 0; 277 ev.code = 0;
269 ev.data1 = 0; 278 ev.data1 = 0;
279 ev.data2 = 0;
280
281 SDL_PushEvent ((SDL_Event *)&ev);
282}
283
284static void
285channel_finished (int channel)
286{
287 SDL_UserEvent ev;
288
289 ev.type = SDL_USEREVENT;
290 ev.code = 1;
291 ev.data1 = (void *)(long)channel;
270 ev.data2 = 0; 292 ev.data2 = 0;
271 293
272 SDL_PushEvent ((SDL_Event *)&ev); 294 SDL_PushEvent ((SDL_Event *)&ev);
273} 295}
274 296
385# undef const_iv 407# undef const_iv
386 }; 408 };
387 409
388 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) 410 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; )
389 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); 411 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv));
412}
390 413
414void
415pango_init ()
416 CODE:
417{
418 // delayed, so it can pick up new fonts added by AddFontResourceEx
391 fontmap = pango_ft2_font_map_new (); 419 ft2_fontmap = pango_ft2_font_map_new ();
392 pango_ft2_font_map_set_default_substitute ((PangoFT2FontMap *)fontmap, substitute_func, 0, 0); 420 pango_ft2_font_map_set_default_substitute ((PangoFT2FontMap *)ft2_fontmap, substitute_func, 0, 0);
393 context = pango_ft2_font_map_create_context ((PangoFT2FontMap *)fontmap); 421 ft2_context = pango_ft2_font_map_create_context ((PangoFT2FontMap *)ft2_fontmap);
422
423 cairo_fontmap = pango_cairo_font_map_get_default ();
424 cairo_context = pango_cairo_font_map_create_context ((PangoCairoFontMap *)cairo_fontmap);
394} 425}
395 426
396int 427int
397SDL_Init (U32 flags = SDL_INIT_VIDEO | SDL_INIT_AUDIO) 428SDL_Init (U32 flags = SDL_INIT_VIDEO | SDL_INIT_AUDIO)
398 429
457 488
458 while (SDL_PollEvent (&ev)) 489 while (SDL_PollEvent (&ev))
459 { 490 {
460 HV *hv = newHV (); 491 HV *hv = newHV ();
461 hv_store (hv, "type", 4, newSViv (ev.type), 0); 492 hv_store (hv, "type", 4, newSViv (ev.type), 0);
493
462 switch (ev.type) 494 switch (ev.type)
463 { 495 {
464 case SDL_KEYDOWN: 496 case SDL_KEYDOWN:
465 case SDL_KEYUP: 497 case SDL_KEYUP:
466 hv_store (hv, "state", 5, newSViv (ev.key.state), 0); 498 hv_store (hv, "state", 5, newSViv (ev.key.state), 0);
486 case SDL_MOUSEBUTTONUP: 518 case SDL_MOUSEBUTTONUP:
487 hv_store (hv, "button", 6, newSViv (ev.button.button), 0); 519 hv_store (hv, "button", 6, newSViv (ev.button.button), 0);
488 hv_store (hv, "state", 5, newSViv (ev.button.state), 0); 520 hv_store (hv, "state", 5, newSViv (ev.button.state), 0);
489 hv_store (hv, "x", 1, newSViv (ev.button.x), 0); 521 hv_store (hv, "x", 1, newSViv (ev.button.x), 0);
490 hv_store (hv, "y", 1, newSViv (ev.button.y), 0); 522 hv_store (hv, "y", 1, newSViv (ev.button.y), 0);
523 break;
524
525 case SDL_USEREVENT:
526 hv_store (hv, "code", 4, newSViv (ev.user.code), 0);
527 hv_store (hv, "data1", 5, newSViv ((IV)ev.user.data1), 0);
528 hv_store (hv, "data2", 5, newSViv ((IV)ev.user.data2), 0);
529 break;
491 } 530 }
492 531
493 XPUSHs (sv_2mortal (newRV_noinc ((SV *)hv))); 532 XPUSHs (sv_2mortal (newRV_noinc ((SV *)hv)));
494 } 533 }
495} 534}
496 535
497int 536int
498Mix_OpenAudio (int frequency = 22050, int format = MIX_DEFAULT_FORMAT, int channels = 1, int chunksize = 512) 537Mix_OpenAudio (int frequency = 48000, int format = MIX_DEFAULT_FORMAT, int channels = 1, int chunksize = 2048)
499 POSTCALL: 538 POSTCALL:
500 Mix_HookMusicFinished (music_finished); 539 Mix_HookMusicFinished (music_finished);
540 Mix_ChannelFinished (channel_finished);
501 541
502void 542void
503Mix_CloseAudio () 543Mix_CloseAudio ()
504 544
505int 545int
528 568
529void 569void
530add_font (char *file) 570add_font (char *file)
531 CODE: 571 CODE:
532 FcConfigAppFontAddFile (0, (const FcChar8 *)file); /* no idea wether this is required */ 572 FcConfigAppFontAddFile (0, (const FcChar8 *)file); /* no idea wether this is required */
573#ifdef _WIN32
574 // cairo... sigh... requires win2000
575 AddFontResourceEx (file, FR_PRIVATE, 0);
576#endif
533 577
534void 578void
535load_image_inline (SV *image_) 579load_image_inline (SV *image_)
536 ALIAS: 580 ALIAS:
537 load_image_file = 1 581 load_image_file = 1
640 exit (1); 684 exit (1);
641 685
642MODULE = CFClient PACKAGE = CFClient::Font 686MODULE = CFClient PACKAGE = CFClient::Font
643 687
644CFClient::Font 688CFClient::Font
645new_from_file (SV *class, char *path) 689new_from_file (SV *class, char *path, int id = 0)
646 CODE: 690 CODE:
647{ 691{
648 int count; 692 int count;
649 FcPattern *pattern = FcFreeTypeQuery ((const FcChar8 *)path, 0, 0, &count); 693 FcPattern *pattern = FcFreeTypeQuery ((const FcChar8 *)path, id, 0, &count);
650 RETVAL = pango_fc_font_description_from_pattern (pattern, 0); 694 RETVAL = pango_fc_font_description_from_pattern (pattern, 0);
651 FcPatternDestroy (pattern); 695 FcPatternDestroy (pattern);
652} 696}
653 OUTPUT: 697 OUTPUT:
654 RETVAL 698 RETVAL
664 default_font = self; 708 default_font = self;
665 709
666MODULE = CFClient PACKAGE = CFClient::Layout 710MODULE = CFClient PACKAGE = CFClient::Layout
667 711
668CFClient::Layout 712CFClient::Layout
669new (SV *class, int base_height = MIN_FONT_HEIGHT) 713new (SV *class, int rgba = 0)
670 CODE: 714 CODE:
671 New (0, RETVAL, 1, struct cf_layout); 715 New (0, RETVAL, 1, struct cf_layout);
672 RETVAL->pl = pango_layout_new (context); 716
673 RETVAL->base_height = base_height; 717 RETVAL->pl = pango_layout_new (rgba ? cairo_context : ft2_context);
718 RETVAL->rgba = rgba;
719 RETVAL->r = 1.;
720 RETVAL->g = 1.;
721 RETVAL->b = 1.;
722 RETVAL->a = 1.;
723 RETVAL->base_height = MIN_FONT_HEIGHT;
674 RETVAL->font = 0; 724 RETVAL->font = 0;
725
675 pango_layout_set_wrap (RETVAL->pl, PANGO_WRAP_WORD_CHAR); 726 pango_layout_set_wrap (RETVAL->pl, PANGO_WRAP_WORD_CHAR);
676 pango_layout_set_font_description (RETVAL->pl, default_font); 727 layout_update_font (RETVAL);
677 OUTPUT: 728 OUTPUT:
678 RETVAL 729 RETVAL
679 730
680void 731void
681DESTROY (CFClient::Layout self) 732DESTROY (CFClient::Layout self)
682 CODE: 733 CODE:
683 g_object_unref (self->pl); 734 g_object_unref (self->pl);
684 Safefree (self); 735 Safefree (self);
685 736
737int
738is_rgba (CFClient::Layout self)
739 CODE:
740 RETVAL = self->rgba;
741 OUTPUT:
742 RETVAL
743
686void 744void
687set_text (CFClient::Layout self, SV *text_) 745set_text (CFClient::Layout self, SV *text_)
688 CODE: 746 CODE:
689{ 747{
690 STRLEN textlen; 748 STRLEN textlen;
708 CODE: 766 CODE:
709 RETVAL = newSVpv (pango_layout_get_text (self->pl), 0); 767 RETVAL = newSVpv (pango_layout_get_text (self->pl), 0);
710 SvUTF8_on (RETVAL); 768 SvUTF8_on (RETVAL);
711 OUTPUT: 769 OUTPUT:
712 RETVAL 770 RETVAL
771
772void
773set_foreground (CFClient::Layout self, float r, float g, float b, float a = 1.)
774 CODE:
775 self->r = r;
776 self->g = g;
777 self->b = b;
778 self->a = a;
713 779
714void 780void
715set_font (CFClient::Layout self, CFClient::Font font = 0) 781set_font (CFClient::Layout self, CFClient::Font font = 0)
716 CODE: 782 CODE:
717 if (self->font != font) 783 if (self->font != font)
775render (CFClient::Layout self) 841render (CFClient::Layout self)
776 PPCODE: 842 PPCODE:
777{ 843{
778 SV *retval; 844 SV *retval;
779 int w, h; 845 int w, h;
780 FT_Bitmap bitmap;
781 846
782 layout_get_pixel_size (self, &w, &h); 847 layout_get_pixel_size (self, &w, &h);
783 848
849 if (self->rgba)
850 {
851 cairo_surface_t *surface;
852 cairo_t *cairo;
853
854 retval = newSV (w * h * 4);
855 SvPOK_only (retval);
856 SvCUR_set (retval, w * h * 4);
857
858 memset (SvPVX (retval), 0, w * h * 4);
859
860 surface = cairo_image_surface_create_for_data (
861 (void*)SvPVX (retval), CAIRO_FORMAT_ARGB32, w, h, w * 4);
862 cairo = cairo_create (surface);
863 cairo_set_source_rgba (cairo, self->r, self->g, self->b, self->a);
864
865 pango_cairo_show_layout (cairo, self->pl);
866
867 cairo_destroy (cairo);
868 cairo_surface_destroy (surface);
869
870 // what a mess, and its premultiplied, too :(
871 {
872 uint32_t *p = (uint32_t *)SvPVX (retval);
873 uint32_t *e = p + w * h;
874
875 while (p < e)
876 {
877 uint32_t rgba = *p;
878 rgba = (rgba >> 24) | (rgba << 8);
879 rgba = SDL_SwapBE32 (rgba);
880 *p++ = rgba;
881 }
882 }
883
884 EXTEND (SP, 5);
885 PUSHs (sv_2mortal (newSViv (w)));
886 PUSHs (sv_2mortal (newSViv (h)));
887 PUSHs (sv_2mortal (retval));
888 PUSHs (sv_2mortal (newSViv (GL_RGBA)));
889 PUSHs (sv_2mortal (newSViv (GL_RGBA)));
890 }
891 else
892 {
893 FT_Bitmap bitmap;
894
784 retval = newSV (w * h); 895 retval = newSV (w * h);
785 SvPOK_only (retval); 896 SvPOK_only (retval);
786 SvCUR_set (retval, w * h); 897 SvCUR_set (retval, w * h);
787 898
788 bitmap.rows = h; 899 bitmap.rows = h;
789 bitmap.width = w; 900 bitmap.width = w;
790 bitmap.pitch = w; 901 bitmap.pitch = w;
791 bitmap.buffer = (unsigned char*)SvPVX (retval); 902 bitmap.buffer = (unsigned char*)SvPVX (retval);
792 bitmap.num_grays = 256; 903 bitmap.num_grays = 256;
793 bitmap.pixel_mode = FT_PIXEL_MODE_GRAY; 904 bitmap.pixel_mode = FT_PIXEL_MODE_GRAY;
794 905
795 memset (bitmap.buffer, 0, w * h); 906 memset (bitmap.buffer, 0, w * h);
796 907
797 pango_ft2_render_layout (&bitmap, self->pl, 0 * PANGO_SCALE, 0 * PANGO_SCALE); 908 pango_ft2_render_layout (&bitmap, self->pl, 0 * PANGO_SCALE, 0 * PANGO_SCALE);
798 909
799 EXTEND (SP, 3); 910 EXTEND (SP, 5);
800 PUSHs (sv_2mortal (newSViv (w))); 911 PUSHs (sv_2mortal (newSViv (w)));
801 PUSHs (sv_2mortal (newSViv (h))); 912 PUSHs (sv_2mortal (newSViv (h)));
802 PUSHs (sv_2mortal (retval)); 913 PUSHs (sv_2mortal (retval));
914 PUSHs (sv_2mortal (newSViv (GL_ALPHA)));
915 PUSHs (sv_2mortal (newSViv (GL_ALPHA)));
916 }
803} 917}
804 918
805MODULE = CFClient PACKAGE = CFClient::Texture 919MODULE = CFClient PACKAGE = CFClient::Texture
806 920
807void 921void
808draw_quad (SV *self, float x, float y, float w = 0, float h = 0) 922draw_quad (SV *self, float x, float y, float w = 0, float h = 0)
809 PROTOTYPE: $$$;$$ 923 PROTOTYPE: $$$;$$
924 ALIAS:
925 draw_quad_alpha = 1
926 draw_quad_alpha_premultiplied = 2
810 CODE: 927 CODE:
811{ 928{
812 HV *hv = (HV *)SvRV (self); 929 HV *hv = (HV *)SvRV (self);
813 float s = SvNV (*hv_fetch (hv, "s", 1, 1)); 930 float s = SvNV (*hv_fetch (hv, "s", 1, 1));
814 float t = SvNV (*hv_fetch (hv, "t", 1, 1)); 931 float t = SvNV (*hv_fetch (hv, "t", 1, 1));
819 { 936 {
820 w = SvNV (*hv_fetch (hv, "w", 1, 1)); 937 w = SvNV (*hv_fetch (hv, "w", 1, 1));
821 h = SvNV (*hv_fetch (hv, "h", 1, 1)); 938 h = SvNV (*hv_fetch (hv, "h", 1, 1));
822 } 939 }
823 940
941 if (ix)
942 {
943 glEnable (GL_BLEND);
944 glBlendFunc (ix == 1 ? GL_SRC_ALPHA : GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
945 }
946
824 glBindTexture (GL_TEXTURE_2D, name); 947 glBindTexture (GL_TEXTURE_2D, name);
948
825 if (wrap_mode) { 949 if (wrap_mode)
950 {
826 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); 951 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
827 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); 952 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
828 } 953 }
954
829 glBegin (GL_QUADS); 955 glBegin (GL_QUADS);
830 glTexCoord2f (0, 0); glVertex2f (x , y ); 956 glTexCoord2f (0, 0); glVertex2f (x , y );
831 glTexCoord2f (0, t); glVertex2f (x , y + h); 957 glTexCoord2f (0, t); glVertex2f (x , y + h);
832 glTexCoord2f (s, t); glVertex2f (x + w, y + h); 958 glTexCoord2f (s, t); glVertex2f (x + w, y + h);
833 glTexCoord2f (s, 0); glVertex2f (x + w, y ); 959 glTexCoord2f (s, 0); glVertex2f (x + w, y );
834 glEnd (); 960 glEnd ();
961
962 if (ix)
963 glDisable (GL_BLEND);
835} 964}
836 965
837MODULE = CFClient PACKAGE = CFClient::Map 966MODULE = CFClient PACKAGE = CFClient::Map
838 967
839CFClient::Map 968CFClient::Map
1362 const_iv (GL_ONE_MINUS_DST_ALPHA), 1491 const_iv (GL_ONE_MINUS_DST_ALPHA),
1363 const_iv (GL_RGB), 1492 const_iv (GL_RGB),
1364 const_iv (GL_RGBA), 1493 const_iv (GL_RGBA),
1365 const_iv (GL_UNSIGNED_BYTE), 1494 const_iv (GL_UNSIGNED_BYTE),
1366 const_iv (GL_ALPHA), 1495 const_iv (GL_ALPHA),
1496 const_iv (GL_LUMINANCE),
1367 const_iv (GL_FLOAT), 1497 const_iv (GL_FLOAT),
1368 const_iv (GL_UNSIGNED_INT_8_8_8_8_REV), 1498 const_iv (GL_UNSIGNED_INT_8_8_8_8_REV),
1369 const_iv (GL_COMPILE), 1499 const_iv (GL_COMPILE),
1370 const_iv (GL_TEXTURE_1D), 1500 const_iv (GL_TEXTURE_1D),
1371 const_iv (GL_TEXTURE_2D), 1501 const_iv (GL_TEXTURE_2D),
1460void glEnd () 1590void glEnd ()
1461 1591
1462void glColor (float r, float g, float b, float a = 1.0) 1592void glColor (float r, float g, float b, float a = 1.0)
1463 PROTOTYPE: @ 1593 PROTOTYPE: @
1464 CODE: 1594 CODE:
1465 glColor4f (r, g, b, a); 1595 glColor4ub (MIN ((int)r * 256., 255),
1596 MIN ((int)g * 256., 255),
1597 MIN ((int)b * 256., 255),
1598 MIN ((int)a * 256., 255));
1466 1599
1467void glVertex (float x, float y, float z = 0.) 1600void glVertex (float x, float y, float z = 0.)
1468 CODE: 1601 CODE:
1469 glVertex3f (x, y, z); 1602 glVertex3f (x, y, z);
1470 1603

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines