ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/Client.xs
Revision: 1.53
Committed: Wed Apr 19 21:38:04 2006 UTC (18 years ago) by root
Branch: MAIN
Changes since 1.52: +47 -2 lines
Log Message:
got rid of SDL::Event

File Contents

# Content
1 #ifdef _WIN32
2 # include <malloc.h>
3 #endif
4
5 #include "EXTERN.h"
6 #include "perl.h"
7 #include "XSUB.h"
8
9 #include <string.h>
10 #include <stdio.h>
11
12 #include <SDL.h>
13 #include <SDL_image.h>
14 #include <SDL_mixer.h>
15 #include <SDL_opengl.h>
16
17 #include <glib/gmacros.h>
18
19 #include <pango/pango.h>
20 #include <pango/pangofc-fontmap.h>
21 #include <pango/pangoft2.h>
22
23 #ifndef _WIN32
24 # include <sys/types.h>
25 # include <sys/socket.h>
26 # include <netinet/in.h>
27 # include <netinet/tcp.h>
28 # include <inttypes.h>
29 #else
30 typedef unsigned char uint8_t;
31 typedef unsigned short uint16_t;
32 typedef unsigned int uint32_t;
33 typedef signed char int8_t;
34 typedef signed short int16_t;
35 typedef signed int int32_t;
36 #endif
37
38 #define FOW_DARKNESS 32
39
40 #define MAP_EXTEND_X 32
41 #define MAP_EXTEND_Y 512
42
43 typedef Mix_Chunk *CFClient__MixChunk;
44 typedef Mix_Music *CFClient__MixMusic;
45
46 static PangoContext *context;
47 static PangoFontMap *fontmap;
48
49 typedef struct cf_layout {
50 PangoLayout *pl;
51 int base_height;
52 } *CFClient__Layout;
53
54 static void
55 substitute_func (FcPattern *pattern, gpointer data)
56 {
57 FcPatternAddBool (pattern, FC_HINTING , 1);
58 FcPatternAddBool (pattern, FC_AUTOHINT, 0);
59 }
60
61 static void
62 layout_update (CFClient__Layout self)
63 {
64 /* use a random scale factor to account for unknown descenders, 0.8 works
65 * reasonably well with bitstream vera
66 */
67 PangoFontDescription *font = pango_context_get_font_description (context);
68
69 int height = self->base_height * (PANGO_SCALE * 8 / 10);
70
71 if (pango_font_description_get_size (font) != height)
72 {
73 pango_font_description_set_absolute_size (font, height);
74 pango_layout_context_changed (self->pl);
75 }
76 }
77
78 static void
79 layout_get_pixel_size (CFClient__Layout self, int *w, int *h)
80 {
81 layout_update (self);
82
83 pango_layout_get_pixel_size (self->pl, w, h);
84
85 *w = (*w + 3) & ~3;
86 if (!*w) *w = 1;
87 if (!*h) *h = 1;
88 }
89
90 typedef uint16_t mapface;
91
92 typedef struct {
93 GLint name;
94 int w, h;
95 float s, t;
96 uint8_t r, g, b, a;
97 } maptex;
98
99 typedef struct {
100 int16_t darkness;
101 mapface face[3];
102 } mapcell;
103
104 typedef struct {
105 int32_t c0, c1;
106 mapcell *col;
107 } maprow;
108
109 typedef struct map {
110 int x, y, w, h;
111 int ox, oy; /* offset to virtual global coordinate system */
112 int faces;
113 mapface *face;
114
115 int texs;
116 maptex *tex;
117
118 int32_t rows;
119 maprow *row;
120 } *CFClient__Map;
121
122 static char *
123 prepend (char *ptr, int sze, int inc)
124 {
125 char *p;
126
127 New (0, p, sze + inc, char);
128 Zero (p, inc, char);
129 Move (ptr, p + inc, sze, char);
130 Safefree (ptr);
131
132 return p;
133 }
134
135 static char *
136 append (char *ptr, int sze, int inc)
137 {
138 Renew (ptr, sze + inc, char);
139 Zero (ptr + sze, inc, char);
140
141 return ptr;
142 }
143
144 #define Append(type,ptr,sze,inc) (ptr) = (type *)append ((char *)ptr, (sze) * sizeof (type), (inc) * sizeof (type))
145 #define Prepend(type,ptr,sze,inc) (ptr) = (type *)prepend ((char *)ptr, (sze) * sizeof (type), (inc) * sizeof (type))
146
147 static maprow *
148 map_get_row (CFClient__Map self, int y)
149 {
150 if (0 > y)
151 {
152 int extend = - y + MAP_EXTEND_Y;
153 Prepend (maprow, self->row, self->rows, extend);
154
155 self->rows += extend;
156 self->y += extend;
157 y += extend;
158 }
159 else if (y >= self->rows)
160 {
161 int extend = y - self->rows + MAP_EXTEND_Y;
162 Append (maprow, self->row, self->rows, extend);
163 self->rows += extend;
164 }
165
166 return self->row + y;
167 }
168
169 static mapcell *
170 row_get_cell (maprow *row, int x)
171 {
172 if (!row->col)
173 {
174 Newz (0, row->col, MAP_EXTEND_X, mapcell);
175 row->c0 = x - MAP_EXTEND_X / 4;
176 row->c1 = row->c0 + MAP_EXTEND_X;
177 }
178
179 if (row->c0 > x)
180 {
181 int extend = row->c0 - x + MAP_EXTEND_X;
182 Prepend (mapcell, row->col, row->c1 - row->c0, extend);
183 row->c0 -= extend;
184 }
185 else if (x >= row->c1)
186 {
187 int extend = x - row->c1 + MAP_EXTEND_X;
188 Append (mapcell, row->col, row->c1 - row->c0, extend);
189 row->c1 += extend;
190 }
191
192 return row->col + (x - row->c0);
193 }
194
195 static mapcell *
196 map_get_cell (CFClient__Map self, int x, int y)
197 {
198 return row_get_cell (map_get_row (self, y), x);
199 }
200
201 static void
202 map_clear (CFClient__Map self)
203 {
204 int r;
205
206 for (r = 0; r < self->rows; r++)
207 Safefree (self->row[r].col);
208
209 Safefree (self->row);
210
211 self->x = 0;
212 self->y = 0;
213 self->ox = 0;
214 self->oy = 0;
215 self->row = 0;
216 self->rows = 0;
217 }
218
219 static void
220 map_blank (CFClient__Map self, int x0, int y0, int w, int h)
221 {
222 int x, y;
223 maprow *row;
224
225 for (y = y0; y < y0 + h; y++)
226 if (y >= 0)
227 {
228 if (y >= self->rows)
229 break;
230
231 row = self->row + y;
232
233 for (x = x0; x < x0 + w; x++)
234 if (x >= row->c0)
235 {
236 if (x >= row->c1)
237 break;
238
239 row->col[x - row->c0].darkness = -1;
240 }
241 }
242 }
243
244 MODULE = CFClient PACKAGE = CFClient
245
246 PROTOTYPES: ENABLE
247
248 BOOT:
249 {
250 HV *stash = gv_stashpv ("CFClient", 1);
251 static const struct {
252 const char *name;
253 IV iv;
254 } *civ, const_iv[] = {
255 # define const_iv(name) { # name, (IV)name }
256 const_iv (SDL_ACTIVEEVENT),
257 const_iv (SDL_KEYDOWN),
258 const_iv (SDL_KEYUP),
259 const_iv (SDL_MOUSEMOTION),
260 const_iv (SDL_MOUSEBUTTONDOWN),
261 const_iv (SDL_MOUSEBUTTONUP),
262 const_iv (SDL_JOYAXISMOTION),
263 const_iv (SDL_JOYBALLMOTION),
264 const_iv (SDL_JOYHATMOTION),
265 const_iv (SDL_JOYBUTTONDOWN),
266 const_iv (SDL_JOYBUTTONUP),
267 const_iv (SDL_QUIT),
268 const_iv (SDL_SYSWMEVENT),
269 const_iv (SDL_EVENT_RESERVEDA),
270 const_iv (SDL_EVENT_RESERVEDB),
271 const_iv (SDL_VIDEORESIZE),
272 const_iv (SDL_VIDEOEXPOSE),
273 const_iv (SDL_USEREVENT),
274 const_iv (SDLK_KP0),
275 const_iv (SDLK_KP1),
276 const_iv (SDLK_KP2),
277 const_iv (SDLK_KP3),
278 const_iv (SDLK_KP4),
279 const_iv (SDLK_KP5),
280 const_iv (SDLK_KP6),
281 const_iv (SDLK_KP7),
282 const_iv (SDLK_KP8),
283 const_iv (SDLK_KP9),
284 const_iv (SDLK_KP_PERIOD),
285 const_iv (SDLK_KP_DIVIDE),
286 const_iv (SDLK_KP_MULTIPLY),
287 const_iv (SDLK_KP_MINUS),
288 const_iv (SDLK_KP_PLUS),
289 const_iv (SDLK_KP_ENTER),
290 const_iv (SDLK_KP_EQUALS),
291 const_iv (SDLK_UP),
292 const_iv (SDLK_DOWN),
293 const_iv (SDLK_RIGHT),
294 const_iv (SDLK_LEFT),
295 const_iv (SDLK_INSERT),
296 const_iv (SDLK_HOME),
297 const_iv (SDLK_END),
298 const_iv (SDLK_PAGEUP),
299 const_iv (SDLK_PAGEDOWN),
300 const_iv (SDLK_F1),
301 const_iv (SDLK_F2),
302 const_iv (SDLK_F3),
303 const_iv (SDLK_F4),
304 const_iv (SDLK_F5),
305 const_iv (SDLK_F6),
306 const_iv (SDLK_F7),
307 const_iv (SDLK_F8),
308 const_iv (SDLK_F9),
309 const_iv (SDLK_F10),
310 const_iv (SDLK_F11),
311 const_iv (SDLK_F12),
312 const_iv (SDLK_F13),
313 const_iv (SDLK_F14),
314 const_iv (SDLK_F15),
315 const_iv (SDLK_NUMLOCK),
316 const_iv (SDLK_CAPSLOCK),
317 const_iv (SDLK_SCROLLOCK),
318 const_iv (SDLK_RSHIFT),
319 const_iv (SDLK_LSHIFT),
320 const_iv (SDLK_RCTRL),
321 const_iv (SDLK_LCTRL),
322 const_iv (SDLK_RALT),
323 const_iv (SDLK_LALT),
324 const_iv (SDLK_RMETA),
325 const_iv (SDLK_LMETA),
326 const_iv (SDLK_LSUPER),
327 const_iv (SDLK_RSUPER),
328 const_iv (SDLK_MODE),
329 const_iv (SDLK_COMPOSE),
330 const_iv (SDLK_HELP),
331 const_iv (SDLK_PRINT),
332 const_iv (SDLK_SYSREQ),
333 const_iv (SDLK_BREAK),
334 const_iv (SDLK_MENU),
335 const_iv (SDLK_POWER),
336 const_iv (SDLK_EURO),
337 const_iv (SDLK_UNDO),
338 const_iv (KMOD_NONE),
339 const_iv (KMOD_LSHIFT),
340 const_iv (KMOD_RSHIFT),
341 const_iv (KMOD_LCTRL),
342 const_iv (KMOD_RCTRL),
343 const_iv (KMOD_LALT),
344 const_iv (KMOD_RALT),
345 const_iv (KMOD_LMETA),
346 const_iv (KMOD_RMETA),
347 const_iv (KMOD_NUM),
348 const_iv (KMOD_CAPS),
349 const_iv (KMOD_MODE),
350 const_iv (KMOD_CTRL),
351 const_iv (KMOD_SHIFT),
352 const_iv (KMOD_ALT),
353 const_iv (KMOD_META)
354 # undef const_iv
355 };
356
357 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; )
358 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv));
359
360 fontmap = pango_ft2_font_map_new ();
361 pango_ft2_font_map_set_default_substitute ((PangoFT2FontMap *)fontmap, substitute_func, 0, 0);
362 context = pango_ft2_font_map_create_context ((PangoFT2FontMap *)fontmap);
363 }
364
365 int
366 SDL_Init (U32 flags = SDL_INIT_VIDEO | SDL_INIT_AUDIO)
367
368 void
369 SDL_Quit ()
370
371 void
372 SDL_ListModes ()
373 PPCODE:
374 {
375 SDL_Rect **m;
376
377 SDL_GL_SetAttribute (SDL_GL_RED_SIZE, 5);
378 SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, 5);
379 SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE, 5);
380 SDL_GL_SetAttribute (SDL_GL_ALPHA_SIZE, 1);
381
382 SDL_GL_SetAttribute (SDL_GL_ACCUM_RED_SIZE, 0);
383 SDL_GL_SetAttribute (SDL_GL_ACCUM_GREEN_SIZE, 0);
384 SDL_GL_SetAttribute (SDL_GL_ACCUM_BLUE_SIZE, 0);
385 SDL_GL_SetAttribute (SDL_GL_ACCUM_ALPHA_SIZE, 0);
386
387 SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1);
388 SDL_GL_SetAttribute (SDL_GL_BUFFER_SIZE, 15);
389 SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 0);
390
391 SDL_EnableUNICODE (1);
392 SDL_EnableKeyRepeat (SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
393
394 m = SDL_ListModes (0, SDL_FULLSCREEN | SDL_OPENGL);
395
396 if (m && m != (SDL_Rect **)-1)
397 while (*m)
398 {
399 AV *av = newAV ();
400 av_push (av, newSViv ((*m)->w));
401 av_push (av, newSViv ((*m)->h));
402 XPUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
403
404 ++m;
405 }
406 }
407
408 int
409 SDL_SetVideoMode (int w, int h, int fullscreen)
410 CODE:
411 RETVAL = !!SDL_SetVideoMode (
412 w, h, 0, SDL_OPENGL | (fullscreen ? SDL_FULLSCREEN : 0)
413 );
414 SDL_WM_SetCaption ("Crossfire+ Client " VERSION, "Crossfire+");
415 OUTPUT:
416 RETVAL
417
418 void
419 SDL_PollEvent ()
420 PPCODE:
421 {
422 SDL_Event ev;
423
424 while (SDL_PollEvent (&ev))
425 {
426 HV *hv = newHV ();
427 hv_store (hv, "type", 4, newSViv (ev.type), 0);
428 switch (ev.type)
429 {
430 case SDL_KEYDOWN:
431 case SDL_KEYUP:
432 hv_store (hv, "state", 5, newSViv (ev.key.state), 0);
433 hv_store (hv, "sym", 3, newSViv (ev.key.keysym.sym), 0);
434 hv_store (hv, "mod", 3, newSViv (ev.key.keysym.mod), 0);
435 hv_store (hv, "unicode", 7, newSViv (ev.key.keysym.unicode), 0);
436 break;
437
438 case SDL_ACTIVEEVENT:
439 hv_store (hv, "gain", 4, newSViv (ev.active.gain), 0);
440 hv_store (hv, "state", 5, newSViv (ev.active.state), 0);
441 break;
442
443 case SDL_MOUSEMOTION:
444 hv_store (hv, "state", 5, newSViv (ev.motion.state), 0);
445 hv_store (hv, "x", 1, newSViv (ev.motion.x), 0);
446 hv_store (hv, "y", 1, newSViv (ev.motion.y), 0);
447 hv_store (hv, "xrel", 4, newSViv (ev.motion.xrel), 0);
448 hv_store (hv, "yrel", 4, newSViv (ev.motion.yrel), 0);
449 break;
450
451 case SDL_MOUSEBUTTONDOWN:
452 case SDL_MOUSEBUTTONUP:
453 hv_store (hv, "button", 6, newSViv (ev.button.button), 0);
454 hv_store (hv, "state", 5, newSViv (ev.button.state), 0);
455 hv_store (hv, "x", 1, newSViv (ev.button.x), 0);
456 hv_store (hv, "y", 1, newSViv (ev.button.y), 0);
457 }
458
459 XPUSHs (sv_2mortal (newRV_noinc ((SV *)hv)));
460 }
461 }
462
463 int
464 Mix_OpenAudio (int frequency = 22050, int format = MIX_DEFAULT_FORMAT, int channels = 1, int chunksize = 512)
465
466 void
467 Mix_CloseAudio ()
468
469 int
470 Mix_AllocateChannels (int numchans = -1)
471
472 void
473 lowdelay (int fd, int val = 1)
474 CODE:
475 #ifndef _WIN32
476 setsockopt (fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof (val));
477 #endif
478
479 char *
480 gl_version ()
481 CODE:
482 RETVAL = (char *)glGetString (GL_VERSION);
483 OUTPUT:
484 RETVAL
485
486 char *
487 gl_extensions ()
488 CODE:
489 RETVAL = (char *)glGetString (GL_EXTENSIONS);
490 OUTPUT:
491 RETVAL
492
493 void
494 add_font (char *file)
495 CODE:
496 FcConfigAppFontAddFile (0, (const FcChar8 *)file); /* no idea wether this is required */
497
498 void
499 set_font (char *file)
500 CODE:
501 {
502 int count;
503 FcPattern *pattern = FcFreeTypeQuery ((const FcChar8 *)file, 0, 0, &count);
504 PangoFontDescription *font = pango_fc_font_description_from_pattern (pattern, 0);
505 FcPatternDestroy (pattern);
506 pango_context_set_font_description (context, font);
507 }
508
509 void
510 load_image_inline (SV *image_)
511 ALIAS:
512 load_image_file = 1
513 PPCODE:
514 {
515 STRLEN image_len;
516 char *image = (char *)SvPVbyte (image_, image_len);
517 SDL_Surface *surface, *surface2;
518 SDL_PixelFormat fmt;
519 SDL_RWops *rw = ix
520 ? SDL_RWFromFile (image, "r")
521 : SDL_RWFromConstMem (image, image_len);
522
523 if (!rw)
524 croak ("load_image: %s", SDL_GetError ());
525
526 surface = IMG_Load_RW (rw, 1);
527 if (!surface)
528 croak ("load_image: %s", SDL_GetError ());
529
530 fmt.palette = NULL;
531 fmt.BitsPerPixel = 32;
532 fmt.BytesPerPixel = 4;
533 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
534 fmt.Rmask = 0x000000ff;
535 fmt.Gmask = 0x0000ff00;
536 fmt.Bmask = 0x00ff0000;
537 fmt.Amask = 0xff000000;
538 #else
539 fmt.Rmask = 0xff000000;
540 fmt.Gmask = 0x00ff0000;
541 fmt.Bmask = 0x0000ff00;
542 fmt.Amask = 0x000000ff;
543 #endif
544 fmt.Rloss = 0;
545 fmt.Gloss = 0;
546 fmt.Bloss = 0;
547 fmt.Aloss = 0;
548 fmt.Rshift = 0;
549 fmt.Gshift = 8;
550 fmt.Bshift = 16;
551 fmt.Ashift = 24;
552 fmt.colorkey = 0;
553 fmt.alpha = 0;
554
555 surface2 = SDL_ConvertSurface (surface, &fmt, SDL_SWSURFACE);
556
557 assert (surface2->pitch == surface2->w * 4);
558
559 EXTEND (SP, 5);
560 PUSHs (sv_2mortal (newSViv (surface2->w)));
561 PUSHs (sv_2mortal (newSViv (surface2->h)));
562 SDL_LockSurface (surface2);
563 PUSHs (sv_2mortal (newSVpvn (surface2->pixels, surface2->h * surface2->pitch)));
564 SDL_UnlockSurface (surface2);
565 PUSHs (sv_2mortal (newSViv (surface->flags & (SDL_SRCCOLORKEY | SDL_SRCALPHA) ? GL_RGBA : GL_RGB)));
566 PUSHs (sv_2mortal (newSViv (GL_RGBA)));
567 PUSHs (sv_2mortal (newSViv (GL_UNSIGNED_BYTE)));
568
569 SDL_FreeSurface (surface);
570 SDL_FreeSurface (surface2);
571 }
572
573 void
574 average (int x, int y, uint32_t *data)
575 PPCODE:
576 {
577 uint32_t r = 0, g = 0, b = 0, a = 0;
578
579 x = y = x * y;
580
581 while (x--)
582 {
583 uint32_t p = *data++;
584
585 r += (p ) & 255;
586 g += (p >> 8) & 255;
587 b += (p >> 16) & 255;
588 a += (p >> 24) & 255;
589 }
590
591 EXTEND (SP, 4);
592 PUSHs (sv_2mortal (newSViv (r / y)));
593 PUSHs (sv_2mortal (newSViv (g / y)));
594 PUSHs (sv_2mortal (newSViv (b / y)));
595 PUSHs (sv_2mortal (newSViv (a / y)));
596 }
597
598 void
599 fatal (char *message)
600 CODE:
601 #ifdef _WIN32
602 MessageBox (0, message, "Crossfire+ Fatal Error", MB_OK | MB_ICONERROR | MB_SETFOREGROUND);
603 #else
604 fprintf (stderr, "FATAL: %s\n", message);
605 #endif
606 exit (1);
607
608 MODULE = CFClient PACKAGE = CFClient::Layout
609
610 CFClient::Layout
611 new (SV *class, int base_height = 10)
612 CODE:
613 New (0, RETVAL, 1, struct cf_layout);
614 RETVAL->base_height = base_height;
615 RETVAL->pl = pango_layout_new (context);
616 pango_layout_set_wrap (RETVAL->pl, PANGO_WRAP_WORD_CHAR);
617 OUTPUT:
618 RETVAL
619
620 void
621 DESTROY (CFClient::Layout self)
622 CODE:
623 g_object_unref (self->pl);
624 Safefree (self);
625
626 void
627 set_text (CFClient::Layout self, SV *text_)
628 CODE:
629 {
630 STRLEN textlen;
631 char *text = SvPVutf8 (text_, textlen);
632
633 pango_layout_set_text (self->pl, text, textlen);
634 }
635
636 void
637 set_markup (CFClient::Layout self, SV *text_)
638 CODE:
639 {
640 STRLEN textlen;
641 char *text = SvPVutf8 (text_, textlen);
642
643 pango_layout_set_markup (self->pl, text, textlen);
644 }
645
646 SV *
647 get_text (CFClient::Layout self)
648 CODE:
649 RETVAL = newSVpv (pango_layout_get_text (self->pl), 0);
650 SvUTF8_on (RETVAL);
651 OUTPUT:
652 RETVAL
653
654 void
655 set_height (CFClient::Layout self, int base_height)
656 CODE:
657 self->base_height = base_height;
658
659 void
660 set_width (CFClient::Layout self, int max_width = -1)
661 CODE:
662 pango_layout_set_width (self->pl, max_width < 0 ? max_width : max_width * PANGO_SCALE);
663
664 void
665 size (CFClient::Layout self)
666 PPCODE:
667 {
668 int w, h;
669
670 layout_update (self);
671 layout_get_pixel_size (self, &w, &h);
672
673 EXTEND (SP, 2);
674 PUSHs (sv_2mortal (newSViv (w)));
675 PUSHs (sv_2mortal (newSViv (h)));
676 }
677
678 int
679 xy_to_index (CFClient::Layout self, int x, int y)
680 CODE:
681 {
682 int index, trailing;
683
684 layout_update (self);
685 pango_layout_xy_to_index (self->pl, x * PANGO_SCALE, y * PANGO_SCALE, &index, &trailing);
686
687 RETVAL = index;
688 }
689 OUTPUT:
690 RETVAL
691
692 void
693 cursor_pos (CFClient::Layout self, int index)
694 PPCODE:
695 {
696 PangoRectangle strong_pos;
697 layout_update (self);
698 pango_layout_get_cursor_pos (self->pl, index, &strong_pos, 0);
699
700 EXTEND (SP, 3);
701 PUSHs (sv_2mortal (newSViv (strong_pos.x / PANGO_SCALE)));
702 PUSHs (sv_2mortal (newSViv (strong_pos.y / PANGO_SCALE)));
703 PUSHs (sv_2mortal (newSViv (strong_pos.height / PANGO_SCALE)));
704 }
705
706 void
707 render (CFClient::Layout self)
708 PPCODE:
709 {
710 SV *retval;
711 int w, h;
712 FT_Bitmap bitmap;
713
714 layout_update (self);
715 layout_get_pixel_size (self, &w, &h);
716
717 retval = newSV (w * h);
718 SvPOK_only (retval);
719 SvCUR_set (retval, w * h);
720
721 bitmap.rows = h;
722 bitmap.width = w;
723 bitmap.pitch = w;
724 bitmap.buffer = (unsigned char*)SvPVX (retval);
725 bitmap.num_grays = 256;
726 bitmap.pixel_mode = FT_PIXEL_MODE_GRAY;
727
728 memset (bitmap.buffer, 0, w * h);
729
730 pango_ft2_render_layout (&bitmap, self->pl, 0 * PANGO_SCALE, 0 * PANGO_SCALE);
731
732 EXTEND (SP, 3);
733 PUSHs (sv_2mortal (newSViv (w)));
734 PUSHs (sv_2mortal (newSViv (h)));
735 PUSHs (sv_2mortal (retval));
736 }
737
738 MODULE = CFClient PACKAGE = CFClient::Texture
739
740 void
741 draw_quad (SV *self, float x, float y, float w = 0, float h = 0)
742 PROTOTYPE: $$$;$$
743 CODE:
744 {
745 HV *hv = (HV *)SvRV (self);
746 float s = SvNV (*hv_fetch (hv, "s", 1, 1));
747 float t = SvNV (*hv_fetch (hv, "t", 1, 1));
748 int name = SvIV (*hv_fetch (hv, "name", 4, 1));
749 int wrap_mode = SvIV (*hv_fetch (hv, "wrap_mode", 9, 1));
750
751 if (items < 5)
752 {
753 w = SvNV (*hv_fetch (hv, "w", 1, 1));
754 h = SvNV (*hv_fetch (hv, "h", 1, 1));
755 }
756
757 glBindTexture (GL_TEXTURE_2D, name);
758 if (wrap_mode) {
759 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
760 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
761 }
762 glBegin (GL_QUADS);
763 glTexCoord2f (0, 0); glVertex2f (x , y );
764 glTexCoord2f (0, t); glVertex2f (x , y + h);
765 glTexCoord2f (s, t); glVertex2f (x + w, y + h);
766 glTexCoord2f (s, 0); glVertex2f (x + w, y );
767 glEnd ();
768 }
769
770 MODULE = CFClient PACKAGE = CFClient::Map
771
772 CFClient::Map
773 new (SV *class, int map_width, int map_height)
774 CODE:
775 New (0, RETVAL, 1, struct map);
776 RETVAL->x = 0;
777 RETVAL->y = 0;
778 RETVAL->w = map_width;
779 RETVAL->h = map_height;
780 RETVAL->ox = 0;
781 RETVAL->oy = 0;
782 RETVAL->faces = 8192;
783 Newz (0, RETVAL->face, RETVAL->faces, mapface);
784 RETVAL->texs = 8192;
785 Newz (0, RETVAL->tex, RETVAL->texs, maptex);
786 RETVAL->rows = 0;
787 RETVAL->row = 0;
788 OUTPUT:
789 RETVAL
790
791 void
792 DESTROY (CFClient::Map self)
793 CODE:
794 {
795 map_clear (self);
796 Safefree (self->face);
797 Safefree (self);
798 }
799
800 void
801 clear (CFClient::Map self)
802 CODE:
803 map_clear (self);
804
805 void
806 set_face (CFClient::Map self, int face, int texid)
807 CODE:
808 {
809 while (self->faces <= face)
810 {
811 Append (mapface, self->face, self->faces, self->faces);
812 self->faces *= 2;
813 }
814
815 self->face [face] = texid;
816 }
817
818 void
819 set_texture (CFClient::Map self, int texid, int name, int w, int h, float s, float t, int r, int g, int b, int a)
820 CODE:
821 {
822 while (self->texs <= texid)
823 {
824 Append (maptex, self->tex, self->texs, self->texs);
825 self->texs *= 2;
826 }
827
828 {
829 maptex *tex = self->tex + texid;
830
831 tex->name = name;
832 tex->w = w;
833 tex->h = h;
834 tex->s = s;
835 tex->t = t;
836 tex->r = r;
837 tex->g = g;
838 tex->b = b;
839 tex->a = a;
840 }
841 }
842
843 int
844 ox (CFClient::Map self)
845 ALIAS:
846 oy = 1
847 CODE:
848 switch (ix)
849 {
850 case 0: RETVAL = self->ox; break;
851 case 1: RETVAL = self->oy; break;
852 }
853 OUTPUT:
854 RETVAL
855
856 void
857 scroll (CFClient::Map self, int dx, int dy)
858 CODE:
859 {
860 if (dx > 0)
861 map_blank (self, self->x, self->y, dx - 1, self->h);
862 else if (dx < 0)
863 map_blank (self, self->x + self->w + dx + 1, self->y, 1 - dx, self->h);
864
865 if (dy > 0)
866 map_blank (self, self->x, self->y, self->w, dy - 1);
867 else if (dy < 0)
868 map_blank (self, self->x, self->y + self->h + dy + 1, self->w, 1 - dy);
869
870 self->ox += dx; self->x += dx;
871 self->oy += dy; self->y += dy;
872
873 while (self->y < 0)
874 {
875 Prepend (maprow, self->row, self->rows, MAP_EXTEND_Y);
876
877 self->rows += MAP_EXTEND_Y;
878 self->y += MAP_EXTEND_Y;
879 }
880 }
881
882 void
883 map1a_update (CFClient::Map self, SV *data_)
884 CODE:
885 {
886 uint8_t *data = (uint8_t *)SvPVbyte_nolen (data_);
887 uint8_t *data_end = (uint8_t *)SvEND (data_);
888 mapcell *cell;
889 int x, y, flags;
890
891 while (data < data_end)
892 {
893 flags = (data [0] << 8) + data [1]; data += 2;
894
895 x = ((flags >> 10) & 63) + self->x;
896 y = ((flags >> 4) & 63) + self->y;
897
898 cell = map_get_cell (self, x, y);
899
900 if (flags & 15)
901 {
902 if (cell->darkness < 0)
903 {
904 cell->darkness = 0;
905 cell->face [0] = 0;
906 cell->face [1] = 0;
907 cell->face [2] = 0;
908 }
909
910 cell->darkness = flags & 8 ? *data++ : 255;
911
912 //TODO: don't trust server data to be in-range(!)
913
914 if (flags & 4)
915 {
916 cell->face [0] = self->face [(data [0] << 8) + data [1]]; data += 2;
917 }
918
919 if (flags & 2)
920 {
921 cell->face [1] = self->face [(data [0] << 8) + data [1]]; data += 2;
922 }
923
924 if (flags & 1)
925 {
926 cell->face [2] = self->face [(data [0] << 8) + data [1]]; data += 2;
927 }
928 }
929 else
930 cell->darkness = -1;
931 }
932 }
933
934 SV *
935 mapmap (CFClient::Map self, int w, int h)
936 CODE:
937 {
938 int x0, x1, x;
939 int y0, y1, y;
940 int z;
941 SV *map_sv = newSV (w * h * sizeof (uint32_t));
942 uint32_t *map = (uint32_t *)SvPVX (map_sv);
943
944 SvPOK_only (map_sv);
945 SvCUR_set (map_sv, w * h * sizeof (uint32_t));
946
947 x0 = self->x - w / 2; x1 = x0 + w;
948 y0 = self->y - h / 2; y1 = y0 + h;
949
950 for (y = y0; y < y1; y++)
951 {
952 maprow *row = 0 <= y && y < self->rows
953 ? self->row + y
954 : 0;
955
956 for (x = x0; x < x1; x++)
957 {
958 int r = 32, g = 32, b = 32, a = 192;
959
960 if (row && row->c0 <= x && x < row->c1)
961 {
962 mapcell *cell = row->col + (x - row->c0);
963
964 for (z = 0; z <= 0; z++)
965 {
966 mapface face = cell->face [z];
967
968 if (face)
969 {
970 maptex tex = self->tex [face];
971 int a0 = 255 - tex.a;
972 int a1 = tex.a;
973
974 r = (r * a0 + tex.r * a1) / 255;
975 g = (g * a0 + tex.g * a1) / 255;
976 b = (b * a0 + tex.b * a1) / 255;
977 a = (a * a0 + tex.a * a1) / 255;
978 }
979 }
980 }
981
982 *map++ = (r )
983 | (g << 8)
984 | (b << 16)
985 | (a << 24);
986 }
987 }
988
989 RETVAL = map_sv;
990 }
991 OUTPUT:
992 RETVAL
993
994 void
995 draw (CFClient::Map self, int shift_x, int shift_y, int x0, int y0, int sw, int sh)
996 PPCODE:
997 {
998 int vx, vy;
999 int x, y, z;
1000 int last_name;
1001 mapface face;
1002 int sw4 = (sw + 3) & ~3;
1003 SV *darkness_sv = sv_2mortal (newSV (sw4 * sh));
1004 uint8_t *darkness = (uint8_t *)SvPVX (darkness_sv);
1005
1006 memset (darkness, 255, sw4 * sh);
1007 SvPOK_only (darkness_sv);
1008 SvCUR_set (darkness_sv, sw4 * sh);
1009
1010 vx = self->x + (self->w - sw) / 2 - shift_x;
1011 vy = self->y + (self->h - sh) / 2 - shift_y;
1012
1013 /*
1014 int vx = self->vx = self->w >= sw
1015 ? self->x + (self->w - sw) / 2
1016 : MIN (self->x, MAX (self->x + self->w - sw + 1, self->vx));
1017
1018 int vy = self->vy = self->h >= sh
1019 ? self->y + (self->h - sh) / 2
1020 : MIN (self->y, MAX (self->y + self->h - sh + 1, self->vy));
1021 */
1022
1023 glColor4ub (255, 255, 255, 255);
1024
1025 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1026 glEnable (GL_BLEND);
1027 glEnable (GL_TEXTURE_2D);
1028 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1029
1030 glBegin (GL_QUADS);
1031
1032 last_name = 0;
1033
1034 for (z = 0; z < 3; z++)
1035 for (y = 0; y < sh; y++)
1036 if (0 <= y + vy && y + vy < self->rows)
1037 {
1038 maprow *row = self->row + (y + vy);
1039
1040 for (x = 0; x < sw; x++)
1041 if (row->c0 <= x + vx && x + vx < row->c1)
1042 {
1043 mapcell *cell = row->col + (x + vx - row->c0);
1044
1045 darkness[y * sw4 + x] = cell->darkness < 0
1046 ? 255 - FOW_DARKNESS
1047 : 255 - cell->darkness;
1048
1049 face = cell->face [z];
1050
1051 if (face)
1052 {
1053 maptex tex = self->tex [face];
1054
1055 int px = (x + 1) * 32 - tex.w;
1056 int py = (y + 1) * 32 - tex.h;
1057
1058 if (last_name != tex.name)
1059 {
1060 glEnd ();
1061 last_name = tex.name;
1062 glBindTexture (GL_TEXTURE_2D, last_name);
1063 glBegin (GL_QUADS);
1064 }
1065
1066 glTexCoord2f (0 , 0 ); glVertex2f (px , py );
1067 glTexCoord2f (0 , tex.t); glVertex2f (px , py + tex.h);
1068 glTexCoord2f (tex.s, tex.t); glVertex2f (px + tex.w, py + tex.h);
1069 glTexCoord2f (tex.s, 0 ); glVertex2f (px + tex.w, py );
1070 }
1071 }
1072 }
1073
1074 glEnd ();
1075
1076 glDisable (GL_TEXTURE_2D);
1077 glDisable (GL_BLEND);
1078
1079 EXTEND (SP, 3);
1080 PUSHs (sv_2mortal (newSViv (sw4)));
1081 PUSHs (sv_2mortal (newSViv (sh)));
1082 PUSHs (darkness_sv);
1083 }
1084
1085 SV *
1086 get_rect (CFClient::Map self, int x0, int y0, int w, int h)
1087 CODE:
1088 {
1089 int x, y, x1, y1;
1090 SV *data_sv = newSV (w * h * 7 + 5);
1091 uint8_t *data = (uint8_t *)SvPVX (data_sv);
1092
1093 *data++ = 0; /* version 0 format */
1094 *data++ = w >> 8; *data++ = w;
1095 *data++ = h >> 8; *data++ = h;
1096
1097 // we need to do this 'cause we don't keep an absolute coord system for rows
1098 // TODO: treat rows as we treat
1099 map_get_row (self, y0 + self->y - self->oy);//D
1100 map_get_row (self, y0 + self->y - self->oy + h - 1);//D
1101
1102 x0 += self->x - self->ox;
1103 y0 += self->y - self->oy;
1104
1105 x1 = x0 + w;
1106 y1 = y0 + h;
1107
1108 for (y = y0; y < y1; y++)
1109 {
1110 maprow *row = 0 <= y && y < self->rows
1111 ? self->row + y
1112 : 0;
1113
1114 for (x = x0; x < x1; x++)
1115 {
1116 if (row && row->c0 <= x && x < row->c1)
1117 {
1118 mapcell *cell = row->col + (x - row->c0);
1119 uint8_t flags = 0;
1120
1121 if (cell->face [0]) flags |= 1;
1122 if (cell->face [1]) flags |= 2;
1123 if (cell->face [2]) flags |= 4;
1124
1125 *data++ = flags;
1126
1127 if (flags & 1)
1128 {
1129 *data++ = cell->face [0] >> 8;
1130 *data++ = cell->face [0];
1131 }
1132
1133 if (flags & 2)
1134 {
1135 *data++ = cell->face [1] >> 8;
1136 *data++ = cell->face [1];
1137 }
1138
1139 if (flags & 4)
1140 {
1141 *data++ = cell->face [2] >> 8;
1142 *data++ = cell->face [2];
1143 }
1144 }
1145 else
1146 *data++ = 0;
1147 }
1148 }
1149
1150 SvPOK_only (data_sv);
1151 SvCUR_set (data_sv, data - (uint8_t *)SvPVX (data_sv));
1152 RETVAL = data_sv;
1153 }
1154 OUTPUT:
1155 RETVAL
1156
1157 void
1158 set_rect (CFClient::Map self, int x0, int y0, uint8_t *data)
1159 PPCODE:
1160 {
1161 int x, y, z;
1162 int w, h;
1163 int x1, y1;
1164
1165 if (*data++ != 0)
1166 return; /* version mismatch */
1167
1168 w = *data++ << 8; w |= *data++;
1169 h = *data++ << 8; h |= *data++;
1170
1171 // we need to do this 'cause we don't keep an absolute coord system for rows
1172 // TODO: treat rows as we treat
1173 map_get_row (self, y0 + self->y - self->oy);//D
1174 map_get_row (self, y0 + self->y - self->oy + h - 1);//D
1175
1176 x0 += self->x - self->ox;
1177 y0 += self->y - self->oy;
1178
1179 x1 = x0 + w;
1180 y1 = y0 + h;
1181
1182 for (y = y0; y < y1; y++)
1183 {
1184 maprow *row = map_get_row (self, y);
1185
1186 for (x = x0; x < x1; x++)
1187 {
1188 uint8_t flags = *data++;
1189
1190 if (flags)
1191 {
1192 mapface face[3] = { 0, 0, 0 };
1193
1194 mapcell *cell = row_get_cell (row, x);
1195
1196 if (flags & 1) { face[0] = *data++ << 8; face[0] |= *data++; }
1197 if (flags & 2) { face[1] = *data++ << 8; face[1] |= *data++; }
1198 if (flags & 4) { face[2] = *data++ << 8; face[2] |= *data++; }
1199
1200 if (cell->darkness <= 0)
1201 {
1202 cell->darkness = -1;
1203
1204 for (z = 0; z <= 2; z++)
1205 {
1206 cell->face[z] = face[z];
1207
1208 if (face[z] && (face[z] >= self->texs || !self->tex[face [z]].name))
1209 XPUSHs (sv_2mortal (newSViv (face[z])));
1210 }
1211 }
1212 }
1213 }
1214 }
1215 }
1216
1217 MODULE = CFClient PACKAGE = CFClient::MixChunk
1218
1219 CFClient::MixChunk
1220 new_from_file (SV *class, char *path)
1221 CODE:
1222 RETVAL = Mix_LoadWAV (path);
1223 OUTPUT:
1224 RETVAL
1225
1226 void
1227 DESTROY (CFClient::MixChunk self)
1228 CODE:
1229 Mix_FreeChunk (self);
1230
1231 int
1232 volume (CFClient::MixChunk self, int volume = -1)
1233 CODE:
1234 RETVAL = Mix_VolumeChunk (self, volume);
1235 OUTPUT:
1236 RETVAL
1237
1238 int
1239 play (CFClient::MixChunk self, int channel = -1, int loops = 0, int ticks = -1)
1240 CODE:
1241 RETVAL = Mix_PlayChannelTimed (channel, self, loops, ticks);
1242 OUTPUT:
1243 RETVAL
1244
1245 MODULE = CFClient PACKAGE = CFClient::MixMusic
1246
1247 int
1248 volume (int volume = -1)
1249 CODE:
1250 RETVAL = Mix_VolumeMusic (volume);
1251 OUTPUT:
1252 RETVAL
1253
1254 CFClient::MixMusic
1255 new_from_file (SV *class, char *path)
1256 CODE:
1257 RETVAL = Mix_LoadMUS (path);
1258 OUTPUT:
1259 RETVAL
1260
1261 void
1262 DESTROY (CFClient::MixMusic self)
1263 CODE:
1264 Mix_FreeMusic (self);
1265
1266 int
1267 play (CFClient::MixMusic self, int loops = -1)
1268 CODE:
1269 RETVAL = Mix_PlayMusic (self, loops);
1270 OUTPUT:
1271 RETVAL
1272