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.129 by root, Wed Jul 12 17:20:04 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;
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 803
808int
809is_rgba (CFClient::Layout self)
810 CODE:
811 RETVAL = self->rgba;
812 OUTPUT:
813 RETVAL
814
815void 804void
816set_text (CFClient::Layout self, SV *text_) 805set_text (CFClient::Layout self, SV *text_)
817 CODE: 806 CODE:
818{ 807{
819 STRLEN textlen; 808 STRLEN textlen;
837 CODE: 826 CODE:
838{ 827{
839 PangoAttrList *attrs = 0; 828 PangoAttrList *attrs = 0;
840 const char *text = pango_layout_get_text (self->pl); 829 const char *text = pango_layout_get_text (self->pl);
841 const char *pos = text; 830 const char *pos = text;
842 int arg = 2; 831 int arg = 4;
843 832
844 while (arg < items && (pos = strstr (pos, OBJ_STR))) 833 while (arg < items && (pos = strstr (pos, OBJ_STR)))
845 { 834 {
846 PangoRectangle rect; 835 PangoRectangle inkrect, rect;
847 PangoAttribute *attr; 836 PangoAttribute *attr;
848 837
838 int x = SvIV (ST (arg - 3));
839 int y = SvIV (ST (arg - 2));
849 int w = SvIV (ST (arg - 1)); 840 int w = SvIV (ST (arg - 1));
850 int h = SvIV (ST (arg)); 841 int h = SvIV (ST (arg ));
851 842
852 arg += 2;
853
854 rect.x = 0; 843 inkrect.x = 0;
844 inkrect.y = 0;
845 inkrect.width = 0;
846 inkrect.height = 0;
847
848 rect.x = x * PANGO_SCALE;
855 rect.y = -h * PANGO_SCALE; 849 rect.y = y * PANGO_SCALE;
856 rect.width = w * PANGO_SCALE; 850 rect.width = w * PANGO_SCALE;
857 rect.height = h * PANGO_SCALE; 851 rect.height = h * PANGO_SCALE;
858 852
859 if (!attrs) 853 if (!attrs)
860 attrs = pango_layout_get_attributes (self->pl); 854 attrs = pango_layout_get_attributes (self->pl);
861 855
862 attr = pango_attr_shape_new (&rect, &rect); 856 attr = pango_attr_shape_new (&inkrect, &rect);
863 attr->start_index = pos - text; 857 attr->start_index = pos - text;
864 attr->end_index = attr->start_index + sizeof (OBJ_STR) - 1; 858 attr->end_index = attr->start_index + sizeof (OBJ_STR) - 1;
865 pango_attr_list_insert (attrs, attr); 859 pango_attr_list_insert (attrs, attr);
866 860
861 arg += 4;
867 pos += sizeof (OBJ_STR) - 1; 862 pos += sizeof (OBJ_STR) - 1;
868 } 863 }
869 864
870 if (attrs) 865 if (attrs)
871 pango_layout_set_attributes (self->pl, attrs); 866 pango_layout_set_attributes (self->pl, attrs);
884 if (run && shape_attr_p (run)) 879 if (run && shape_attr_p (run))
885 { 880 {
886 PangoRectangle extents; 881 PangoRectangle extents;
887 pango_layout_iter_get_run_extents (iter, 0, &extents); 882 pango_layout_iter_get_run_extents (iter, 0, &extents);
888 883
884 EXTEND (SP, 2);
889 PUSHs (sv_2mortal (newSViv (PANGO_PIXELS (extents.x)))); 885 PUSHs (sv_2mortal (newSViv (PANGO_PIXELS (extents.x))));
890 PUSHs (sv_2mortal (newSViv (PANGO_PIXELS (extents.y)))); 886 PUSHs (sv_2mortal (newSViv (PANGO_PIXELS (extents.y))));
891 } 887 }
892 } 888 }
893 while (pango_layout_iter_next_run (iter)); 889 while (pango_layout_iter_next_run (iter));
986 PUSHs (sv_2mortal (newSViv (w))); 982 PUSHs (sv_2mortal (newSViv (w)));
987 PUSHs (sv_2mortal (newSViv (h))); 983 PUSHs (sv_2mortal (newSViv (h)));
988} 984}
989 985
990int 986int
987descent (CFClient::Layout self)
988 CODE:
989{
990 PangoRectangle rect;
991 PangoLayoutLine *line = pango_layout_get_line (self->pl, 0);
992 pango_layout_line_get_pixel_extents (line, 0, &rect);
993 RETVAL = PANGO_DESCENT (rect);
994}
995 OUTPUT:
996 RETVAL
997
998int
991xy_to_index (CFClient::Layout self, int x, int y) 999xy_to_index (CFClient::Layout self, int x, int y)
992 CODE: 1000 CODE:
993{ 1001{
994 int index, trailing; 1002 int index, trailing;
995 pango_layout_xy_to_index (self->pl, x * PANGO_SCALE, y * PANGO_SCALE, &index, &trailing); 1003 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))); 1018 PUSHs (sv_2mortal (newSViv (strong_pos.y / PANGO_SCALE)));
1011 PUSHs (sv_2mortal (newSViv (strong_pos.height / PANGO_SCALE))); 1019 PUSHs (sv_2mortal (newSViv (strong_pos.height / PANGO_SCALE)));
1012} 1020}
1013 1021
1014void 1022void
1015render (CFClient::Layout self) 1023render (CFClient::Layout self, float x, float y)
1016 PPCODE: 1024 PPCODE:
1017{ 1025 pango_opengl_render_layout_subpixel (
1018 SV *retval; 1026 self->pl,
1019 int w, h; 1027 x * PANGO_SCALE, y * PANGO_SCALE,
1020 1028 self->r, self->g, self->b, self->a
1021 layout_get_pixel_size (self, &w, &h);
1022
1023 if (self->rgba)
1024 { 1029 );
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 1030
1105MODULE = CFClient PACKAGE = CFClient::Texture 1031MODULE = CFClient PACKAGE = CFClient::Texture
1106 1032
1107void 1033void
1108pad2pot (SV *data_, SV *w_, SV *h_) 1034pad2pot (SV *data_, SV *w_, SV *h_)
1510void 1436void
1511draw_magicmap (CFClient::Map self, int dx, int dy, int w, int h, unsigned char *data) 1437draw_magicmap (CFClient::Map self, int dx, int dy, int w, int h, unsigned char *data)
1512 CODE: 1438 CODE:
1513{ 1439{
1514 static float color[16][3] = { 1440 static float color[16][3] = {
1515 { 0.00, 0.00, 0.00 }, 1441 { 0.00F, 0.00F, 0.00F },
1516 { 1.00, 1.00, 1.00 }, 1442 { 1.00F, 1.00F, 1.00F },
1517 { 0.00, 0.00, 0.55 }, 1443 { 0.00F, 0.00F, 0.55F },
1518 { 1.00, 0.00, 0.00 }, 1444 { 1.00F, 0.00F, 0.00F },
1519 1445
1520 { 1.00, 0.54, 0.00 }, 1446 { 1.00F, 0.54F, 0.00F },
1521 { 0.11, 0.56, 1.00 }, 1447 { 0.11F, 0.56F, 1.00F },
1522 { 0.93, 0.46, 0.00 }, 1448 { 0.93F, 0.46F, 0.00F },
1523 { 0.18, 0.54, 0.34 }, 1449 { 0.18F, 0.54F, 0.34F },
1524 1450
1525 { 0.56, 0.73, 0.56 }, 1451 { 0.56F, 0.73F, 0.56F },
1526 { 0.80, 0.80, 0.80 }, 1452 { 0.80F, 0.80F, 0.80F },
1527 { 0.55, 0.41, 0.13 }, 1453 { 0.55F, 0.41F, 0.13F },
1528 { 0.99, 0.77, 0.26 }, 1454 { 0.99F, 0.77F, 0.26F },
1529 1455
1530 { 0.74, 0.65, 0.41 }, 1456 { 0.74F, 0.65F, 0.41F },
1531 1457
1532 { 0.00, 1.00, 1.00 }, 1458 { 0.00F, 1.00F, 1.00F },
1533 { 1.00, 0.00, 1.00 }, 1459 { 1.00F, 0.00F, 1.00F },
1534 { 1.00, 1.00, 0.00 }, 1460 { 1.00F, 1.00F, 0.00F },
1535 }; 1461 };
1536 1462
1537 int x, y; 1463 int x, y;
1538 1464
1539 glEnable (GL_TEXTURE_2D); 1465 glEnable (GL_TEXTURE_2D);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines