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.40 by root, Sat Apr 15 01:13:46 2006 UTC vs.
Revision 1.46 by root, Mon Apr 17 19:20:59 2006 UTC

47{ 47{
48 /* 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
49 * reasonably well with bitstream vera 49 * reasonably well with bitstream vera
50 */ 50 */
51 PangoFontDescription *font = pango_context_get_font_description (context); 51 PangoFontDescription *font = pango_context_get_font_description (context);
52 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 }
53} 60}
54 61
55static void 62static void
56layout_get_pixel_size (CFClient__Layout self, int *w, int *h) 63layout_get_pixel_size (CFClient__Layout self, int *w, int *h)
57{ 64{
61 68
62 *w = (*w + 3) & ~3; 69 *w = (*w + 3) & ~3;
63 if (!*w) *w = 1; 70 if (!*w) *w = 1;
64 if (!*h) *h = 1; 71 if (!*h) *h = 1;
65} 72}
73
74typedef uint16_t mapface;
66 75
67typedef struct { 76typedef struct {
68 GLint name; 77 GLint name;
69 int w, h; 78 int w, h;
70 float s, t; 79 float s, t;
71 uint8_t r, g, b, a; 80 uint8_t r, g, b, a;
72} mapface; 81} maptex;
73 82
74typedef struct { 83typedef struct {
75 int16_t darkness; 84 int16_t darkness;
76 uint16_t face[3]; 85 mapface face[3];
77} mapcell; 86} mapcell;
78 87
79typedef struct { 88typedef struct {
80 int32_t c0, c1; 89 int32_t c0, c1;
81 mapcell *col; 90 mapcell *col;
82} maprow; 91} maprow;
83 92
84typedef struct map { 93typedef struct map {
85 int x, y, w, h; 94 int x, y, w, h;
86 int vx, vy; 95 int ox, oy; /* offset to virtual global coordinate system */
87 int faces; 96 int faces;
88 mapface *face; 97 mapface *face;
98
99 int texs;
100 maptex *tex;
89 101
90 uint32_t rows; 102 uint32_t rows;
91 maprow *row; 103 maprow *row;
92} *CFClient__Map; 104} *CFClient__Map;
93 105
114} 126}
115 127
116#define Append(type,ptr,sze,inc) (ptr) = (type *)append ((char *)ptr, (sze) * sizeof (type), (inc) * sizeof (type)) 128#define Append(type,ptr,sze,inc) (ptr) = (type *)append ((char *)ptr, (sze) * sizeof (type), (inc) * sizeof (type))
117#define Prepend(type,ptr,sze,inc) (ptr) = (type *)prepend ((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))
118 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
119static void 185static void
120map_clear (CFClient__Map self) 186map_clear (CFClient__Map self)
121{ 187{
122 int r; 188 int r;
123 189
126 192
127 Safefree (self->row); 193 Safefree (self->row);
128 194
129 self->x = 0; 195 self->x = 0;
130 self->y = 0; 196 self->y = 0;
131 self->vx = 0; 197 self->ox = 0;
132 self->vy = 0; 198 self->oy = 0;
133 self->row = 0; 199 self->row = 0;
134 self->rows = 0; 200 self->rows = 0;
135} 201}
136 202
137static void 203static void
217 SDL_RWops *rw = ix 283 SDL_RWops *rw = ix
218 ? SDL_RWFromFile (image, "r") 284 ? SDL_RWFromFile (image, "r")
219 : SDL_RWFromConstMem (image, image_len); 285 : SDL_RWFromConstMem (image, image_len);
220 286
221 if (!rw) 287 if (!rw)
222 croak ("load_image: unable to open file"); 288 croak ("load_image: %s", SDL_GetError ());
223 289
224 surface = IMG_Load_RW (rw, 1); 290 surface = IMG_Load_RW (rw, 1);
225 if (!surface) 291 if (!surface)
226 croak ("load_image: unable to read file"); 292 croak ("load_image: %s", SDL_GetError ());
227 293
228 fmt.palette = NULL; 294 fmt.palette = NULL;
229 fmt.BitsPerPixel = 32; 295 fmt.BitsPerPixel = 32;
230 fmt.BytesPerPixel = 4; 296 fmt.BytesPerPixel = 4;
231 fmt.Rmask = 0x000000ff; 297 fmt.Rmask = 0x000000ff;
332 char *text = SvPVutf8 (text_, textlen); 398 char *text = SvPVutf8 (text_, textlen);
333 399
334 pango_layout_set_markup (self->pl, text, textlen); 400 pango_layout_set_markup (self->pl, text, textlen);
335} 401}
336 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
410
337void 411void
338set_height (CFClient::Layout self, int base_height) 412set_height (CFClient::Layout self, int base_height)
339 CODE: 413 CODE:
340 self->base_height = base_height; 414 self->base_height = base_height;
341 415
454 528
455CFClient::Map 529CFClient::Map
456new (SV *class, int map_width, int map_height) 530new (SV *class, int map_width, int map_height)
457 CODE: 531 CODE:
458 New (0, RETVAL, 1, struct map); 532 New (0, RETVAL, 1, struct map);
459 RETVAL->x = 0; 533 RETVAL->x = 0;
460 RETVAL->y = 0; 534 RETVAL->y = 0;
461 RETVAL->w = map_width; 535 RETVAL->w = map_width;
462 RETVAL->h = map_height; 536 RETVAL->h = map_height;
463 RETVAL->vx = 0; 537 RETVAL->ox = 0;
464 RETVAL->vy = 0; 538 RETVAL->oy = 0;
465 RETVAL->faces = 8192; 539 RETVAL->faces = 8192;
466 Newz (0, RETVAL->face, RETVAL->faces, mapface); 540 Newz (0, RETVAL->face, RETVAL->faces, mapface);
541 RETVAL->texs = 8192;
542 Newz (0, RETVAL->tex, RETVAL->texs, maptex);
467 RETVAL->rows = 0; 543 RETVAL->rows = 0;
468 RETVAL->row = 0; 544 RETVAL->row = 0;
469 OUTPUT: 545 OUTPUT:
470 RETVAL 546 RETVAL
471 547
482clear (CFClient::Map self) 558clear (CFClient::Map self)
483 CODE: 559 CODE:
484 map_clear (self); 560 map_clear (self);
485 561
486void 562void
487set_texture (CFClient::Map self, int face, int name, int w, int h, float s, float t, int r, int g, int b, int a) 563set_face (CFClient::Map self, int face, int texid)
488 CODE: 564 CODE:
489{ 565{
490 while (self->faces < face) 566 while (self->faces <= face)
491 { 567 {
492 Append (mapface, self->face, self->faces, self->faces); 568 Append (mapface, self->face, self->faces, self->faces);
493 self->faces *= 2; 569 self->faces *= 2;
494 } 570 }
495 571
496 mapface *f = self->face + face; 572 self->face [face] = texid;
573}
497 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
498 f->name = name; 587 tex->name = name;
499 f->w = w; 588 tex->w = w;
500 f->h = h; 589 tex->h = h;
501 f->s = s; 590 tex->s = s;
502 f->t = t; 591 tex->t = t;
503 f->r = r; 592 tex->r = r;
504 f->g = g; 593 tex->g = g;
505 f->b = b; 594 tex->b = b;
506 f->a = a; 595 tex->a = a;
507} 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
508 610
509void 611void
510scroll (CFClient::Map self, int dx, int dy) 612scroll (CFClient::Map self, int dx, int dy)
511 CODE: 613 CODE:
512{ 614{
518 if (dy > 0) 620 if (dy > 0)
519 map_blank (self, self->x, self->y, self->w, dy - 1); 621 map_blank (self, self->x, self->y, self->w, dy - 1);
520 else if (dy < 0) 622 else if (dy < 0)
521 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);
522 624
523 self->x += dx; 625 self->ox += dx; self->x += dx;
524 self->y += dy; 626 self->oy += dy; self->y += dy;
525 627
526 while (self->y < 0) 628 while (self->y < 0)
527 { 629 {
528 Prepend (maprow, self->row, self->rows, MAP_EXTEND_Y); 630 Prepend (maprow, self->row, self->rows, MAP_EXTEND_Y);
529 631
536map1a_update (CFClient::Map self, SV *data_) 638map1a_update (CFClient::Map self, SV *data_)
537 CODE: 639 CODE:
538{ 640{
539 uint8_t *data = (uint8_t *)SvPVbyte_nolen (data_); 641 uint8_t *data = (uint8_t *)SvPVbyte_nolen (data_);
540 uint8_t *data_end = (uint8_t *)SvEND (data_); 642 uint8_t *data_end = (uint8_t *)SvEND (data_);
541 643
542 while (data < data_end) 644 while (data < data_end)
543 { 645 {
544 int flags = (data [0] << 8) + data [1]; data += 2; 646 int flags = (data [0] << 8) + data [1]; data += 2;
545 647
546 int x = ((flags >> 10) & 63) + self->x; 648 int x = ((flags >> 10) & 63) + self->x;
547 int y = ((flags >> 4) & 63) + self->y; 649 int y = ((flags >> 4) & 63) + self->y;
548 650
549 while (y >= self->rows) 651 mapcell *cell = map_get_cell (self, x, y);
550 {
551 Prepend (maprow, self->row, self->rows, MAP_EXTEND_Y);
552
553 self->rows += MAP_EXTEND_Y;
554 }
555
556 maprow *row = self->row + y;
557
558 if (!row->col)
559 {
560 Newz (0, row->col, MAP_EXTEND_X, mapcell);
561 row->c0 = self->x - MAP_EXTEND_X / 4;
562 row->c1 = row->c0 + MAP_EXTEND_X;
563 }
564
565 if (row->c0 > x)
566 {
567 int extend = row->c0 - x + MAP_EXTEND_X;
568 Prepend (mapcell, row->col, row->c1 - row->c0, extend);
569 row->c0 -= extend;
570 }
571 else if (x >= row->c1)
572 {
573 int extend = x - row->c1 + MAP_EXTEND_X;
574 Append (mapcell, row->col, row->c1 - row->c0, extend);
575 row->c1 += extend;
576 }
577
578 assert (y >= 0);
579 assert (y < self->rows);
580 assert (x >= row->c0);
581 assert (x < row->c1);
582 mapcell *cell = row->col + (x - row->c0);
583 652
584 if (flags & 15) 653 if (flags & 15)
585 { 654 {
586 if (cell->darkness < 0 && x < self->w && y < self->h) 655 if (cell->darkness < 0)
587 { 656 {
588 cell->darkness = 0; 657 cell->darkness = 0;
589 cell->face [0] = 0; 658 cell->face [0] = 0;
590 cell->face [1] = 0; 659 cell->face [1] = 0;
591 cell->face [2] = 0; 660 cell->face [2] = 0;
592 } 661 }
593 662
594 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(!)
595 666
596 if (flags & 4) 667 if (flags & 4)
597 { 668 {
598 cell->face [0] = (data [0] << 8) + data [1]; data += 2; 669 cell->face [0] = self->face [(data [0] << 8) + data [1]]; data += 2;
599 } 670 }
600 671
601 if (flags & 2) 672 if (flags & 2)
602 { 673 {
603 cell->face [1] = (data [0] << 8) + data [1]; data += 2; 674 cell->face [1] = self->face [(data [0] << 8) + data [1]]; data += 2;
604 } 675 }
605 676
606 if (flags & 1) 677 if (flags & 1)
607 { 678 {
608 cell->face [2] = (data [0] << 8) + data [1]; data += 2; 679 cell->face [2] = self->face [(data [0] << 8) + data [1]]; data += 2;
609 } 680 }
610 } 681 }
611 else 682 else
612 cell->darkness = -1; 683 cell->darkness = -1;
613 } 684 }
643 { 714 {
644 mapcell *cell = row->col + (x - row->c0); 715 mapcell *cell = row->col + (x - row->c0);
645 716
646 for (z = 0; z <= 0; z++) 717 for (z = 0; z <= 0; z++)
647 { 718 {
648 uint16_t face = cell->face [z]; 719 mapface face = cell->face [z];
649 720
650 if (face) 721 if (face)
651 { 722 {
652 mapface tex = self->face [face]; 723 maptex tex = self->tex [face];
653 int a0 = 255 - tex.a; 724 int a0 = 255 - tex.a;
654 int a1 = tex.a; 725 int a1 = tex.a;
655 726
656 r = (r * a0 + tex.r * a1) / 255; 727 r = (r * a0 + tex.r * a1) / 255;
657 g = (g * a0 + tex.g * a1) / 255; 728 g = (g * a0 + tex.g * a1) / 255;
678 PPCODE: 749 PPCODE:
679{ 750{
680 int sw4 = (sw + 3) & ~3; 751 int sw4 = (sw + 3) & ~3;
681 SV *darkness_sv = sv_2mortal (newSV (sw4 * sh)); 752 SV *darkness_sv = sv_2mortal (newSV (sw4 * sh));
682 uint8_t *darkness = (uint8_t *)SvPVX (darkness_sv); 753 uint8_t *darkness = (uint8_t *)SvPVX (darkness_sv);
683 754 memset (darkness, 255, sw4 * sh);
684 SvPOK_only (darkness_sv); 755 SvPOK_only (darkness_sv);
685 SvCUR_set (darkness_sv, sw4 * sh); 756 SvCUR_set (darkness_sv, sw4 * sh);
686 757
687 int vx = self->x + (self->w - sw) / 2 - shift_x; 758 int vx = self->x + (self->w - sw) / 2 - shift_x;
688 int vy = self->y + (self->h - sh) / 2 - shift_y; 759 int vy = self->y + (self->h - sh) / 2 - shift_y;
689 760
690 if (0)
691 { 761 /*
692 int vx = self->vx = self->w >= sw 762 int vx = self->vx = self->w >= sw
693 ? self->x + (self->w - sw) / 2 763 ? self->x + (self->w - sw) / 2
694 : MIN (self->x, MAX (self->x + self->w - sw + 1, self->vx)); 764 : MIN (self->x, MAX (self->x + self->w - sw + 1, self->vx));
695 765
696 int vy = self->vy = self->h >= sh 766 int vy = self->vy = self->h >= sh
697 ? self->y + (self->h - sh) / 2 767 ? self->y + (self->h - sh) / 2
698 : MIN (self->y, MAX (self->y + self->h - sh + 1, self->vy)); 768 : MIN (self->y, MAX (self->y + self->h - sh + 1, self->vy));
699 } 769 */
700 770
701 glColor4ub (255, 255, 255, 255); 771 glColor4ub (255, 255, 255, 255);
702 772
703 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 773 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
704 glEnable (GL_BLEND); 774 glEnable (GL_BLEND);
724 794
725 darkness[y * sw4 + x] = cell->darkness < 0 795 darkness[y * sw4 + x] = cell->darkness < 0
726 ? 255 - FOW_DARKNESS 796 ? 255 - FOW_DARKNESS
727 : 255 - cell->darkness; 797 : 255 - cell->darkness;
728 798
729 uint16_t face = cell->face [z]; 799 mapface face = cell->face [z];
730 800
731 if (face) 801 if (face)
732 { 802 {
733 mapface tex = self->face [face]; 803 maptex tex = self->tex [face];
734 804
735 int px = (x + 1) * 32 - tex.w; 805 int px = (x + 1) * 32 - tex.w;
736 int py = (y + 1) * 32 - tex.h; 806 int py = (y + 1) * 32 - tex.h;
737 807
738 if (last_name != tex.name) 808 if (last_name != tex.name)
760 PUSHs (sv_2mortal (newSViv (sw4))); 830 PUSHs (sv_2mortal (newSViv (sw4)));
761 PUSHs (sv_2mortal (newSViv (sh))); 831 PUSHs (sv_2mortal (newSViv (sh)));
762 PUSHs (darkness_sv); 832 PUSHs (darkness_sv);
763} 833}
764 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