ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/Client.xs
(Generate patch)

Comparing deliantra/Deliantra-Client/Client.xs (file contents):
Revision 1.35 by root, Fri Apr 14 02:03:10 2006 UTC vs.
Revision 1.48 by root, Tue Apr 18 00:14:16 2006 UTC

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
55static void 74static void
56layout_get_pixel_size (CFClient__Layout self, int *w, int *h) 75layout_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
86typedef uint16_t mapface;
66 87
67typedef struct { 88typedef 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
73typedef struct { 95typedef struct {
74 int16_t darkness; 96 int16_t darkness;
75 uint16_t face[3]; 97 mapface face[3];
76} mapcell; 98} mapcell;
77 99
78typedef struct { 100typedef struct {
79 int32_t c0, c1; 101 int32_t c0, c1;
80 mapcell *col; 102 mapcell *col;
81} maprow; 103} maprow;
82 104
83typedef struct map { 105typedef 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
93static char * 118static char *
94prepend (char *ptr, int sze, int inc) 119prepend (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
143static maprow *
144map_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
165static mapcell *
166row_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
191static mapcell *
192map_get_cell (CFClient__Map self, int x, int y)
193{
194 return row_get_cell (map_get_row (self, y), x);
195}
196
118static void 197static void
119map_clear (CFClient__Map self) 198map_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
136static void 215static void
137map_blank (CFClient__Map self, int x0, int y0, int w, int h) 216map_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
171void 251void
172lowdelay (int fd, int val = 1) 252lowdelay (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
176char * 258char *
177gl_version () 259gl_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;
230 fmt.Rmask = 0x000000ff; 312 fmt.Rmask = 0x000000ff;
242 fmt.colorkey = 0; 324 fmt.colorkey = 0;
243 fmt.alpha = 0; 325 fmt.alpha = 0;
244 326
245 surface2 = SDL_ConvertSurface (surface, &fmt, SDL_SWSURFACE); 327 surface2 = SDL_ConvertSurface (surface, &fmt, SDL_SWSURFACE);
246 328
329 assert (surface2->pitch == surface2->w * 4);
330
247 EXTEND (SP, 5); 331 EXTEND (SP, 5);
248 PUSHs (sv_2mortal (newSViv (surface2->w))); 332 PUSHs (sv_2mortal (newSViv (surface2->w)));
249 PUSHs (sv_2mortal (newSViv (surface2->h))); 333 PUSHs (sv_2mortal (newSViv (surface2->h)));
250 SDL_LockSurface (surface2); 334 SDL_LockSurface (surface2);
251 PUSHs (sv_2mortal (newSVpvn (surface2->pixels, surface2->h * surface2->pitch))); 335 PUSHs (sv_2mortal (newSVpvn (surface2->pixels, surface2->h * surface2->pitch)));
257 SDL_FreeSurface (surface); 341 SDL_FreeSurface (surface);
258 SDL_FreeSurface (surface2); 342 SDL_FreeSurface (surface2);
259} 343}
260 344
261void 345void
346average (int x, int y, uint32_t *data)
347 PPCODE:
348{
349 uint32_t r = 0, g = 0, b = 0, a = 0;
350
351 x = y = x * y;
352
353 while (x--)
354 {
355 uint32_t p = *data++;
356
357 r += (p ) & 255;
358 g += (p >> 8) & 255;
359 b += (p >> 16) & 255;
360 a += (p >> 24) & 255;
361 }
362
363 EXTEND (SP, 4);
364 PUSHs (sv_2mortal (newSViv (r / y)));
365 PUSHs (sv_2mortal (newSViv (g / y)));
366 PUSHs (sv_2mortal (newSViv (b / y)));
367 PUSHs (sv_2mortal (newSViv (a / y)));
368}
369
370void
262fatal (char *message) 371fatal (char *message)
263 CODE: 372 CODE:
264#ifdef WIN32 373#ifdef WIN32
265 MessageBox (0, message, "Crossfire+ Fatal Error", MB_OK | MB_ICONERROR | MB_SETFOREGROUND); 374 MessageBox (0, message, "Crossfire+ Fatal Error", MB_OK | MB_ICONERROR | MB_SETFOREGROUND);
266#else 375#else
267 fprintf (stderr, "%s\n", message); 376 fprintf (stderr, "FATAL: %s\n", message);
268#endif 377#endif
269 exit (1); 378 exit (1);
270 379
271MODULE = CFClient PACKAGE = CFClient::Layout 380MODULE = CFClient PACKAGE = CFClient::Layout
272 381
304 char *text = SvPVutf8 (text_, textlen); 413 char *text = SvPVutf8 (text_, textlen);
305 414
306 pango_layout_set_markup (self->pl, text, textlen); 415 pango_layout_set_markup (self->pl, text, textlen);
307} 416}
308 417
418SV *
419get_text (CFClient::Layout self)
420 CODE:
421 RETVAL = newSVpv (pango_layout_get_text (self->pl), 0);
422 SvUTF8_on (RETVAL);
423 OUTPUT:
424 RETVAL
425
309void 426void
310set_height (CFClient::Layout self, int base_height) 427set_height (CFClient::Layout self, int base_height)
311 CODE: 428 CODE:
312 self->base_height = base_height; 429 self->base_height = base_height;
313 430
391} 508}
392 509
393MODULE = CFClient PACKAGE = CFClient::Texture 510MODULE = CFClient PACKAGE = CFClient::Texture
394 511
395void 512void
396draw_quad (SV *self, double x, double y, double w = 0, double h = 0) 513draw_quad (SV *self, float x, float y, float w = 0, float h = 0)
397 PROTOTYPE: $$$;$$ 514 PROTOTYPE: $$$;$$
398 CODE: 515 CODE:
399{ 516{
400 HV *hv = (HV *)SvRV (self); 517 HV *hv = (HV *)SvRV (self);
401 double s = SvNV (*hv_fetch (hv, "s", 1, 1)); 518 float s = SvNV (*hv_fetch (hv, "s", 1, 1));
402 double t = SvNV (*hv_fetch (hv, "t", 1, 1)); 519 float t = SvNV (*hv_fetch (hv, "t", 1, 1));
403 int name = SvIV (*hv_fetch (hv, "name", 4, 1)); 520 int name = SvIV (*hv_fetch (hv, "name", 4, 1));
521 int wrap_mode = SvIV (*hv_fetch (hv, "wrap_mode", 9, 1));
404 522
405 if (items < 5) 523 if (items < 5)
406 { 524 {
407 w = SvNV (*hv_fetch (hv, "w", 1, 1)); 525 w = SvNV (*hv_fetch (hv, "w", 1, 1));
408 h = SvNV (*hv_fetch (hv, "h", 1, 1)); 526 h = SvNV (*hv_fetch (hv, "h", 1, 1));
409 } 527 }
410 528
411 glBindTexture (GL_TEXTURE_2D, name); 529 glBindTexture (GL_TEXTURE_2D, name);
530 if (wrap_mode) {
531 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
532 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
533 }
412 glBegin (GL_QUADS); 534 glBegin (GL_QUADS);
413 glTexCoord2d (0, 0); glVertex2d (x , y ); 535 glTexCoord2f (0, 0); glVertex2f (x , y );
414 glTexCoord2d (0, t); glVertex2d (x , y + h); 536 glTexCoord2f (0, t); glVertex2f (x , y + h);
415 glTexCoord2d (s, t); glVertex2d (x + w, y + h); 537 glTexCoord2f (s, t); glVertex2f (x + w, y + h);
416 glTexCoord2d (s, 0); glVertex2d (x + w, y ); 538 glTexCoord2f (s, 0); glVertex2f (x + w, y );
417 glEnd (); 539 glEnd ();
418} 540}
419 541
420MODULE = CFClient PACKAGE = CFClient::Map 542MODULE = CFClient PACKAGE = CFClient::Map
421 543
422CFClient::Map 544CFClient::Map
423new (SV *class, int map_width, int map_height) 545new (SV *class, int map_width, int map_height)
424 CODE: 546 CODE:
425 New (0, RETVAL, 1, struct map); 547 New (0, RETVAL, 1, struct map);
426 RETVAL->x = 0; 548 RETVAL->x = 0;
427 RETVAL->y = 0; 549 RETVAL->y = 0;
428 RETVAL->w = map_width; 550 RETVAL->w = map_width;
429 RETVAL->h = map_height; 551 RETVAL->h = map_height;
430 RETVAL->vx = 0; 552 RETVAL->ox = 0;
431 RETVAL->vy = 0; 553 RETVAL->oy = 0;
432 RETVAL->faces = 8192; 554 RETVAL->faces = 8192;
433 Newz (0, RETVAL->face, RETVAL->faces, mapface); 555 Newz (0, RETVAL->face, RETVAL->faces, mapface);
556 RETVAL->texs = 8192;
557 Newz (0, RETVAL->tex, RETVAL->texs, maptex);
434 RETVAL->rows = 0; 558 RETVAL->rows = 0;
435 RETVAL->row = 0; 559 RETVAL->row = 0;
436 OUTPUT: 560 OUTPUT:
437 RETVAL 561 RETVAL
438 562
449clear (CFClient::Map self) 573clear (CFClient::Map self)
450 CODE: 574 CODE:
451 map_clear (self); 575 map_clear (self);
452 576
453void 577void
454set_texture (CFClient::Map self, int face, int name, int w, int h, float s, float t) 578set_face (CFClient::Map self, int face, int texid)
455 CODE: 579 CODE:
456{ 580{
457 while (self->faces < face) 581 while (self->faces <= face)
458 { 582 {
459 Append (mapface, self->face, self->faces, self->faces); 583 Append (mapface, self->face, self->faces, self->faces);
460 self->faces *= 2; 584 self->faces *= 2;
461 } 585 }
462 586
463 self->face [face].name = name;
464 self->face [face].w = w; 587 self->face [face] = texid;
465 self->face [face].h = h;
466 self->face [face].s = s;
467 self->face [face].t = t;
468} 588}
589
590void
591set_texture (CFClient::Map self, int texid, int name, int w, int h, float s, float t, int r, int g, int b, int a)
592 CODE:
593{
594 while (self->texs <= texid)
595 {
596 Append (maptex, self->tex, self->texs, self->texs);
597 self->texs *= 2;
598 }
599
600 {
601 maptex *tex = self->tex + texid;
602
603 tex->name = name;
604 tex->w = w;
605 tex->h = h;
606 tex->s = s;
607 tex->t = t;
608 tex->r = r;
609 tex->g = g;
610 tex->b = b;
611 tex->a = a;
612 }
613}
614
615int
616ox (CFClient::Map self)
617 ALIAS:
618 oy = 1
619 CODE:
620 switch (ix)
621 {
622 case 0: RETVAL = self->ox; break;
623 case 1: RETVAL = self->oy; break;
624 }
625 OUTPUT:
626 RETVAL
469 627
470void 628void
471scroll (CFClient::Map self, int dx, int dy) 629scroll (CFClient::Map self, int dx, int dy)
472 CODE: 630 CODE:
473{ 631{
479 if (dy > 0) 637 if (dy > 0)
480 map_blank (self, self->x, self->y, self->w, dy - 1); 638 map_blank (self, self->x, self->y, self->w, dy - 1);
481 else if (dy < 0) 639 else if (dy < 0)
482 map_blank (self, self->x, self->y + self->h + dy + 1, self->w, 1 - dy); 640 map_blank (self, self->x, self->y + self->h + dy + 1, self->w, 1 - dy);
483 641
484 self->x += dx; 642 self->ox += dx; self->x += dx;
485 self->y += dy; 643 self->oy += dy; self->y += dy;
486 644
487 while (self->y < 0) 645 while (self->y < 0)
488 { 646 {
489 Prepend (maprow, self->row, self->rows, MAP_EXTEND_Y); 647 Prepend (maprow, self->row, self->rows, MAP_EXTEND_Y);
490 648
497map1a_update (CFClient::Map self, SV *data_) 655map1a_update (CFClient::Map self, SV *data_)
498 CODE: 656 CODE:
499{ 657{
500 uint8_t *data = (uint8_t *)SvPVbyte_nolen (data_); 658 uint8_t *data = (uint8_t *)SvPVbyte_nolen (data_);
501 uint8_t *data_end = (uint8_t *)SvEND (data_); 659 uint8_t *data_end = (uint8_t *)SvEND (data_);
502 660 mapcell *cell;
661 int x, y, flags;
662
503 while (data < data_end) 663 while (data < data_end)
504 { 664 {
505 int flags = (data [0] << 8) + data [1]; data += 2; 665 flags = (data [0] << 8) + data [1]; data += 2;
506 666
507 int x = ((flags >> 10) & 63) + self->x; 667 x = ((flags >> 10) & 63) + self->x;
508 int y = ((flags >> 4) & 63) + self->y; 668 y = ((flags >> 4) & 63) + self->y;
509 669
510 while (y >= self->rows) 670 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 671
545 if (flags & 15) 672 if (flags & 15)
546 { 673 {
547 if (cell->darkness < 0 && x < self->w && y < self->h) 674 if (cell->darkness < 0)
548 { 675 {
549 cell->darkness = 0; 676 cell->darkness = 0;
550 cell->face [0] = 0; 677 cell->face [0] = 0;
551 cell->face [1] = 0; 678 cell->face [1] = 0;
552 cell->face [2] = 0; 679 cell->face [2] = 0;
553 } 680 }
554 681
555 cell->darkness = flags & 8 ? *data++ : 255; 682 cell->darkness = flags & 8 ? *data++ : 255;
683
684 //TODO: don't trust server data to be in-range(!)
556 685
557 if (flags & 4) 686 if (flags & 4)
558 { 687 {
559 cell->face [0] = (data [0] << 8) + data [1]; data += 2; 688 cell->face [0] = self->face [(data [0] << 8) + data [1]]; data += 2;
560 } 689 }
561 690
562 if (flags & 2) 691 if (flags & 2)
563 { 692 {
564 cell->face [1] = (data [0] << 8) + data [1]; data += 2; 693 cell->face [1] = self->face [(data [0] << 8) + data [1]]; data += 2;
565 } 694 }
566 695
567 if (flags & 1) 696 if (flags & 1)
568 { 697 {
569 cell->face [2] = (data [0] << 8) + data [1]; data += 2; 698 cell->face [2] = self->face [(data [0] << 8) + data [1]]; data += 2;
570 } 699 }
571 } 700 }
572 else 701 else
573 cell->darkness = -1; 702 cell->darkness = -1;
574 } 703 }
575} 704}
576 705
706SV *
707mapmap (CFClient::Map self, int w, int h)
708 CODE:
709{
710 int x0, x1, x;
711 int y0, y1, y;
712 int z;
713 SV *map_sv = newSV (w * h * sizeof (uint32_t));
714 uint32_t *map = (uint32_t *)SvPVX (map_sv);
715
716 SvPOK_only (map_sv);
717 SvCUR_set (map_sv, w * h * sizeof (uint32_t));
718
719 x0 = self->x - w / 2; x1 = x0 + w;
720 y0 = self->y - h / 2; y1 = y0 + h;
721
722 for (y = y0; y < y1; y++)
723 {
724 maprow *row = 0 <= y && y < self->rows
725 ? self->row + y
726 : 0;
727
728 for (x = x0; x < x1; x++)
729 {
730 int r = 32, g = 32, b = 32, a = 192;
731
732 if (row && row->c0 <= x && x < row->c1)
733 {
734 mapcell *cell = row->col + (x - row->c0);
735
736 for (z = 0; z <= 0; z++)
737 {
738 mapface face = cell->face [z];
739
740 if (face)
741 {
742 maptex tex = self->tex [face];
743 int a0 = 255 - tex.a;
744 int a1 = tex.a;
745
746 r = (r * a0 + tex.r * a1) / 255;
747 g = (g * a0 + tex.g * a1) / 255;
748 b = (b * a0 + tex.b * a1) / 255;
749 a = (a * a0 + tex.a * a1) / 255;
750 }
751 }
752 }
753
754 *map++ = (r )
755 | (g << 8)
756 | (b << 16)
757 | (a << 24);
758 }
759 }
760
761 RETVAL = map_sv;
762}
763 OUTPUT:
764 RETVAL
765
577void 766void
578draw (CFClient::Map self, int x0, int y0, int sw, int sh) 767draw (CFClient::Map self, int shift_x, int shift_y, int x0, int y0, int sw, int sh)
579 PPCODE: 768 PPCODE:
580{ 769{
770 int vx, vy;
771 int x, y, z;
772 int last_name;
773 mapface face;
581 int sw4 = (sw + 3) & ~3; 774 int sw4 = (sw + 3) & ~3;
582 SV *darkness_sv = sv_2mortal (newSV (sw4 * sh)); 775 SV *darkness_sv = sv_2mortal (newSV (sw4 * sh));
583 uint8_t *darkness = (uint8_t *)SvPVX (darkness_sv); 776 uint8_t *darkness = (uint8_t *)SvPVX (darkness_sv);
584 777
778 memset (darkness, 255, sw4 * sh);
585 SvPOK_only (darkness_sv); 779 SvPOK_only (darkness_sv);
586 SvCUR_set (darkness_sv, sw4 * sh); 780 SvCUR_set (darkness_sv, sw4 * sh);
587 781
782 vx = self->x + (self->w - sw) / 2 - shift_x;
783 vy = self->y + (self->h - sh) / 2 - shift_y;
784
785 /*
588 int vx = self->vx = self->w >= sw 786 int vx = self->vx = self->w >= sw
589 ? self->x + (self->w - sw) / 2 787 ? self->x + (self->w - sw) / 2
590 : MIN (self->x, MAX (self->x + self->w - sw + 1, self->vx)); 788 : MIN (self->x, MAX (self->x + self->w - sw + 1, self->vx));
591 789
592 int vy = self->vy = self->h >= sh 790 int vy = self->vy = self->h >= sh
593 ? self->y + (self->h - sh) / 2 791 ? self->y + (self->h - sh) / 2
594 : MIN (self->y, MAX (self->y + self->h - sh + 1, self->vy)); 792 : MIN (self->y, MAX (self->y + self->h - sh + 1, self->vy));
793 */
595 794
596 glColor4ub (255, 255, 255, 255); 795 glColor4ub (255, 255, 255, 255);
597 796
598 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 797 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
599 glEnable (GL_BLEND); 798 glEnable (GL_BLEND);
600 glEnable (GL_TEXTURE_2D); 799 glEnable (GL_TEXTURE_2D);
601 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); 800 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
602 801
603 int x, y, z;
604
605 int last_name = 0;
606
607 glBegin (GL_QUADS); 802 glBegin (GL_QUADS);
803
804 last_name = 0;
608 805
609 for (z = 0; z < 3; z++) 806 for (z = 0; z < 3; z++)
610 for (y = 0; y < sh; y++) 807 for (y = 0; y < sh; y++)
611 if (0 <= y + vy && y + vy < self->rows) 808 if (0 <= y + vy && y + vy < self->rows)
612 { 809 {
619 816
620 darkness[y * sw4 + x] = cell->darkness < 0 817 darkness[y * sw4 + x] = cell->darkness < 0
621 ? 255 - FOW_DARKNESS 818 ? 255 - FOW_DARKNESS
622 : 255 - cell->darkness; 819 : 255 - cell->darkness;
623 820
624 uint16_t face = cell->face [z]; 821 face = cell->face [z];
625 822
626 if (face) 823 if (face)
627 { 824 {
628 mapface tex = self->face [face]; 825 maptex tex = self->tex [face];
629 826
630 int px = (x + 1) * 32 - tex.w; 827 int px = (x + 1) * 32 - tex.w;
631 int py = (y + 1) * 32 - tex.h; 828 int py = (y + 1) * 32 - tex.h;
632 829
633 if (last_name != tex.name) 830 if (last_name != tex.name)
655 PUSHs (sv_2mortal (newSViv (sw4))); 852 PUSHs (sv_2mortal (newSViv (sw4)));
656 PUSHs (sv_2mortal (newSViv (sh))); 853 PUSHs (sv_2mortal (newSViv (sh)));
657 PUSHs (darkness_sv); 854 PUSHs (darkness_sv);
658} 855}
659 856
857SV *
858get_rect (CFClient::Map self, int x0, int y0, int w, int h)
859 CODE:
860{
861 int x, y, x1, y1;
862 SV *data_sv = newSV (w * h * 7 + 5);
863 uint8_t *data = (uint8_t *)SvPVX (data_sv);
864
865 *data++ = 0; /* version 0 format */
866 *data++ = w >> 8; *data++ = w;
867 *data++ = h >> 8; *data++ = h;
868
869 // we need to do this 'cause we don't keep an absolute coord system for rows
870 // TODO: treat rows as we treat
871 map_get_row (self, y0 + self->y - self->oy);//D
872 map_get_row (self, y0 + self->y - self->oy + h - 1);//D
873
874 x0 += self->x - self->ox;
875 y0 += self->y - self->oy;
876
877 x1 = x0 + w;
878 y1 = y0 + h;
879
880 for (y = y0; y < y1; y++)
881 {
882 maprow *row = 0 <= y && y < self->rows
883 ? self->row + y
884 : 0;
885
886 for (x = x0; x < x1; x++)
887 {
888 if (row && row->c0 <= x && x < row->c1)
889 {
890 mapcell *cell = row->col + (x - row->c0);
891 uint8_t flags = 0;
892
893 if (cell->face [0]) flags |= 1;
894 if (cell->face [1]) flags |= 2;
895 if (cell->face [2]) flags |= 4;
896
897 *data++ = flags;
898
899 if (flags & 1)
900 {
901 *data++ = cell->face [0] >> 8;
902 *data++ = cell->face [0];
903 }
904
905 if (flags & 2)
906 {
907 *data++ = cell->face [1] >> 8;
908 *data++ = cell->face [1];
909 }
910
911 if (flags & 4)
912 {
913 *data++ = cell->face [2] >> 8;
914 *data++ = cell->face [2];
915 }
916 }
917 else
918 *data++ = 0;
919 }
920 }
921
922 SvPOK_only (data_sv);
923 SvCUR_set (data_sv, data - (uint8_t *)SvPVX (data_sv));
924 RETVAL = data_sv;
925}
926 OUTPUT:
927 RETVAL
928
929void
930set_rect (CFClient::Map self, int x0, int y0, uint8_t *data)
931 PPCODE:
932{
933 int x, y, z;
934 int w, h;
935 int x1, y1;
936
937 if (*data++ != 0)
938 return; /* version mismatch */
939
940 w = *data++ << 8; w |= *data++;
941 h = *data++ << 8; h |= *data++;
942
943 // we need to do this 'cause we don't keep an absolute coord system for rows
944 // TODO: treat rows as we treat
945 map_get_row (self, y0 + self->y - self->oy);//D
946 map_get_row (self, y0 + self->y - self->oy + h - 1);//D
947
948 x0 += self->x - self->ox;
949 y0 += self->y - self->oy;
950
951 x1 = x0 + w;
952 y1 = y0 + h;
953
954 for (y = y0; y < y1; y++)
955 {
956 maprow *row = map_get_row (self, y);
957
958 for (x = x0; x < x1; x++)
959 {
960 uint8_t flags = *data++;
961
962 if (flags)
963 {
964 mapface face[3] = { 0, 0, 0 };
965
966 mapcell *cell = row_get_cell (row, x);
967
968 if (flags & 1) { face[0] = *data++ << 8; face[0] |= *data++; }
969 if (flags & 2) { face[1] = *data++ << 8; face[1] |= *data++; }
970 if (flags & 4) { face[2] = *data++ << 8; face[2] |= *data++; }
971
972 if (cell->darkness <= 0)
973 {
974 cell->darkness = -1;
975
976 for (z = 0; z <= 2; z++)
977 {
978 cell->face[z] = face[z];
979
980 if (face[z] && (face[z] >= self->texs || !self->tex[face [z]].name))
981 XPUSHs (sv_2mortal (newSViv (face[z])));
982 }
983 }
984 }
985 }
986 }
987}
988

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines