ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/Client.xs
Revision: 1.47
Committed: Mon Apr 17 21:52:42 2006 UTC (18 years ago) by root
Branch: MAIN
Changes since 1.46: +1 -1 lines
Log Message:
improve \!windows config

File Contents

# User Rev Content
1 root 1.1 #include "EXTERN.h"
2     #include "perl.h"
3     #include "XSUB.h"
4    
5 root 1.5 #include <string.h>
6 root 1.25 #include <stdio.h>
7 root 1.5
8 root 1.2 #include <SDL.h>
9 root 1.23 #include <SDL_image.h>
10 root 1.3 #include <SDL_opengl.h>
11 root 1.5
12 root 1.30 #include <glib/gmacros.h>
13    
14 root 1.5 #include <pango/pango.h>
15 root 1.10 #include <pango/pangofc-fontmap.h>
16 root 1.5 #include <pango/pangoft2.h>
17    
18 root 1.10 #include <sys/types.h>
19     #include <sys/socket.h>
20     #include <netinet/in.h>
21     #include <netinet/tcp.h>
22    
23 root 1.28 #include <inttypes.h>
24    
25 root 1.29 #define FOW_DARKNESS 32
26    
27     #define MAP_EXTEND_X 32
28     #define MAP_EXTEND_Y 512
29    
30 root 1.5 static PangoContext *context;
31     static PangoFontMap *fontmap;
32 root 1.2
33 root 1.14 typedef struct cf_layout {
34     PangoLayout *pl;
35     int base_height;
36 root 1.15 } *CFClient__Layout;
37 root 1.14
38     static void
39 root 1.19 substitute_func (FcPattern *pattern, gpointer data)
40     {
41 root 1.20 FcPatternAddBool (pattern, FC_HINTING , 1);
42 root 1.35 FcPatternAddBool (pattern, FC_AUTOHINT, 0);
43 root 1.19 }
44    
45     static void
46 root 1.17 layout_update (CFClient__Layout self)
47     {
48 root 1.19 /* use a random scale factor to account for unknown descenders, 0.8 works
49     * reasonably well with bitstream vera
50     */
51 root 1.17 PangoFontDescription *font = pango_context_get_font_description (context);
52 root 1.46
53     int height = self->base_height * (PANGO_SCALE * 8 / 10);
54    
55     if (pango_font_description_get_size (font) != height)
56     {
57     pango_font_description_set_absolute_size (font, height);
58     pango_layout_context_changed (self->pl);
59     }
60 root 1.17 }
61    
62     static void
63 root 1.15 layout_get_pixel_size (CFClient__Layout self, int *w, int *h)
64 root 1.14 {
65 root 1.19 layout_update (self);
66 root 1.16
67 root 1.14 pango_layout_get_pixel_size (self->pl, w, h);
68    
69     *w = (*w + 3) & ~3;
70     if (!*w) *w = 1;
71     if (!*h) *h = 1;
72     }
73    
74 root 1.42 typedef uint16_t mapface;
75    
76 root 1.28 typedef struct {
77 root 1.30 GLint name;
78     int w, h;
79     float s, t;
80 root 1.39 uint8_t r, g, b, a;
81 root 1.42 } maptex;
82 root 1.30
83     typedef struct {
84 root 1.29 int16_t darkness;
85 root 1.42 mapface face[3];
86 root 1.28 } mapcell;
87    
88     typedef struct {
89 root 1.30 int32_t c0, c1;
90 root 1.28 mapcell *col;
91     } maprow;
92    
93     typedef struct map {
94     int x, y, w, h;
95 root 1.42 int ox, oy; /* offset to virtual global coordinate system */
96 root 1.28 int faces;
97 root 1.30 mapface *face;
98 root 1.28
99 root 1.42 int texs;
100     maptex *tex;
101    
102 root 1.28 uint32_t rows;
103     maprow *row;
104     } *CFClient__Map;
105    
106 root 1.30 static char *
107     prepend (char *ptr, int sze, int inc)
108     {
109     char *p;
110    
111     New (0, p, sze + inc, char);
112     Zero (p, inc, char);
113     Move (ptr, p + inc, sze, char);
114     Safefree (ptr);
115    
116     return p;
117     }
118    
119     static char *
120     append (char *ptr, int sze, int inc)
121     {
122     Renew (ptr, sze + inc, char);
123     Zero (ptr + sze, inc, char);
124    
125     return ptr;
126     }
127    
128     #define Append(type,ptr,sze,inc) (ptr) = (type *)append ((char *)ptr, (sze) * sizeof (type), (inc) * sizeof (type))
129     #define Prepend(type,ptr,sze,inc) (ptr) = (type *)prepend ((char *)ptr, (sze) * sizeof (type), (inc) * sizeof (type))
130    
131 root 1.42 static maprow *
132     map_get_row (CFClient__Map self, int y)
133     {
134     if (0 > y)
135     {
136     int extend = - y + MAP_EXTEND_Y;
137     Prepend (maprow, self->row, self->rows, extend);
138    
139     self->rows += extend;
140     self->y += extend;
141     y += extend;
142     }
143     else if (y >= self->rows)
144     {
145     int extend = y - self->rows + MAP_EXTEND_Y;
146     Append (maprow, self->row, self->rows, extend);
147     self->rows += extend;
148     }
149    
150     return self->row + y;
151     }
152    
153     static mapcell *
154     row_get_cell (maprow *row, int x)
155     {
156     if (!row->col)
157     {
158     Newz (0, row->col, MAP_EXTEND_X, mapcell);
159     row->c0 = x - MAP_EXTEND_X / 4;
160     row->c1 = row->c0 + MAP_EXTEND_X;
161     }
162    
163     if (row->c0 > x)
164     {
165     int extend = row->c0 - x + MAP_EXTEND_X;
166     Prepend (mapcell, row->col, row->c1 - row->c0, extend);
167     row->c0 -= extend;
168     }
169     else if (x >= row->c1)
170     {
171     int extend = x - row->c1 + MAP_EXTEND_X;
172     Append (mapcell, row->col, row->c1 - row->c0, extend);
173     row->c1 += extend;
174     }
175    
176     return row->col + (x - row->c0);
177     }
178    
179     static mapcell *
180     map_get_cell (CFClient__Map self, int x, int y)
181     {
182     return row_get_cell (map_get_row (self, y), x);
183     }
184    
185 root 1.30 static void
186     map_clear (CFClient__Map self)
187     {
188     int r;
189    
190     for (r = 0; r < self->rows; r++)
191     Safefree (self->row[r].col);
192    
193     Safefree (self->row);
194    
195     self->x = 0;
196     self->y = 0;
197 root 1.42 self->ox = 0;
198     self->oy = 0;
199 root 1.30 self->row = 0;
200     self->rows = 0;
201     }
202    
203 root 1.29 static void
204     map_blank (CFClient__Map self, int x0, int y0, int w, int h)
205     {
206     int x, y;
207    
208     for (y = y0; y < y0 + h; y++)
209 root 1.30 if (y >= 0)
210     {
211     if (y >= self->rows)
212     break;
213    
214     maprow *row = self->row + y;
215    
216     for (x = x0; x < x0 + w; x++)
217     if (x >= row->c0)
218     {
219     if (x >= row->c1)
220     break;
221 root 1.29
222 root 1.31 row->col[x - row->c0].darkness = -1;
223 root 1.30 }
224     }
225 root 1.29 }
226    
227 root 1.15 MODULE = CFClient PACKAGE = CFClient
228 root 1.1
229 root 1.11 PROTOTYPES: ENABLE
230    
231 root 1.5 BOOT:
232     {
233     fontmap = pango_ft2_font_map_new ();
234 root 1.19 pango_ft2_font_map_set_default_substitute ((PangoFT2FontMap *)fontmap, substitute_func, 0, 0);
235 root 1.8 context = pango_ft2_font_map_create_context ((PangoFT2FontMap *)fontmap);
236 root 1.5 }
237    
238 root 1.10 void
239     lowdelay (int fd, int val = 1)
240     CODE:
241     setsockopt (fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof (val));
242    
243 root 1.3 char *
244 root 1.4 gl_version ()
245     CODE:
246 root 1.5 RETVAL = (char *)glGetString (GL_VERSION);
247 root 1.4 OUTPUT:
248     RETVAL
249    
250     char *
251 root 1.3 gl_extensions ()
252     CODE:
253 root 1.5 RETVAL = (char *)glGetString (GL_EXTENSIONS);
254 root 1.3 OUTPUT:
255     RETVAL
256    
257 root 1.5 void
258 root 1.13 add_font (char *file)
259     CODE:
260     FcConfigAppFontAddFile (0, (const FcChar8 *)file); /* no idea wether this is required */
261    
262     void
263 root 1.9 set_font (char *file)
264 root 1.8 CODE:
265     {
266 root 1.9 int count;
267     FcPattern *pattern = FcFreeTypeQuery ((const FcChar8 *)file, 0, 0, &count);
268 root 1.10 PangoFontDescription *font = pango_fc_font_description_from_pattern (pattern, 0);
269 root 1.9 FcPatternDestroy (pattern);
270 root 1.8 pango_context_set_font_description (context, font);
271     }
272    
273 root 1.23 void
274     load_image_inline (SV *image_)
275     ALIAS:
276     load_image_file = 1
277     PPCODE:
278     {
279     STRLEN image_len;
280     char *image = (char *)SvPVbyte (image_, image_len);
281     SDL_Surface *surface, *surface2;
282     SDL_PixelFormat fmt;
283     SDL_RWops *rw = ix
284     ? SDL_RWFromFile (image, "r")
285     : SDL_RWFromConstMem (image, image_len);
286    
287     if (!rw)
288 root 1.41 croak ("load_image: %s", SDL_GetError ());
289 root 1.23
290     surface = IMG_Load_RW (rw, 1);
291     if (!surface)
292 root 1.41 croak ("load_image: %s", SDL_GetError ());
293 root 1.23
294     fmt.palette = NULL;
295     fmt.BitsPerPixel = 32;
296     fmt.BytesPerPixel = 4;
297     fmt.Rmask = 0x000000ff;
298     fmt.Gmask = 0x0000ff00;
299     fmt.Bmask = 0x00ff0000;
300     fmt.Amask = 0xff000000;
301     fmt.Rloss = 0;
302     fmt.Gloss = 0;
303     fmt.Bloss = 0;
304     fmt.Aloss = 0;
305     fmt.Rshift = 0;
306     fmt.Gshift = 8;
307     fmt.Bshift = 16;
308     fmt.Ashift = 24;
309     fmt.colorkey = 0;
310     fmt.alpha = 0;
311    
312     surface2 = SDL_ConvertSurface (surface, &fmt, SDL_SWSURFACE);
313    
314 root 1.39 assert (surface2->pitch == surface2->w * 4);
315    
316 root 1.23 EXTEND (SP, 5);
317     PUSHs (sv_2mortal (newSViv (surface2->w)));
318     PUSHs (sv_2mortal (newSViv (surface2->h)));
319     SDL_LockSurface (surface2);
320     PUSHs (sv_2mortal (newSVpvn (surface2->pixels, surface2->h * surface2->pitch)));
321     SDL_UnlockSurface (surface2);
322 root 1.24 PUSHs (sv_2mortal (newSViv (surface->flags & (SDL_SRCCOLORKEY | SDL_SRCALPHA) ? GL_RGBA : GL_RGB)));
323 root 1.23 PUSHs (sv_2mortal (newSViv (GL_RGBA)));
324     PUSHs (sv_2mortal (newSViv (GL_UNSIGNED_INT_8_8_8_8_REV)));
325    
326     SDL_FreeSurface (surface);
327     SDL_FreeSurface (surface2);
328     }
329    
330 root 1.25 void
331 root 1.39 average (int x, int y, uint32_t *data)
332     PPCODE:
333     {
334     uint32_t r = 0, g = 0, b = 0, a = 0;
335    
336     x = y = x * y;
337    
338     while (x--)
339     {
340     uint32_t p = *data++;
341    
342     r += (p ) & 255;
343     g += (p >> 8) & 255;
344     b += (p >> 16) & 255;
345     a += (p >> 24) & 255;
346     }
347    
348     EXTEND (SP, 4);
349 root 1.40 PUSHs (sv_2mortal (newSViv (r / y)));
350     PUSHs (sv_2mortal (newSViv (g / y)));
351     PUSHs (sv_2mortal (newSViv (b / y)));
352     PUSHs (sv_2mortal (newSViv (a / y)));
353 root 1.39 }
354    
355     void
356 root 1.25 fatal (char *message)
357     CODE:
358     #ifdef WIN32
359     MessageBox (0, message, "Crossfire+ Fatal Error", MB_OK | MB_ICONERROR | MB_SETFOREGROUND);
360     #else
361 root 1.40 fprintf (stderr, "FATAL: %s\n", message);
362 root 1.25 #endif
363     exit (1);
364    
365 root 1.15 MODULE = CFClient PACKAGE = CFClient::Layout
366 root 1.14
367 root 1.15 CFClient::Layout
368 root 1.14 new (SV *class, int base_height = 10)
369     CODE:
370     New (0, RETVAL, 1, struct cf_layout);
371     RETVAL->base_height = base_height;
372     RETVAL->pl = pango_layout_new (context);
373     pango_layout_set_wrap (RETVAL->pl, PANGO_WRAP_WORD_CHAR);
374     OUTPUT:
375     RETVAL
376    
377     void
378 root 1.15 DESTROY (CFClient::Layout self)
379 root 1.14 CODE:
380     g_object_unref (self->pl);
381     Safefree (self);
382 root 1.13
383 root 1.8 void
384 root 1.35 set_text (CFClient::Layout self, SV *text_)
385     CODE:
386     {
387     STRLEN textlen;
388     char *text = SvPVutf8 (text_, textlen);
389    
390     pango_layout_set_text (self->pl, text, textlen);
391     }
392    
393     void
394 root 1.15 set_markup (CFClient::Layout self, SV *text_)
395 root 1.14 CODE:
396 root 1.5 {
397     STRLEN textlen;
398     char *text = SvPVutf8 (text_, textlen);
399 root 1.14
400     pango_layout_set_markup (self->pl, text, textlen);
401     }
402    
403 root 1.46 SV *
404     get_text (CFClient::Layout self)
405     CODE:
406 root 1.47 RETVAL = newSVpv (pango_layout_get_text (self->pl), 0);
407 root 1.46 SvUTF8_on (RETVAL);
408     OUTPUT:
409     RETVAL
410    
411 root 1.14 void
412 root 1.16 set_height (CFClient::Layout self, int base_height)
413     CODE:
414     self->base_height = base_height;
415    
416     void
417 root 1.15 set_width (CFClient::Layout self, int max_width = -1)
418 root 1.14 CODE:
419     pango_layout_set_width (self->pl, max_width < 0 ? max_width : max_width * PANGO_SCALE);
420    
421     void
422 root 1.15 size (CFClient::Layout self)
423 root 1.14 PPCODE:
424     {
425     int w, h;
426    
427 root 1.17 layout_update (self);
428 root 1.14 layout_get_pixel_size (self, &w, &h);
429    
430     EXTEND (SP, 2);
431     PUSHs (sv_2mortal (newSViv (w)));
432     PUSHs (sv_2mortal (newSViv (h)));
433     }
434    
435 root 1.17 int
436     xy_to_index (CFClient::Layout self, int x, int y)
437     CODE:
438     {
439     int index, trailing;
440    
441     layout_update (self);
442     pango_layout_xy_to_index (self->pl, x * PANGO_SCALE, y * PANGO_SCALE, &index, &trailing);
443    
444     RETVAL = index;
445     }
446     OUTPUT:
447     RETVAL
448    
449     void
450     cursor_pos (CFClient::Layout self, int index)
451     PPCODE:
452     {
453     PangoRectangle strong_pos;
454     layout_update (self);
455     pango_layout_get_cursor_pos (self->pl, index, &strong_pos, 0);
456 root 1.30
457 root 1.17 EXTEND (SP, 3);
458     PUSHs (sv_2mortal (newSViv (strong_pos.x / PANGO_SCALE)));
459     PUSHs (sv_2mortal (newSViv (strong_pos.y / PANGO_SCALE)));
460     PUSHs (sv_2mortal (newSViv (strong_pos.height / PANGO_SCALE)));
461     }
462    
463 root 1.14 void
464 root 1.15 render (CFClient::Layout self)
465 root 1.14 PPCODE:
466     {
467 root 1.5 SV *retval;
468     int w, h;
469     FT_Bitmap bitmap;
470    
471 root 1.17 layout_update (self);
472 root 1.14 layout_get_pixel_size (self, &w, &h);
473 root 1.5
474     retval = newSV (w * h);
475     SvPOK_only (retval);
476     SvCUR_set (retval, w * h);
477    
478     bitmap.rows = h;
479     bitmap.width = w;
480     bitmap.pitch = w;
481     bitmap.buffer = (unsigned char*)SvPVX (retval);
482     bitmap.num_grays = 256;
483     bitmap.pixel_mode = FT_PIXEL_MODE_GRAY;
484    
485     memset (bitmap.buffer, 0, w * h);
486    
487 root 1.14 pango_ft2_render_layout (&bitmap, self->pl, 0 * PANGO_SCALE, 0 * PANGO_SCALE);
488 root 1.1
489 root 1.5 EXTEND (SP, 3);
490     PUSHs (sv_2mortal (newSViv (w)));
491     PUSHs (sv_2mortal (newSViv (h)));
492     PUSHs (sv_2mortal (retval));
493     }
494 root 1.11
495 root 1.15 MODULE = CFClient PACKAGE = CFClient::Texture
496 root 1.11
497     void
498 root 1.37 draw_quad (SV *self, float x, float y, float w = 0, float h = 0)
499 root 1.12 PROTOTYPE: $$$;$$
500 root 1.11 CODE:
501     {
502 root 1.12 HV *hv = (HV *)SvRV (self);
503 root 1.37 float s = SvNV (*hv_fetch (hv, "s", 1, 1));
504     float t = SvNV (*hv_fetch (hv, "t", 1, 1));
505 root 1.12 int name = SvIV (*hv_fetch (hv, "name", 4, 1));
506 elmex 1.36 int wrap_mode = SvIV (*hv_fetch (hv, "wrap_mode", 9, 1));
507 root 1.12
508     if (items < 5)
509     {
510 root 1.18 w = SvNV (*hv_fetch (hv, "w", 1, 1));
511     h = SvNV (*hv_fetch (hv, "h", 1, 1));
512 root 1.12 }
513    
514     glBindTexture (GL_TEXTURE_2D, name);
515 elmex 1.36 if (wrap_mode) {
516     glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
517     glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
518     }
519 root 1.12 glBegin (GL_QUADS);
520 root 1.37 glTexCoord2f (0, 0); glVertex2f (x , y );
521     glTexCoord2f (0, t); glVertex2f (x , y + h);
522     glTexCoord2f (s, t); glVertex2f (x + w, y + h);
523     glTexCoord2f (s, 0); glVertex2f (x + w, y );
524 root 1.12 glEnd ();
525 root 1.11 }
526 root 1.28
527     MODULE = CFClient PACKAGE = CFClient::Map
528    
529     CFClient::Map
530     new (SV *class, int map_width, int map_height)
531     CODE:
532     New (0, RETVAL, 1, struct map);
533 root 1.42 RETVAL->x = 0;
534     RETVAL->y = 0;
535     RETVAL->w = map_width;
536     RETVAL->h = map_height;
537     RETVAL->ox = 0;
538     RETVAL->oy = 0;
539 root 1.30 RETVAL->faces = 8192;
540     Newz (0, RETVAL->face, RETVAL->faces, mapface);
541 root 1.42 RETVAL->texs = 8192;
542     Newz (0, RETVAL->tex, RETVAL->texs, maptex);
543 root 1.28 RETVAL->rows = 0;
544     RETVAL->row = 0;
545     OUTPUT:
546     RETVAL
547    
548     void
549     DESTROY (CFClient::Map self)
550     CODE:
551     {
552 root 1.30 map_clear (self);
553 root 1.28 Safefree (self->face);
554 root 1.29 Safefree (self);
555     }
556    
557     void
558 root 1.30 clear (CFClient::Map self)
559     CODE:
560     map_clear (self);
561    
562     void
563 root 1.42 set_face (CFClient::Map self, int face, int texid)
564 root 1.29 CODE:
565     {
566 root 1.42 while (self->faces <= face)
567 root 1.28 {
568 root 1.30 Append (mapface, self->face, self->faces, self->faces);
569 root 1.29 self->faces *= 2;
570     }
571 root 1.28
572 root 1.42 self->face [face] = texid;
573     }
574    
575     void
576     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)
577     CODE:
578     {
579     while (self->texs <= texid)
580     {
581     Append (maptex, self->tex, self->texs, self->texs);
582     self->texs *= 2;
583     }
584    
585     maptex *tex = self->tex + texid;
586 root 1.39
587 root 1.42 tex->name = name;
588     tex->w = w;
589     tex->h = h;
590     tex->s = s;
591     tex->t = t;
592     tex->r = r;
593     tex->g = g;
594     tex->b = b;
595     tex->a = a;
596 root 1.29 }
597    
598 root 1.42 int
599     ox (CFClient::Map self)
600     ALIAS:
601     oy = 1
602     CODE:
603     switch (ix)
604     {
605     case 0: RETVAL = self->ox; break;
606     case 1: RETVAL = self->oy; break;
607     }
608     OUTPUT:
609     RETVAL
610    
611 root 1.29 void
612 root 1.43 scroll (CFClient::Map self, int dx, int dy)
613     CODE:
614     {
615 root 1.44 if (dx > 0)
616     map_blank (self, self->x, self->y, dx - 1, self->h);
617     else if (dx < 0)
618     map_blank (self, self->x + self->w + dx + 1, self->y, 1 - dx, self->h);
619    
620     if (dy > 0)
621     map_blank (self, self->x, self->y, self->w, dy - 1);
622     else if (dy < 0)
623     map_blank (self, self->x, self->y + self->h + dy + 1, self->w, 1 - dy);
624 root 1.43
625 root 1.44 self->ox += dx; self->x += dx;
626     self->oy += dy; self->y += dy;
627 root 1.43
628     while (self->y < 0)
629     {
630     Prepend (maprow, self->row, self->rows, MAP_EXTEND_Y);
631    
632     self->rows += MAP_EXTEND_Y;
633     self->y += MAP_EXTEND_Y;
634     }
635 root 1.44 }
636 root 1.43
637 root 1.44 void
638     map1a_update (CFClient::Map self, SV *data_)
639     CODE:
640     {
641 root 1.30 uint8_t *data = (uint8_t *)SvPVbyte_nolen (data_);
642     uint8_t *data_end = (uint8_t *)SvEND (data_);
643 root 1.43
644 root 1.29 while (data < data_end)
645     {
646     int flags = (data [0] << 8) + data [1]; data += 2;
647 root 1.30
648 root 1.29 int x = ((flags >> 10) & 63) + self->x;
649     int y = ((flags >> 4) & 63) + self->y;
650    
651 root 1.42 mapcell *cell = map_get_cell (self, x, y);
652 root 1.29
653     if (flags & 15)
654     {
655 root 1.45 if (cell->darkness < 0)
656 root 1.29 {
657     cell->darkness = 0;
658     cell->face [0] = 0;
659     cell->face [1] = 0;
660     cell->face [2] = 0;
661     }
662 root 1.45
663 root 1.29 cell->darkness = flags & 8 ? *data++ : 255;
664    
665 root 1.42 //TODO: don't trust server data to be in-range(!)
666    
667 root 1.29 if (flags & 4)
668     {
669 root 1.42 cell->face [0] = self->face [(data [0] << 8) + data [1]]; data += 2;
670 root 1.29 }
671    
672     if (flags & 2)
673     {
674 root 1.42 cell->face [1] = self->face [(data [0] << 8) + data [1]]; data += 2;
675 root 1.29 }
676    
677     if (flags & 1)
678     {
679 root 1.42 cell->face [2] = self->face [(data [0] << 8) + data [1]]; data += 2;
680 root 1.29 }
681     }
682     else
683 root 1.31 cell->darkness = -1;
684 root 1.29 }
685 root 1.28 }
686    
687 root 1.40 SV *
688     mapmap (CFClient::Map self, int w, int h)
689     CODE:
690     {
691     int x0, x1, x;
692     int y0, y1, y;
693     int z;
694     SV *map_sv = newSV (w * h * sizeof (uint32_t));
695     uint32_t *map = (uint32_t *)SvPVX (map_sv);
696    
697     SvPOK_only (map_sv);
698     SvCUR_set (map_sv, w * h * sizeof (uint32_t));
699    
700     x0 = self->x - w / 2; x1 = x0 + w;
701     y0 = self->y - h / 2; y1 = y0 + h;
702    
703     for (y = y0; y < y1; y++)
704     {
705     maprow *row = 0 <= y && y < self->rows
706     ? self->row + y
707     : 0;
708    
709     for (x = x0; x < x1; x++)
710     {
711     int r = 32, g = 32, b = 32, a = 192;
712    
713     if (row && row->c0 <= x && x < row->c1)
714     {
715     mapcell *cell = row->col + (x - row->c0);
716    
717     for (z = 0; z <= 0; z++)
718     {
719 root 1.42 mapface face = cell->face [z];
720 root 1.40
721     if (face)
722     {
723 root 1.42 maptex tex = self->tex [face];
724 root 1.40 int a0 = 255 - tex.a;
725     int a1 = tex.a;
726    
727     r = (r * a0 + tex.r * a1) / 255;
728     g = (g * a0 + tex.g * a1) / 255;
729     b = (b * a0 + tex.b * a1) / 255;
730     a = (a * a0 + tex.a * a1) / 255;
731     }
732     }
733     }
734    
735     *map++ = (r )
736     | (g << 8)
737     | (b << 16)
738     | (a << 24);
739     }
740     }
741    
742     RETVAL = map_sv;
743     }
744     OUTPUT:
745     RETVAL
746    
747 root 1.30 void
748 root 1.38 draw (CFClient::Map self, int shift_x, int shift_y, int x0, int y0, int sw, int sh)
749 root 1.32 PPCODE:
750 root 1.30 {
751 root 1.32 int sw4 = (sw + 3) & ~3;
752     SV *darkness_sv = sv_2mortal (newSV (sw4 * sh));
753 root 1.35 uint8_t *darkness = (uint8_t *)SvPVX (darkness_sv);
754 root 1.42 memset (darkness, 255, sw4 * sh);
755 root 1.32 SvPOK_only (darkness_sv);
756     SvCUR_set (darkness_sv, sw4 * sh);
757    
758 root 1.38 int vx = self->x + (self->w - sw) / 2 - shift_x;
759     int vy = self->y + (self->h - sh) / 2 - shift_y;
760    
761 root 1.42 /*
762     int vx = self->vx = self->w >= sw
763     ? self->x + (self->w - sw) / 2
764     : MIN (self->x, MAX (self->x + self->w - sw + 1, self->vx));
765    
766     int vy = self->vy = self->h >= sh
767     ? self->y + (self->h - sh) / 2
768     : MIN (self->y, MAX (self->y + self->h - sh + 1, self->vy));
769     */
770 root 1.30
771     glColor4ub (255, 255, 255, 255);
772    
773     glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
774     glEnable (GL_BLEND);
775     glEnable (GL_TEXTURE_2D);
776     glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
777    
778     int x, y, z;
779    
780     int last_name = 0;
781    
782     glBegin (GL_QUADS);
783    
784     for (z = 0; z < 3; z++)
785     for (y = 0; y < sh; y++)
786     if (0 <= y + vy && y + vy < self->rows)
787     {
788     maprow *row = self->row + (y + vy);
789    
790     for (x = 0; x < sw; x++)
791     if (row->c0 <= x + vx && x + vx < row->c1)
792     {
793     mapcell *cell = row->col + (x + vx - row->c0);
794 root 1.32
795     darkness[y * sw4 + x] = cell->darkness < 0
796 root 1.33 ? 255 - FOW_DARKNESS
797 root 1.32 : 255 - cell->darkness;
798    
799 root 1.42 mapface face = cell->face [z];
800 root 1.30
801     if (face)
802     {
803 root 1.42 maptex tex = self->tex [face];
804 root 1.30
805     int px = (x + 1) * 32 - tex.w;
806     int py = (y + 1) * 32 - tex.h;
807    
808     if (last_name != tex.name)
809     {
810     glEnd ();
811     last_name = tex.name;
812     glBindTexture (GL_TEXTURE_2D, last_name);
813     glBegin (GL_QUADS);
814     }
815    
816     glTexCoord2f (0 , 0 ); glVertex2f (px , py );
817     glTexCoord2f (0 , tex.t); glVertex2f (px , py + tex.h);
818     glTexCoord2f (tex.s, tex.t); glVertex2f (px + tex.w, py + tex.h);
819     glTexCoord2f (tex.s, 0 ); glVertex2f (px + tex.w, py );
820     }
821     }
822     }
823    
824     glEnd ();
825 root 1.32
826 root 1.34 glDisable (GL_TEXTURE_2D);
827     glDisable (GL_BLEND);
828    
829 root 1.32 EXTEND (SP, 3);
830     PUSHs (sv_2mortal (newSViv (sw4)));
831     PUSHs (sv_2mortal (newSViv (sh)));
832     PUSHs (darkness_sv);
833 root 1.30 }
834    
835 root 1.42 SV *
836     get_rect (CFClient::Map self, int x0, int y0, int w, int h)
837     CODE:
838     {
839     int x, y, x1, y1;
840     SV *data_sv = newSV (w * h * 7 + 5);
841     uint8_t *data = (uint8_t *)SvPVX (data_sv);
842    
843     *data++ = 0; /* version 0 format */
844     *data++ = w >> 8; *data++ = w;
845     *data++ = h >> 8; *data++ = h;
846    
847     // we need to do this 'cause we don't keep an absolute coord system for rows
848     // TODO: treat rows as we treat
849     map_get_row (self, y0 + self->y - self->oy);//D
850     map_get_row (self, y0 + self->y - self->oy + h - 1);//D
851    
852     x0 += self->x - self->ox;
853     y0 += self->y - self->oy;
854    
855     x1 = x0 + w;
856     y1 = y0 + h;
857    
858     for (y = y0; y < y1; y++)
859     {
860     maprow *row = 0 <= y && y < self->rows
861     ? self->row + y
862     : 0;
863    
864     for (x = x0; x < x1; x++)
865     {
866     if (row && row->c0 <= x && x < row->c1)
867     {
868     mapcell *cell = row->col + (x - row->c0);
869    
870     uint8_t flags = 0;
871    
872     if (cell->face [0]) flags |= 1;
873     if (cell->face [1]) flags |= 2;
874     if (cell->face [2]) flags |= 4;
875    
876     *data++ = flags;
877    
878     if (flags & 1)
879     {
880     *data++ = cell->face [0] >> 8;
881     *data++ = cell->face [0];
882     }
883    
884     if (flags & 2)
885     {
886     *data++ = cell->face [1] >> 8;
887     *data++ = cell->face [1];
888     }
889    
890     if (flags & 4)
891     {
892     *data++ = cell->face [2] >> 8;
893     *data++ = cell->face [2];
894     }
895     }
896     else
897     *data++ = 0;
898     }
899     }
900    
901     SvPOK_only (data_sv);
902     SvCUR_set (data_sv, data - (uint8_t *)SvPVX (data_sv));
903     RETVAL = data_sv;
904     }
905     OUTPUT:
906     RETVAL
907    
908     void
909     set_rect (CFClient::Map self, int x0, int y0, uint8_t *data)
910     PPCODE:
911     {
912     int x, y, z;
913     int x1, y1;
914    
915     if (*data++ != 0)
916     return; /* version mismatch */
917    
918     int w = *data++ << 8; w |= *data++;
919     int h = *data++ << 8; h |= *data++;
920    
921     // we need to do this 'cause we don't keep an absolute coord system for rows
922     // TODO: treat rows as we treat
923     map_get_row (self, y0 + self->y - self->oy);//D
924     map_get_row (self, y0 + self->y - self->oy + h - 1);//D
925    
926     x0 += self->x - self->ox;
927     y0 += self->y - self->oy;
928    
929     x1 = x0 + w;
930     y1 = y0 + h;
931    
932     for (y = y0; y < y1; y++)
933     {
934     maprow *row = map_get_row (self, y);
935    
936     for (x = x0; x < x1; x++)
937     {
938     uint8_t flags = *data++;
939    
940     if (flags)
941     {
942     mapface face[3] = { 0, 0, 0 };
943    
944     mapcell *cell = row_get_cell (row, x);
945    
946     if (flags & 1) { face[0] = *data++ << 8; face[0] |= *data++; }
947     if (flags & 2) { face[1] = *data++ << 8; face[1] |= *data++; }
948     if (flags & 4) { face[2] = *data++ << 8; face[2] |= *data++; }
949    
950     if (cell->darkness <= 0)
951     {
952     cell->darkness = -1;
953    
954     for (z = 0; z <= 2; z++)
955     {
956     cell->face[z] = face[z];
957    
958     if (face[z] && (face[z] >= self->texs || !self->tex[face [z]].name))
959     XPUSHs (sv_2mortal (newSViv (face[z])));
960     }
961     }
962     }
963     }
964     }
965     }
966