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.121 by root, Fri Jun 23 20:28:19 2006 UTC vs.
Revision 1.125 by root, Tue Jul 4 23:44:23 2006 UTC

18#include <SDL_endian.h> 18#include <SDL_endian.h>
19#include <SDL_image.h> 19#include <SDL_image.h>
20#include <SDL_mixer.h> 20#include <SDL_mixer.h>
21#include <SDL_opengl.h> 21#include <SDL_opengl.h>
22 22
23#define PANGO_ENABLE_BACKEND
24#define G_DISABLE_CAST_CHECKS
25
23#include <glib/gmacros.h> 26#include <glib/gmacros.h>
24 27
25#include <pango/pango.h> 28#include <pango/pango.h>
26#include <pango/pangofc-fontmap.h>
27#include <pango/pangoft2.h>
28#include <pango/pangocairo.h>
29 29
30#ifndef _WIN32 30#ifndef _WIN32
31# include <sys/types.h> 31# include <sys/types.h>
32# include <sys/socket.h> 32# include <sys/socket.h>
33# include <netinet/in.h> 33# include <netinet/in.h>
51#define MAP_EXTEND_X 32 51#define MAP_EXTEND_X 32
52#define MAP_EXTEND_Y 512 52#define MAP_EXTEND_Y 512
53 53
54#define MIN_FONT_HEIGHT 10 54#define MIN_FONT_HEIGHT 10
55 55
56#if 1
57# define PARACHUTE SDL_INIT_NOPARACHUTE
58#else
59# define PARACHUTE 0
60#endif
61
56static struct 62static struct
57{ 63{
58#define GL_FUNC(ptr,name) ptr name; 64#define GL_FUNC(ptr,name) ptr name;
59#include "glfunc.h" 65#include "glfunc.h"
60#undef GL_FUNC 66#undef GL_FUNC
68 gl.BlendFuncSeparateEXT (sa, da, saa, daa); 74 gl.BlendFuncSeparateEXT (sa, da, saa, daa);
69 else 75 else
70 glBlendFunc (sa, da); 76 glBlendFunc (sa, da);
71} 77}
72 78
79#include "texcache.c"
80
81#include "pango-font.c"
82#include "pango-fontmap.c"
83#include "pango-render.c"
84
73typedef Mix_Chunk *CFClient__MixChunk; 85typedef Mix_Chunk *CFClient__MixChunk;
74typedef Mix_Music *CFClient__MixMusic; 86typedef Mix_Music *CFClient__MixMusic;
75 87
76typedef PangoFontDescription *CFClient__Font; 88typedef PangoFontDescription *CFClient__Font;
77 89
92 104
93 return 0; 105 return 0;
94} 106}
95 107
96typedef struct cf_layout { 108typedef struct cf_layout {
97 PangoLayout *pl; // either derived from a cairo or ft2 context 109 PangoLayout *pl;
98 int rgba; // wether we use rgba (cairo) or grayscale (ft2)
99 float r, g, b, a; // default color for rgba mode 110 float r, g, b, a; // default color for rgba mode
100 int base_height; 111 int base_height;
101 CFClient__Font font; 112 CFClient__Font font;
102} *CFClient__Layout; 113} *CFClient__Layout;
103 114
104static CFClient__Font default_font; 115static CFClient__Font default_font;
105static PangoContext *ft2_context, *cairo_context; 116static PangoContext *opengl_context;
106static PangoFontMap *ft2_fontmap, *cairo_fontmap; 117static PangoFontMap *opengl_fontmap;
107 118
108static void 119static void
109substitute_func (FcPattern *pattern, gpointer data) 120substitute_func (FcPattern *pattern, gpointer data)
110{ 121{
111 FcPatternAddBool (pattern, FC_HINTING, 1); 122 FcPatternAddBool (pattern, FC_HINTING, 1);
112#ifdef FC_HINT_STYLE 123#ifdef FC_HINT_STYLE
113 FcPatternAddBool (pattern, FC_HINT_STYLE, FC_HINT_FULL); 124 FcPatternAddBool (pattern, FC_HINT_STYLE, FC_HINT_FULL);
114#endif 125#endif
115#ifdef _WIN32
116 FcPatternAddBool (pattern, FC_AUTOHINT, 1);
117#else
118 FcPatternAddBool (pattern, FC_AUTOHINT, 0); 126 FcPatternAddBool (pattern, FC_AUTOHINT, 0);
119#endif
120} 127}
121 128
122static void 129static void
123layout_update_font (CFClient__Layout self) 130layout_update_font (CFClient__Layout self)
124{ 131{
463NV ceil (NV x) 470NV ceil (NV x)
464 471
465void 472void
466pango_init () 473pango_init ()
467 CODE: 474 CODE:
468 // delayed, so it can pick up new fonts added by AddFontResourceEx
469{ 475{
470 {
471 ft2_fontmap = pango_ft2_font_map_new (); 476 opengl_fontmap = pango_opengl_font_map_new ();
472 pango_ft2_font_map_set_default_substitute ((PangoFT2FontMap *)ft2_fontmap, substitute_func, 0, 0); 477 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); 478 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} 479}
491 480
492int 481int
493SDL_Init (U32 flags = SDL_INIT_VIDEO | SDL_INIT_AUDIO) 482SDL_Init (U32 flags = SDL_INIT_VIDEO | SDL_INIT_AUDIO | PARACHUTE)
494 483
495void 484void
496SDL_Quit () 485SDL_Quit ()
497 486
498void 487void
636 625
637void 626void
638add_font (char *file) 627add_font (char *file)
639 CODE: 628 CODE:
640 FcConfigAppFontAddFile (0, (const FcChar8 *)file); /* no idea wether this is required */ 629 FcConfigAppFontAddFile (0, (const FcChar8 *)file); /* no idea wether this is required */
641#ifdef _WIN32
642 // cairo... sigh... requires win2000
643 AddFontResourceEx (file, FR_PRIVATE, 0);
644#endif
645 630
646void 631void
647load_image_inline (SV *image_) 632load_image_inline (SV *image_)
648 ALIAS: 633 ALIAS:
649 load_image_file = 1 634 load_image_file = 1
778 CODE: 763 CODE:
779 default_font = self; 764 default_font = self;
780 765
781MODULE = CFClient PACKAGE = CFClient::Layout 766MODULE = CFClient PACKAGE = CFClient::Layout
782 767
768void
769clear_font_cache ()
770 CODE:
771 pango_fc_font_map_cache_clear ((PangoFcFontMap *)opengl_fontmap);
772 tc_clear ();
773
783CFClient::Layout 774CFClient::Layout
784new (SV *class, int rgba = 0) 775new (SV *class, int type = 0)
785 CODE: 776 CODE:
786 New (0, RETVAL, 1, struct cf_layout); 777 New (0, RETVAL, 1, struct cf_layout);
787 778
788 RETVAL->pl = pango_layout_new (rgba ? cairo_context : ft2_context); 779 RETVAL->pl = pango_layout_new (opengl_context);
789 RETVAL->rgba = rgba;
790 RETVAL->r = 1.; 780 RETVAL->r = 1.;
791 RETVAL->g = 1.; 781 RETVAL->g = 1.;
792 RETVAL->b = 1.; 782 RETVAL->b = 1.;
793 RETVAL->a = 1.; 783 RETVAL->a = 1.;
794 RETVAL->base_height = MIN_FONT_HEIGHT; 784 RETVAL->base_height = MIN_FONT_HEIGHT;
803DESTROY (CFClient::Layout self) 793DESTROY (CFClient::Layout self)
804 CODE: 794 CODE:
805 g_object_unref (self->pl); 795 g_object_unref (self->pl);
806 Safefree (self); 796 Safefree (self);
807 797
808int
809is_rgba (CFClient::Layout self)
810 CODE:
811 RETVAL = self->rgba;
812 OUTPUT:
813 RETVAL
814
815void 798void
816set_text (CFClient::Layout self, SV *text_) 799set_text (CFClient::Layout self, SV *text_)
817 CODE: 800 CODE:
818{ 801{
819 STRLEN textlen; 802 STRLEN textlen;
837 CODE: 820 CODE:
838{ 821{
839 PangoAttrList *attrs = 0; 822 PangoAttrList *attrs = 0;
840 const char *text = pango_layout_get_text (self->pl); 823 const char *text = pango_layout_get_text (self->pl);
841 const char *pos = text; 824 const char *pos = text;
842 int arg = 2; 825 int arg = 4;
843 826
844 while (arg < items && (pos = strstr (pos, OBJ_STR))) 827 while (arg < items && (pos = strstr (pos, OBJ_STR)))
845 { 828 {
846 PangoRectangle rect; 829 PangoRectangle inkrect, rect;
847 PangoAttribute *attr; 830 PangoAttribute *attr;
848 831
832 int x = SvIV (ST (arg - 3));
833 int y = SvIV (ST (arg - 2));
849 int w = SvIV (ST (arg - 1)); 834 int w = SvIV (ST (arg - 1));
850 int h = SvIV (ST (arg)); 835 int h = SvIV (ST (arg ));
851 836
852 arg += 2;
853
854 rect.x = 0; 837 inkrect.x = 0;
838 inkrect.y = 0;
839 inkrect.width = 0;
840 inkrect.height = 0;
841
842 rect.x = x * PANGO_SCALE;
855 rect.y = -h * PANGO_SCALE; 843 rect.y = y * PANGO_SCALE;
856 rect.width = w * PANGO_SCALE; 844 rect.width = w * PANGO_SCALE;
857 rect.height = h * PANGO_SCALE; 845 rect.height = h * PANGO_SCALE;
858 846
859 if (!attrs) 847 if (!attrs)
860 attrs = pango_layout_get_attributes (self->pl); 848 attrs = pango_layout_get_attributes (self->pl);
861 849
862 attr = pango_attr_shape_new (&rect, &rect); 850 attr = pango_attr_shape_new (&inkrect, &rect);
863 attr->start_index = pos - text; 851 attr->start_index = pos - text;
864 attr->end_index = attr->start_index + sizeof (OBJ_STR) - 1; 852 attr->end_index = attr->start_index + sizeof (OBJ_STR) - 1;
865 pango_attr_list_insert (attrs, attr); 853 pango_attr_list_insert (attrs, attr);
866 854
855 arg += 4;
867 pos += sizeof (OBJ_STR) - 1; 856 pos += sizeof (OBJ_STR) - 1;
868 } 857 }
869 858
870 if (attrs) 859 if (attrs)
871 pango_layout_set_attributes (self->pl, attrs); 860 pango_layout_set_attributes (self->pl, attrs);
986 PUSHs (sv_2mortal (newSViv (w))); 975 PUSHs (sv_2mortal (newSViv (w)));
987 PUSHs (sv_2mortal (newSViv (h))); 976 PUSHs (sv_2mortal (newSViv (h)));
988} 977}
989 978
990int 979int
980descent (CFClient::Layout self)
981 CODE:
982{
983 PangoRectangle rect;
984 PangoLayoutLine *line = pango_layout_get_line (self->pl, 0);
985 pango_layout_line_get_pixel_extents (line, 0, &rect);
986 RETVAL = PANGO_DESCENT (rect);
987}
988 OUTPUT:
989 RETVAL
990
991int
991xy_to_index (CFClient::Layout self, int x, int y) 992xy_to_index (CFClient::Layout self, int x, int y)
992 CODE: 993 CODE:
993{ 994{
994 int index, trailing; 995 int index, trailing;
995 pango_layout_xy_to_index (self->pl, x * PANGO_SCALE, y * PANGO_SCALE, &index, &trailing); 996 pango_layout_xy_to_index (self->pl, x * PANGO_SCALE, y * PANGO_SCALE, &index, &trailing);
1010 PUSHs (sv_2mortal (newSViv (strong_pos.y / PANGO_SCALE))); 1011 PUSHs (sv_2mortal (newSViv (strong_pos.y / PANGO_SCALE)));
1011 PUSHs (sv_2mortal (newSViv (strong_pos.height / PANGO_SCALE))); 1012 PUSHs (sv_2mortal (newSViv (strong_pos.height / PANGO_SCALE)));
1012} 1013}
1013 1014
1014void 1015void
1015render (CFClient::Layout self) 1016render (CFClient::Layout self, float x, float y)
1016 PPCODE: 1017 PPCODE:
1017{ 1018 pango_opengl_render_layout_subpixel (
1018 SV *retval; 1019 self->pl,
1019 int w, h; 1020 x * PANGO_SCALE, y * PANGO_SCALE,
1020 1021 self->r, self->g, self->b, self->a
1021 layout_get_pixel_size (self, &w, &h);
1022
1023 if (self->rgba)
1024 { 1022 );
1025 cairo_surface_t *surface;
1026 cairo_t *cairo;
1027
1028 retval = newSV (w * h * 4);
1029 SvPOK_only (retval);
1030 SvCUR_set (retval, w * h * 4);
1031
1032 memset (SvPVX (retval), 0, w * h * 4);
1033
1034 surface = cairo_image_surface_create_for_data (
1035 (void*)SvPVX (retval), CAIRO_FORMAT_ARGB32, w, h, w * 4);
1036 cairo = cairo_create (surface);
1037 cairo_set_source_rgba (cairo, self->r, self->g, self->b, self->a);
1038
1039 pango_cairo_show_layout (cairo, self->pl);
1040
1041 cairo_destroy (cairo);
1042 cairo_surface_destroy (surface);
1043
1044 // what a mess, and its premultiplied, too :(
1045 {
1046 uint32_t *p = (uint32_t *)SvPVX (retval);
1047 uint32_t *e = p + w * h;
1048
1049 while (p < e)
1050 {
1051 uint32_t rgba = *p;
1052 rgba = (rgba >> 24) | (rgba << 8);
1053#if 0
1054#ifdef _WIN32
1055 {//D
1056 uint8_t r = rgba >> 24;
1057 uint8_t g = rgba >> 16;
1058 uint8_t b = rgba >> 8;
1059 uint8_t a = rgba >> 0;
1060
1061 rgba = (rgba & 0xffffff00) | a;
1062 }
1063#endif
1064#endif
1065 rgba = SDL_SwapBE32 (rgba);
1066 *p++ = rgba;
1067 }
1068 }
1069
1070 EXTEND (SP, 5);
1071 PUSHs (sv_2mortal (newSViv (w)));
1072 PUSHs (sv_2mortal (newSViv (h)));
1073 PUSHs (sv_2mortal (retval));
1074 PUSHs (sv_2mortal (newSViv (GL_RGBA)));
1075 PUSHs (sv_2mortal (newSViv (GL_RGBA)));
1076 }
1077 else
1078 {
1079 FT_Bitmap bitmap;
1080
1081 retval = newSV (w * h);
1082 SvPOK_only (retval);
1083 SvCUR_set (retval, w * h);
1084
1085 bitmap.rows = h;
1086 bitmap.width = w;
1087 bitmap.pitch = w;
1088 bitmap.buffer = (unsigned char*)SvPVX (retval);
1089 bitmap.num_grays = 256;
1090 bitmap.pixel_mode = FT_PIXEL_MODE_GRAY;
1091
1092 memset (bitmap.buffer, 0, w * h);
1093
1094 pango_ft2_render_layout (&bitmap, self->pl, 0 * PANGO_SCALE, 0 * PANGO_SCALE);
1095
1096 EXTEND (SP, 5);
1097 PUSHs (sv_2mortal (newSViv (w)));
1098 PUSHs (sv_2mortal (newSViv (h)));
1099 PUSHs (sv_2mortal (retval));
1100 PUSHs (sv_2mortal (newSViv (GL_ALPHA)));
1101 PUSHs (sv_2mortal (newSViv (GL_ALPHA)));
1102 }
1103}
1104 1023
1105MODULE = CFClient PACKAGE = CFClient::Texture 1024MODULE = CFClient PACKAGE = CFClient::Texture
1106 1025
1107void 1026void
1108pad2pot (SV *data_, SV *w_, SV *h_) 1027pad2pot (SV *data_, SV *w_, SV *h_)
1510void 1429void
1511draw_magicmap (CFClient::Map self, int dx, int dy, int w, int h, unsigned char *data) 1430draw_magicmap (CFClient::Map self, int dx, int dy, int w, int h, unsigned char *data)
1512 CODE: 1431 CODE:
1513{ 1432{
1514 static float color[16][3] = { 1433 static float color[16][3] = {
1515 { 0.00, 0.00, 0.00 }, 1434 { 0.00F, 0.00F, 0.00F },
1516 { 1.00, 1.00, 1.00 }, 1435 { 1.00F, 1.00F, 1.00F },
1517 { 0.00, 0.00, 0.55 }, 1436 { 0.00F, 0.00F, 0.55F },
1518 { 1.00, 0.00, 0.00 }, 1437 { 1.00F, 0.00F, 0.00F },
1519 1438
1520 { 1.00, 0.54, 0.00 }, 1439 { 1.00F, 0.54F, 0.00F },
1521 { 0.11, 0.56, 1.00 }, 1440 { 0.11F, 0.56F, 1.00F },
1522 { 0.93, 0.46, 0.00 }, 1441 { 0.93F, 0.46F, 0.00F },
1523 { 0.18, 0.54, 0.34 }, 1442 { 0.18F, 0.54F, 0.34F },
1524 1443
1525 { 0.56, 0.73, 0.56 }, 1444 { 0.56F, 0.73F, 0.56F },
1526 { 0.80, 0.80, 0.80 }, 1445 { 0.80F, 0.80F, 0.80F },
1527 { 0.55, 0.41, 0.13 }, 1446 { 0.55F, 0.41F, 0.13F },
1528 { 0.99, 0.77, 0.26 }, 1447 { 0.99F, 0.77F, 0.26F },
1529 1448
1530 { 0.74, 0.65, 0.41 }, 1449 { 0.74F, 0.65F, 0.41F },
1531 1450
1532 { 0.00, 1.00, 1.00 }, 1451 { 0.00F, 1.00F, 1.00F },
1533 { 1.00, 0.00, 1.00 }, 1452 { 1.00F, 0.00F, 1.00F },
1534 { 1.00, 1.00, 0.00 }, 1453 { 1.00F, 1.00F, 0.00F },
1535 }; 1454 };
1536 1455
1537 int x, y; 1456 int x, y;
1538 1457
1539 glEnable (GL_TEXTURE_2D); 1458 glEnable (GL_TEXTURE_2D);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines