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.29 by root, Thu Apr 13 01:55:38 2006 UTC vs.
Revision 1.46 by root, Mon Apr 17 19:20:59 2006 UTC

6#include <stdio.h> 6#include <stdio.h>
7 7
8#include <SDL.h> 8#include <SDL.h>
9#include <SDL_image.h> 9#include <SDL_image.h>
10#include <SDL_opengl.h> 10#include <SDL_opengl.h>
11
12#include <glib/gmacros.h>
11 13
12#include <pango/pango.h> 14#include <pango/pango.h>
13#include <pango/pangofc-fontmap.h> 15#include <pango/pangofc-fontmap.h>
14#include <pango/pangoft2.h> 16#include <pango/pangoft2.h>
15 17
35 37
36static void 38static void
37substitute_func (FcPattern *pattern, gpointer data) 39substitute_func (FcPattern *pattern, gpointer data)
38{ 40{
39 FcPatternAddBool (pattern, FC_HINTING , 1); 41 FcPatternAddBool (pattern, FC_HINTING , 1);
40 FcPatternAddBool (pattern, FC_AUTOHINT, 1); 42 FcPatternAddBool (pattern, FC_AUTOHINT, 0);
41} 43}
42 44
43static void 45static void
44layout_update (CFClient__Layout self) 46layout_update (CFClient__Layout self)
45{ 47{
46 /* use a random scale factor to account for unknown descenders, 0.8 works 48 /* use a random scale factor to account for unknown descenders, 0.8 works
47 * reasonably well with bitstream vera 49 * reasonably well with bitstream vera
48 */ 50 */
49 PangoFontDescription *font = pango_context_get_font_description (context); 51 PangoFontDescription *font = pango_context_get_font_description (context);
50 pango_font_description_set_absolute_size (font, self->base_height * (PANGO_SCALE * 8 / 10)); 52
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 }
51} 60}
52 61
53static void 62static void
54layout_get_pixel_size (CFClient__Layout self, int *w, int *h) 63layout_get_pixel_size (CFClient__Layout self, int *w, int *h)
55{ 64{
60 *w = (*w + 3) & ~3; 69 *w = (*w + 3) & ~3;
61 if (!*w) *w = 1; 70 if (!*w) *w = 1;
62 if (!*h) *h = 1; 71 if (!*h) *h = 1;
63} 72}
64 73
74typedef uint16_t mapface;
75
76typedef struct {
77 GLint name;
78 int w, h;
79 float s, t;
80 uint8_t r, g, b, a;
81} maptex;
82
65typedef struct { 83typedef struct {
66 int16_t darkness; 84 int16_t darkness;
67 uint16_t face[3]; 85 mapface face[3];
68} mapcell; 86} mapcell;
69 87
70typedef struct { 88typedef struct {
71 uint32_t c0, c1; 89 int32_t c0, c1;
72 mapcell *col; 90 mapcell *col;
73} maprow; 91} maprow;
74 92
75typedef struct map { 93typedef struct map {
76 int x, y, w, h; 94 int x, y, w, h;
95 int ox, oy; /* offset to virtual global coordinate system */
77 int faces; 96 int faces;
78 GLint *face; 97 mapface *face;
98
99 int texs;
100 maptex *tex;
79 101
80 uint32_t rows; 102 uint32_t rows;
81 maprow *row; 103 maprow *row;
82} *CFClient__Map; 104} *CFClient__Map;
83 105
106static char *
107prepend (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
119static char *
120append (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
131static maprow *
132map_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
153static mapcell *
154row_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
179static mapcell *
180map_get_cell (CFClient__Map self, int x, int y)
181{
182 return row_get_cell (map_get_row (self, y), x);
183}
184
185static void
186map_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 self->ox = 0;
198 self->oy = 0;
199 self->row = 0;
200 self->rows = 0;
201}
202
84static void 203static void
85map_blank (CFClient__Map self, int x0, int y0, int w, int h) 204map_blank (CFClient__Map self, int x0, int y0, int w, int h)
86{ 205{
87 int x, y; 206 int x, y;
88 207
89 for (y = y0; y < y0 + h; y++) 208 for (y = y0; y < y0 + h; y++)
209 if (y >= 0)
90 { 210 {
91 if (y >= self->rows) 211 if (y >= self->rows)
92 break; 212 break;
93 213
94 maprow *row = self->row + y; 214 maprow *row = self->row + y;
95 215
96 for (x = x0; x < x0 + w; x++) 216 for (x = x0; x < x0 + w; x++)
97 if (x >= row->c0) 217 if (x >= row->c0)
98 { 218 {
99 if (x >= row->c1) 219 if (x >= row->c1)
100 break; 220 break;
101 221
102 row->col[x].darkness = -1; 222 row->col[x - row->c0].darkness = -1;
103 } 223 }
104 } 224 }
105} 225}
106 226
107MODULE = CFClient PACKAGE = CFClient 227MODULE = CFClient PACKAGE = CFClient
108 228
109PROTOTYPES: ENABLE 229PROTOTYPES: ENABLE
163 SDL_RWops *rw = ix 283 SDL_RWops *rw = ix
164 ? SDL_RWFromFile (image, "r") 284 ? SDL_RWFromFile (image, "r")
165 : SDL_RWFromConstMem (image, image_len); 285 : SDL_RWFromConstMem (image, image_len);
166 286
167 if (!rw) 287 if (!rw)
168 croak ("load_image: unable to open file"); 288 croak ("load_image: %s", SDL_GetError ());
169 289
170 surface = IMG_Load_RW (rw, 1); 290 surface = IMG_Load_RW (rw, 1);
171 if (!surface) 291 if (!surface)
172 croak ("load_image: unable to read file"); 292 croak ("load_image: %s", SDL_GetError ());
173 293
174 fmt.palette = NULL; 294 fmt.palette = NULL;
175 fmt.BitsPerPixel = 32; 295 fmt.BitsPerPixel = 32;
176 fmt.BytesPerPixel = 4; 296 fmt.BytesPerPixel = 4;
177 fmt.Rmask = 0x000000ff; 297 fmt.Rmask = 0x000000ff;
189 fmt.colorkey = 0; 309 fmt.colorkey = 0;
190 fmt.alpha = 0; 310 fmt.alpha = 0;
191 311
192 surface2 = SDL_ConvertSurface (surface, &fmt, SDL_SWSURFACE); 312 surface2 = SDL_ConvertSurface (surface, &fmt, SDL_SWSURFACE);
193 313
314 assert (surface2->pitch == surface2->w * 4);
315
194 EXTEND (SP, 5); 316 EXTEND (SP, 5);
195 PUSHs (sv_2mortal (newSViv (surface2->w))); 317 PUSHs (sv_2mortal (newSViv (surface2->w)));
196 PUSHs (sv_2mortal (newSViv (surface2->h))); 318 PUSHs (sv_2mortal (newSViv (surface2->h)));
197 SDL_LockSurface (surface2); 319 SDL_LockSurface (surface2);
198 PUSHs (sv_2mortal (newSVpvn (surface2->pixels, surface2->h * surface2->pitch))); 320 PUSHs (sv_2mortal (newSVpvn (surface2->pixels, surface2->h * surface2->pitch)));
204 SDL_FreeSurface (surface); 326 SDL_FreeSurface (surface);
205 SDL_FreeSurface (surface2); 327 SDL_FreeSurface (surface2);
206} 328}
207 329
208void 330void
331average (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 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}
354
355void
209fatal (char *message) 356fatal (char *message)
210 CODE: 357 CODE:
211#ifdef WIN32 358#ifdef WIN32
212 MessageBox (0, message, "Crossfire+ Fatal Error", MB_OK | MB_ICONERROR | MB_SETFOREGROUND); 359 MessageBox (0, message, "Crossfire+ Fatal Error", MB_OK | MB_ICONERROR | MB_SETFOREGROUND);
213#else 360#else
214 fprintf (stderr, "%s\n", message); 361 fprintf (stderr, "FATAL: %s\n", message);
215#endif 362#endif
216 exit (1); 363 exit (1);
217 364
218MODULE = CFClient PACKAGE = CFClient::Layout 365MODULE = CFClient PACKAGE = CFClient::Layout
219 366
232 CODE: 379 CODE:
233 g_object_unref (self->pl); 380 g_object_unref (self->pl);
234 Safefree (self); 381 Safefree (self);
235 382
236void 383void
237set_markup (CFClient::Layout self, SV *text_) 384set_text (CFClient::Layout self, SV *text_)
238 CODE: 385 CODE:
239{ 386{
240 STRLEN textlen; 387 STRLEN textlen;
241 char *text = SvPVutf8 (text_, textlen); 388 char *text = SvPVutf8 (text_, textlen);
242 389
390 pango_layout_set_text (self->pl, text, textlen);
391}
392
393void
394set_markup (CFClient::Layout self, SV *text_)
395 CODE:
396{
397 STRLEN textlen;
398 char *text = SvPVutf8 (text_, textlen);
399
243 pango_layout_set_markup (self->pl, text, textlen); 400 pango_layout_set_markup (self->pl, text, textlen);
244} 401}
402
403SV *
404get_text (CFClient::Layout self)
405 CODE:
406 RETVAL = newSVpv (pango_layout_get_text (self), 0);
407 SvUTF8_on (RETVAL);
408 OUTPUT:
409 RETVAL
245 410
246void 411void
247set_height (CFClient::Layout self, int base_height) 412set_height (CFClient::Layout self, int base_height)
248 CODE: 413 CODE:
249 self->base_height = base_height; 414 self->base_height = base_height;
286 PPCODE: 451 PPCODE:
287{ 452{
288 PangoRectangle strong_pos; 453 PangoRectangle strong_pos;
289 layout_update (self); 454 layout_update (self);
290 pango_layout_get_cursor_pos (self->pl, index, &strong_pos, 0); 455 pango_layout_get_cursor_pos (self->pl, index, &strong_pos, 0);
456
291 EXTEND (SP, 3); 457 EXTEND (SP, 3);
292 PUSHs (sv_2mortal (newSViv (strong_pos.x / PANGO_SCALE))); 458 PUSHs (sv_2mortal (newSViv (strong_pos.x / PANGO_SCALE)));
293 PUSHs (sv_2mortal (newSViv (strong_pos.y / PANGO_SCALE))); 459 PUSHs (sv_2mortal (newSViv (strong_pos.y / PANGO_SCALE)));
294 PUSHs (sv_2mortal (newSViv (strong_pos.height / PANGO_SCALE))); 460 PUSHs (sv_2mortal (newSViv (strong_pos.height / PANGO_SCALE)));
295} 461}
327} 493}
328 494
329MODULE = CFClient PACKAGE = CFClient::Texture 495MODULE = CFClient PACKAGE = CFClient::Texture
330 496
331void 497void
332draw_quad (SV *self, double x, double y, double w = 0, double h = 0) 498draw_quad (SV *self, float x, float y, float w = 0, float h = 0)
333 PROTOTYPE: $$$;$$ 499 PROTOTYPE: $$$;$$
334 CODE: 500 CODE:
335{ 501{
336 HV *hv = (HV *)SvRV (self); 502 HV *hv = (HV *)SvRV (self);
337 double s = SvNV (*hv_fetch (hv, "s", 1, 1)); 503 float s = SvNV (*hv_fetch (hv, "s", 1, 1));
338 double t = SvNV (*hv_fetch (hv, "t", 1, 1)); 504 float t = SvNV (*hv_fetch (hv, "t", 1, 1));
339 int name = SvIV (*hv_fetch (hv, "name", 4, 1)); 505 int name = SvIV (*hv_fetch (hv, "name", 4, 1));
506 int wrap_mode = SvIV (*hv_fetch (hv, "wrap_mode", 9, 1));
340 507
341 if (items < 5) 508 if (items < 5)
342 { 509 {
343 w = SvNV (*hv_fetch (hv, "w", 1, 1)); 510 w = SvNV (*hv_fetch (hv, "w", 1, 1));
344 h = SvNV (*hv_fetch (hv, "h", 1, 1)); 511 h = SvNV (*hv_fetch (hv, "h", 1, 1));
345 } 512 }
346 513
347 glBindTexture (GL_TEXTURE_2D, name); 514 glBindTexture (GL_TEXTURE_2D, name);
515 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 }
348 glBegin (GL_QUADS); 519 glBegin (GL_QUADS);
349 glTexCoord2d (0, 0); glVertex2d (x , y ); 520 glTexCoord2f (0, 0); glVertex2f (x , y );
350 glTexCoord2d (0, t); glVertex2d (x , y + h); 521 glTexCoord2f (0, t); glVertex2f (x , y + h);
351 glTexCoord2d (s, t); glVertex2d (x + w, y + h); 522 glTexCoord2f (s, t); glVertex2f (x + w, y + h);
352 glTexCoord2d (s, 0); glVertex2d (x + w, y ); 523 glTexCoord2f (s, 0); glVertex2f (x + w, y );
353 glEnd (); 524 glEnd ();
354} 525}
355 526
356MODULE = CFClient PACKAGE = CFClient::Map 527MODULE = CFClient PACKAGE = CFClient::Map
357 528
358CFClient::Map 529CFClient::Map
359new (SV *class, int map_width, int map_height) 530new (SV *class, int map_width, int map_height)
360 CODE: 531 CODE:
361 New (0, RETVAL, 1, struct map); 532 New (0, RETVAL, 1, struct map);
362 RETVAL->x = 0; 533 RETVAL->x = 0;
363 RETVAL->y = 0; 534 RETVAL->y = 0;
364 RETVAL->w = map_width; 535 RETVAL->w = map_width;
365 RETVAL->h = map_height; 536 RETVAL->h = map_height;
537 RETVAL->ox = 0;
538 RETVAL->oy = 0;
366 RETVAL->faces = 1; 539 RETVAL->faces = 8192;
367 Newz (0, RETVAL->face, 1, GLint); 540 Newz (0, RETVAL->face, RETVAL->faces, mapface);
541 RETVAL->texs = 8192;
542 Newz (0, RETVAL->tex, RETVAL->texs, maptex);
368 RETVAL->rows = 0; 543 RETVAL->rows = 0;
369 RETVAL->row = 0; 544 RETVAL->row = 0;
370 printf ("new map %d,%d\n", map_width, map_height);//D
371 OUTPUT: 545 OUTPUT:
372 RETVAL 546 RETVAL
373 547
374void 548void
375DESTROY (CFClient::Map self) 549DESTROY (CFClient::Map self)
376 CODE: 550 CODE:
377{ 551{
378 int r, c; 552 map_clear (self);
379
380 printf ("detroy map\n");//D
381 Safefree (self->face); 553 Safefree (self->face);
382
383 for (r = 0; r < self->rows; r++)
384 Safefree (self->row[r].col);
385
386 Safefree (self->row);
387 Safefree (self); 554 Safefree (self);
388} 555}
389 556
390void 557void
558clear (CFClient::Map self)
559 CODE:
560 map_clear (self);
561
562void
391set_face (CFClient::Map self, int facenum, int face) 563set_face (CFClient::Map self, int face, int texid)
392 CODE: 564 CODE:
393{ 565{
394 while (self->faces < facenum) 566 while (self->faces <= face)
395 { 567 {
396 printf ("setface %d (%d) = %d\n",facenum, self->faces, face);//D
397 Renew (self->face, self->faces * 2, GLint);
398 Zero (self->face + self->faces, self->faces, GLint); 568 Append (mapface, self->face, self->faces, self->faces);
399 self->faces *= 2; 569 self->faces *= 2;
400 } 570 }
401 571
402 self->face [facenum] = face; 572 self->face [face] = texid;
403} 573}
574
575void
576set_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
587 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}
597
598int
599ox (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
404 610
405void 611void
406scroll (CFClient::Map self, int dx, int dy) 612scroll (CFClient::Map self, int dx, int dy)
407 CODE: 613 CODE:
408{ 614{
409 printf ("map_scroll %d,%d\n", dx, dy);//D
410 if (dx > 0) 615 if (dx > 0)
411 map_blank (self, self->x, self->y, dx - 1, self->h); 616 map_blank (self, self->x, self->y, dx - 1, self->h);
412 else if (dx < 0) 617 else if (dx < 0)
413 map_blank (self, self->x + self->w + dx + 1, self->y, 1 - dx, self->h); 618 map_blank (self, self->x + self->w + dx + 1, self->y, 1 - dx, self->h);
414 619
415 if (dy > 0) 620 if (dy > 0)
416 map_blank (self, self->x, self->y, self->w, dy - 1); 621 map_blank (self, self->x, self->y, self->w, dy - 1);
417 else if (dy < 0) 622 else if (dy < 0)
418 map_blank (self, self->x, self->y + self->h + dy + 1, self->w, 1 - dy); 623 map_blank (self, self->x, self->y + self->h + dy + 1, self->w, 1 - dy);
419 624
420 self->x += dx; 625 self->ox += dx; self->x += dx;
421 self->y += dy; 626 self->oy += dy; self->y += dy;
422 627
423 while (self->y < 0) 628 while (self->y < 0)
424 { 629 {
425 maprow *row; 630 Prepend (maprow, self->row, self->rows, MAP_EXTEND_Y);
426
427 New (0, row, self->rows + MAP_EXTEND_Y, maprow);
428 Move (self->row, row + MAP_EXTEND_Y, self->rows, maprow);
429 Zero (row, MAP_EXTEND_Y, maprow);
430 631
431 self->rows += MAP_EXTEND_Y; 632 self->rows += MAP_EXTEND_Y;
432 self->y += MAP_EXTEND_Y; 633 self->y += MAP_EXTEND_Y;
433 self->row = row;
434 } 634 }
435
436 if (self->x < 0)
437 {
438 int y;
439 for (y = 0; y < self->rows; y++)
440 {
441 self->row[y].c0 += self->x;
442 self->row[y].c1 += self->x;
443 }
444
445 self->x = 0;
446 }
447} 635}
448 636
449void 637void
450map1a_update (CFClient::Map self, SV *data_) 638map1a_update (CFClient::Map self, SV *data_)
451 CODE: 639 CODE:
452{ 640{
453 uint8_t *data = (uint8_t *)SvPVbytes_nolen (data_); 641 uint8_t *data = (uint8_t *)SvPVbyte_nolen (data_);
454 uint8_t *data_end = (uint8_t *)SvEND (data_) + 1; 642 uint8_t *data_end = (uint8_t *)SvEND (data_);
455 643
456 while (data < data_end) 644 while (data < data_end)
457 { 645 {
458 int flags = (data [0] << 8) + data [1]; data += 2; 646 int flags = (data [0] << 8) + data [1]; data += 2;
647
459 int x = ((flags >> 10) & 63) + self->x; 648 int x = ((flags >> 10) & 63) + self->x;
460 int y = ((flags >> 4) & 63) + self->y; 649 int y = ((flags >> 4) & 63) + self->y;
461 650
462 while (y >= self->rows) 651 mapcell *cell = map_get_cell (self, x, y);
463 {
464 Renew (self->row, self->rows + MAP_EXTEND_Y, maprow);
465 Zero (self->row + self->rows, MAP_EXTEND_Y, maprow);
466
467 self->rows += MAP_EXTEND_Y;
468 }
469
470 assert (y < self->rows);//D
471
472 maprow *row = self->row + y;
473
474 if (!row->col)
475 {
476 Newz (0, row->col, MAP_EXTEND_X, mapcell);
477 row->c0 = self->x - MAP_EXTEND_X / 4;
478 row->c1 = row->c0 + MAP_EXTEND_X;
479 }
480
481 if (row->c0 > x)
482 {
483 int extend = row->c0 - x + MAP_EXTEND_X;
484 mapcell *col;
485
486 New (0, col, row->c1 - row->c0 + extend, mapcell);
487 Move (row->col, col + extend, row->c1 - row->c0, mapcell);
488 Zero (col, extend, mapcell);
489
490 row->col = col;
491 row->c0 -= extend;
492 }
493 else if (x >= row->c1)
494 {
495 int extend = x - row->c1 + MAP_EXTEND_X;
496
497 Renew (row->col, row->c1 - row->c0 + extend, mapcell);
498 Zero (row->col + row->c1 - row->c0, extend, mapcell);
499
500 row->c1 += extend;
501 }
502
503 assert (x >= row->c0);//D
504 assert (x < row->c1);//D
505 mapcell *cell = row->col + (x - row->c0);
506 652
507 if (flags & 15) 653 if (flags & 15)
508 { 654 {
509 if (cell->darkness < 0) 655 if (cell->darkness < 0)
510 { 656 {
511 cell->darkness = 0; 657 cell->darkness = 0;
512 cell->face [0] = 0; 658 cell->face [0] = 0;
513 cell->face [1] = 0; 659 cell->face [1] = 0;
514 cell->face [2] = 0; 660 cell->face [2] = 0;
515 } 661 }
516 662
517 cell->darkness = flags & 8 ? *data++ : 255; 663 cell->darkness = flags & 8 ? *data++ : 255;
664
665 //TODO: don't trust server data to be in-range(!)
518 666
519 if (flags & 4) 667 if (flags & 4)
520 { 668 {
521 cell->face [0] = (data [0] << 8) + data [1]; data += 2; 669 cell->face [0] = self->face [(data [0] << 8) + data [1]]; data += 2;
522 } 670 }
523 671
524 if (flags & 2) 672 if (flags & 2)
525 { 673 {
526 cell->face [1] = (data [0] << 8) + data [1]; data += 2; 674 cell->face [1] = self->face [(data [0] << 8) + data [1]]; data += 2;
527 } 675 }
528 676
529 if (flags & 1) 677 if (flags & 1)
530 { 678 {
531 cell->face [2] = (data [0] << 8) + data [1]; data += 2; 679 cell->face [2] = self->face [(data [0] << 8) + data [1]]; data += 2;
532 } 680 }
533 } 681 }
534 else 682 else
535 cell->darkness = -1; 683 cell->darkness = -1;
536 } 684 }
537} 685}
538 686
687SV *
688mapmap (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 mapface face = cell->face [z];
720
721 if (face)
722 {
723 maptex tex = self->tex [face];
724 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
747void
748draw (CFClient::Map self, int shift_x, int shift_y, int x0, int y0, int sw, int sh)
749 PPCODE:
750{
751 int sw4 = (sw + 3) & ~3;
752 SV *darkness_sv = sv_2mortal (newSV (sw4 * sh));
753 uint8_t *darkness = (uint8_t *)SvPVX (darkness_sv);
754 memset (darkness, 255, sw4 * sh);
755 SvPOK_only (darkness_sv);
756 SvCUR_set (darkness_sv, sw4 * sh);
757
758 int vx = self->x + (self->w - sw) / 2 - shift_x;
759 int vy = self->y + (self->h - sh) / 2 - shift_y;
760
761 /*
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
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
795 darkness[y * sw4 + x] = cell->darkness < 0
796 ? 255 - FOW_DARKNESS
797 : 255 - cell->darkness;
798
799 mapface face = cell->face [z];
800
801 if (face)
802 {
803 maptex tex = self->tex [face];
804
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
826 glDisable (GL_TEXTURE_2D);
827 glDisable (GL_BLEND);
828
829 EXTEND (SP, 3);
830 PUSHs (sv_2mortal (newSViv (sw4)));
831 PUSHs (sv_2mortal (newSViv (sh)));
832 PUSHs (darkness_sv);
833}
834
835SV *
836get_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
908void
909set_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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines