1 | #include "EXTERN.h" |
1 | #include "EXTERN.h" |
2 | #include "perl.h" |
2 | #include "perl.h" |
3 | #include "XSUB.h" |
3 | #include "XSUB.h" |
4 | |
4 | |
5 | #include <string.h> |
5 | #include <string.h> |
|
|
6 | #include <stdio.h> |
6 | |
7 | |
7 | #include <SDL.h> |
8 | #include <SDL.h> |
|
|
9 | #include <SDL_image.h> |
8 | #include <SDL_opengl.h> |
10 | #include <SDL_opengl.h> |
9 | |
11 | |
10 | #include <pango/pango.h> |
12 | #include <pango/pango.h> |
|
|
13 | #include <pango/pangofc-fontmap.h> |
11 | #include <pango/pangoft2.h> |
14 | #include <pango/pangoft2.h> |
|
|
15 | |
|
|
16 | #include <sys/types.h> |
|
|
17 | #include <sys/socket.h> |
|
|
18 | #include <netinet/in.h> |
|
|
19 | #include <netinet/tcp.h> |
12 | |
20 | |
13 | static PangoContext *context; |
21 | static PangoContext *context; |
14 | static PangoFontMap *fontmap; |
22 | static PangoFontMap *fontmap; |
15 | |
23 | |
|
|
24 | typedef struct cf_layout { |
|
|
25 | PangoLayout *pl; |
|
|
26 | int base_height; |
|
|
27 | } *CFClient__Layout; |
|
|
28 | |
|
|
29 | static void |
|
|
30 | substitute_func (FcPattern *pattern, gpointer data) |
|
|
31 | { |
|
|
32 | FcPatternAddBool (pattern, FC_HINTING , 1); |
|
|
33 | FcPatternAddBool (pattern, FC_AUTOHINT, 1); |
|
|
34 | } |
|
|
35 | |
|
|
36 | static void |
|
|
37 | layout_update (CFClient__Layout self) |
|
|
38 | { |
|
|
39 | /* use a random scale factor to account for unknown descenders, 0.8 works |
|
|
40 | * reasonably well with bitstream vera |
|
|
41 | */ |
|
|
42 | PangoFontDescription *font = pango_context_get_font_description (context); |
|
|
43 | pango_font_description_set_absolute_size (font, self->base_height * (PANGO_SCALE * 8 / 10)); |
|
|
44 | } |
|
|
45 | |
|
|
46 | static void |
|
|
47 | layout_get_pixel_size (CFClient__Layout self, int *w, int *h) |
|
|
48 | { |
|
|
49 | layout_update (self); |
|
|
50 | |
|
|
51 | pango_layout_get_pixel_size (self->pl, w, h); |
|
|
52 | |
|
|
53 | *w = (*w + 3) & ~3; |
|
|
54 | if (!*w) *w = 1; |
|
|
55 | if (!*h) *h = 1; |
|
|
56 | } |
|
|
57 | |
16 | MODULE = Crossfire::Client PACKAGE = Crossfire::Client |
58 | MODULE = CFClient PACKAGE = CFClient |
|
|
59 | |
|
|
60 | PROTOTYPES: ENABLE |
17 | |
61 | |
18 | BOOT: |
62 | BOOT: |
19 | { |
63 | { |
20 | fontmap = pango_ft2_font_map_new (); |
64 | fontmap = pango_ft2_font_map_new (); |
|
|
65 | pango_ft2_font_map_set_default_substitute ((PangoFT2FontMap *)fontmap, substitute_func, 0, 0); |
21 | context = pango_ft2_font_map_create_context ((PangoFT2FontMap *)fontmap); |
66 | context = pango_ft2_font_map_create_context ((PangoFT2FontMap *)fontmap); |
22 | } |
67 | } |
|
|
68 | |
|
|
69 | void |
|
|
70 | lowdelay (int fd, int val = 1) |
|
|
71 | CODE: |
|
|
72 | setsockopt (fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof (val)); |
23 | |
73 | |
24 | char * |
74 | char * |
25 | gl_version () |
75 | gl_version () |
26 | CODE: |
76 | CODE: |
27 | RETVAL = (char *)glGetString (GL_VERSION); |
77 | RETVAL = (char *)glGetString (GL_VERSION); |
… | |
… | |
34 | RETVAL = (char *)glGetString (GL_EXTENSIONS); |
84 | RETVAL = (char *)glGetString (GL_EXTENSIONS); |
35 | OUTPUT: |
85 | OUTPUT: |
36 | RETVAL |
86 | RETVAL |
37 | |
87 | |
38 | void |
88 | void |
|
|
89 | glConvolutionParameteri (U32 target, U32 name, I32 value) |
|
|
90 | |
|
|
91 | void |
|
|
92 | glConvolutionFilter2D (U32 target, U32 internalformat, I32 width, I32 height, U32 format, U32 type, char *image) |
|
|
93 | |
|
|
94 | void |
|
|
95 | add_font (char *file) |
|
|
96 | CODE: |
|
|
97 | FcConfigAppFontAddFile (0, (const FcChar8 *)file); /* no idea wether this is required */ |
|
|
98 | |
|
|
99 | void |
39 | set_font (char *file) |
100 | set_font (char *file) |
40 | CODE: |
101 | CODE: |
41 | { |
102 | { |
42 | int count; |
103 | int count; |
43 | FcPattern *pattern = FcFreeTypeQuery ((const FcChar8 *)file, 0, 0, &count); |
104 | FcPattern *pattern = FcFreeTypeQuery ((const FcChar8 *)file, 0, 0, &count); |
44 | FcConfigAppFontAddFile (0, (const FcChar8 *)file); /* no idea wether this is required */ |
|
|
45 | PangoFontDescription *font = pango_fc_font_description_from_pattern (pattern); |
105 | PangoFontDescription *font = pango_fc_font_description_from_pattern (pattern, 0); |
46 | FcPatternDestroy (pattern); |
106 | FcPatternDestroy (pattern); |
47 | pango_context_set_font_description (context, font); |
107 | pango_context_set_font_description (context, font); |
48 | } |
108 | } |
49 | |
109 | |
50 | void |
110 | void |
51 | font_render (SV *text_, int height = 10) |
111 | load_image_inline (SV *image_) |
|
|
112 | ALIAS: |
|
|
113 | load_image_file = 1 |
52 | PPCODE: |
114 | PPCODE: |
|
|
115 | { |
|
|
116 | STRLEN image_len; |
|
|
117 | char *image = (char *)SvPVbyte (image_, image_len); |
|
|
118 | SDL_Surface *surface, *surface2; |
|
|
119 | SDL_PixelFormat fmt; |
|
|
120 | SDL_RWops *rw = ix |
|
|
121 | ? SDL_RWFromFile (image, "r") |
|
|
122 | : SDL_RWFromConstMem (image, image_len); |
|
|
123 | |
|
|
124 | if (!rw) |
|
|
125 | croak ("load_image: unable to open file"); |
|
|
126 | |
|
|
127 | surface = IMG_Load_RW (rw, 1); |
|
|
128 | if (!surface) |
|
|
129 | croak ("load_image: unable to read file"); |
|
|
130 | |
|
|
131 | fmt.palette = NULL; |
|
|
132 | fmt.BitsPerPixel = 32; |
|
|
133 | fmt.BytesPerPixel = 4; |
|
|
134 | fmt.Rmask = 0x000000ff; |
|
|
135 | fmt.Gmask = 0x0000ff00; |
|
|
136 | fmt.Bmask = 0x00ff0000; |
|
|
137 | fmt.Amask = 0xff000000; |
|
|
138 | fmt.Rloss = 0; |
|
|
139 | fmt.Gloss = 0; |
|
|
140 | fmt.Bloss = 0; |
|
|
141 | fmt.Aloss = 0; |
|
|
142 | fmt.Rshift = 0; |
|
|
143 | fmt.Gshift = 8; |
|
|
144 | fmt.Bshift = 16; |
|
|
145 | fmt.Ashift = 24; |
|
|
146 | fmt.colorkey = 0; |
|
|
147 | fmt.alpha = 0; |
|
|
148 | |
|
|
149 | surface2 = SDL_ConvertSurface (surface, &fmt, SDL_SWSURFACE); |
|
|
150 | |
|
|
151 | EXTEND (SP, 5); |
|
|
152 | PUSHs (sv_2mortal (newSViv (surface2->w))); |
|
|
153 | PUSHs (sv_2mortal (newSViv (surface2->h))); |
|
|
154 | SDL_LockSurface (surface2); |
|
|
155 | PUSHs (sv_2mortal (newSVpvn (surface2->pixels, surface2->h * surface2->pitch))); |
|
|
156 | SDL_UnlockSurface (surface2); |
|
|
157 | PUSHs (sv_2mortal (newSViv (surface->flags & (SDL_SRCCOLORKEY | SDL_SRCALPHA) ? GL_RGBA : GL_RGB))); |
|
|
158 | PUSHs (sv_2mortal (newSViv (GL_RGBA))); |
|
|
159 | PUSHs (sv_2mortal (newSViv (GL_UNSIGNED_INT_8_8_8_8_REV))); |
|
|
160 | |
|
|
161 | SDL_FreeSurface (surface); |
|
|
162 | SDL_FreeSurface (surface2); |
|
|
163 | } |
|
|
164 | |
|
|
165 | void |
|
|
166 | fatal (char *message) |
|
|
167 | CODE: |
|
|
168 | #ifdef WIN32 |
|
|
169 | MessageBox (0, message, "Crossfire+ Fatal Error", MB_OK | MB_ICONERROR | MB_SETFOREGROUND); |
|
|
170 | #else |
|
|
171 | fprintf (stderr, "%s\n", message); |
|
|
172 | #endif |
|
|
173 | exit (1); |
|
|
174 | |
|
|
175 | MODULE = CFClient PACKAGE = CFClient::Layout |
|
|
176 | |
|
|
177 | CFClient::Layout |
|
|
178 | new (SV *class, int base_height = 10) |
|
|
179 | CODE: |
|
|
180 | New (0, RETVAL, 1, struct cf_layout); |
|
|
181 | RETVAL->base_height = base_height; |
|
|
182 | RETVAL->pl = pango_layout_new (context); |
|
|
183 | pango_layout_set_wrap (RETVAL->pl, PANGO_WRAP_WORD_CHAR); |
|
|
184 | OUTPUT: |
|
|
185 | RETVAL |
|
|
186 | |
|
|
187 | void |
|
|
188 | DESTROY (CFClient::Layout self) |
|
|
189 | CODE: |
|
|
190 | g_object_unref (self->pl); |
|
|
191 | Safefree (self); |
|
|
192 | |
|
|
193 | void |
|
|
194 | set_markup (CFClient::Layout self, SV *text_) |
|
|
195 | CODE: |
53 | { |
196 | { |
54 | STRLEN textlen; |
197 | STRLEN textlen; |
55 | char *text = SvPVutf8 (text_, textlen); |
198 | char *text = SvPVutf8 (text_, textlen); |
|
|
199 | |
|
|
200 | pango_layout_set_markup (self->pl, text, textlen); |
|
|
201 | } |
|
|
202 | |
|
|
203 | void |
|
|
204 | set_height (CFClient::Layout self, int base_height) |
|
|
205 | CODE: |
|
|
206 | self->base_height = base_height; |
|
|
207 | |
|
|
208 | void |
|
|
209 | set_width (CFClient::Layout self, int max_width = -1) |
|
|
210 | CODE: |
|
|
211 | pango_layout_set_width (self->pl, max_width < 0 ? max_width : max_width * PANGO_SCALE); |
|
|
212 | |
|
|
213 | void |
|
|
214 | size (CFClient::Layout self) |
|
|
215 | PPCODE: |
|
|
216 | { |
|
|
217 | int w, h; |
|
|
218 | |
|
|
219 | layout_update (self); |
|
|
220 | layout_get_pixel_size (self, &w, &h); |
|
|
221 | |
|
|
222 | EXTEND (SP, 2); |
|
|
223 | PUSHs (sv_2mortal (newSViv (w))); |
|
|
224 | PUSHs (sv_2mortal (newSViv (h))); |
|
|
225 | } |
|
|
226 | |
|
|
227 | int |
|
|
228 | xy_to_index (CFClient::Layout self, int x, int y) |
|
|
229 | CODE: |
|
|
230 | { |
|
|
231 | int index, trailing; |
|
|
232 | |
|
|
233 | layout_update (self); |
|
|
234 | pango_layout_xy_to_index (self->pl, x * PANGO_SCALE, y * PANGO_SCALE, &index, &trailing); |
|
|
235 | |
|
|
236 | RETVAL = index; |
|
|
237 | } |
|
|
238 | OUTPUT: |
|
|
239 | RETVAL |
|
|
240 | |
|
|
241 | void |
|
|
242 | cursor_pos (CFClient::Layout self, int index) |
|
|
243 | PPCODE: |
|
|
244 | { |
|
|
245 | PangoRectangle strong_pos; |
|
|
246 | layout_update (self); |
|
|
247 | pango_layout_get_cursor_pos (self->pl, index, &strong_pos, 0); |
|
|
248 | EXTEND (SP, 3); |
|
|
249 | PUSHs (sv_2mortal (newSViv (strong_pos.x / PANGO_SCALE))); |
|
|
250 | PUSHs (sv_2mortal (newSViv (strong_pos.y / PANGO_SCALE))); |
|
|
251 | PUSHs (sv_2mortal (newSViv (strong_pos.height / PANGO_SCALE))); |
|
|
252 | } |
|
|
253 | |
|
|
254 | void |
|
|
255 | render (CFClient::Layout self) |
|
|
256 | PPCODE: |
|
|
257 | { |
56 | SV *retval; |
258 | SV *retval; |
57 | int w, h; |
259 | int w, h; |
58 | FT_Bitmap bitmap; |
260 | FT_Bitmap bitmap; |
59 | PangoLayout *layout; |
|
|
60 | PangoFontDescription *font = pango_context_get_font_description (context); |
|
|
61 | pango_font_description_set_absolute_size (font, height * PANGO_SCALE); |
|
|
62 | |
261 | |
63 | layout = pango_layout_new (context); |
262 | layout_update (self); |
64 | pango_layout_set_markup (layout, text, textlen); |
|
|
65 | pango_layout_get_pixel_size (layout, &w, &h); |
263 | layout_get_pixel_size (self, &w, &h); |
66 | |
|
|
67 | w = (w + 3) & ~3; |
|
|
68 | if (!w) w = 1; |
|
|
69 | if (!h) h = 1; |
|
|
70 | |
264 | |
71 | retval = newSV (w * h); |
265 | retval = newSV (w * h); |
72 | SvPOK_only (retval); |
266 | SvPOK_only (retval); |
73 | SvCUR_set (retval, w * h); |
267 | SvCUR_set (retval, w * h); |
74 | |
268 | |
… | |
… | |
79 | bitmap.num_grays = 256; |
273 | bitmap.num_grays = 256; |
80 | bitmap.pixel_mode = FT_PIXEL_MODE_GRAY; |
274 | bitmap.pixel_mode = FT_PIXEL_MODE_GRAY; |
81 | |
275 | |
82 | memset (bitmap.buffer, 0, w * h); |
276 | memset (bitmap.buffer, 0, w * h); |
83 | |
277 | |
84 | pango_ft2_render_layout (&bitmap, layout, 0 * PANGO_SCALE, 0 * PANGO_SCALE); |
278 | pango_ft2_render_layout (&bitmap, self->pl, 0 * PANGO_SCALE, 0 * PANGO_SCALE); |
85 | g_object_unref (layout); |
|
|
86 | |
279 | |
87 | EXTEND (SP, 3); |
280 | EXTEND (SP, 3); |
88 | PUSHs (sv_2mortal (newSViv (w))); |
281 | PUSHs (sv_2mortal (newSViv (w))); |
89 | PUSHs (sv_2mortal (newSViv (h))); |
282 | PUSHs (sv_2mortal (newSViv (h))); |
90 | PUSHs (sv_2mortal (retval)); |
283 | PUSHs (sv_2mortal (retval)); |
91 | } |
284 | } |
|
|
285 | |
|
|
286 | MODULE = CFClient PACKAGE = CFClient::Texture |
|
|
287 | |
|
|
288 | void |
|
|
289 | draw_quad (SV *self, double x, double y, double w = 0, double h = 0) |
|
|
290 | PROTOTYPE: $$$;$$ |
|
|
291 | CODE: |
|
|
292 | { |
|
|
293 | HV *hv = (HV *)SvRV (self); |
|
|
294 | double s = SvNV (*hv_fetch (hv, "s", 1, 1)); |
|
|
295 | double t = SvNV (*hv_fetch (hv, "t", 1, 1)); |
|
|
296 | int name = SvIV (*hv_fetch (hv, "name", 4, 1)); |
|
|
297 | |
|
|
298 | if (items < 5) |
|
|
299 | { |
|
|
300 | w = SvNV (*hv_fetch (hv, "w", 1, 1)); |
|
|
301 | h = SvNV (*hv_fetch (hv, "h", 1, 1)); |
|
|
302 | } |
|
|
303 | |
|
|
304 | glBindTexture (GL_TEXTURE_2D, name); |
|
|
305 | glBegin (GL_QUADS); |
|
|
306 | glTexCoord2d (0, 0); glVertex2d (x , y ); |
|
|
307 | glTexCoord2d (0, t); glVertex2d (x , y + h); |
|
|
308 | glTexCoord2d (s, t); glVertex2d (x + w, y + h); |
|
|
309 | glTexCoord2d (s, 0); glVertex2d (x + w, y ); |
|
|
310 | glEnd (); |
|
|
311 | } |