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.123 by root, Wed Jun 28 23:41:11 2006 UTC vs.
Revision 1.124 by root, Tue Jul 4 23:23:31 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);
463NV ceil (NV x) 474NV ceil (NV x)
464 475
465void 476void
466pango_init () 477pango_init ()
467 CODE: 478 CODE:
468 // delayed, so it can pick up new fonts added by AddFontResourceEx
469{ 479{
470 {
471 ft2_fontmap = pango_ft2_font_map_new (); 480 opengl_fontmap = pango_opengl_font_map_new ();
472 pango_ft2_font_map_set_default_substitute ((PangoFT2FontMap *)ft2_fontmap, substitute_func, 0, 0); 481 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); 482 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} 483}
491 484
492int 485int
493SDL_Init (U32 flags = SDL_INIT_VIDEO | SDL_INIT_AUDIO) 486SDL_Init (U32 flags = SDL_INIT_VIDEO | SDL_INIT_AUDIO | PARACHUTE)
494 487
495void 488void
496SDL_Quit () 489SDL_Quit ()
497 490
498void 491void
636 629
637void 630void
638add_font (char *file) 631add_font (char *file)
639 CODE: 632 CODE:
640 FcConfigAppFontAddFile (0, (const FcChar8 *)file); /* no idea wether this is required */ 633 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 634
646void 635void
647load_image_inline (SV *image_) 636load_image_inline (SV *image_)
648 ALIAS: 637 ALIAS:
649 load_image_file = 1 638 load_image_file = 1
778 CODE: 767 CODE:
779 default_font = self; 768 default_font = self;
780 769
781MODULE = CFClient PACKAGE = CFClient::Layout 770MODULE = CFClient PACKAGE = CFClient::Layout
782 771
772void
773clear_font_cache ()
774 CODE:
775 pango_fc_font_map_cache_clear ((PangoFcFontMap *)opengl_fontmap);
776 tc_clear ();
777
783CFClient::Layout 778CFClient::Layout
784new (SV *class, int rgba = 0) 779new (SV *class, int type = 0)
785 CODE: 780 CODE:
786 New (0, RETVAL, 1, struct cf_layout); 781 New (0, RETVAL, 1, struct cf_layout);
787 782
788 RETVAL->pl = pango_layout_new (rgba ? cairo_context : ft2_context); 783 RETVAL->pl = pango_layout_new (opengl_context);
789 RETVAL->rgba = rgba;
790 RETVAL->r = 1.; 784 RETVAL->r = 1.;
791 RETVAL->g = 1.; 785 RETVAL->g = 1.;
792 RETVAL->b = 1.; 786 RETVAL->b = 1.;
793 RETVAL->a = 1.; 787 RETVAL->a = 1.;
794 RETVAL->base_height = MIN_FONT_HEIGHT; 788 RETVAL->base_height = MIN_FONT_HEIGHT;
802void 796void
803DESTROY (CFClient::Layout self) 797DESTROY (CFClient::Layout self)
804 CODE: 798 CODE:
805 g_object_unref (self->pl); 799 g_object_unref (self->pl);
806 Safefree (self); 800 Safefree (self);
807
808int
809is_rgba (CFClient::Layout self)
810 CODE:
811 RETVAL = self->rgba;
812 OUTPUT:
813 RETVAL
814 801
815void 802void
816set_text (CFClient::Layout self, SV *text_) 803set_text (CFClient::Layout self, SV *text_)
817 CODE: 804 CODE:
818{ 805{
1028 PUSHs (sv_2mortal (newSViv (strong_pos.y / PANGO_SCALE))); 1015 PUSHs (sv_2mortal (newSViv (strong_pos.y / PANGO_SCALE)));
1029 PUSHs (sv_2mortal (newSViv (strong_pos.height / PANGO_SCALE))); 1016 PUSHs (sv_2mortal (newSViv (strong_pos.height / PANGO_SCALE)));
1030} 1017}
1031 1018
1032void 1019void
1033render (CFClient::Layout self) 1020render (CFClient::Layout self, float x, float y)
1034 PPCODE: 1021 PPCODE:
1035{ 1022 pango_opengl_render_layout_subpixel (
1036 SV *retval; 1023 self->pl,
1037 int w, h; 1024 x * PANGO_SCALE, y * PANGO_SCALE,
1038 1025 self->r, self->g, self->b, self->a
1039 layout_get_pixel_size (self, &w, &h);
1040
1041 if (self->rgba)
1042 { 1026 );
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 1027
1123MODULE = CFClient PACKAGE = CFClient::Texture 1028MODULE = CFClient PACKAGE = CFClient::Texture
1124 1029
1125void 1030void
1126pad2pot (SV *data_, SV *w_, SV *h_) 1031pad2pot (SV *data_, SV *w_, SV *h_)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines