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.24 by root, Wed Apr 12 21:01:45 2006 UTC vs.
Revision 1.35 by root, Fri Apr 14 02:03:10 2006 UTC

1#include "EXTERN.h" 1#include "EXTERN.h"
2#include "perl.h" 2#include "perl.h"
3#include "XSUB.h" 3#include "XSUB.h"
4 4
5#include <string.h> 5#include <string.h>
6#include <stdio.h>
6 7
7#include <SDL.h> 8#include <SDL.h>
8#include <SDL_image.h> 9#include <SDL_image.h>
9#include <SDL_opengl.h> 10#include <SDL_opengl.h>
10 11
12#include <glib/gmacros.h>
13
11#include <pango/pango.h> 14#include <pango/pango.h>
12#include <pango/pangofc-fontmap.h> 15#include <pango/pangofc-fontmap.h>
13#include <pango/pangoft2.h> 16#include <pango/pangoft2.h>
14
15#include <sys/time.h>
16#include <sys/types.h>
17#include <unistd.h>
18 17
19#include <sys/types.h> 18#include <sys/types.h>
20#include <sys/socket.h> 19#include <sys/socket.h>
21#include <netinet/in.h> 20#include <netinet/in.h>
22#include <netinet/tcp.h> 21#include <netinet/tcp.h>
23 22
23#include <inttypes.h>
24
25#define FOW_DARKNESS 32
26
27#define MAP_EXTEND_X 32
28#define MAP_EXTEND_Y 512
29
24static PangoContext *context; 30static PangoContext *context;
25static PangoFontMap *fontmap; 31static PangoFontMap *fontmap;
26 32
27typedef struct cf_layout { 33typedef struct cf_layout {
28 PangoLayout *pl; 34 PangoLayout *pl;
31 37
32static void 38static void
33substitute_func (FcPattern *pattern, gpointer data) 39substitute_func (FcPattern *pattern, gpointer data)
34{ 40{
35 FcPatternAddBool (pattern, FC_HINTING , 1); 41 FcPatternAddBool (pattern, FC_HINTING , 1);
36 FcPatternAddBool (pattern, FC_AUTOHINT, 1); 42 FcPatternAddBool (pattern, FC_AUTOHINT, 0);
37} 43}
38 44
39static void 45static void
40layout_update (CFClient__Layout self) 46layout_update (CFClient__Layout self)
41{ 47{
54 pango_layout_get_pixel_size (self->pl, w, h); 60 pango_layout_get_pixel_size (self->pl, w, h);
55 61
56 *w = (*w + 3) & ~3; 62 *w = (*w + 3) & ~3;
57 if (!*w) *w = 1; 63 if (!*w) *w = 1;
58 if (!*h) *h = 1; 64 if (!*h) *h = 1;
65}
66
67typedef struct {
68 GLint name;
69 int w, h;
70 float s, t;
71} mapface;
72
73typedef struct {
74 int16_t darkness;
75 uint16_t face[3];
76} mapcell;
77
78typedef struct {
79 int32_t c0, c1;
80 mapcell *col;
81} maprow;
82
83typedef struct map {
84 int x, y, w, h;
85 int vx, vy;
86 int faces;
87 mapface *face;
88
89 uint32_t rows;
90 maprow *row;
91} *CFClient__Map;
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
136static void
137map_blank (CFClient__Map self, int x0, int y0, int w, int h)
138{
139 int x, y;
140
141 for (y = y0; y < y0 + h; y++)
142 if (y >= 0)
143 {
144 if (y >= self->rows)
145 break;
146
147 maprow *row = self->row + y;
148
149 for (x = x0; x < x0 + w; x++)
150 if (x >= row->c0)
151 {
152 if (x >= row->c1)
153 break;
154
155 row->col[x - row->c0].darkness = -1;
156 }
157 }
59} 158}
60 159
61MODULE = CFClient PACKAGE = CFClient 160MODULE = CFClient PACKAGE = CFClient
62 161
63PROTOTYPES: ENABLE 162PROTOTYPES: ENABLE
157 256
158 SDL_FreeSurface (surface); 257 SDL_FreeSurface (surface);
159 SDL_FreeSurface (surface2); 258 SDL_FreeSurface (surface2);
160} 259}
161 260
261void
262fatal (char *message)
263 CODE:
264#ifdef WIN32
265 MessageBox (0, message, "Crossfire+ Fatal Error", MB_OK | MB_ICONERROR | MB_SETFOREGROUND);
266#else
267 fprintf (stderr, "%s\n", message);
268#endif
269 exit (1);
270
162MODULE = CFClient PACKAGE = CFClient::Layout 271MODULE = CFClient PACKAGE = CFClient::Layout
163 272
164CFClient::Layout 273CFClient::Layout
165new (SV *class, int base_height = 10) 274new (SV *class, int base_height = 10)
166 CODE: 275 CODE:
176 CODE: 285 CODE:
177 g_object_unref (self->pl); 286 g_object_unref (self->pl);
178 Safefree (self); 287 Safefree (self);
179 288
180void 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
181set_markup (CFClient::Layout self, SV *text_) 300set_markup (CFClient::Layout self, SV *text_)
182 CODE: 301 CODE:
183{ 302{
184 STRLEN textlen; 303 STRLEN textlen;
185 char *text = SvPVutf8 (text_, textlen); 304 char *text = SvPVutf8 (text_, textlen);
230 PPCODE: 349 PPCODE:
231{ 350{
232 PangoRectangle strong_pos; 351 PangoRectangle strong_pos;
233 layout_update (self); 352 layout_update (self);
234 pango_layout_get_cursor_pos (self->pl, index, &strong_pos, 0); 353 pango_layout_get_cursor_pos (self->pl, index, &strong_pos, 0);
354
235 EXTEND (SP, 3); 355 EXTEND (SP, 3);
236 PUSHs (sv_2mortal (newSViv (strong_pos.x / PANGO_SCALE))); 356 PUSHs (sv_2mortal (newSViv (strong_pos.x / PANGO_SCALE)));
237 PUSHs (sv_2mortal (newSViv (strong_pos.y / PANGO_SCALE))); 357 PUSHs (sv_2mortal (newSViv (strong_pos.y / PANGO_SCALE)));
238 PUSHs (sv_2mortal (newSViv (strong_pos.height / PANGO_SCALE))); 358 PUSHs (sv_2mortal (newSViv (strong_pos.height / PANGO_SCALE)));
239} 359}
294 glTexCoord2d (0, t); glVertex2d (x , y + h); 414 glTexCoord2d (0, t); glVertex2d (x , y + h);
295 glTexCoord2d (s, t); glVertex2d (x + w, y + h); 415 glTexCoord2d (s, t); glVertex2d (x + w, y + h);
296 glTexCoord2d (s, 0); glVertex2d (x + w, y ); 416 glTexCoord2d (s, 0); glVertex2d (x + w, y );
297 glEnd (); 417 glEnd ();
298} 418}
419
420MODULE = CFClient PACKAGE = CFClient::Map
421
422CFClient::Map
423new (SV *class, int map_width, int map_height)
424 CODE:
425 New (0, RETVAL, 1, struct map);
426 RETVAL->x = 0;
427 RETVAL->y = 0;
428 RETVAL->w = map_width;
429 RETVAL->h = map_height;
430 RETVAL->vx = 0;
431 RETVAL->vy = 0;
432 RETVAL->faces = 8192;
433 Newz (0, RETVAL->face, RETVAL->faces, mapface);
434 RETVAL->rows = 0;
435 RETVAL->row = 0;
436 OUTPUT:
437 RETVAL
438
439void
440DESTROY (CFClient::Map self)
441 CODE:
442{
443 map_clear (self);
444 Safefree (self->face);
445 Safefree (self);
446}
447
448void
449clear (CFClient::Map self)
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:
456{
457 while (self->faces < face)
458 {
459 Append (mapface, self->face, self->faces, self->faces);
460 self->faces *= 2;
461 }
462
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;
468}
469
470void
471scroll (CFClient::Map self, int dx, int dy)
472 CODE:
473{
474 if (dx > 0)
475 map_blank (self, self->x, self->y, dx - 1, self->h);
476 else if (dx < 0)
477 map_blank (self, self->x + self->w + dx + 1, self->y, 1 - dx, self->h);
478
479 if (dy > 0)
480 map_blank (self, self->x, self->y, self->w, dy - 1);
481 else if (dy < 0)
482 map_blank (self, self->x, self->y + self->h + dy + 1, self->w, 1 - dy);
483
484 self->x += dx;
485 self->y += dy;
486
487 while (self->y < 0)
488 {
489 Prepend (maprow, self->row, self->rows, MAP_EXTEND_Y);
490
491 self->rows += MAP_EXTEND_Y;
492 self->y += MAP_EXTEND_Y;
493 }
494}
495
496void
497map1a_update (CFClient::Map self, SV *data_)
498 CODE:
499{
500 uint8_t *data = (uint8_t *)SvPVbyte_nolen (data_);
501 uint8_t *data_end = (uint8_t *)SvEND (data_);
502
503 while (data < data_end)
504 {
505 int flags = (data [0] << 8) + data [1]; data += 2;
506
507 int x = ((flags >> 10) & 63) + self->x;
508 int y = ((flags >> 4) & 63) + self->y;
509
510 while (y >= self->rows)
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
545 if (flags & 15)
546 {
547 if (cell->darkness < 0 && x < self->w && y < self->h)
548 {
549 cell->darkness = 0;
550 cell->face [0] = 0;
551 cell->face [1] = 0;
552 cell->face [2] = 0;
553 }
554
555 cell->darkness = flags & 8 ? *data++ : 255;
556
557 if (flags & 4)
558 {
559 cell->face [0] = (data [0] << 8) + data [1]; data += 2;
560 }
561
562 if (flags & 2)
563 {
564 cell->face [1] = (data [0] << 8) + data [1]; data += 2;
565 }
566
567 if (flags & 1)
568 {
569 cell->face [2] = (data [0] << 8) + data [1]; data += 2;
570 }
571 }
572 else
573 cell->darkness = -1;
574 }
575}
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