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.12 by root, Mon Apr 10 22:16:33 2006 UTC vs.
Revision 1.23 by root, Wed Apr 12 20:42:52 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines