|
|
1 | #ifdef _WIN32 |
|
|
2 | # include <malloc.h> |
|
|
3 | #endif |
|
|
4 | |
1 | #include "EXTERN.h" |
5 | #include "EXTERN.h" |
2 | #include "perl.h" |
6 | #include "perl.h" |
3 | #include "XSUB.h" |
7 | #include "XSUB.h" |
4 | |
8 | |
5 | #include <string.h> |
9 | #include <string.h> |
… | |
… | |
13 | |
17 | |
14 | #include <pango/pango.h> |
18 | #include <pango/pango.h> |
15 | #include <pango/pangofc-fontmap.h> |
19 | #include <pango/pangofc-fontmap.h> |
16 | #include <pango/pangoft2.h> |
20 | #include <pango/pangoft2.h> |
17 | |
21 | |
|
|
22 | #ifndef _WIN32 |
18 | #include <sys/types.h> |
23 | # include <sys/types.h> |
19 | #include <sys/socket.h> |
24 | # include <sys/socket.h> |
20 | #include <netinet/in.h> |
25 | # include <netinet/in.h> |
21 | #include <netinet/tcp.h> |
26 | # include <netinet/tcp.h> |
22 | |
|
|
23 | #include <inttypes.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 |
24 | |
36 | |
25 | #define FOW_DARKNESS 32 |
37 | #define FOW_DARKNESS 32 |
26 | |
38 | |
27 | #define MAP_EXTEND_X 32 |
39 | #define MAP_EXTEND_X 32 |
28 | #define MAP_EXTEND_Y 512 |
40 | #define MAP_EXTEND_Y 512 |
… | |
… | |
47 | { |
59 | { |
48 | /* use a random scale factor to account for unknown descenders, 0.8 works |
60 | /* use a random scale factor to account for unknown descenders, 0.8 works |
49 | * reasonably well with bitstream vera |
61 | * reasonably well with bitstream vera |
50 | */ |
62 | */ |
51 | PangoFontDescription *font = pango_context_get_font_description (context); |
63 | PangoFontDescription *font = pango_context_get_font_description (context); |
52 | pango_font_description_set_absolute_size (font, self->base_height * (PANGO_SCALE * 8 / 10)); |
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 | } |
53 | } |
72 | } |
54 | |
73 | |
55 | static void |
74 | static void |
56 | layout_get_pixel_size (CFClient__Layout self, int *w, int *h) |
75 | layout_get_pixel_size (CFClient__Layout self, int *w, int *h) |
57 | { |
76 | { |
… | |
… | |
61 | |
80 | |
62 | *w = (*w + 3) & ~3; |
81 | *w = (*w + 3) & ~3; |
63 | if (!*w) *w = 1; |
82 | if (!*w) *w = 1; |
64 | if (!*h) *h = 1; |
83 | if (!*h) *h = 1; |
65 | } |
84 | } |
|
|
85 | |
|
|
86 | typedef uint16_t mapface; |
66 | |
87 | |
67 | typedef struct { |
88 | typedef struct { |
68 | GLint name; |
89 | GLint name; |
69 | int w, h; |
90 | int w, h; |
70 | float s, t; |
91 | float s, t; |
71 | } mapface; |
92 | uint8_t r, g, b, a; |
|
|
93 | } maptex; |
72 | |
94 | |
73 | typedef struct { |
95 | typedef struct { |
74 | int16_t darkness; |
96 | int16_t darkness; |
75 | uint16_t face[3]; |
97 | mapface face[3]; |
76 | } mapcell; |
98 | } mapcell; |
77 | |
99 | |
78 | typedef struct { |
100 | typedef struct { |
79 | int32_t c0, c1; |
101 | int32_t c0, c1; |
80 | mapcell *col; |
102 | mapcell *col; |
81 | } maprow; |
103 | } maprow; |
82 | |
104 | |
83 | typedef struct map { |
105 | typedef struct map { |
84 | int x, y, w, h; |
106 | int x, y, w, h; |
85 | int vx, vy; |
107 | int ox, oy; /* offset to virtual global coordinate system */ |
86 | int faces; |
108 | int faces; |
87 | mapface *face; |
109 | mapface *face; |
88 | |
110 | |
|
|
111 | int texs; |
|
|
112 | maptex *tex; |
|
|
113 | |
89 | uint32_t rows; |
114 | int32_t rows; |
90 | maprow *row; |
115 | maprow *row; |
91 | } *CFClient__Map; |
116 | } *CFClient__Map; |
92 | |
117 | |
93 | static char * |
118 | static char * |
94 | prepend (char *ptr, int sze, int inc) |
119 | prepend (char *ptr, int sze, int inc) |
… | |
… | |
113 | } |
138 | } |
114 | |
139 | |
115 | #define Append(type,ptr,sze,inc) (ptr) = (type *)append ((char *)ptr, (sze) * sizeof (type), (inc) * sizeof (type)) |
140 | #define Append(type,ptr,sze,inc) (ptr) = (type *)append ((char *)ptr, (sze) * sizeof (type), (inc) * sizeof (type)) |
116 | #define Prepend(type,ptr,sze,inc) (ptr) = (type *)prepend ((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)) |
117 | |
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 | |
118 | static void |
197 | static void |
119 | map_clear (CFClient__Map self) |
198 | map_clear (CFClient__Map self) |
120 | { |
199 | { |
121 | int r; |
200 | int r; |
122 | |
201 | |
… | |
… | |
125 | |
204 | |
126 | Safefree (self->row); |
205 | Safefree (self->row); |
127 | |
206 | |
128 | self->x = 0; |
207 | self->x = 0; |
129 | self->y = 0; |
208 | self->y = 0; |
130 | self->vx = 0; |
209 | self->ox = 0; |
131 | self->vy = 0; |
210 | self->oy = 0; |
132 | self->row = 0; |
211 | self->row = 0; |
133 | self->rows = 0; |
212 | self->rows = 0; |
134 | } |
213 | } |
135 | |
214 | |
136 | static void |
215 | static void |
137 | map_blank (CFClient__Map self, int x0, int y0, int w, int h) |
216 | map_blank (CFClient__Map self, int x0, int y0, int w, int h) |
138 | { |
217 | { |
139 | int x, y; |
218 | int x, y; |
|
|
219 | maprow *row; |
140 | |
220 | |
141 | for (y = y0; y < y0 + h; y++) |
221 | for (y = y0; y < y0 + h; y++) |
142 | if (y >= 0) |
222 | if (y >= 0) |
143 | { |
223 | { |
144 | if (y >= self->rows) |
224 | if (y >= self->rows) |
145 | break; |
225 | break; |
146 | |
226 | |
147 | maprow *row = self->row + y; |
227 | row = self->row + y; |
148 | |
228 | |
149 | for (x = x0; x < x0 + w; x++) |
229 | for (x = x0; x < x0 + w; x++) |
150 | if (x >= row->c0) |
230 | if (x >= row->c0) |
151 | { |
231 | { |
152 | if (x >= row->c1) |
232 | if (x >= row->c1) |
… | |
… | |
169 | } |
249 | } |
170 | |
250 | |
171 | void |
251 | void |
172 | lowdelay (int fd, int val = 1) |
252 | lowdelay (int fd, int val = 1) |
173 | CODE: |
253 | CODE: |
|
|
254 | #ifndef _WIN32 |
174 | setsockopt (fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof (val)); |
255 | setsockopt (fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof (val)); |
|
|
256 | #endif |
175 | |
257 | |
176 | char * |
258 | char * |
177 | gl_version () |
259 | gl_version () |
178 | CODE: |
260 | CODE: |
179 | RETVAL = (char *)glGetString (GL_VERSION); |
261 | RETVAL = (char *)glGetString (GL_VERSION); |
… | |
… | |
216 | SDL_RWops *rw = ix |
298 | SDL_RWops *rw = ix |
217 | ? SDL_RWFromFile (image, "r") |
299 | ? SDL_RWFromFile (image, "r") |
218 | : SDL_RWFromConstMem (image, image_len); |
300 | : SDL_RWFromConstMem (image, image_len); |
219 | |
301 | |
220 | if (!rw) |
302 | if (!rw) |
221 | croak ("load_image: unable to open file"); |
303 | croak ("load_image: %s", SDL_GetError ()); |
222 | |
304 | |
223 | surface = IMG_Load_RW (rw, 1); |
305 | surface = IMG_Load_RW (rw, 1); |
224 | if (!surface) |
306 | if (!surface) |
225 | croak ("load_image: unable to read file"); |
307 | croak ("load_image: %s", SDL_GetError ()); |
226 | |
308 | |
227 | fmt.palette = NULL; |
309 | fmt.palette = NULL; |
228 | fmt.BitsPerPixel = 32; |
310 | fmt.BitsPerPixel = 32; |
229 | fmt.BytesPerPixel = 4; |
311 | fmt.BytesPerPixel = 4; |
|
|
312 | #if SDL_BYTEORDER == SDL_LIL_ENDIAN |
230 | fmt.Rmask = 0x000000ff; |
313 | fmt.Rmask = 0x000000ff; |
231 | fmt.Gmask = 0x0000ff00; |
314 | fmt.Gmask = 0x0000ff00; |
232 | fmt.Bmask = 0x00ff0000; |
315 | fmt.Bmask = 0x00ff0000; |
233 | fmt.Amask = 0xff000000; |
316 | fmt.Amask = 0xff000000; |
|
|
317 | #else |
|
|
318 | fmt.Rmask = 0xff000000; |
|
|
319 | fmt.Gmask = 0x00ff0000; |
|
|
320 | fmt.Bmask = 0x0000ff00; |
|
|
321 | fmt.Amask = 0x000000ff; |
|
|
322 | #endif |
234 | fmt.Rloss = 0; |
323 | fmt.Rloss = 0; |
235 | fmt.Gloss = 0; |
324 | fmt.Gloss = 0; |
236 | fmt.Bloss = 0; |
325 | fmt.Bloss = 0; |
237 | fmt.Aloss = 0; |
326 | fmt.Aloss = 0; |
238 | fmt.Rshift = 0; |
327 | fmt.Rshift = 0; |
… | |
… | |
242 | fmt.colorkey = 0; |
331 | fmt.colorkey = 0; |
243 | fmt.alpha = 0; |
332 | fmt.alpha = 0; |
244 | |
333 | |
245 | surface2 = SDL_ConvertSurface (surface, &fmt, SDL_SWSURFACE); |
334 | surface2 = SDL_ConvertSurface (surface, &fmt, SDL_SWSURFACE); |
246 | |
335 | |
|
|
336 | assert (surface2->pitch == surface2->w * 4); |
|
|
337 | |
247 | EXTEND (SP, 5); |
338 | EXTEND (SP, 5); |
248 | PUSHs (sv_2mortal (newSViv (surface2->w))); |
339 | PUSHs (sv_2mortal (newSViv (surface2->w))); |
249 | PUSHs (sv_2mortal (newSViv (surface2->h))); |
340 | PUSHs (sv_2mortal (newSViv (surface2->h))); |
250 | SDL_LockSurface (surface2); |
341 | SDL_LockSurface (surface2); |
251 | PUSHs (sv_2mortal (newSVpvn (surface2->pixels, surface2->h * surface2->pitch))); |
342 | PUSHs (sv_2mortal (newSVpvn (surface2->pixels, surface2->h * surface2->pitch))); |
252 | SDL_UnlockSurface (surface2); |
343 | SDL_UnlockSurface (surface2); |
253 | PUSHs (sv_2mortal (newSViv (surface->flags & (SDL_SRCCOLORKEY | SDL_SRCALPHA) ? GL_RGBA : GL_RGB))); |
344 | PUSHs (sv_2mortal (newSViv (surface->flags & (SDL_SRCCOLORKEY | SDL_SRCALPHA) ? GL_RGBA : GL_RGB))); |
254 | PUSHs (sv_2mortal (newSViv (GL_RGBA))); |
345 | PUSHs (sv_2mortal (newSViv (GL_RGBA))); |
255 | PUSHs (sv_2mortal (newSViv (GL_UNSIGNED_INT_8_8_8_8_REV))); |
346 | PUSHs (sv_2mortal (newSViv (GL_UNSIGNED_BYTE))); |
256 | |
347 | |
257 | SDL_FreeSurface (surface); |
348 | SDL_FreeSurface (surface); |
258 | SDL_FreeSurface (surface2); |
349 | SDL_FreeSurface (surface2); |
|
|
350 | } |
|
|
351 | |
|
|
352 | void |
|
|
353 | average (int x, int y, uint32_t *data) |
|
|
354 | PPCODE: |
|
|
355 | { |
|
|
356 | uint32_t r = 0, g = 0, b = 0, a = 0; |
|
|
357 | |
|
|
358 | x = y = x * y; |
|
|
359 | |
|
|
360 | while (x--) |
|
|
361 | { |
|
|
362 | uint32_t p = *data++; |
|
|
363 | |
|
|
364 | r += (p ) & 255; |
|
|
365 | g += (p >> 8) & 255; |
|
|
366 | b += (p >> 16) & 255; |
|
|
367 | a += (p >> 24) & 255; |
|
|
368 | } |
|
|
369 | |
|
|
370 | EXTEND (SP, 4); |
|
|
371 | PUSHs (sv_2mortal (newSViv (r / y))); |
|
|
372 | PUSHs (sv_2mortal (newSViv (g / y))); |
|
|
373 | PUSHs (sv_2mortal (newSViv (b / y))); |
|
|
374 | PUSHs (sv_2mortal (newSViv (a / y))); |
259 | } |
375 | } |
260 | |
376 | |
261 | void |
377 | void |
262 | fatal (char *message) |
378 | fatal (char *message) |
263 | CODE: |
379 | CODE: |
264 | #ifdef WIN32 |
380 | #ifdef WIN32 |
265 | MessageBox (0, message, "Crossfire+ Fatal Error", MB_OK | MB_ICONERROR | MB_SETFOREGROUND); |
381 | MessageBox (0, message, "Crossfire+ Fatal Error", MB_OK | MB_ICONERROR | MB_SETFOREGROUND); |
266 | #else |
382 | #else |
267 | fprintf (stderr, "%s\n", message); |
383 | fprintf (stderr, "FATAL: %s\n", message); |
268 | #endif |
384 | #endif |
269 | exit (1); |
385 | exit (1); |
270 | |
386 | |
271 | MODULE = CFClient PACKAGE = CFClient::Layout |
387 | MODULE = CFClient PACKAGE = CFClient::Layout |
272 | |
388 | |
… | |
… | |
303 | STRLEN textlen; |
419 | STRLEN textlen; |
304 | char *text = SvPVutf8 (text_, textlen); |
420 | char *text = SvPVutf8 (text_, textlen); |
305 | |
421 | |
306 | pango_layout_set_markup (self->pl, text, textlen); |
422 | pango_layout_set_markup (self->pl, text, textlen); |
307 | } |
423 | } |
|
|
424 | |
|
|
425 | SV * |
|
|
426 | get_text (CFClient::Layout self) |
|
|
427 | CODE: |
|
|
428 | RETVAL = newSVpv (pango_layout_get_text (self->pl), 0); |
|
|
429 | SvUTF8_on (RETVAL); |
|
|
430 | OUTPUT: |
|
|
431 | RETVAL |
308 | |
432 | |
309 | void |
433 | void |
310 | set_height (CFClient::Layout self, int base_height) |
434 | set_height (CFClient::Layout self, int base_height) |
311 | CODE: |
435 | CODE: |
312 | self->base_height = base_height; |
436 | self->base_height = base_height; |
… | |
… | |
426 | |
550 | |
427 | CFClient::Map |
551 | CFClient::Map |
428 | new (SV *class, int map_width, int map_height) |
552 | new (SV *class, int map_width, int map_height) |
429 | CODE: |
553 | CODE: |
430 | New (0, RETVAL, 1, struct map); |
554 | New (0, RETVAL, 1, struct map); |
431 | RETVAL->x = 0; |
555 | RETVAL->x = 0; |
432 | RETVAL->y = 0; |
556 | RETVAL->y = 0; |
433 | RETVAL->w = map_width; |
557 | RETVAL->w = map_width; |
434 | RETVAL->h = map_height; |
558 | RETVAL->h = map_height; |
435 | RETVAL->vx = 0; |
559 | RETVAL->ox = 0; |
436 | RETVAL->vy = 0; |
560 | RETVAL->oy = 0; |
437 | RETVAL->faces = 8192; |
561 | RETVAL->faces = 8192; |
438 | Newz (0, RETVAL->face, RETVAL->faces, mapface); |
562 | Newz (0, RETVAL->face, RETVAL->faces, mapface); |
|
|
563 | RETVAL->texs = 8192; |
|
|
564 | Newz (0, RETVAL->tex, RETVAL->texs, maptex); |
439 | RETVAL->rows = 0; |
565 | RETVAL->rows = 0; |
440 | RETVAL->row = 0; |
566 | RETVAL->row = 0; |
441 | OUTPUT: |
567 | OUTPUT: |
442 | RETVAL |
568 | RETVAL |
443 | |
569 | |
… | |
… | |
454 | clear (CFClient::Map self) |
580 | clear (CFClient::Map self) |
455 | CODE: |
581 | CODE: |
456 | map_clear (self); |
582 | map_clear (self); |
457 | |
583 | |
458 | void |
584 | void |
459 | set_texture (CFClient::Map self, int face, int name, int w, int h, float s, float t) |
585 | set_face (CFClient::Map self, int face, int texid) |
460 | CODE: |
586 | CODE: |
461 | { |
587 | { |
462 | while (self->faces < face) |
588 | while (self->faces <= face) |
463 | { |
589 | { |
464 | Append (mapface, self->face, self->faces, self->faces); |
590 | Append (mapface, self->face, self->faces, self->faces); |
465 | self->faces *= 2; |
591 | self->faces *= 2; |
466 | } |
592 | } |
467 | |
593 | |
468 | self->face [face].name = name; |
|
|
469 | self->face [face].w = w; |
594 | self->face [face] = texid; |
470 | self->face [face].h = h; |
|
|
471 | self->face [face].s = s; |
|
|
472 | self->face [face].t = t; |
|
|
473 | } |
595 | } |
|
|
596 | |
|
|
597 | void |
|
|
598 | 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) |
|
|
599 | CODE: |
|
|
600 | { |
|
|
601 | while (self->texs <= texid) |
|
|
602 | { |
|
|
603 | Append (maptex, self->tex, self->texs, self->texs); |
|
|
604 | self->texs *= 2; |
|
|
605 | } |
|
|
606 | |
|
|
607 | { |
|
|
608 | maptex *tex = self->tex + texid; |
|
|
609 | |
|
|
610 | tex->name = name; |
|
|
611 | tex->w = w; |
|
|
612 | tex->h = h; |
|
|
613 | tex->s = s; |
|
|
614 | tex->t = t; |
|
|
615 | tex->r = r; |
|
|
616 | tex->g = g; |
|
|
617 | tex->b = b; |
|
|
618 | tex->a = a; |
|
|
619 | } |
|
|
620 | } |
|
|
621 | |
|
|
622 | int |
|
|
623 | ox (CFClient::Map self) |
|
|
624 | ALIAS: |
|
|
625 | oy = 1 |
|
|
626 | CODE: |
|
|
627 | switch (ix) |
|
|
628 | { |
|
|
629 | case 0: RETVAL = self->ox; break; |
|
|
630 | case 1: RETVAL = self->oy; break; |
|
|
631 | } |
|
|
632 | OUTPUT: |
|
|
633 | RETVAL |
474 | |
634 | |
475 | void |
635 | void |
476 | scroll (CFClient::Map self, int dx, int dy) |
636 | scroll (CFClient::Map self, int dx, int dy) |
477 | CODE: |
637 | CODE: |
478 | { |
638 | { |
… | |
… | |
484 | if (dy > 0) |
644 | if (dy > 0) |
485 | map_blank (self, self->x, self->y, self->w, dy - 1); |
645 | map_blank (self, self->x, self->y, self->w, dy - 1); |
486 | else if (dy < 0) |
646 | else if (dy < 0) |
487 | map_blank (self, self->x, self->y + self->h + dy + 1, self->w, 1 - dy); |
647 | map_blank (self, self->x, self->y + self->h + dy + 1, self->w, 1 - dy); |
488 | |
648 | |
489 | self->x += dx; |
649 | self->ox += dx; self->x += dx; |
490 | self->y += dy; |
650 | self->oy += dy; self->y += dy; |
491 | |
651 | |
492 | while (self->y < 0) |
652 | while (self->y < 0) |
493 | { |
653 | { |
494 | Prepend (maprow, self->row, self->rows, MAP_EXTEND_Y); |
654 | Prepend (maprow, self->row, self->rows, MAP_EXTEND_Y); |
495 | |
655 | |
… | |
… | |
502 | map1a_update (CFClient::Map self, SV *data_) |
662 | map1a_update (CFClient::Map self, SV *data_) |
503 | CODE: |
663 | CODE: |
504 | { |
664 | { |
505 | uint8_t *data = (uint8_t *)SvPVbyte_nolen (data_); |
665 | uint8_t *data = (uint8_t *)SvPVbyte_nolen (data_); |
506 | uint8_t *data_end = (uint8_t *)SvEND (data_); |
666 | uint8_t *data_end = (uint8_t *)SvEND (data_); |
507 | |
667 | mapcell *cell; |
|
|
668 | int x, y, flags; |
|
|
669 | |
508 | while (data < data_end) |
670 | while (data < data_end) |
509 | { |
671 | { |
510 | int flags = (data [0] << 8) + data [1]; data += 2; |
672 | flags = (data [0] << 8) + data [1]; data += 2; |
511 | |
673 | |
512 | int x = ((flags >> 10) & 63) + self->x; |
674 | x = ((flags >> 10) & 63) + self->x; |
513 | int y = ((flags >> 4) & 63) + self->y; |
675 | y = ((flags >> 4) & 63) + self->y; |
514 | |
676 | |
515 | while (y >= self->rows) |
677 | cell = map_get_cell (self, x, y); |
516 | { |
|
|
517 | Prepend (maprow, self->row, self->rows, MAP_EXTEND_Y); |
|
|
518 | |
|
|
519 | self->rows += MAP_EXTEND_Y; |
|
|
520 | } |
|
|
521 | |
|
|
522 | maprow *row = self->row + y; |
|
|
523 | |
|
|
524 | if (!row->col) |
|
|
525 | { |
|
|
526 | Newz (0, row->col, MAP_EXTEND_X, mapcell); |
|
|
527 | row->c0 = self->x - MAP_EXTEND_X / 4; |
|
|
528 | row->c1 = row->c0 + MAP_EXTEND_X; |
|
|
529 | } |
|
|
530 | |
|
|
531 | if (row->c0 > x) |
|
|
532 | { |
|
|
533 | int extend = row->c0 - x + MAP_EXTEND_X; |
|
|
534 | Prepend (mapcell, row->col, row->c1 - row->c0, extend); |
|
|
535 | row->c0 -= extend; |
|
|
536 | } |
|
|
537 | else if (x >= row->c1) |
|
|
538 | { |
|
|
539 | int extend = x - row->c1 + MAP_EXTEND_X; |
|
|
540 | Append (mapcell, row->col, row->c1 - row->c0, extend); |
|
|
541 | row->c1 += extend; |
|
|
542 | } |
|
|
543 | |
|
|
544 | assert (y >= 0); |
|
|
545 | assert (y < self->rows); |
|
|
546 | assert (x >= row->c0); |
|
|
547 | assert (x < row->c1); |
|
|
548 | mapcell *cell = row->col + (x - row->c0); |
|
|
549 | |
678 | |
550 | if (flags & 15) |
679 | if (flags & 15) |
551 | { |
680 | { |
552 | if (cell->darkness < 0 && x < self->w && y < self->h) |
681 | if (cell->darkness < 0) |
553 | { |
682 | { |
554 | cell->darkness = 0; |
683 | cell->darkness = 0; |
555 | cell->face [0] = 0; |
684 | cell->face [0] = 0; |
556 | cell->face [1] = 0; |
685 | cell->face [1] = 0; |
557 | cell->face [2] = 0; |
686 | cell->face [2] = 0; |
558 | } |
687 | } |
559 | |
688 | |
560 | cell->darkness = flags & 8 ? *data++ : 255; |
689 | cell->darkness = flags & 8 ? *data++ : 255; |
|
|
690 | |
|
|
691 | //TODO: don't trust server data to be in-range(!) |
561 | |
692 | |
562 | if (flags & 4) |
693 | if (flags & 4) |
563 | { |
694 | { |
564 | cell->face [0] = (data [0] << 8) + data [1]; data += 2; |
695 | cell->face [0] = self->face [(data [0] << 8) + data [1]]; data += 2; |
565 | } |
696 | } |
566 | |
697 | |
567 | if (flags & 2) |
698 | if (flags & 2) |
568 | { |
699 | { |
569 | cell->face [1] = (data [0] << 8) + data [1]; data += 2; |
700 | cell->face [1] = self->face [(data [0] << 8) + data [1]]; data += 2; |
570 | } |
701 | } |
571 | |
702 | |
572 | if (flags & 1) |
703 | if (flags & 1) |
573 | { |
704 | { |
574 | cell->face [2] = (data [0] << 8) + data [1]; data += 2; |
705 | cell->face [2] = self->face [(data [0] << 8) + data [1]]; data += 2; |
575 | } |
706 | } |
576 | } |
707 | } |
577 | else |
708 | else |
578 | cell->darkness = -1; |
709 | cell->darkness = -1; |
579 | } |
710 | } |
580 | } |
711 | } |
581 | |
712 | |
|
|
713 | SV * |
|
|
714 | mapmap (CFClient::Map self, int w, int h) |
|
|
715 | CODE: |
|
|
716 | { |
|
|
717 | int x0, x1, x; |
|
|
718 | int y0, y1, y; |
|
|
719 | int z; |
|
|
720 | SV *map_sv = newSV (w * h * sizeof (uint32_t)); |
|
|
721 | uint32_t *map = (uint32_t *)SvPVX (map_sv); |
|
|
722 | |
|
|
723 | SvPOK_only (map_sv); |
|
|
724 | SvCUR_set (map_sv, w * h * sizeof (uint32_t)); |
|
|
725 | |
|
|
726 | x0 = self->x - w / 2; x1 = x0 + w; |
|
|
727 | y0 = self->y - h / 2; y1 = y0 + h; |
|
|
728 | |
|
|
729 | for (y = y0; y < y1; y++) |
|
|
730 | { |
|
|
731 | maprow *row = 0 <= y && y < self->rows |
|
|
732 | ? self->row + y |
|
|
733 | : 0; |
|
|
734 | |
|
|
735 | for (x = x0; x < x1; x++) |
|
|
736 | { |
|
|
737 | int r = 32, g = 32, b = 32, a = 192; |
|
|
738 | |
|
|
739 | if (row && row->c0 <= x && x < row->c1) |
|
|
740 | { |
|
|
741 | mapcell *cell = row->col + (x - row->c0); |
|
|
742 | |
|
|
743 | for (z = 0; z <= 0; z++) |
|
|
744 | { |
|
|
745 | mapface face = cell->face [z]; |
|
|
746 | |
|
|
747 | if (face) |
|
|
748 | { |
|
|
749 | maptex tex = self->tex [face]; |
|
|
750 | int a0 = 255 - tex.a; |
|
|
751 | int a1 = tex.a; |
|
|
752 | |
|
|
753 | r = (r * a0 + tex.r * a1) / 255; |
|
|
754 | g = (g * a0 + tex.g * a1) / 255; |
|
|
755 | b = (b * a0 + tex.b * a1) / 255; |
|
|
756 | a = (a * a0 + tex.a * a1) / 255; |
|
|
757 | } |
|
|
758 | } |
|
|
759 | } |
|
|
760 | |
|
|
761 | *map++ = (r ) |
|
|
762 | | (g << 8) |
|
|
763 | | (b << 16) |
|
|
764 | | (a << 24); |
|
|
765 | } |
|
|
766 | } |
|
|
767 | |
|
|
768 | RETVAL = map_sv; |
|
|
769 | } |
|
|
770 | OUTPUT: |
|
|
771 | RETVAL |
|
|
772 | |
582 | void |
773 | void |
583 | draw (CFClient::Map self, int shift_x, int shift_y, int x0, int y0, int sw, int sh) |
774 | draw (CFClient::Map self, int shift_x, int shift_y, int x0, int y0, int sw, int sh) |
584 | PPCODE: |
775 | PPCODE: |
585 | { |
776 | { |
|
|
777 | int vx, vy; |
|
|
778 | int x, y, z; |
|
|
779 | int last_name; |
|
|
780 | mapface face; |
586 | int sw4 = (sw + 3) & ~3; |
781 | int sw4 = (sw + 3) & ~3; |
587 | SV *darkness_sv = sv_2mortal (newSV (sw4 * sh)); |
782 | SV *darkness_sv = sv_2mortal (newSV (sw4 * sh)); |
588 | uint8_t *darkness = (uint8_t *)SvPVX (darkness_sv); |
783 | uint8_t *darkness = (uint8_t *)SvPVX (darkness_sv); |
589 | |
784 | |
|
|
785 | memset (darkness, 255, sw4 * sh); |
590 | SvPOK_only (darkness_sv); |
786 | SvPOK_only (darkness_sv); |
591 | SvCUR_set (darkness_sv, sw4 * sh); |
787 | SvCUR_set (darkness_sv, sw4 * sh); |
592 | |
788 | |
593 | int vx = self->x + (self->w - sw) / 2 - shift_x; |
789 | vx = self->x + (self->w - sw) / 2 - shift_x; |
594 | int vy = self->y + (self->h - sh) / 2 - shift_y; |
790 | vy = self->y + (self->h - sh) / 2 - shift_y; |
595 | |
791 | |
596 | if (0) |
|
|
597 | { |
792 | /* |
598 | int vx = self->vx = self->w >= sw |
793 | int vx = self->vx = self->w >= sw |
599 | ? self->x + (self->w - sw) / 2 |
794 | ? self->x + (self->w - sw) / 2 |
600 | : MIN (self->x, MAX (self->x + self->w - sw + 1, self->vx)); |
795 | : MIN (self->x, MAX (self->x + self->w - sw + 1, self->vx)); |
601 | |
796 | |
602 | int vy = self->vy = self->h >= sh |
797 | int vy = self->vy = self->h >= sh |
603 | ? self->y + (self->h - sh) / 2 |
798 | ? self->y + (self->h - sh) / 2 |
604 | : MIN (self->y, MAX (self->y + self->h - sh + 1, self->vy)); |
799 | : MIN (self->y, MAX (self->y + self->h - sh + 1, self->vy)); |
605 | } |
800 | */ |
606 | |
801 | |
607 | glColor4ub (255, 255, 255, 255); |
802 | glColor4ub (255, 255, 255, 255); |
608 | |
803 | |
609 | glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
804 | glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
610 | glEnable (GL_BLEND); |
805 | glEnable (GL_BLEND); |
611 | glEnable (GL_TEXTURE_2D); |
806 | glEnable (GL_TEXTURE_2D); |
612 | glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); |
807 | glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); |
613 | |
808 | |
614 | int x, y, z; |
|
|
615 | |
|
|
616 | int last_name = 0; |
|
|
617 | |
|
|
618 | glBegin (GL_QUADS); |
809 | glBegin (GL_QUADS); |
|
|
810 | |
|
|
811 | last_name = 0; |
619 | |
812 | |
620 | for (z = 0; z < 3; z++) |
813 | for (z = 0; z < 3; z++) |
621 | for (y = 0; y < sh; y++) |
814 | for (y = 0; y < sh; y++) |
622 | if (0 <= y + vy && y + vy < self->rows) |
815 | if (0 <= y + vy && y + vy < self->rows) |
623 | { |
816 | { |
… | |
… | |
630 | |
823 | |
631 | darkness[y * sw4 + x] = cell->darkness < 0 |
824 | darkness[y * sw4 + x] = cell->darkness < 0 |
632 | ? 255 - FOW_DARKNESS |
825 | ? 255 - FOW_DARKNESS |
633 | : 255 - cell->darkness; |
826 | : 255 - cell->darkness; |
634 | |
827 | |
635 | uint16_t face = cell->face [z]; |
828 | face = cell->face [z]; |
636 | |
829 | |
637 | if (face) |
830 | if (face) |
638 | { |
831 | { |
639 | mapface tex = self->face [face]; |
832 | maptex tex = self->tex [face]; |
640 | |
833 | |
641 | int px = (x + 1) * 32 - tex.w; |
834 | int px = (x + 1) * 32 - tex.w; |
642 | int py = (y + 1) * 32 - tex.h; |
835 | int py = (y + 1) * 32 - tex.h; |
643 | |
836 | |
644 | if (last_name != tex.name) |
837 | if (last_name != tex.name) |
… | |
… | |
666 | PUSHs (sv_2mortal (newSViv (sw4))); |
859 | PUSHs (sv_2mortal (newSViv (sw4))); |
667 | PUSHs (sv_2mortal (newSViv (sh))); |
860 | PUSHs (sv_2mortal (newSViv (sh))); |
668 | PUSHs (darkness_sv); |
861 | PUSHs (darkness_sv); |
669 | } |
862 | } |
670 | |
863 | |
|
|
864 | SV * |
|
|
865 | get_rect (CFClient::Map self, int x0, int y0, int w, int h) |
|
|
866 | CODE: |
|
|
867 | { |
|
|
868 | int x, y, x1, y1; |
|
|
869 | SV *data_sv = newSV (w * h * 7 + 5); |
|
|
870 | uint8_t *data = (uint8_t *)SvPVX (data_sv); |
|
|
871 | |
|
|
872 | *data++ = 0; /* version 0 format */ |
|
|
873 | *data++ = w >> 8; *data++ = w; |
|
|
874 | *data++ = h >> 8; *data++ = h; |
|
|
875 | |
|
|
876 | // we need to do this 'cause we don't keep an absolute coord system for rows |
|
|
877 | // TODO: treat rows as we treat |
|
|
878 | map_get_row (self, y0 + self->y - self->oy);//D |
|
|
879 | map_get_row (self, y0 + self->y - self->oy + h - 1);//D |
|
|
880 | |
|
|
881 | x0 += self->x - self->ox; |
|
|
882 | y0 += self->y - self->oy; |
|
|
883 | |
|
|
884 | x1 = x0 + w; |
|
|
885 | y1 = y0 + h; |
|
|
886 | |
|
|
887 | for (y = y0; y < y1; y++) |
|
|
888 | { |
|
|
889 | maprow *row = 0 <= y && y < self->rows |
|
|
890 | ? self->row + y |
|
|
891 | : 0; |
|
|
892 | |
|
|
893 | for (x = x0; x < x1; x++) |
|
|
894 | { |
|
|
895 | if (row && row->c0 <= x && x < row->c1) |
|
|
896 | { |
|
|
897 | mapcell *cell = row->col + (x - row->c0); |
|
|
898 | uint8_t flags = 0; |
|
|
899 | |
|
|
900 | if (cell->face [0]) flags |= 1; |
|
|
901 | if (cell->face [1]) flags |= 2; |
|
|
902 | if (cell->face [2]) flags |= 4; |
|
|
903 | |
|
|
904 | *data++ = flags; |
|
|
905 | |
|
|
906 | if (flags & 1) |
|
|
907 | { |
|
|
908 | *data++ = cell->face [0] >> 8; |
|
|
909 | *data++ = cell->face [0]; |
|
|
910 | } |
|
|
911 | |
|
|
912 | if (flags & 2) |
|
|
913 | { |
|
|
914 | *data++ = cell->face [1] >> 8; |
|
|
915 | *data++ = cell->face [1]; |
|
|
916 | } |
|
|
917 | |
|
|
918 | if (flags & 4) |
|
|
919 | { |
|
|
920 | *data++ = cell->face [2] >> 8; |
|
|
921 | *data++ = cell->face [2]; |
|
|
922 | } |
|
|
923 | } |
|
|
924 | else |
|
|
925 | *data++ = 0; |
|
|
926 | } |
|
|
927 | } |
|
|
928 | |
|
|
929 | SvPOK_only (data_sv); |
|
|
930 | SvCUR_set (data_sv, data - (uint8_t *)SvPVX (data_sv)); |
|
|
931 | RETVAL = data_sv; |
|
|
932 | } |
|
|
933 | OUTPUT: |
|
|
934 | RETVAL |
|
|
935 | |
|
|
936 | void |
|
|
937 | set_rect (CFClient::Map self, int x0, int y0, uint8_t *data) |
|
|
938 | PPCODE: |
|
|
939 | { |
|
|
940 | int x, y, z; |
|
|
941 | int w, h; |
|
|
942 | int x1, y1; |
|
|
943 | |
|
|
944 | if (*data++ != 0) |
|
|
945 | return; /* version mismatch */ |
|
|
946 | |
|
|
947 | w = *data++ << 8; w |= *data++; |
|
|
948 | h = *data++ << 8; h |= *data++; |
|
|
949 | |
|
|
950 | // we need to do this 'cause we don't keep an absolute coord system for rows |
|
|
951 | // TODO: treat rows as we treat |
|
|
952 | map_get_row (self, y0 + self->y - self->oy);//D |
|
|
953 | map_get_row (self, y0 + self->y - self->oy + h - 1);//D |
|
|
954 | |
|
|
955 | x0 += self->x - self->ox; |
|
|
956 | y0 += self->y - self->oy; |
|
|
957 | |
|
|
958 | x1 = x0 + w; |
|
|
959 | y1 = y0 + h; |
|
|
960 | |
|
|
961 | for (y = y0; y < y1; y++) |
|
|
962 | { |
|
|
963 | maprow *row = map_get_row (self, y); |
|
|
964 | |
|
|
965 | for (x = x0; x < x1; x++) |
|
|
966 | { |
|
|
967 | uint8_t flags = *data++; |
|
|
968 | |
|
|
969 | if (flags) |
|
|
970 | { |
|
|
971 | mapface face[3] = { 0, 0, 0 }; |
|
|
972 | |
|
|
973 | mapcell *cell = row_get_cell (row, x); |
|
|
974 | |
|
|
975 | if (flags & 1) { face[0] = *data++ << 8; face[0] |= *data++; } |
|
|
976 | if (flags & 2) { face[1] = *data++ << 8; face[1] |= *data++; } |
|
|
977 | if (flags & 4) { face[2] = *data++ << 8; face[2] |= *data++; } |
|
|
978 | |
|
|
979 | if (cell->darkness <= 0) |
|
|
980 | { |
|
|
981 | cell->darkness = -1; |
|
|
982 | |
|
|
983 | for (z = 0; z <= 2; z++) |
|
|
984 | { |
|
|
985 | cell->face[z] = face[z]; |
|
|
986 | |
|
|
987 | if (face[z] && (face[z] >= self->texs || !self->tex[face [z]].name)) |
|
|
988 | XPUSHs (sv_2mortal (newSViv (face[z]))); |
|
|
989 | } |
|
|
990 | } |
|
|
991 | } |
|
|
992 | } |
|
|
993 | } |
|
|
994 | } |
|
|
995 | |