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.14 by root, Tue Apr 11 13:05:11 2006 UTC vs.
Revision 1.25 by root, Wed Apr 12 21:20:42 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines