… | |
… | |
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 | |
56 | static struct |
62 | static 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 | |
73 | typedef Mix_Chunk *CFClient__MixChunk; |
85 | typedef Mix_Chunk *CFClient__MixChunk; |
74 | typedef Mix_Music *CFClient__MixMusic; |
86 | typedef Mix_Music *CFClient__MixMusic; |
75 | |
87 | |
76 | typedef PangoFontDescription *CFClient__Font; |
88 | typedef PangoFontDescription *CFClient__Font; |
77 | |
89 | |
… | |
… | |
92 | |
104 | |
93 | return 0; |
105 | return 0; |
94 | } |
106 | } |
95 | |
107 | |
96 | typedef struct cf_layout { |
108 | typedef 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 | |
104 | static CFClient__Font default_font; |
115 | static CFClient__Font default_font; |
105 | static PangoContext *ft2_context, *cairo_context; |
116 | static PangoContext *opengl_context; |
106 | static PangoFontMap *ft2_fontmap, *cairo_fontmap; |
117 | static PangoFontMap *opengl_fontmap; |
107 | |
118 | |
108 | static void |
119 | static void |
109 | substitute_func (FcPattern *pattern, gpointer data) |
120 | substitute_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 | |
122 | static void |
129 | static void |
123 | layout_update_font (CFClient__Layout self) |
130 | layout_update_font (CFClient__Layout self) |
124 | { |
131 | { |
… | |
… | |
463 | NV ceil (NV x) |
470 | NV ceil (NV x) |
464 | |
471 | |
465 | void |
472 | void |
466 | pango_init () |
473 | pango_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 | |
492 | int |
481 | int |
493 | SDL_Init (U32 flags = SDL_INIT_VIDEO | SDL_INIT_AUDIO) |
482 | SDL_Init (U32 flags = SDL_INIT_VIDEO | SDL_INIT_AUDIO | PARACHUTE) |
494 | |
483 | |
495 | void |
484 | void |
496 | SDL_Quit () |
485 | SDL_Quit () |
497 | |
486 | |
498 | void |
487 | void |
… | |
… | |
636 | |
625 | |
637 | void |
626 | void |
638 | add_font (char *file) |
627 | add_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 | |
646 | void |
631 | void |
647 | load_image_inline (SV *image_) |
632 | load_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 | |
781 | MODULE = CFClient PACKAGE = CFClient::Layout |
766 | MODULE = CFClient PACKAGE = CFClient::Layout |
782 | |
767 | |
|
|
768 | void |
|
|
769 | clear_font_cache () |
|
|
770 | CODE: |
|
|
771 | tc_clear (); |
|
|
772 | |
783 | CFClient::Layout |
773 | CFClient::Layout |
784 | new (SV *class, int rgba = 0) |
774 | new (SV *class, int type = 0) |
785 | CODE: |
775 | CODE: |
786 | New (0, RETVAL, 1, struct cf_layout); |
776 | New (0, RETVAL, 1, struct cf_layout); |
787 | |
777 | |
788 | RETVAL->pl = pango_layout_new (rgba ? cairo_context : ft2_context); |
778 | RETVAL->pl = pango_layout_new (opengl_context); |
789 | RETVAL->rgba = rgba; |
|
|
790 | RETVAL->r = 1.; |
779 | RETVAL->r = 1.; |
791 | RETVAL->g = 1.; |
780 | RETVAL->g = 1.; |
792 | RETVAL->b = 1.; |
781 | RETVAL->b = 1.; |
793 | RETVAL->a = 1.; |
782 | RETVAL->a = 1.; |
794 | RETVAL->base_height = MIN_FONT_HEIGHT; |
783 | RETVAL->base_height = MIN_FONT_HEIGHT; |
… | |
… | |
802 | void |
791 | void |
803 | DESTROY (CFClient::Layout self) |
792 | DESTROY (CFClient::Layout self) |
804 | CODE: |
793 | CODE: |
805 | g_object_unref (self->pl); |
794 | g_object_unref (self->pl); |
806 | Safefree (self); |
795 | Safefree (self); |
807 | |
|
|
808 | int |
|
|
809 | is_rgba (CFClient::Layout self) |
|
|
810 | CODE: |
|
|
811 | RETVAL = self->rgba; |
|
|
812 | OUTPUT: |
|
|
813 | RETVAL |
|
|
814 | |
796 | |
815 | void |
797 | void |
816 | set_text (CFClient::Layout self, SV *text_) |
798 | set_text (CFClient::Layout self, SV *text_) |
817 | CODE: |
799 | CODE: |
818 | { |
800 | { |
… | |
… | |
1028 | PUSHs (sv_2mortal (newSViv (strong_pos.y / PANGO_SCALE))); |
1010 | PUSHs (sv_2mortal (newSViv (strong_pos.y / PANGO_SCALE))); |
1029 | PUSHs (sv_2mortal (newSViv (strong_pos.height / PANGO_SCALE))); |
1011 | PUSHs (sv_2mortal (newSViv (strong_pos.height / PANGO_SCALE))); |
1030 | } |
1012 | } |
1031 | |
1013 | |
1032 | void |
1014 | void |
1033 | render (CFClient::Layout self) |
1015 | render (CFClient::Layout self, float x, float y) |
1034 | PPCODE: |
1016 | PPCODE: |
1035 | { |
1017 | pango_opengl_render_layout_subpixel ( |
1036 | SV *retval; |
1018 | self->pl, |
1037 | int w, h; |
1019 | x * PANGO_SCALE, y * PANGO_SCALE, |
1038 | |
1020 | self->r, self->g, self->b, self->a |
1039 | layout_get_pixel_size (self, &w, &h); |
|
|
1040 | |
|
|
1041 | if (self->rgba) |
|
|
1042 | { |
1021 | ); |
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 | |
1022 | |
1123 | MODULE = CFClient PACKAGE = CFClient::Texture |
1023 | MODULE = CFClient PACKAGE = CFClient::Texture |
1124 | |
1024 | |
1125 | void |
1025 | void |
1126 | pad2pot (SV *data_, SV *w_, SV *h_) |
1026 | pad2pot (SV *data_, SV *w_, SV *h_) |
… | |
… | |
1528 | void |
1428 | void |
1529 | draw_magicmap (CFClient::Map self, int dx, int dy, int w, int h, unsigned char *data) |
1429 | draw_magicmap (CFClient::Map self, int dx, int dy, int w, int h, unsigned char *data) |
1530 | CODE: |
1430 | CODE: |
1531 | { |
1431 | { |
1532 | static float color[16][3] = { |
1432 | static float color[16][3] = { |
1533 | { 0.00, 0.00, 0.00 }, |
1433 | { 0.00F, 0.00F, 0.00F }, |
1534 | { 1.00, 1.00, 1.00 }, |
1434 | { 1.00F, 1.00F, 1.00F }, |
1535 | { 0.00, 0.00, 0.55 }, |
1435 | { 0.00F, 0.00F, 0.55F }, |
1536 | { 1.00, 0.00, 0.00 }, |
1436 | { 1.00F, 0.00F, 0.00F }, |
1537 | |
1437 | |
1538 | { 1.00, 0.54, 0.00 }, |
1438 | { 1.00F, 0.54F, 0.00F }, |
1539 | { 0.11, 0.56, 1.00 }, |
1439 | { 0.11F, 0.56F, 1.00F }, |
1540 | { 0.93, 0.46, 0.00 }, |
1440 | { 0.93F, 0.46F, 0.00F }, |
1541 | { 0.18, 0.54, 0.34 }, |
1441 | { 0.18F, 0.54F, 0.34F }, |
1542 | |
1442 | |
1543 | { 0.56, 0.73, 0.56 }, |
1443 | { 0.56F, 0.73F, 0.56F }, |
1544 | { 0.80, 0.80, 0.80 }, |
1444 | { 0.80F, 0.80F, 0.80F }, |
1545 | { 0.55, 0.41, 0.13 }, |
1445 | { 0.55F, 0.41F, 0.13F }, |
1546 | { 0.99, 0.77, 0.26 }, |
1446 | { 0.99F, 0.77F, 0.26F }, |
1547 | |
1447 | |
1548 | { 0.74, 0.65, 0.41 }, |
1448 | { 0.74F, 0.65F, 0.41F }, |
1549 | |
1449 | |
1550 | { 0.00, 1.00, 1.00 }, |
1450 | { 0.00F, 1.00F, 1.00F }, |
1551 | { 1.00, 0.00, 1.00 }, |
1451 | { 1.00F, 0.00F, 1.00F }, |
1552 | { 1.00, 1.00, 0.00 }, |
1452 | { 1.00F, 1.00F, 0.00F }, |
1553 | }; |
1453 | }; |
1554 | |
1454 | |
1555 | int x, y; |
1455 | int x, y; |
1556 | |
1456 | |
1557 | glEnable (GL_TEXTURE_2D); |
1457 | glEnable (GL_TEXTURE_2D); |