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.130 by root, Wed Jul 12 17:21:01 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 0
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{
456 463
457 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) 464 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; )
458 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); 465 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv));
459} 466}
460 467
468int
469in_destruct ()
470 CODE:
471 RETVAL = PL_main_cv == Nullcv;
472 OUTPUT:
473 RETVAL
474
461NV floor (NV x) 475NV floor (NV x)
462 476
463NV ceil (NV x) 477NV ceil (NV x)
464 478
465void 479void
466pango_init () 480pango_init ()
467 CODE: 481 CODE:
468 // delayed, so it can pick up new fonts added by AddFontResourceEx
469{ 482{
470 {
471 ft2_fontmap = pango_ft2_font_map_new (); 483 opengl_fontmap = pango_opengl_font_map_new ();
472 pango_ft2_font_map_set_default_substitute ((PangoFT2FontMap *)ft2_fontmap, substitute_func, 0, 0); 484 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); 485 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} 486}
491 487
492int 488int
493SDL_Init (U32 flags = SDL_INIT_VIDEO | SDL_INIT_AUDIO) 489SDL_Init (U32 flags = SDL_INIT_VIDEO | SDL_INIT_AUDIO | PARACHUTE)
494 490
495void 491void
496SDL_Quit () 492SDL_Quit ()
497 493
498void 494void
635#endif 631#endif
636 632
637void 633void
638add_font (char *file) 634add_font (char *file)
639 CODE: 635 CODE:
640 FcConfigAppFontAddFile (0, (const FcChar8 *)file); /* no idea wether this is required */ 636 FcConfigAppFontAddFile (0, (const FcChar8 *)file);
641#ifdef _WIN32
642 // cairo... sigh... requires win2000
643 AddFontResourceEx (file, FR_PRIVATE, 0);
644#endif
645 637
646void 638void
647load_image_inline (SV *image_) 639load_image_inline (SV *image_)
648 ALIAS: 640 ALIAS:
649 load_image_file = 1 641 load_image_file = 1
691 683
692 surface2 = SDL_ConvertSurface (surface, &fmt, SDL_SWSURFACE); 684 surface2 = SDL_ConvertSurface (surface, &fmt, SDL_SWSURFACE);
693 685
694 assert (surface2->pitch == surface2->w * 4); 686 assert (surface2->pitch == surface2->w * 4);
695 687
688 SDL_LockSurface (surface2);
696 EXTEND (SP, 5); 689 EXTEND (SP, 6);
697 PUSHs (sv_2mortal (newSViv (surface2->w))); 690 PUSHs (sv_2mortal (newSViv (surface2->w)));
698 PUSHs (sv_2mortal (newSViv (surface2->h))); 691 PUSHs (sv_2mortal (newSViv (surface2->h)));
699 SDL_LockSurface (surface2);
700 PUSHs (sv_2mortal (newSVpvn (surface2->pixels, surface2->h * surface2->pitch))); 692 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))); 693 PUSHs (sv_2mortal (newSViv (surface->flags & (SDL_SRCCOLORKEY | SDL_SRCALPHA) ? GL_RGBA : GL_RGB)));
703 PUSHs (sv_2mortal (newSViv (GL_RGBA))); 694 PUSHs (sv_2mortal (newSViv (GL_RGBA)));
704 PUSHs (sv_2mortal (newSViv (GL_UNSIGNED_BYTE))); 695 PUSHs (sv_2mortal (newSViv (GL_UNSIGNED_BYTE)));
696 SDL_UnlockSurface (surface2);
705 697
706 SDL_FreeSurface (surface); 698 SDL_FreeSurface (surface);
707 SDL_FreeSurface (surface2); 699 SDL_FreeSurface (surface2);
708} 700}
709 701
778 CODE: 770 CODE:
779 default_font = self; 771 default_font = self;
780 772
781MODULE = CFClient PACKAGE = CFClient::Layout 773MODULE = CFClient PACKAGE = CFClient::Layout
782 774
775void
776reset_glyph_cache ()
777 CODE:
778 tc_clear ();
779
783CFClient::Layout 780CFClient::Layout
784new (SV *class, int rgba = 0) 781new (SV *class)
785 CODE: 782 CODE:
786 New (0, RETVAL, 1, struct cf_layout); 783 New (0, RETVAL, 1, struct cf_layout);
787 784
788 RETVAL->pl = pango_layout_new (rgba ? cairo_context : ft2_context); 785 RETVAL->pl = pango_layout_new (opengl_context);
789 RETVAL->rgba = rgba;
790 RETVAL->r = 1.; 786 RETVAL->r = 1.;
791 RETVAL->g = 1.; 787 RETVAL->g = 1.;
792 RETVAL->b = 1.; 788 RETVAL->b = 1.;
793 RETVAL->a = 1.; 789 RETVAL->a = 1.;
794 RETVAL->base_height = MIN_FONT_HEIGHT; 790 RETVAL->base_height = MIN_FONT_HEIGHT;
802void 798void
803DESTROY (CFClient::Layout self) 799DESTROY (CFClient::Layout self)
804 CODE: 800 CODE:
805 g_object_unref (self->pl); 801 g_object_unref (self->pl);
806 Safefree (self); 802 Safefree (self);
807
808int
809is_rgba (CFClient::Layout self)
810 CODE:
811 RETVAL = self->rgba;
812 OUTPUT:
813 RETVAL
814 803
815void 804void
816set_text (CFClient::Layout self, SV *text_) 805set_text (CFClient::Layout self, SV *text_)
817 CODE: 806 CODE:
818{ 807{
890 if (run && shape_attr_p (run)) 879 if (run && shape_attr_p (run))
891 { 880 {
892 PangoRectangle extents; 881 PangoRectangle extents;
893 pango_layout_iter_get_run_extents (iter, 0, &extents); 882 pango_layout_iter_get_run_extents (iter, 0, &extents);
894 883
884 EXTEND (SP, 2);
895 PUSHs (sv_2mortal (newSViv (PANGO_PIXELS (extents.x)))); 885 PUSHs (sv_2mortal (newSViv (PANGO_PIXELS (extents.x))));
896 PUSHs (sv_2mortal (newSViv (PANGO_PIXELS (extents.y)))); 886 PUSHs (sv_2mortal (newSViv (PANGO_PIXELS (extents.y))));
897 } 887 }
898 } 888 }
899 while (pango_layout_iter_next_run (iter)); 889 while (pango_layout_iter_next_run (iter));
1028 PUSHs (sv_2mortal (newSViv (strong_pos.y / PANGO_SCALE))); 1018 PUSHs (sv_2mortal (newSViv (strong_pos.y / PANGO_SCALE)));
1029 PUSHs (sv_2mortal (newSViv (strong_pos.height / PANGO_SCALE))); 1019 PUSHs (sv_2mortal (newSViv (strong_pos.height / PANGO_SCALE)));
1030} 1020}
1031 1021
1032void 1022void
1033render (CFClient::Layout self) 1023render (CFClient::Layout self, float x, float y)
1034 PPCODE: 1024 PPCODE:
1035{ 1025 pango_opengl_render_layout_subpixel (
1036 SV *retval; 1026 self->pl,
1037 int w, h; 1027 x * PANGO_SCALE, y * PANGO_SCALE,
1038 1028 self->r, self->g, self->b, self->a
1039 layout_get_pixel_size (self, &w, &h);
1040
1041 if (self->rgba)
1042 { 1029 );
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 1030
1123MODULE = CFClient PACKAGE = CFClient::Texture 1031MODULE = CFClient PACKAGE = CFClient::Texture
1124 1032
1125void 1033void
1126pad2pot (SV *data_, SV *w_, SV *h_) 1034pad2pot (SV *data_, SV *w_, SV *h_)
2006 r *= a; 1914 r *= a;
2007 g *= a; 1915 g *= a;
2008 b *= a; 1916 b *= a;
2009 } 1917 }
2010 // microsoft visual "c" rounds instead of truncating... 1918 // microsoft visual "c" rounds instead of truncating...
2011 glColor4ub (MIN ((int)(r * 256.f), 255), 1919 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 1920
2016void glInterleavedArrays (int format, int stride, char *data) 1921void glInterleavedArrays (int format, int stride, char *data)
2017 1922
2018void glDrawElements (int mode, int count, int type, char *indices) 1923void glDrawElements (int mode, int count, int type, char *indices)
2019 1924

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines