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.122 by root, Fri Jun 23 22:35:15 2006 UTC vs.
Revision 1.131 by root, Thu Jul 13 01:54:18 2006 UTC

1#ifdef _WIN32 1#ifdef _WIN32
2# define WIN32_LEAN_AND_MEAN
2# define _WIN32_WINNT 0x0500 // needed to get win2000 api calls 3# define _WIN32_WINNT 0x0500 // needed to get win2000 api calls
3# include <malloc.h> 4# include <malloc.h>
4# include <windows.h> 5# include <windows.h>
5# pragma warning(disable:4244) 6# pragma warning(disable:4244)
6#endif 7#endif
7 8
8#include "EXTERN.h" 9#include "EXTERN.h"
9#include "perl.h" 10#include "perl.h"
10#include "XSUB.h" 11#include "XSUB.h"
11 12
13#ifdef _WIN32
14# undef pipe
15#endif
16
12#include <math.h> 17#include <math.h>
13#include <string.h> 18#include <string.h>
14#include <stdio.h> 19#include <stdio.h>
15#include <stdlib.h> 20#include <stdlib.h>
16 21
18#include <SDL_endian.h> 23#include <SDL_endian.h>
19#include <SDL_image.h> 24#include <SDL_image.h>
20#include <SDL_mixer.h> 25#include <SDL_mixer.h>
21#include <SDL_opengl.h> 26#include <SDL_opengl.h>
22 27
28#define PANGO_ENABLE_BACKEND
29#define G_DISABLE_CAST_CHECKS
30
23#include <glib/gmacros.h> 31#include <glib/gmacros.h>
24 32
25#include <pango/pango.h> 33#include <pango/pango.h>
26#include <pango/pangofc-fontmap.h>
27#include <pango/pangoft2.h>
28#include <pango/pangocairo.h>
29 34
30#ifndef _WIN32 35#ifndef _WIN32
31# include <sys/types.h> 36# include <sys/types.h>
32# include <sys/socket.h> 37# include <sys/socket.h>
33# include <netinet/in.h> 38# include <netinet/in.h>
40 typedef signed char int8_t; 45 typedef signed char int8_t;
41 typedef signed short int16_t; 46 typedef signed short int16_t;
42 typedef signed int int32_t; 47 typedef signed int int32_t;
43#endif 48#endif
44 49
45#include "glext.h"
46
47#define OBJ_STR "\xef\xbf\xbc" /* U+FFFC, objetc replacement character */ 50#define OBJ_STR "\xef\xbf\xbc" /* U+FFFC, objetc replacement character */
48 51
49#define FOW_DARKNESS 32 52#define FOW_DARKNESS 32
50 53
51#define MAP_EXTEND_X 32 54#define MAP_EXTEND_X 32
52#define MAP_EXTEND_Y 512 55#define MAP_EXTEND_Y 512
53 56
54#define MIN_FONT_HEIGHT 10 57#define MIN_FONT_HEIGHT 10
58
59#if 0
60# define PARACHUTE SDL_INIT_NOPARACHUTE
61#else
62# define PARACHUTE 0
63#endif
55 64
56static struct 65static struct
57{ 66{
58#define GL_FUNC(ptr,name) ptr name; 67#define GL_FUNC(ptr,name) ptr name;
59#include "glfunc.h" 68#include "glfunc.h"
68 gl.BlendFuncSeparateEXT (sa, da, saa, daa); 77 gl.BlendFuncSeparateEXT (sa, da, saa, daa);
69 else 78 else
70 glBlendFunc (sa, da); 79 glBlendFunc (sa, da);
71} 80}
72 81
82#include "texcache.c"
83
84#include "pango-font.c"
85#include "pango-fontmap.c"
86#include "pango-render.c"
87
73typedef Mix_Chunk *CFClient__MixChunk; 88typedef Mix_Chunk *CFClient__MixChunk;
74typedef Mix_Music *CFClient__MixMusic; 89typedef Mix_Music *CFClient__MixMusic;
75 90
76typedef PangoFontDescription *CFClient__Font; 91typedef PangoFontDescription *CFClient__Font;
77 92
92 107
93 return 0; 108 return 0;
94} 109}
95 110
96typedef struct cf_layout { 111typedef struct cf_layout {
97 PangoLayout *pl; // either derived from a cairo or ft2 context 112 PangoLayout *pl;
98 int rgba; // wether we use rgba (cairo) or grayscale (ft2)
99 float r, g, b, a; // default color for rgba mode 113 float r, g, b, a; // default color for rgba mode
100 int base_height; 114 int base_height;
101 CFClient__Font font; 115 CFClient__Font font;
102} *CFClient__Layout; 116} *CFClient__Layout;
103 117
104static CFClient__Font default_font; 118static CFClient__Font default_font;
105static PangoContext *ft2_context, *cairo_context; 119static PangoContext *opengl_context;
106static PangoFontMap *ft2_fontmap, *cairo_fontmap; 120static PangoFontMap *opengl_fontmap;
107 121
108static void 122static void
109substitute_func (FcPattern *pattern, gpointer data) 123substitute_func (FcPattern *pattern, gpointer data)
110{ 124{
111 FcPatternAddBool (pattern, FC_HINTING, 1); 125 FcPatternAddBool (pattern, FC_HINTING, 1);
112#ifdef FC_HINT_STYLE 126#ifdef FC_HINT_STYLE
113 FcPatternAddBool (pattern, FC_HINT_STYLE, FC_HINT_FULL); 127 FcPatternAddBool (pattern, FC_HINT_STYLE, FC_HINT_FULL);
114#endif 128#endif
115#ifdef _WIN32
116 FcPatternAddBool (pattern, FC_AUTOHINT, 1);
117#else
118 FcPatternAddBool (pattern, FC_AUTOHINT, 0); 129 FcPatternAddBool (pattern, FC_AUTOHINT, 0);
119#endif
120} 130}
121 131
122static void 132static void
123layout_update_font (CFClient__Layout self) 133layout_update_font (CFClient__Layout self)
124{ 134{
456 466
457 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) 467 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; )
458 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); 468 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv));
459} 469}
460 470
471int
472in_destruct ()
473 CODE:
474 RETVAL = PL_main_cv == Nullcv;
475 OUTPUT:
476 RETVAL
477
461NV floor (NV x) 478NV floor (NV x)
462 479
463NV ceil (NV x) 480NV ceil (NV x)
464 481
465void 482void
466pango_init () 483pango_init ()
467 CODE: 484 CODE:
468 // delayed, so it can pick up new fonts added by AddFontResourceEx
469{ 485{
470 {
471 ft2_fontmap = pango_ft2_font_map_new (); 486 opengl_fontmap = pango_opengl_font_map_new ();
472 pango_ft2_font_map_set_default_substitute ((PangoFT2FontMap *)ft2_fontmap, substitute_func, 0, 0); 487 pango_opengl_font_map_set_default_substitute ((PangoOpenGLFontMap *)opengl_fontmap, substitute_func, 0, 0);
473 ft2_context = pango_ft2_font_map_create_context ((PangoFT2FontMap *)ft2_fontmap); 488 opengl_context = pango_opengl_font_map_create_context ((PangoOpenGLFontMap *)opengl_fontmap);
474 }
475 {
476 cairo_font_options_t *fopt = cairo_font_options_create ();
477 cairo_fontmap = pango_cairo_font_map_get_default ();
478 cairo_context = pango_cairo_font_map_create_context ((PangoCairoFontMap *)cairo_fontmap);
479#ifdef _WIN32
480 // cairo looks like shit eaten twice on windows
481 cairo_font_options_set_antialias (fopt, CAIRO_ANTIALIAS_NONE);
482#else
483 cairo_font_options_set_antialias (fopt, CAIRO_ANTIALIAS_GRAY);
484#endif
485 cairo_font_options_set_hint_style (fopt, CAIRO_HINT_STYLE_FULL);
486 cairo_font_options_set_hint_metrics (fopt, CAIRO_HINT_METRICS_ON);
487 pango_cairo_context_set_font_options (cairo_context, fopt);
488 cairo_font_options_destroy (fopt);
489 }
490} 489}
491 490
492int 491int
493SDL_Init (U32 flags = SDL_INIT_VIDEO | SDL_INIT_AUDIO) 492SDL_Init (U32 flags = SDL_INIT_VIDEO | SDL_INIT_AUDIO | PARACHUTE)
494 493
495void 494void
496SDL_Quit () 495SDL_Quit ()
497 496
498void 497void
513 SDL_GL_SetAttribute (SDL_GL_ACCUM_GREEN_SIZE, 0); 512 SDL_GL_SetAttribute (SDL_GL_ACCUM_GREEN_SIZE, 0);
514 SDL_GL_SetAttribute (SDL_GL_ACCUM_BLUE_SIZE, 0); 513 SDL_GL_SetAttribute (SDL_GL_ACCUM_BLUE_SIZE, 0);
515 SDL_GL_SetAttribute (SDL_GL_ACCUM_ALPHA_SIZE, 0); 514 SDL_GL_SetAttribute (SDL_GL_ACCUM_ALPHA_SIZE, 0);
516 515
517 SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1); 516 SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1);
517 SDL_GL_SetAttribute (SDL_GL_ACCELERATED_VISUAL, 1);
518 SDL_GL_SetAttribute (SDL_GL_SWAP_CONTROL, 1);
518 519
519 SDL_EnableUNICODE (1); 520 SDL_EnableUNICODE (1);
520 SDL_EnableKeyRepeat (SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL); 521 SDL_EnableKeyRepeat (SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
521 522
522 m = SDL_ListModes (0, SDL_FULLSCREEN | SDL_OPENGL); 523 m = SDL_ListModes (0, SDL_FULLSCREEN | SDL_OPENGL);
635#endif 636#endif
636 637
637void 638void
638add_font (char *file) 639add_font (char *file)
639 CODE: 640 CODE:
640 FcConfigAppFontAddFile (0, (const FcChar8 *)file); /* no idea wether this is required */ 641 FcConfigAppFontAddFile (0, (const FcChar8 *)file);
641#ifdef _WIN32
642 // cairo... sigh... requires win2000
643 AddFontResourceEx (file, FR_PRIVATE, 0);
644#endif
645 642
646void 643void
647load_image_inline (SV *image_) 644load_image_inline (SV *image_)
648 ALIAS: 645 ALIAS:
649 load_image_file = 1 646 load_image_file = 1
691 688
692 surface2 = SDL_ConvertSurface (surface, &fmt, SDL_SWSURFACE); 689 surface2 = SDL_ConvertSurface (surface, &fmt, SDL_SWSURFACE);
693 690
694 assert (surface2->pitch == surface2->w * 4); 691 assert (surface2->pitch == surface2->w * 4);
695 692
693 SDL_LockSurface (surface2);
696 EXTEND (SP, 5); 694 EXTEND (SP, 6);
697 PUSHs (sv_2mortal (newSViv (surface2->w))); 695 PUSHs (sv_2mortal (newSViv (surface2->w)));
698 PUSHs (sv_2mortal (newSViv (surface2->h))); 696 PUSHs (sv_2mortal (newSViv (surface2->h)));
699 SDL_LockSurface (surface2);
700 PUSHs (sv_2mortal (newSVpvn (surface2->pixels, surface2->h * surface2->pitch))); 697 PUSHs (sv_2mortal (newSVpvn (surface2->pixels, surface2->h * surface2->pitch)));
701 SDL_UnlockSurface (surface2);
702 PUSHs (sv_2mortal (newSViv (surface->flags & (SDL_SRCCOLORKEY | SDL_SRCALPHA) ? GL_RGBA : GL_RGB))); 698 PUSHs (sv_2mortal (newSViv (surface->flags & (SDL_SRCCOLORKEY | SDL_SRCALPHA) ? GL_RGBA : GL_RGB)));
703 PUSHs (sv_2mortal (newSViv (GL_RGBA))); 699 PUSHs (sv_2mortal (newSViv (GL_RGBA)));
704 PUSHs (sv_2mortal (newSViv (GL_UNSIGNED_BYTE))); 700 PUSHs (sv_2mortal (newSViv (GL_UNSIGNED_BYTE)));
701 SDL_UnlockSurface (surface2);
705 702
706 SDL_FreeSurface (surface); 703 SDL_FreeSurface (surface);
707 SDL_FreeSurface (surface2); 704 SDL_FreeSurface (surface2);
708} 705}
709 706
778 CODE: 775 CODE:
779 default_font = self; 776 default_font = self;
780 777
781MODULE = CFClient PACKAGE = CFClient::Layout 778MODULE = CFClient PACKAGE = CFClient::Layout
782 779
780void
781reset_glyph_cache ()
782 CODE:
783 tc_clear ();
784
783CFClient::Layout 785CFClient::Layout
784new (SV *class, int rgba = 0) 786new (SV *class)
785 CODE: 787 CODE:
786 New (0, RETVAL, 1, struct cf_layout); 788 New (0, RETVAL, 1, struct cf_layout);
787 789
788 RETVAL->pl = pango_layout_new (rgba ? cairo_context : ft2_context); 790 RETVAL->pl = pango_layout_new (opengl_context);
789 RETVAL->rgba = rgba;
790 RETVAL->r = 1.; 791 RETVAL->r = 1.;
791 RETVAL->g = 1.; 792 RETVAL->g = 1.;
792 RETVAL->b = 1.; 793 RETVAL->b = 1.;
793 RETVAL->a = 1.; 794 RETVAL->a = 1.;
794 RETVAL->base_height = MIN_FONT_HEIGHT; 795 RETVAL->base_height = MIN_FONT_HEIGHT;
802void 803void
803DESTROY (CFClient::Layout self) 804DESTROY (CFClient::Layout self)
804 CODE: 805 CODE:
805 g_object_unref (self->pl); 806 g_object_unref (self->pl);
806 Safefree (self); 807 Safefree (self);
807
808int
809is_rgba (CFClient::Layout self)
810 CODE:
811 RETVAL = self->rgba;
812 OUTPUT:
813 RETVAL
814 808
815void 809void
816set_text (CFClient::Layout self, SV *text_) 810set_text (CFClient::Layout self, SV *text_)
817 CODE: 811 CODE:
818{ 812{
890 if (run && shape_attr_p (run)) 884 if (run && shape_attr_p (run))
891 { 885 {
892 PangoRectangle extents; 886 PangoRectangle extents;
893 pango_layout_iter_get_run_extents (iter, 0, &extents); 887 pango_layout_iter_get_run_extents (iter, 0, &extents);
894 888
889 EXTEND (SP, 2);
895 PUSHs (sv_2mortal (newSViv (PANGO_PIXELS (extents.x)))); 890 PUSHs (sv_2mortal (newSViv (PANGO_PIXELS (extents.x))));
896 PUSHs (sv_2mortal (newSViv (PANGO_PIXELS (extents.y)))); 891 PUSHs (sv_2mortal (newSViv (PANGO_PIXELS (extents.y))));
897 } 892 }
898 } 893 }
899 while (pango_layout_iter_next_run (iter)); 894 while (pango_layout_iter_next_run (iter));
1028 PUSHs (sv_2mortal (newSViv (strong_pos.y / PANGO_SCALE))); 1023 PUSHs (sv_2mortal (newSViv (strong_pos.y / PANGO_SCALE)));
1029 PUSHs (sv_2mortal (newSViv (strong_pos.height / PANGO_SCALE))); 1024 PUSHs (sv_2mortal (newSViv (strong_pos.height / PANGO_SCALE)));
1030} 1025}
1031 1026
1032void 1027void
1033render (CFClient::Layout self) 1028render (CFClient::Layout self, float x, float y)
1034 PPCODE: 1029 PPCODE:
1035{ 1030 pango_opengl_render_layout_subpixel (
1036 SV *retval; 1031 self->pl,
1037 int w, h; 1032 x * PANGO_SCALE, y * PANGO_SCALE,
1038 1033 self->r, self->g, self->b, self->a
1039 layout_get_pixel_size (self, &w, &h);
1040
1041 if (self->rgba)
1042 { 1034 );
1043 cairo_surface_t *surface;
1044 cairo_t *cairo;
1045
1046 retval = newSV (w * h * 4);
1047 SvPOK_only (retval);
1048 SvCUR_set (retval, w * h * 4);
1049
1050 memset (SvPVX (retval), 0, w * h * 4);
1051
1052 surface = cairo_image_surface_create_for_data (
1053 (void*)SvPVX (retval), CAIRO_FORMAT_ARGB32, w, h, w * 4);
1054 cairo = cairo_create (surface);
1055 cairo_set_source_rgba (cairo, self->r, self->g, self->b, self->a);
1056
1057 pango_cairo_show_layout (cairo, self->pl);
1058
1059 cairo_destroy (cairo);
1060 cairo_surface_destroy (surface);
1061
1062 // what a mess, and its premultiplied, too :(
1063 {
1064 uint32_t *p = (uint32_t *)SvPVX (retval);
1065 uint32_t *e = p + w * h;
1066
1067 while (p < e)
1068 {
1069 uint32_t rgba = *p;
1070 rgba = (rgba >> 24) | (rgba << 8);
1071#if 0
1072#ifdef _WIN32
1073 {//D
1074 uint8_t r = rgba >> 24;
1075 uint8_t g = rgba >> 16;
1076 uint8_t b = rgba >> 8;
1077 uint8_t a = rgba >> 0;
1078
1079 rgba = (rgba & 0xffffff00) | a;
1080 }
1081#endif
1082#endif
1083 rgba = SDL_SwapBE32 (rgba);
1084 *p++ = rgba;
1085 }
1086 }
1087
1088 EXTEND (SP, 5);
1089 PUSHs (sv_2mortal (newSViv (w)));
1090 PUSHs (sv_2mortal (newSViv (h)));
1091 PUSHs (sv_2mortal (retval));
1092 PUSHs (sv_2mortal (newSViv (GL_RGBA)));
1093 PUSHs (sv_2mortal (newSViv (GL_RGBA)));
1094 }
1095 else
1096 {
1097 FT_Bitmap bitmap;
1098
1099 retval = newSV (w * h);
1100 SvPOK_only (retval);
1101 SvCUR_set (retval, w * h);
1102
1103 bitmap.rows = h;
1104 bitmap.width = w;
1105 bitmap.pitch = w;
1106 bitmap.buffer = (unsigned char*)SvPVX (retval);
1107 bitmap.num_grays = 256;
1108 bitmap.pixel_mode = FT_PIXEL_MODE_GRAY;
1109
1110 memset (bitmap.buffer, 0, w * h);
1111
1112 pango_ft2_render_layout (&bitmap, self->pl, 0 * PANGO_SCALE, 0 * PANGO_SCALE);
1113
1114 EXTEND (SP, 5);
1115 PUSHs (sv_2mortal (newSViv (w)));
1116 PUSHs (sv_2mortal (newSViv (h)));
1117 PUSHs (sv_2mortal (retval));
1118 PUSHs (sv_2mortal (newSViv (GL_ALPHA)));
1119 PUSHs (sv_2mortal (newSViv (GL_ALPHA)));
1120 }
1121}
1122 1035
1123MODULE = CFClient PACKAGE = CFClient::Texture 1036MODULE = CFClient PACKAGE = CFClient::Texture
1124 1037
1125void 1038void
1126pad2pot (SV *data_, SV *w_, SV *h_) 1039pad2pot (SV *data_, SV *w_, SV *h_)
1528void 1441void
1529draw_magicmap (CFClient::Map self, int dx, int dy, int w, int h, unsigned char *data) 1442draw_magicmap (CFClient::Map self, int dx, int dy, int w, int h, unsigned char *data)
1530 CODE: 1443 CODE:
1531{ 1444{
1532 static float color[16][3] = { 1445 static float color[16][3] = {
1533 { 0.00, 0.00, 0.00 }, 1446 { 0.00F, 0.00F, 0.00F },
1534 { 1.00, 1.00, 1.00 }, 1447 { 1.00F, 1.00F, 1.00F },
1535 { 0.00, 0.00, 0.55 }, 1448 { 0.00F, 0.00F, 0.55F },
1536 { 1.00, 0.00, 0.00 }, 1449 { 1.00F, 0.00F, 0.00F },
1537 1450
1538 { 1.00, 0.54, 0.00 }, 1451 { 1.00F, 0.54F, 0.00F },
1539 { 0.11, 0.56, 1.00 }, 1452 { 0.11F, 0.56F, 1.00F },
1540 { 0.93, 0.46, 0.00 }, 1453 { 0.93F, 0.46F, 0.00F },
1541 { 0.18, 0.54, 0.34 }, 1454 { 0.18F, 0.54F, 0.34F },
1542 1455
1543 { 0.56, 0.73, 0.56 }, 1456 { 0.56F, 0.73F, 0.56F },
1544 { 0.80, 0.80, 0.80 }, 1457 { 0.80F, 0.80F, 0.80F },
1545 { 0.55, 0.41, 0.13 }, 1458 { 0.55F, 0.41F, 0.13F },
1546 { 0.99, 0.77, 0.26 }, 1459 { 0.99F, 0.77F, 0.26F },
1547 1460
1548 { 0.74, 0.65, 0.41 }, 1461 { 0.74F, 0.65F, 0.41F },
1549 1462
1550 { 0.00, 1.00, 1.00 }, 1463 { 0.00F, 1.00F, 1.00F },
1551 { 1.00, 0.00, 1.00 }, 1464 { 1.00F, 0.00F, 1.00F },
1552 { 1.00, 1.00, 0.00 }, 1465 { 1.00F, 1.00F, 0.00F },
1553 }; 1466 };
1554 1467
1555 int x, y; 1468 int x, y;
1556 1469
1557 glEnable (GL_TEXTURE_2D); 1470 glEnable (GL_TEXTURE_2D);
2006 r *= a; 1919 r *= a;
2007 g *= a; 1920 g *= a;
2008 b *= a; 1921 b *= a;
2009 } 1922 }
2010 // microsoft visual "c" rounds instead of truncating... 1923 // microsoft visual "c" rounds instead of truncating...
2011 glColor4ub (MIN ((int)(r * 256.f), 255), 1924 glColor4f (r, g, b, a);
2012 MIN ((int)(g * 256.f), 255),
2013 MIN ((int)(b * 256.f), 255),
2014 MIN ((int)(a * 256.f), 255));
2015 1925
2016void glInterleavedArrays (int format, int stride, char *data) 1926void glInterleavedArrays (int format, int stride, char *data)
2017 1927
2018void glDrawElements (int mode, int count, int type, char *indices) 1928void glDrawElements (int mode, int count, int type, char *indices)
2019 1929

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines