ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/Client.xs
Revision: 1.51
Committed: Wed Apr 19 20:46:44 2006 UTC (18 years ago) by root
Branch: MAIN
Changes since 1.50: +163 -3 lines
Log Message:
got rid of base SDL perl module

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