… | |
… | |
62 | *w = (*w + 3) & ~3; |
62 | *w = (*w + 3) & ~3; |
63 | if (!*w) *w = 1; |
63 | if (!*w) *w = 1; |
64 | if (!*h) *h = 1; |
64 | if (!*h) *h = 1; |
65 | } |
65 | } |
66 | |
66 | |
|
|
67 | typedef uint16_t mapface; |
|
|
68 | |
67 | typedef struct { |
69 | typedef struct { |
68 | GLint name; |
70 | GLint name; |
69 | int w, h; |
71 | int w, h; |
70 | float s, t; |
72 | float s, t; |
71 | } mapface; |
73 | uint8_t r, g, b, a; |
|
|
74 | } maptex; |
72 | |
75 | |
73 | typedef struct { |
76 | typedef struct { |
74 | int16_t darkness; |
77 | int16_t darkness; |
75 | uint16_t face[3]; |
78 | mapface face[3]; |
76 | } mapcell; |
79 | } mapcell; |
77 | |
80 | |
78 | typedef struct { |
81 | typedef struct { |
79 | int32_t c0, c1; |
82 | int32_t c0, c1; |
80 | mapcell *col; |
83 | mapcell *col; |
81 | } maprow; |
84 | } maprow; |
82 | |
85 | |
83 | typedef struct map { |
86 | typedef struct map { |
84 | int x, y, w, h; |
87 | int x, y, w, h; |
85 | int vx, vy; |
88 | int dx, dy; /* delayed map scrolling */ |
|
|
89 | int ox, oy; /* offset to virtual global coordinate system */ |
86 | int faces; |
90 | int faces; |
87 | mapface *face; |
91 | mapface *face; |
|
|
92 | |
|
|
93 | int texs; |
|
|
94 | maptex *tex; |
88 | |
95 | |
89 | uint32_t rows; |
96 | uint32_t rows; |
90 | maprow *row; |
97 | maprow *row; |
91 | } *CFClient__Map; |
98 | } *CFClient__Map; |
92 | |
99 | |
… | |
… | |
113 | } |
120 | } |
114 | |
121 | |
115 | #define Append(type,ptr,sze,inc) (ptr) = (type *)append ((char *)ptr, (sze) * sizeof (type), (inc) * sizeof (type)) |
122 | #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)) |
123 | #define Prepend(type,ptr,sze,inc) (ptr) = (type *)prepend ((char *)ptr, (sze) * sizeof (type), (inc) * sizeof (type)) |
117 | |
124 | |
|
|
125 | static maprow * |
|
|
126 | map_get_row (CFClient__Map self, int y) |
|
|
127 | { |
|
|
128 | if (0 > y) |
|
|
129 | { |
|
|
130 | int extend = - y + MAP_EXTEND_Y; |
|
|
131 | Prepend (maprow, self->row, self->rows, extend); |
|
|
132 | |
|
|
133 | self->rows += extend; |
|
|
134 | self->y += extend; |
|
|
135 | y += extend; |
|
|
136 | } |
|
|
137 | else if (y >= self->rows) |
|
|
138 | { |
|
|
139 | int extend = y - self->rows + MAP_EXTEND_Y; |
|
|
140 | Append (maprow, self->row, self->rows, extend); |
|
|
141 | self->rows += extend; |
|
|
142 | } |
|
|
143 | |
|
|
144 | return self->row + y; |
|
|
145 | } |
|
|
146 | |
|
|
147 | static mapcell * |
|
|
148 | row_get_cell (maprow *row, int x) |
|
|
149 | { |
|
|
150 | if (!row->col) |
|
|
151 | { |
|
|
152 | Newz (0, row->col, MAP_EXTEND_X, mapcell); |
|
|
153 | row->c0 = x - MAP_EXTEND_X / 4; |
|
|
154 | row->c1 = row->c0 + MAP_EXTEND_X; |
|
|
155 | } |
|
|
156 | |
|
|
157 | if (row->c0 > x) |
|
|
158 | { |
|
|
159 | int extend = row->c0 - x + MAP_EXTEND_X; |
|
|
160 | Prepend (mapcell, row->col, row->c1 - row->c0, extend); |
|
|
161 | row->c0 -= extend; |
|
|
162 | } |
|
|
163 | else if (x >= row->c1) |
|
|
164 | { |
|
|
165 | int extend = x - row->c1 + MAP_EXTEND_X; |
|
|
166 | Append (mapcell, row->col, row->c1 - row->c0, extend); |
|
|
167 | row->c1 += extend; |
|
|
168 | } |
|
|
169 | |
|
|
170 | return row->col + (x - row->c0); |
|
|
171 | } |
|
|
172 | |
|
|
173 | static mapcell * |
|
|
174 | map_get_cell (CFClient__Map self, int x, int y) |
|
|
175 | { |
|
|
176 | return row_get_cell (map_get_row (self, y), x); |
|
|
177 | } |
|
|
178 | |
118 | static void |
179 | static void |
119 | map_clear (CFClient__Map self) |
180 | map_clear (CFClient__Map self) |
120 | { |
181 | { |
121 | int r; |
182 | int r; |
122 | |
183 | |
… | |
… | |
125 | |
186 | |
126 | Safefree (self->row); |
187 | Safefree (self->row); |
127 | |
188 | |
128 | self->x = 0; |
189 | self->x = 0; |
129 | self->y = 0; |
190 | self->y = 0; |
130 | self->vx = 0; |
191 | self->ox = 0; |
131 | self->vy = 0; |
192 | self->oy = 0; |
132 | self->row = 0; |
193 | self->row = 0; |
133 | self->rows = 0; |
194 | self->rows = 0; |
134 | } |
195 | } |
135 | |
196 | |
136 | static void |
197 | static void |
… | |
… | |
216 | SDL_RWops *rw = ix |
277 | SDL_RWops *rw = ix |
217 | ? SDL_RWFromFile (image, "r") |
278 | ? SDL_RWFromFile (image, "r") |
218 | : SDL_RWFromConstMem (image, image_len); |
279 | : SDL_RWFromConstMem (image, image_len); |
219 | |
280 | |
220 | if (!rw) |
281 | if (!rw) |
221 | croak ("load_image: unable to open file"); |
282 | croak ("load_image: %s", SDL_GetError ()); |
222 | |
283 | |
223 | surface = IMG_Load_RW (rw, 1); |
284 | surface = IMG_Load_RW (rw, 1); |
224 | if (!surface) |
285 | if (!surface) |
225 | croak ("load_image: unable to read file"); |
286 | croak ("load_image: %s", SDL_GetError ()); |
226 | |
287 | |
227 | fmt.palette = NULL; |
288 | fmt.palette = NULL; |
228 | fmt.BitsPerPixel = 32; |
289 | fmt.BitsPerPixel = 32; |
229 | fmt.BytesPerPixel = 4; |
290 | fmt.BytesPerPixel = 4; |
230 | fmt.Rmask = 0x000000ff; |
291 | fmt.Rmask = 0x000000ff; |
… | |
… | |
242 | fmt.colorkey = 0; |
303 | fmt.colorkey = 0; |
243 | fmt.alpha = 0; |
304 | fmt.alpha = 0; |
244 | |
305 | |
245 | surface2 = SDL_ConvertSurface (surface, &fmt, SDL_SWSURFACE); |
306 | surface2 = SDL_ConvertSurface (surface, &fmt, SDL_SWSURFACE); |
246 | |
307 | |
|
|
308 | assert (surface2->pitch == surface2->w * 4); |
|
|
309 | |
247 | EXTEND (SP, 5); |
310 | EXTEND (SP, 5); |
248 | PUSHs (sv_2mortal (newSViv (surface2->w))); |
311 | PUSHs (sv_2mortal (newSViv (surface2->w))); |
249 | PUSHs (sv_2mortal (newSViv (surface2->h))); |
312 | PUSHs (sv_2mortal (newSViv (surface2->h))); |
250 | SDL_LockSurface (surface2); |
313 | SDL_LockSurface (surface2); |
251 | PUSHs (sv_2mortal (newSVpvn (surface2->pixels, surface2->h * surface2->pitch))); |
314 | PUSHs (sv_2mortal (newSVpvn (surface2->pixels, surface2->h * surface2->pitch))); |
… | |
… | |
257 | SDL_FreeSurface (surface); |
320 | SDL_FreeSurface (surface); |
258 | SDL_FreeSurface (surface2); |
321 | SDL_FreeSurface (surface2); |
259 | } |
322 | } |
260 | |
323 | |
261 | void |
324 | void |
|
|
325 | average (int x, int y, uint32_t *data) |
|
|
326 | PPCODE: |
|
|
327 | { |
|
|
328 | uint32_t r = 0, g = 0, b = 0, a = 0; |
|
|
329 | |
|
|
330 | x = y = x * y; |
|
|
331 | |
|
|
332 | while (x--) |
|
|
333 | { |
|
|
334 | uint32_t p = *data++; |
|
|
335 | |
|
|
336 | r += (p ) & 255; |
|
|
337 | g += (p >> 8) & 255; |
|
|
338 | b += (p >> 16) & 255; |
|
|
339 | a += (p >> 24) & 255; |
|
|
340 | } |
|
|
341 | |
|
|
342 | EXTEND (SP, 4); |
|
|
343 | PUSHs (sv_2mortal (newSViv (r / y))); |
|
|
344 | PUSHs (sv_2mortal (newSViv (g / y))); |
|
|
345 | PUSHs (sv_2mortal (newSViv (b / y))); |
|
|
346 | PUSHs (sv_2mortal (newSViv (a / y))); |
|
|
347 | } |
|
|
348 | |
|
|
349 | void |
262 | fatal (char *message) |
350 | fatal (char *message) |
263 | CODE: |
351 | CODE: |
264 | #ifdef WIN32 |
352 | #ifdef WIN32 |
265 | MessageBox (0, message, "Crossfire+ Fatal Error", MB_OK | MB_ICONERROR | MB_SETFOREGROUND); |
353 | MessageBox (0, message, "Crossfire+ Fatal Error", MB_OK | MB_ICONERROR | MB_SETFOREGROUND); |
266 | #else |
354 | #else |
267 | fprintf (stderr, "%s\n", message); |
355 | fprintf (stderr, "FATAL: %s\n", message); |
268 | #endif |
356 | #endif |
269 | exit (1); |
357 | exit (1); |
270 | |
358 | |
271 | MODULE = CFClient PACKAGE = CFClient::Layout |
359 | MODULE = CFClient PACKAGE = CFClient::Layout |
272 | |
360 | |
… | |
… | |
391 | } |
479 | } |
392 | |
480 | |
393 | MODULE = CFClient PACKAGE = CFClient::Texture |
481 | MODULE = CFClient PACKAGE = CFClient::Texture |
394 | |
482 | |
395 | void |
483 | void |
396 | draw_quad (SV *self, double x, double y, double w = 0, double h = 0) |
484 | draw_quad (SV *self, float x, float y, float w = 0, float h = 0) |
397 | PROTOTYPE: $$$;$$ |
485 | PROTOTYPE: $$$;$$ |
398 | CODE: |
486 | CODE: |
399 | { |
487 | { |
400 | HV *hv = (HV *)SvRV (self); |
488 | HV *hv = (HV *)SvRV (self); |
401 | double s = SvNV (*hv_fetch (hv, "s", 1, 1)); |
489 | float s = SvNV (*hv_fetch (hv, "s", 1, 1)); |
402 | double t = SvNV (*hv_fetch (hv, "t", 1, 1)); |
490 | float t = SvNV (*hv_fetch (hv, "t", 1, 1)); |
403 | int name = SvIV (*hv_fetch (hv, "name", 4, 1)); |
491 | int name = SvIV (*hv_fetch (hv, "name", 4, 1)); |
|
|
492 | int wrap_mode = SvIV (*hv_fetch (hv, "wrap_mode", 9, 1)); |
404 | |
493 | |
405 | if (items < 5) |
494 | if (items < 5) |
406 | { |
495 | { |
407 | w = SvNV (*hv_fetch (hv, "w", 1, 1)); |
496 | w = SvNV (*hv_fetch (hv, "w", 1, 1)); |
408 | h = SvNV (*hv_fetch (hv, "h", 1, 1)); |
497 | h = SvNV (*hv_fetch (hv, "h", 1, 1)); |
409 | } |
498 | } |
410 | |
499 | |
411 | glBindTexture (GL_TEXTURE_2D, name); |
500 | glBindTexture (GL_TEXTURE_2D, name); |
|
|
501 | if (wrap_mode) { |
|
|
502 | glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); |
|
|
503 | glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); |
|
|
504 | } |
412 | glBegin (GL_QUADS); |
505 | glBegin (GL_QUADS); |
413 | glTexCoord2d (0, 0); glVertex2d (x , y ); |
506 | glTexCoord2f (0, 0); glVertex2f (x , y ); |
414 | glTexCoord2d (0, t); glVertex2d (x , y + h); |
507 | glTexCoord2f (0, t); glVertex2f (x , y + h); |
415 | glTexCoord2d (s, t); glVertex2d (x + w, y + h); |
508 | glTexCoord2f (s, t); glVertex2f (x + w, y + h); |
416 | glTexCoord2d (s, 0); glVertex2d (x + w, y ); |
509 | glTexCoord2f (s, 0); glVertex2f (x + w, y ); |
417 | glEnd (); |
510 | glEnd (); |
418 | } |
511 | } |
419 | |
512 | |
420 | MODULE = CFClient PACKAGE = CFClient::Map |
513 | MODULE = CFClient PACKAGE = CFClient::Map |
421 | |
514 | |
422 | CFClient::Map |
515 | CFClient::Map |
423 | new (SV *class, int map_width, int map_height) |
516 | new (SV *class, int map_width, int map_height) |
424 | CODE: |
517 | CODE: |
425 | New (0, RETVAL, 1, struct map); |
518 | New (0, RETVAL, 1, struct map); |
426 | RETVAL->x = 0; |
519 | RETVAL->x = 0; |
427 | RETVAL->y = 0; |
520 | RETVAL->y = 0; |
428 | RETVAL->w = map_width; |
521 | RETVAL->w = map_width; |
429 | RETVAL->h = map_height; |
522 | RETVAL->h = map_height; |
430 | RETVAL->vx = 0; |
523 | RETVAL->ox = 0; |
431 | RETVAL->vy = 0; |
524 | RETVAL->oy = 0; |
432 | RETVAL->faces = 8192; |
525 | RETVAL->faces = 8192; |
433 | Newz (0, RETVAL->face, RETVAL->faces, mapface); |
526 | Newz (0, RETVAL->face, RETVAL->faces, mapface); |
|
|
527 | RETVAL->texs = 8192; |
|
|
528 | Newz (0, RETVAL->tex, RETVAL->texs, maptex); |
434 | RETVAL->rows = 0; |
529 | RETVAL->rows = 0; |
435 | RETVAL->row = 0; |
530 | RETVAL->row = 0; |
436 | OUTPUT: |
531 | OUTPUT: |
437 | RETVAL |
532 | RETVAL |
438 | |
533 | |
… | |
… | |
449 | clear (CFClient::Map self) |
544 | clear (CFClient::Map self) |
450 | CODE: |
545 | CODE: |
451 | map_clear (self); |
546 | map_clear (self); |
452 | |
547 | |
453 | void |
548 | void |
454 | set_texture (CFClient::Map self, int face, int name, int w, int h, float s, float t) |
549 | set_face (CFClient::Map self, int face, int texid) |
455 | CODE: |
550 | CODE: |
456 | { |
551 | { |
457 | while (self->faces < face) |
552 | while (self->faces <= face) |
458 | { |
553 | { |
459 | Append (mapface, self->face, self->faces, self->faces); |
554 | Append (mapface, self->face, self->faces, self->faces); |
460 | self->faces *= 2; |
555 | self->faces *= 2; |
461 | } |
556 | } |
462 | |
557 | |
463 | self->face [face].name = name; |
|
|
464 | self->face [face].w = w; |
558 | self->face [face] = texid; |
465 | self->face [face].h = h; |
|
|
466 | self->face [face].s = s; |
|
|
467 | self->face [face].t = t; |
|
|
468 | } |
559 | } |
|
|
560 | |
|
|
561 | void |
|
|
562 | 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) |
|
|
563 | CODE: |
|
|
564 | { |
|
|
565 | while (self->texs <= texid) |
|
|
566 | { |
|
|
567 | Append (maptex, self->tex, self->texs, self->texs); |
|
|
568 | self->texs *= 2; |
|
|
569 | } |
|
|
570 | |
|
|
571 | maptex *tex = self->tex + texid; |
|
|
572 | |
|
|
573 | tex->name = name; |
|
|
574 | tex->w = w; |
|
|
575 | tex->h = h; |
|
|
576 | tex->s = s; |
|
|
577 | tex->t = t; |
|
|
578 | tex->r = r; |
|
|
579 | tex->g = g; |
|
|
580 | tex->b = b; |
|
|
581 | tex->a = a; |
|
|
582 | } |
|
|
583 | |
|
|
584 | int |
|
|
585 | ox (CFClient::Map self) |
|
|
586 | ALIAS: |
|
|
587 | oy = 1 |
|
|
588 | CODE: |
|
|
589 | switch (ix) |
|
|
590 | { |
|
|
591 | case 0: RETVAL = self->ox; break; |
|
|
592 | case 1: RETVAL = self->oy; break; |
|
|
593 | } |
|
|
594 | OUTPUT: |
|
|
595 | RETVAL |
469 | |
596 | |
470 | void |
597 | void |
471 | scroll (CFClient::Map self, int dx, int dy) |
598 | scroll (CFClient::Map self, int dx, int dy) |
472 | CODE: |
599 | CODE: |
473 | { |
600 | { |
|
|
601 | self->dx += dx; self->ox += dx; |
|
|
602 | self->dy += dy; self->oy += dy; |
|
|
603 | } |
|
|
604 | |
|
|
605 | void |
|
|
606 | map1a_update (CFClient::Map self, SV *data_) |
|
|
607 | CODE: |
|
|
608 | { |
474 | if (dx > 0) |
609 | if (self->dx > 0) |
475 | map_blank (self, self->x, self->y, dx - 1, self->h); |
610 | map_blank (self, self->x, self->y, self->dx - 1, self->h); |
476 | else if (dx < 0) |
611 | else if (self->dx < 0) |
477 | map_blank (self, self->x + self->w + dx + 1, self->y, 1 - dx, self->h); |
612 | map_blank (self, self->x + self->w + self->dx + 1, self->y, 1 - self->dx, self->h); |
478 | |
613 | |
479 | if (dy > 0) |
614 | if (self->dy > 0) |
480 | map_blank (self, self->x, self->y, self->w, dy - 1); |
615 | map_blank (self, self->x, self->y, self->w, self->dy - 1); |
481 | else if (dy < 0) |
616 | else if (self->dy < 0) |
482 | map_blank (self, self->x, self->y + self->h + dy + 1, self->w, 1 - dy); |
617 | map_blank (self, self->x, self->y + self->h + self->dy + 1, self->w, 1 - self->dy); |
483 | |
618 | |
484 | self->x += dx; |
619 | self->x += self->dx; self->dx = 0; |
485 | self->y += dy; |
620 | self->y += self->dy; self->dy = 0; |
486 | |
621 | |
487 | while (self->y < 0) |
622 | while (self->y < 0) |
488 | { |
623 | { |
489 | Prepend (maprow, self->row, self->rows, MAP_EXTEND_Y); |
624 | Prepend (maprow, self->row, self->rows, MAP_EXTEND_Y); |
490 | |
625 | |
491 | self->rows += MAP_EXTEND_Y; |
626 | self->rows += MAP_EXTEND_Y; |
492 | self->y += MAP_EXTEND_Y; |
627 | self->y += MAP_EXTEND_Y; |
493 | } |
628 | } |
494 | } |
|
|
495 | |
629 | |
496 | void |
|
|
497 | map1a_update (CFClient::Map self, SV *data_) |
|
|
498 | CODE: |
|
|
499 | { |
|
|
500 | uint8_t *data = (uint8_t *)SvPVbyte_nolen (data_); |
630 | uint8_t *data = (uint8_t *)SvPVbyte_nolen (data_); |
501 | uint8_t *data_end = (uint8_t *)SvEND (data_); |
631 | uint8_t *data_end = (uint8_t *)SvEND (data_); |
502 | |
632 | |
503 | while (data < data_end) |
633 | while (data < data_end) |
504 | { |
634 | { |
505 | int flags = (data [0] << 8) + data [1]; data += 2; |
635 | int flags = (data [0] << 8) + data [1]; data += 2; |
506 | |
636 | |
507 | int x = ((flags >> 10) & 63) + self->x; |
637 | int x = ((flags >> 10) & 63) + self->x; |
508 | int y = ((flags >> 4) & 63) + self->y; |
638 | int y = ((flags >> 4) & 63) + self->y; |
509 | |
639 | |
510 | while (y >= self->rows) |
640 | mapcell *cell = map_get_cell (self, x, y); |
511 | { |
|
|
512 | Prepend (maprow, self->row, self->rows, MAP_EXTEND_Y); |
|
|
513 | |
|
|
514 | self->rows += MAP_EXTEND_Y; |
|
|
515 | } |
|
|
516 | |
|
|
517 | maprow *row = self->row + y; |
|
|
518 | |
|
|
519 | if (!row->col) |
|
|
520 | { |
|
|
521 | Newz (0, row->col, MAP_EXTEND_X, mapcell); |
|
|
522 | row->c0 = self->x - MAP_EXTEND_X / 4; |
|
|
523 | row->c1 = row->c0 + MAP_EXTEND_X; |
|
|
524 | } |
|
|
525 | |
|
|
526 | if (row->c0 > x) |
|
|
527 | { |
|
|
528 | int extend = row->c0 - x + MAP_EXTEND_X; |
|
|
529 | Prepend (mapcell, row->col, row->c1 - row->c0, extend); |
|
|
530 | row->c0 -= extend; |
|
|
531 | } |
|
|
532 | else if (x >= row->c1) |
|
|
533 | { |
|
|
534 | int extend = x - row->c1 + MAP_EXTEND_X; |
|
|
535 | Append (mapcell, row->col, row->c1 - row->c0, extend); |
|
|
536 | row->c1 += extend; |
|
|
537 | } |
|
|
538 | |
|
|
539 | assert (y >= 0); |
|
|
540 | assert (y < self->rows); |
|
|
541 | assert (x >= row->c0); |
|
|
542 | assert (x < row->c1); |
|
|
543 | mapcell *cell = row->col + (x - row->c0); |
|
|
544 | |
641 | |
545 | if (flags & 15) |
642 | if (flags & 15) |
546 | { |
643 | { |
547 | if (cell->darkness < 0 && x < self->w && y < self->h) |
644 | if (cell->darkness < 0) // && x < self->w && y < self->h) |
548 | { |
645 | { |
549 | cell->darkness = 0; |
646 | cell->darkness = 0; |
550 | cell->face [0] = 0; |
647 | cell->face [0] = 0; |
551 | cell->face [1] = 0; |
648 | cell->face [1] = 0; |
552 | cell->face [2] = 0; |
649 | cell->face [2] = 0; |
553 | } |
650 | } |
554 | |
651 | |
555 | cell->darkness = flags & 8 ? *data++ : 255; |
652 | cell->darkness = flags & 8 ? *data++ : 255; |
556 | |
653 | |
|
|
654 | //TODO: don't trust server data to be in-range(!) |
|
|
655 | |
557 | if (flags & 4) |
656 | if (flags & 4) |
558 | { |
657 | { |
559 | cell->face [0] = (data [0] << 8) + data [1]; data += 2; |
658 | cell->face [0] = self->face [(data [0] << 8) + data [1]]; data += 2; |
560 | } |
659 | } |
561 | |
660 | |
562 | if (flags & 2) |
661 | if (flags & 2) |
563 | { |
662 | { |
564 | cell->face [1] = (data [0] << 8) + data [1]; data += 2; |
663 | cell->face [1] = self->face [(data [0] << 8) + data [1]]; data += 2; |
565 | } |
664 | } |
566 | |
665 | |
567 | if (flags & 1) |
666 | if (flags & 1) |
568 | { |
667 | { |
569 | cell->face [2] = (data [0] << 8) + data [1]; data += 2; |
668 | cell->face [2] = self->face [(data [0] << 8) + data [1]]; data += 2; |
570 | } |
669 | } |
571 | } |
670 | } |
572 | else |
671 | else |
573 | cell->darkness = -1; |
672 | cell->darkness = -1; |
574 | } |
673 | } |
575 | } |
674 | } |
576 | |
675 | |
|
|
676 | SV * |
|
|
677 | mapmap (CFClient::Map self, int w, int h) |
|
|
678 | CODE: |
|
|
679 | { |
|
|
680 | int x0, x1, x; |
|
|
681 | int y0, y1, y; |
|
|
682 | int z; |
|
|
683 | SV *map_sv = newSV (w * h * sizeof (uint32_t)); |
|
|
684 | uint32_t *map = (uint32_t *)SvPVX (map_sv); |
|
|
685 | |
|
|
686 | SvPOK_only (map_sv); |
|
|
687 | SvCUR_set (map_sv, w * h * sizeof (uint32_t)); |
|
|
688 | |
|
|
689 | x0 = self->x - w / 2; x1 = x0 + w; |
|
|
690 | y0 = self->y - h / 2; y1 = y0 + h; |
|
|
691 | |
|
|
692 | for (y = y0; y < y1; y++) |
|
|
693 | { |
|
|
694 | maprow *row = 0 <= y && y < self->rows |
|
|
695 | ? self->row + y |
|
|
696 | : 0; |
|
|
697 | |
|
|
698 | for (x = x0; x < x1; x++) |
|
|
699 | { |
|
|
700 | int r = 32, g = 32, b = 32, a = 192; |
|
|
701 | |
|
|
702 | if (row && row->c0 <= x && x < row->c1) |
|
|
703 | { |
|
|
704 | mapcell *cell = row->col + (x - row->c0); |
|
|
705 | |
|
|
706 | for (z = 0; z <= 0; z++) |
|
|
707 | { |
|
|
708 | mapface face = cell->face [z]; |
|
|
709 | |
|
|
710 | if (face) |
|
|
711 | { |
|
|
712 | maptex tex = self->tex [face]; |
|
|
713 | int a0 = 255 - tex.a; |
|
|
714 | int a1 = tex.a; |
|
|
715 | |
|
|
716 | r = (r * a0 + tex.r * a1) / 255; |
|
|
717 | g = (g * a0 + tex.g * a1) / 255; |
|
|
718 | b = (b * a0 + tex.b * a1) / 255; |
|
|
719 | a = (a * a0 + tex.a * a1) / 255; |
|
|
720 | } |
|
|
721 | } |
|
|
722 | } |
|
|
723 | |
|
|
724 | *map++ = (r ) |
|
|
725 | | (g << 8) |
|
|
726 | | (b << 16) |
|
|
727 | | (a << 24); |
|
|
728 | } |
|
|
729 | } |
|
|
730 | |
|
|
731 | RETVAL = map_sv; |
|
|
732 | } |
|
|
733 | OUTPUT: |
|
|
734 | RETVAL |
|
|
735 | |
577 | void |
736 | void |
578 | draw (CFClient::Map self, int x0, int y0, int sw, int sh) |
737 | draw (CFClient::Map self, int shift_x, int shift_y, int x0, int y0, int sw, int sh) |
579 | PPCODE: |
738 | PPCODE: |
580 | { |
739 | { |
581 | int sw4 = (sw + 3) & ~3; |
740 | int sw4 = (sw + 3) & ~3; |
582 | SV *darkness_sv = sv_2mortal (newSV (sw4 * sh)); |
741 | SV *darkness_sv = sv_2mortal (newSV (sw4 * sh)); |
583 | uint8_t *darkness = (uint8_t *)SvPVX (darkness_sv); |
742 | uint8_t *darkness = (uint8_t *)SvPVX (darkness_sv); |
584 | |
743 | memset (darkness, 255, sw4 * sh); |
585 | SvPOK_only (darkness_sv); |
744 | SvPOK_only (darkness_sv); |
586 | SvCUR_set (darkness_sv, sw4 * sh); |
745 | SvCUR_set (darkness_sv, sw4 * sh); |
587 | |
746 | |
|
|
747 | int vx = self->x + (self->w - sw) / 2 - shift_x; |
|
|
748 | int vy = self->y + (self->h - sh) / 2 - shift_y; |
|
|
749 | |
|
|
750 | /* |
588 | int vx = self->vx = self->w >= sw |
751 | int vx = self->vx = self->w >= sw |
589 | ? self->x + (self->w - sw) / 2 |
752 | ? self->x + (self->w - sw) / 2 |
590 | : MIN (self->x, MAX (self->x + self->w - sw + 1, self->vx)); |
753 | : MIN (self->x, MAX (self->x + self->w - sw + 1, self->vx)); |
591 | |
754 | |
592 | int vy = self->vy = self->h >= sh |
755 | int vy = self->vy = self->h >= sh |
593 | ? self->y + (self->h - sh) / 2 |
756 | ? self->y + (self->h - sh) / 2 |
594 | : MIN (self->y, MAX (self->y + self->h - sh + 1, self->vy)); |
757 | : MIN (self->y, MAX (self->y + self->h - sh + 1, self->vy)); |
|
|
758 | */ |
595 | |
759 | |
596 | glColor4ub (255, 255, 255, 255); |
760 | glColor4ub (255, 255, 255, 255); |
597 | |
761 | |
598 | glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
762 | glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
599 | glEnable (GL_BLEND); |
763 | glEnable (GL_BLEND); |
… | |
… | |
619 | |
783 | |
620 | darkness[y * sw4 + x] = cell->darkness < 0 |
784 | darkness[y * sw4 + x] = cell->darkness < 0 |
621 | ? 255 - FOW_DARKNESS |
785 | ? 255 - FOW_DARKNESS |
622 | : 255 - cell->darkness; |
786 | : 255 - cell->darkness; |
623 | |
787 | |
624 | uint16_t face = cell->face [z]; |
788 | mapface face = cell->face [z]; |
625 | |
789 | |
626 | if (face) |
790 | if (face) |
627 | { |
791 | { |
628 | mapface tex = self->face [face]; |
792 | maptex tex = self->tex [face]; |
629 | |
793 | |
630 | int px = (x + 1) * 32 - tex.w; |
794 | int px = (x + 1) * 32 - tex.w; |
631 | int py = (y + 1) * 32 - tex.h; |
795 | int py = (y + 1) * 32 - tex.h; |
632 | |
796 | |
633 | if (last_name != tex.name) |
797 | if (last_name != tex.name) |
… | |
… | |
655 | PUSHs (sv_2mortal (newSViv (sw4))); |
819 | PUSHs (sv_2mortal (newSViv (sw4))); |
656 | PUSHs (sv_2mortal (newSViv (sh))); |
820 | PUSHs (sv_2mortal (newSViv (sh))); |
657 | PUSHs (darkness_sv); |
821 | PUSHs (darkness_sv); |
658 | } |
822 | } |
659 | |
823 | |
|
|
824 | SV * |
|
|
825 | get_rect (CFClient::Map self, int x0, int y0, int w, int h) |
|
|
826 | CODE: |
|
|
827 | { |
|
|
828 | int x, y, x1, y1; |
|
|
829 | SV *data_sv = newSV (w * h * 7 + 5); |
|
|
830 | uint8_t *data = (uint8_t *)SvPVX (data_sv); |
|
|
831 | |
|
|
832 | *data++ = 0; /* version 0 format */ |
|
|
833 | *data++ = w >> 8; *data++ = w; |
|
|
834 | *data++ = h >> 8; *data++ = h; |
|
|
835 | |
|
|
836 | // we need to do this 'cause we don't keep an absolute coord system for rows |
|
|
837 | // TODO: treat rows as we treat |
|
|
838 | map_get_row (self, y0 + self->y - self->oy);//D |
|
|
839 | map_get_row (self, y0 + self->y - self->oy + h - 1);//D |
|
|
840 | |
|
|
841 | x0 += self->x - self->ox; |
|
|
842 | y0 += self->y - self->oy; |
|
|
843 | |
|
|
844 | x1 = x0 + w; |
|
|
845 | y1 = y0 + h; |
|
|
846 | |
|
|
847 | for (y = y0; y < y1; y++) |
|
|
848 | { |
|
|
849 | maprow *row = 0 <= y && y < self->rows |
|
|
850 | ? self->row + y |
|
|
851 | : 0; |
|
|
852 | |
|
|
853 | for (x = x0; x < x1; x++) |
|
|
854 | { |
|
|
855 | if (row && row->c0 <= x && x < row->c1) |
|
|
856 | { |
|
|
857 | mapcell *cell = row->col + (x - row->c0); |
|
|
858 | |
|
|
859 | uint8_t flags = 0; |
|
|
860 | |
|
|
861 | if (cell->face [0]) flags |= 1; |
|
|
862 | if (cell->face [1]) flags |= 2; |
|
|
863 | if (cell->face [2]) flags |= 4; |
|
|
864 | |
|
|
865 | *data++ = flags; |
|
|
866 | |
|
|
867 | if (flags & 1) |
|
|
868 | { |
|
|
869 | *data++ = cell->face [0] >> 8; |
|
|
870 | *data++ = cell->face [0]; |
|
|
871 | } |
|
|
872 | |
|
|
873 | if (flags & 2) |
|
|
874 | { |
|
|
875 | *data++ = cell->face [1] >> 8; |
|
|
876 | *data++ = cell->face [1]; |
|
|
877 | } |
|
|
878 | |
|
|
879 | if (flags & 4) |
|
|
880 | { |
|
|
881 | *data++ = cell->face [2] >> 8; |
|
|
882 | *data++ = cell->face [2]; |
|
|
883 | } |
|
|
884 | } |
|
|
885 | else |
|
|
886 | *data++ = 0; |
|
|
887 | } |
|
|
888 | } |
|
|
889 | |
|
|
890 | SvPOK_only (data_sv); |
|
|
891 | SvCUR_set (data_sv, data - (uint8_t *)SvPVX (data_sv)); |
|
|
892 | RETVAL = data_sv; |
|
|
893 | } |
|
|
894 | OUTPUT: |
|
|
895 | RETVAL |
|
|
896 | |
|
|
897 | void |
|
|
898 | set_rect (CFClient::Map self, int x0, int y0, uint8_t *data) |
|
|
899 | PPCODE: |
|
|
900 | { |
|
|
901 | int x, y, z; |
|
|
902 | int x1, y1; |
|
|
903 | |
|
|
904 | if (*data++ != 0) |
|
|
905 | return; /* version mismatch */ |
|
|
906 | |
|
|
907 | int w = *data++ << 8; w |= *data++; |
|
|
908 | int h = *data++ << 8; h |= *data++; |
|
|
909 | |
|
|
910 | // we need to do this 'cause we don't keep an absolute coord system for rows |
|
|
911 | // TODO: treat rows as we treat |
|
|
912 | map_get_row (self, y0 + self->y - self->oy);//D |
|
|
913 | map_get_row (self, y0 + self->y - self->oy + h - 1);//D |
|
|
914 | |
|
|
915 | x0 += self->x - self->ox; |
|
|
916 | y0 += self->y - self->oy; |
|
|
917 | |
|
|
918 | x1 = x0 + w; |
|
|
919 | y1 = y0 + h; |
|
|
920 | |
|
|
921 | for (y = y0; y < y1; y++) |
|
|
922 | { |
|
|
923 | maprow *row = map_get_row (self, y); |
|
|
924 | |
|
|
925 | for (x = x0; x < x1; x++) |
|
|
926 | { |
|
|
927 | uint8_t flags = *data++; |
|
|
928 | |
|
|
929 | if (flags) |
|
|
930 | { |
|
|
931 | mapface face[3] = { 0, 0, 0 }; |
|
|
932 | |
|
|
933 | mapcell *cell = row_get_cell (row, x); |
|
|
934 | |
|
|
935 | if (flags & 1) { face[0] = *data++ << 8; face[0] |= *data++; } |
|
|
936 | if (flags & 2) { face[1] = *data++ << 8; face[1] |= *data++; } |
|
|
937 | if (flags & 4) { face[2] = *data++ << 8; face[2] |= *data++; } |
|
|
938 | |
|
|
939 | if (cell->darkness <= 0) |
|
|
940 | { |
|
|
941 | cell->darkness = -1; |
|
|
942 | |
|
|
943 | for (z = 0; z <= 2; z++) |
|
|
944 | { |
|
|
945 | cell->face[z] = face[z]; |
|
|
946 | |
|
|
947 | if (face[z] && (face[z] >= self->texs || !self->tex[face [z]].name)) |
|
|
948 | XPUSHs (sv_2mortal (newSViv (face[z]))); |
|
|
949 | } |
|
|
950 | } |
|
|
951 | } |
|
|
952 | } |
|
|
953 | } |
|
|
954 | } |
|
|
955 | |