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.15 by root, Tue Apr 11 13:14:36 2006 UTC vs.
Revision 1.28 by root, Thu Apr 13 00:25:28 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>
14#include <sys/types.h> 16#include <sys/types.h>
15#include <sys/socket.h> 17#include <sys/socket.h>
16#include <netinet/in.h> 18#include <netinet/in.h>
17#include <netinet/tcp.h> 19#include <netinet/tcp.h>
18 20
21#include <inttypes.h>
22
19static PangoContext *context; 23static PangoContext *context;
20static PangoFontMap *fontmap; 24static PangoFontMap *fontmap;
21 25
22typedef struct cf_layout { 26typedef struct cf_layout {
23 PangoLayout *pl; 27 PangoLayout *pl;
24 int base_height; 28 int base_height;
25} *CFClient__Layout; 29} *CFClient__Layout;
26 30
27static void 31static void
32substitute_func (FcPattern *pattern, gpointer data)
33{
34 FcPatternAddBool (pattern, FC_HINTING , 1);
35 FcPatternAddBool (pattern, FC_AUTOHINT, 1);
36}
37
38static void
39layout_update (CFClient__Layout self)
40{
41 /* use a random scale factor to account for unknown descenders, 0.8 works
42 * reasonably well with bitstream vera
43 */
44 PangoFontDescription *font = pango_context_get_font_description (context);
45 pango_font_description_set_absolute_size (font, self->base_height * (PANGO_SCALE * 8 / 10));
46}
47
48static void
28layout_get_pixel_size (CFClient__Layout self, int *w, int *h) 49layout_get_pixel_size (CFClient__Layout self, int *w, int *h)
29{ 50{
51 layout_update (self);
52
30 pango_layout_get_pixel_size (self->pl, w, h); 53 pango_layout_get_pixel_size (self->pl, w, h);
31 54
32 *w = (*w + 3) & ~3; 55 *w = (*w + 3) & ~3;
33 if (!*w) *w = 1; 56 if (!*w) *w = 1;
34 if (!*h) *h = 1; 57 if (!*h) *h = 1;
35} 58}
36 59
60typedef struct {
61 uint16_t face[3];
62 uint8_t darkness;
63 uint8_t padding;
64} mapcell;
65
66typedef struct {
67 uint32_t cols;
68 mapcell *col;
69} maprow;
70
71typedef struct map {
72 int x, y, w, h;
73 int faces;
74 GLint *face;
75
76 uint32_t rows;
77 maprow *row;
78} *CFClient__Map;
79
37MODULE = CFClient PACKAGE = CFClient 80MODULE = CFClient PACKAGE = CFClient
38 81
39PROTOTYPES: ENABLE 82PROTOTYPES: ENABLE
40 83
41BOOT: 84BOOT:
42{ 85{
43 fontmap = pango_ft2_font_map_new (); 86 fontmap = pango_ft2_font_map_new ();
87 pango_ft2_font_map_set_default_substitute ((PangoFT2FontMap *)fontmap, substitute_func, 0, 0);
44 context = pango_ft2_font_map_create_context ((PangoFT2FontMap *)fontmap); 88 context = pango_ft2_font_map_create_context ((PangoFT2FontMap *)fontmap);
45} 89}
46 90
47void 91void
48lowdelay (int fd, int val = 1) 92lowdelay (int fd, int val = 1)
77 PangoFontDescription *font = pango_fc_font_description_from_pattern (pattern, 0); 121 PangoFontDescription *font = pango_fc_font_description_from_pattern (pattern, 0);
78 FcPatternDestroy (pattern); 122 FcPatternDestroy (pattern);
79 pango_context_set_font_description (context, font); 123 pango_context_set_font_description (context, font);
80} 124}
81 125
126void
127load_image_inline (SV *image_)
128 ALIAS:
129 load_image_file = 1
130 PPCODE:
131{
132 STRLEN image_len;
133 char *image = (char *)SvPVbyte (image_, image_len);
134 SDL_Surface *surface, *surface2;
135 SDL_PixelFormat fmt;
136 SDL_RWops *rw = ix
137 ? SDL_RWFromFile (image, "r")
138 : SDL_RWFromConstMem (image, image_len);
139
140 if (!rw)
141 croak ("load_image: unable to open file");
142
143 surface = IMG_Load_RW (rw, 1);
144 if (!surface)
145 croak ("load_image: unable to read file");
146
147 fmt.palette = NULL;
148 fmt.BitsPerPixel = 32;
149 fmt.BytesPerPixel = 4;
150 fmt.Rmask = 0x000000ff;
151 fmt.Gmask = 0x0000ff00;
152 fmt.Bmask = 0x00ff0000;
153 fmt.Amask = 0xff000000;
154 fmt.Rloss = 0;
155 fmt.Gloss = 0;
156 fmt.Bloss = 0;
157 fmt.Aloss = 0;
158 fmt.Rshift = 0;
159 fmt.Gshift = 8;
160 fmt.Bshift = 16;
161 fmt.Ashift = 24;
162 fmt.colorkey = 0;
163 fmt.alpha = 0;
164
165 surface2 = SDL_ConvertSurface (surface, &fmt, SDL_SWSURFACE);
166
167 EXTEND (SP, 5);
168 PUSHs (sv_2mortal (newSViv (surface2->w)));
169 PUSHs (sv_2mortal (newSViv (surface2->h)));
170 SDL_LockSurface (surface2);
171 PUSHs (sv_2mortal (newSVpvn (surface2->pixels, surface2->h * surface2->pitch)));
172 SDL_UnlockSurface (surface2);
173 PUSHs (sv_2mortal (newSViv (surface->flags & (SDL_SRCCOLORKEY | SDL_SRCALPHA) ? GL_RGBA : GL_RGB)));
174 PUSHs (sv_2mortal (newSViv (GL_RGBA)));
175 PUSHs (sv_2mortal (newSViv (GL_UNSIGNED_INT_8_8_8_8_REV)));
176
177 SDL_FreeSurface (surface);
178 SDL_FreeSurface (surface2);
179}
180
181void
182fatal (char *message)
183 CODE:
184#ifdef WIN32
185 MessageBox (0, message, "Crossfire+ Fatal Error", MB_OK | MB_ICONERROR | MB_SETFOREGROUND);
186#else
187 fprintf (stderr, "%s\n", message);
188#endif
189 exit (1);
190
82MODULE = CFClient PACKAGE = CFClient::Layout 191MODULE = CFClient PACKAGE = CFClient::Layout
83 192
84CFClient::Layout 193CFClient::Layout
85new (SV *class, int base_height = 10) 194new (SV *class, int base_height = 10)
86 CODE: 195 CODE:
106 215
107 pango_layout_set_markup (self->pl, text, textlen); 216 pango_layout_set_markup (self->pl, text, textlen);
108} 217}
109 218
110void 219void
220set_height (CFClient::Layout self, int base_height)
221 CODE:
222 self->base_height = base_height;
223
224void
111set_width (CFClient::Layout self, int max_width = -1) 225set_width (CFClient::Layout self, int max_width = -1)
112 CODE: 226 CODE:
113 pango_layout_set_width (self->pl, max_width < 0 ? max_width : max_width * PANGO_SCALE); 227 pango_layout_set_width (self->pl, max_width < 0 ? max_width : max_width * PANGO_SCALE);
114 228
115void 229void
116size (CFClient::Layout self) 230size (CFClient::Layout self)
117 PPCODE: 231 PPCODE:
118{ 232{
119 int w, h; 233 int w, h;
120 234
121 PangoFontDescription *font = pango_context_get_font_description (context); 235 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); 236 layout_get_pixel_size (self, &w, &h);
125 237
126 EXTEND (SP, 2); 238 EXTEND (SP, 2);
127 PUSHs (sv_2mortal (newSViv (w))); 239 PUSHs (sv_2mortal (newSViv (w)));
128 PUSHs (sv_2mortal (newSViv (h))); 240 PUSHs (sv_2mortal (newSViv (h)));
129} 241}
130 242
243int
244xy_to_index (CFClient::Layout self, int x, int y)
245 CODE:
246{
247 int index, trailing;
248
249 layout_update (self);
250 pango_layout_xy_to_index (self->pl, x * PANGO_SCALE, y * PANGO_SCALE, &index, &trailing);
251
252 RETVAL = index;
253}
254 OUTPUT:
255 RETVAL
256
257void
258cursor_pos (CFClient::Layout self, int index)
259 PPCODE:
260{
261 PangoRectangle strong_pos;
262 layout_update (self);
263 pango_layout_get_cursor_pos (self->pl, index, &strong_pos, 0);
264 EXTEND (SP, 3);
265 PUSHs (sv_2mortal (newSViv (strong_pos.x / PANGO_SCALE)));
266 PUSHs (sv_2mortal (newSViv (strong_pos.y / PANGO_SCALE)));
267 PUSHs (sv_2mortal (newSViv (strong_pos.height / PANGO_SCALE)));
268}
269
131void 270void
132render (CFClient::Layout self) 271render (CFClient::Layout self)
133 PPCODE: 272 PPCODE:
134{ 273{
135 SV *retval; 274 SV *retval;
136 int w, h; 275 int w, h;
137 FT_Bitmap bitmap; 276 FT_Bitmap bitmap;
138 277
278 layout_update (self);
139 layout_get_pixel_size (self, &w, &h); 279 layout_get_pixel_size (self, &w, &h);
140 280
141 retval = newSV (w * h); 281 retval = newSV (w * h);
142 SvPOK_only (retval); 282 SvPOK_only (retval);
143 SvCUR_set (retval, w * h); 283 SvCUR_set (retval, w * h);
171 double t = SvNV (*hv_fetch (hv, "t", 1, 1)); 311 double t = SvNV (*hv_fetch (hv, "t", 1, 1));
172 int name = SvIV (*hv_fetch (hv, "name", 4, 1)); 312 int name = SvIV (*hv_fetch (hv, "name", 4, 1));
173 313
174 if (items < 5) 314 if (items < 5)
175 { 315 {
176 w = SvNV (*hv_fetch (hv, "width", 5, 1)); 316 w = SvNV (*hv_fetch (hv, "w", 1, 1));
177 h = SvNV (*hv_fetch (hv, "height", 6, 1)); 317 h = SvNV (*hv_fetch (hv, "h", 1, 1));
178 } 318 }
179 319
180 glBindTexture (GL_TEXTURE_2D, name); 320 glBindTexture (GL_TEXTURE_2D, name);
181 glBegin (GL_QUADS); 321 glBegin (GL_QUADS);
182 glTexCoord2d (0, 0); glVertex2d (x , y ); 322 glTexCoord2d (0, 0); glVertex2d (x , y );
183 glTexCoord2d (0, t); glVertex2d (x , y + h); 323 glTexCoord2d (0, t); glVertex2d (x , y + h);
184 glTexCoord2d (s, t); glVertex2d (x + w, y + h); 324 glTexCoord2d (s, t); glVertex2d (x + w, y + h);
185 glTexCoord2d (s, 0); glVertex2d (x + w, y ); 325 glTexCoord2d (s, 0); glVertex2d (x + w, y );
186 glEnd (); 326 glEnd ();
187} 327}
328
329MODULE = CFClient PACKAGE = CFClient::Map
330
331CFClient::Map
332new (SV *class, int map_width, int map_height)
333 CODE:
334 New (0, RETVAL, 1, struct map);
335 RETVAL->x = 0;
336 RETVAL->y = 0;
337 RETVAL->w = map_width;
338 RETVAL->h = map_height;
339 RETVAL->faces = 0;
340 RETVAL->face = 0;
341
342 RETVAL->rows = 0;
343 RETVAL->row = 0;
344 OUTPUT:
345 RETVAL
346
347void
348DESTROY (CFClient::Map self)
349 CODE:
350{
351 int r, c;
352
353 Safefree (self->face);
354 for (r = 0; r < self->rows; r++)
355 {
356 maprow *row = self->row + r;
357 if (!row)
358 continue;
359
360 Safefree (row->col);
361 }
362
363 Safefree (self->row);
364 Safefree (self);
365}
366

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines