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.35 by root, Fri Apr 14 02:03:10 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{
61 if (!*w) *w = 1; 63 if (!*w) *w = 1;
62 if (!*h) *h = 1; 64 if (!*h) *h = 1;
63} 65}
64 66
65typedef struct { 67typedef struct {
68 GLint name;
69 int w, h;
70 float s, t;
71} mapface;
72
73typedef struct {
66 int16_t darkness; 74 int16_t darkness;
67 uint16_t face[3]; 75 uint16_t face[3];
68} mapcell; 76} mapcell;
69 77
70typedef struct { 78typedef struct {
71 uint32_t c0, c1; 79 int32_t c0, c1;
72 mapcell *col; 80 mapcell *col;
73} maprow; 81} maprow;
74 82
75typedef struct map { 83typedef struct map {
76 int x, y, w, h; 84 int x, y, w, h;
85 int vx, vy;
77 int faces; 86 int faces;
78 GLint *face; 87 mapface *face;
79 88
80 uint32_t rows; 89 uint32_t rows;
81 maprow *row; 90 maprow *row;
82} *CFClient__Map; 91} *CFClient__Map;
83 92
93static char *
94prepend (char *ptr, int sze, int inc)
95{
96 char *p;
97
98 New (0, p, sze + inc, char);
99 Zero (p, inc, char);
100 Move (ptr, p + inc, sze, char);
101 Safefree (ptr);
102
103 return p;
104}
105
106static char *
107append (char *ptr, int sze, int inc)
108{
109 Renew (ptr, sze + inc, char);
110 Zero (ptr + sze, inc, char);
111
112 return ptr;
113}
114
115#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))
117
118static void
119map_clear (CFClient__Map self)
120{
121 int r;
122
123 for (r = 0; r < self->rows; r++)
124 Safefree (self->row[r].col);
125
126 Safefree (self->row);
127
128 self->x = 0;
129 self->y = 0;
130 self->vx = 0;
131 self->vy = 0;
132 self->row = 0;
133 self->rows = 0;
134}
135
84static void 136static void
85map_blank (CFClient__Map self, int x0, int y0, int w, int h) 137map_blank (CFClient__Map self, int x0, int y0, int w, int h)
86{ 138{
87 int x, y; 139 int x, y;
88 140
89 for (y = y0; y < y0 + h; y++) 141 for (y = y0; y < y0 + h; y++)
142 if (y >= 0)
90 { 143 {
91 if (y >= self->rows) 144 if (y >= self->rows)
92 break; 145 break;
93 146
94 maprow *row = self->row + y; 147 maprow *row = self->row + y;
95 148
96 for (x = x0; x < x0 + w; x++) 149 for (x = x0; x < x0 + w; x++)
97 if (x >= row->c0) 150 if (x >= row->c0)
98 { 151 {
99 if (x >= row->c1) 152 if (x >= row->c1)
100 break; 153 break;
101 154
102 row->col[x].darkness = -1; 155 row->col[x - row->c0].darkness = -1;
103 } 156 }
104 } 157 }
105} 158}
106 159
107MODULE = CFClient PACKAGE = CFClient 160MODULE = CFClient PACKAGE = CFClient
108 161
109PROTOTYPES: ENABLE 162PROTOTYPES: ENABLE
232 CODE: 285 CODE:
233 g_object_unref (self->pl); 286 g_object_unref (self->pl);
234 Safefree (self); 287 Safefree (self);
235 288
236void 289void
290set_text (CFClient::Layout self, SV *text_)
291 CODE:
292{
293 STRLEN textlen;
294 char *text = SvPVutf8 (text_, textlen);
295
296 pango_layout_set_text (self->pl, text, textlen);
297}
298
299void
237set_markup (CFClient::Layout self, SV *text_) 300set_markup (CFClient::Layout self, SV *text_)
238 CODE: 301 CODE:
239{ 302{
240 STRLEN textlen; 303 STRLEN textlen;
241 char *text = SvPVutf8 (text_, textlen); 304 char *text = SvPVutf8 (text_, textlen);
286 PPCODE: 349 PPCODE:
287{ 350{
288 PangoRectangle strong_pos; 351 PangoRectangle strong_pos;
289 layout_update (self); 352 layout_update (self);
290 pango_layout_get_cursor_pos (self->pl, index, &strong_pos, 0); 353 pango_layout_get_cursor_pos (self->pl, index, &strong_pos, 0);
354
291 EXTEND (SP, 3); 355 EXTEND (SP, 3);
292 PUSHs (sv_2mortal (newSViv (strong_pos.x / PANGO_SCALE))); 356 PUSHs (sv_2mortal (newSViv (strong_pos.x / PANGO_SCALE)));
293 PUSHs (sv_2mortal (newSViv (strong_pos.y / PANGO_SCALE))); 357 PUSHs (sv_2mortal (newSViv (strong_pos.y / PANGO_SCALE)));
294 PUSHs (sv_2mortal (newSViv (strong_pos.height / PANGO_SCALE))); 358 PUSHs (sv_2mortal (newSViv (strong_pos.height / PANGO_SCALE)));
295} 359}
361 New (0, RETVAL, 1, struct map); 425 New (0, RETVAL, 1, struct map);
362 RETVAL->x = 0; 426 RETVAL->x = 0;
363 RETVAL->y = 0; 427 RETVAL->y = 0;
364 RETVAL->w = map_width; 428 RETVAL->w = map_width;
365 RETVAL->h = map_height; 429 RETVAL->h = map_height;
430 RETVAL->vx = 0;
431 RETVAL->vy = 0;
366 RETVAL->faces = 1; 432 RETVAL->faces = 8192;
367 Newz (0, RETVAL->face, 1, GLint); 433 Newz (0, RETVAL->face, RETVAL->faces, mapface);
368 RETVAL->rows = 0; 434 RETVAL->rows = 0;
369 RETVAL->row = 0; 435 RETVAL->row = 0;
370 printf ("new map %d,%d\n", map_width, map_height);//D
371 OUTPUT: 436 OUTPUT:
372 RETVAL 437 RETVAL
373 438
374void 439void
375DESTROY (CFClient::Map self) 440DESTROY (CFClient::Map self)
376 CODE: 441 CODE:
377{ 442{
378 int r, c; 443 map_clear (self);
379
380 printf ("detroy map\n");//D
381 Safefree (self->face); 444 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); 445 Safefree (self);
388} 446}
389 447
390void 448void
391set_face (CFClient::Map self, int facenum, int face) 449clear (CFClient::Map self)
392 CODE: 450 CODE:
451 map_clear (self);
452
453void
454set_texture (CFClient::Map self, int face, int name, int w, int h, float s, float t)
455 CODE:
393{ 456{
394 while (self->faces < facenum) 457 while (self->faces < face)
395 { 458 {
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); 459 Append (mapface, self->face, self->faces, self->faces);
399 self->faces *= 2; 460 self->faces *= 2;
400 } 461 }
401 462
402 self->face [facenum] = face; 463 self->face [face].name = name;
464 self->face [face].w = w;
465 self->face [face].h = h;
466 self->face [face].s = s;
467 self->face [face].t = t;
403} 468}
404 469
405void 470void
406scroll (CFClient::Map self, int dx, int dy) 471scroll (CFClient::Map self, int dx, int dy)
407 CODE: 472 CODE:
408{ 473{
409 printf ("map_scroll %d,%d\n", dx, dy);//D
410 if (dx > 0) 474 if (dx > 0)
411 map_blank (self, self->x, self->y, dx - 1, self->h); 475 map_blank (self, self->x, self->y, dx - 1, self->h);
412 else if (dx < 0) 476 else if (dx < 0)
413 map_blank (self, self->x + self->w + dx + 1, self->y, 1 - dx, self->h); 477 map_blank (self, self->x + self->w + dx + 1, self->y, 1 - dx, self->h);
414 478
420 self->x += dx; 484 self->x += dx;
421 self->y += dy; 485 self->y += dy;
422 486
423 while (self->y < 0) 487 while (self->y < 0)
424 { 488 {
425 maprow *row; 489 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 490
431 self->rows += MAP_EXTEND_Y; 491 self->rows += MAP_EXTEND_Y;
432 self->y += MAP_EXTEND_Y; 492 self->y += MAP_EXTEND_Y;
433 self->row = row;
434 } 493 }
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} 494}
448 495
449void 496void
450map1a_update (CFClient::Map self, SV *data_) 497map1a_update (CFClient::Map self, SV *data_)
451 CODE: 498 CODE:
452{ 499{
453 uint8_t *data = (uint8_t *)SvPVbytes_nolen (data_); 500 uint8_t *data = (uint8_t *)SvPVbyte_nolen (data_);
454 uint8_t *data_end = (uint8_t *)SvEND (data_) + 1; 501 uint8_t *data_end = (uint8_t *)SvEND (data_);
455 502
456 while (data < data_end) 503 while (data < data_end)
457 { 504 {
458 int flags = (data [0] << 8) + data [1]; data += 2; 505 int flags = (data [0] << 8) + data [1]; data += 2;
506
459 int x = ((flags >> 10) & 63) + self->x; 507 int x = ((flags >> 10) & 63) + self->x;
460 int y = ((flags >> 4) & 63) + self->y; 508 int y = ((flags >> 4) & 63) + self->y;
461 509
462 while (y >= self->rows) 510 while (y >= self->rows)
463 { 511 {
464 Renew (self->row, self->rows + MAP_EXTEND_Y, maprow);
465 Zero (self->row + self->rows, MAP_EXTEND_Y, maprow); 512 Prepend (maprow, self->row, self->rows, MAP_EXTEND_Y);
466 513
467 self->rows += MAP_EXTEND_Y; 514 self->rows += MAP_EXTEND_Y;
468 } 515 }
469
470 assert (y < self->rows);//D
471 516
472 maprow *row = self->row + y; 517 maprow *row = self->row + y;
473 518
474 if (!row->col) 519 if (!row->col)
475 { 520 {
479 } 524 }
480 525
481 if (row->c0 > x) 526 if (row->c0 > x)
482 { 527 {
483 int extend = row->c0 - x + MAP_EXTEND_X; 528 int extend = row->c0 - x + MAP_EXTEND_X;
484 mapcell *col; 529 Prepend (mapcell, row->col, row->c1 - row->c0, extend);
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; 530 row->c0 -= extend;
492 } 531 }
493 else if (x >= row->c1) 532 else if (x >= row->c1)
494 { 533 {
495 int extend = x - row->c1 + MAP_EXTEND_X; 534 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); 535 Append (mapcell, row->col, row->c1 - row->c0, extend);
499
500 row->c1 += extend; 536 row->c1 += extend;
501 } 537 }
502 538
539 assert (y >= 0);
540 assert (y < self->rows);
503 assert (x >= row->c0);//D 541 assert (x >= row->c0);
504 assert (x < row->c1);//D 542 assert (x < row->c1);
505 mapcell *cell = row->col + (x - row->c0); 543 mapcell *cell = row->col + (x - row->c0);
506 544
507 if (flags & 15) 545 if (flags & 15)
508 { 546 {
509 if (cell->darkness < 0) 547 if (cell->darkness < 0 && x < self->w && y < self->h)
510 { 548 {
511 cell->darkness = 0; 549 cell->darkness = 0;
512 cell->face [0] = 0; 550 cell->face [0] = 0;
513 cell->face [1] = 0; 551 cell->face [1] = 0;
514 cell->face [2] = 0; 552 cell->face [2] = 0;
534 else 572 else
535 cell->darkness = -1; 573 cell->darkness = -1;
536 } 574 }
537} 575}
538 576
577void
578draw (CFClient::Map self, int x0, int y0, int sw, int sh)
579 PPCODE:
580{
581 int sw4 = (sw + 3) & ~3;
582 SV *darkness_sv = sv_2mortal (newSV (sw4 * sh));
583 uint8_t *darkness = (uint8_t *)SvPVX (darkness_sv);
584
585 SvPOK_only (darkness_sv);
586 SvCUR_set (darkness_sv, sw4 * sh);
587
588 int vx = self->vx = self->w >= sw
589 ? self->x + (self->w - sw) / 2
590 : MIN (self->x, MAX (self->x + self->w - sw + 1, self->vx));
591
592 int vy = self->vy = self->h >= sh
593 ? self->y + (self->h - sh) / 2
594 : MIN (self->y, MAX (self->y + self->h - sh + 1, self->vy));
595
596 glColor4ub (255, 255, 255, 255);
597
598 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
599 glEnable (GL_BLEND);
600 glEnable (GL_TEXTURE_2D);
601 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
602
603 int x, y, z;
604
605 int last_name = 0;
606
607 glBegin (GL_QUADS);
608
609 for (z = 0; z < 3; z++)
610 for (y = 0; y < sh; y++)
611 if (0 <= y + vy && y + vy < self->rows)
612 {
613 maprow *row = self->row + (y + vy);
614
615 for (x = 0; x < sw; x++)
616 if (row->c0 <= x + vx && x + vx < row->c1)
617 {
618 mapcell *cell = row->col + (x + vx - row->c0);
619
620 darkness[y * sw4 + x] = cell->darkness < 0
621 ? 255 - FOW_DARKNESS
622 : 255 - cell->darkness;
623
624 uint16_t face = cell->face [z];
625
626 if (face)
627 {
628 mapface tex = self->face [face];
629
630 int px = (x + 1) * 32 - tex.w;
631 int py = (y + 1) * 32 - tex.h;
632
633 if (last_name != tex.name)
634 {
635 glEnd ();
636 last_name = tex.name;
637 glBindTexture (GL_TEXTURE_2D, last_name);
638 glBegin (GL_QUADS);
639 }
640
641 glTexCoord2f (0 , 0 ); glVertex2f (px , py );
642 glTexCoord2f (0 , tex.t); glVertex2f (px , py + tex.h);
643 glTexCoord2f (tex.s, tex.t); glVertex2f (px + tex.w, py + tex.h);
644 glTexCoord2f (tex.s, 0 ); glVertex2f (px + tex.w, py );
645 }
646 }
647 }
648
649 glEnd ();
650
651 glDisable (GL_TEXTURE_2D);
652 glDisable (GL_BLEND);
653
654 EXTEND (SP, 3);
655 PUSHs (sv_2mortal (newSViv (sw4)));
656 PUSHs (sv_2mortal (newSViv (sh)));
657 PUSHs (darkness_sv);
658}
659

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines