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> |
|
|
9 | #include <SDL_image.h> |
8 | #include <SDL_opengl.h> |
10 | #include <SDL_opengl.h> |
|
|
11 | |
|
|
12 | #include <glib/gmacros.h> |
9 | |
13 | |
10 | #include <pango/pango.h> |
14 | #include <pango/pango.h> |
11 | #include <pango/pangofc-fontmap.h> |
15 | #include <pango/pangofc-fontmap.h> |
12 | #include <pango/pangoft2.h> |
16 | #include <pango/pangoft2.h> |
13 | |
17 | |
14 | #include <sys/types.h> |
18 | #include <sys/types.h> |
15 | #include <sys/socket.h> |
19 | #include <sys/socket.h> |
16 | #include <netinet/in.h> |
20 | #include <netinet/in.h> |
17 | #include <netinet/tcp.h> |
21 | #include <netinet/tcp.h> |
18 | |
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 | |
19 | static PangoContext *context; |
30 | static PangoContext *context; |
20 | static PangoFontMap *fontmap; |
31 | static PangoFontMap *fontmap; |
21 | |
32 | |
22 | typedef struct cf_layout { |
33 | typedef struct cf_layout { |
23 | PangoLayout *pl; |
34 | PangoLayout *pl; |
… | |
… | |
25 | } *CFClient__Layout; |
36 | } *CFClient__Layout; |
26 | |
37 | |
27 | static void |
38 | static void |
28 | substitute_func (FcPattern *pattern, gpointer data) |
39 | substitute_func (FcPattern *pattern, gpointer data) |
29 | { |
40 | { |
30 | //FcPatternAddBool (pattern, FC_HINTING, 1); |
41 | FcPatternAddBool (pattern, FC_HINTING , 1); |
31 | //FcPatternAddBool (pattern, FC_AUTOHINT, 1); |
42 | FcPatternAddBool (pattern, FC_AUTOHINT, 1); |
32 | } |
43 | } |
33 | |
44 | |
34 | static void |
45 | static void |
35 | layout_update (CFClient__Layout self) |
46 | layout_update (CFClient__Layout self) |
36 | { |
47 | { |
… | |
… | |
51 | *w = (*w + 3) & ~3; |
62 | *w = (*w + 3) & ~3; |
52 | if (!*w) *w = 1; |
63 | if (!*w) *w = 1; |
53 | if (!*h) *h = 1; |
64 | if (!*h) *h = 1; |
54 | } |
65 | } |
55 | |
66 | |
|
|
67 | typedef struct { |
|
|
68 | GLint name; |
|
|
69 | int w, h; |
|
|
70 | float s, t; |
|
|
71 | } mapface; |
|
|
72 | |
|
|
73 | typedef struct { |
|
|
74 | int16_t darkness; |
|
|
75 | uint16_t face[3]; |
|
|
76 | } mapcell; |
|
|
77 | |
|
|
78 | typedef struct { |
|
|
79 | int32_t c0, c1; |
|
|
80 | mapcell *col; |
|
|
81 | } maprow; |
|
|
82 | |
|
|
83 | typedef 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 | |
|
|
93 | static char * |
|
|
94 | prepend (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 | |
|
|
106 | static char * |
|
|
107 | append (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 | |
|
|
118 | static void |
|
|
119 | map_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 | |
|
|
136 | static void |
|
|
137 | map_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 | } |
|
|
158 | } |
|
|
159 | |
56 | MODULE = CFClient PACKAGE = CFClient |
160 | MODULE = CFClient PACKAGE = CFClient |
57 | |
161 | |
58 | PROTOTYPES: ENABLE |
162 | PROTOTYPES: ENABLE |
59 | |
163 | |
60 | BOOT: |
164 | BOOT: |
… | |
… | |
96 | FcPattern *pattern = FcFreeTypeQuery ((const FcChar8 *)file, 0, 0, &count); |
200 | FcPattern *pattern = FcFreeTypeQuery ((const FcChar8 *)file, 0, 0, &count); |
97 | PangoFontDescription *font = pango_fc_font_description_from_pattern (pattern, 0); |
201 | PangoFontDescription *font = pango_fc_font_description_from_pattern (pattern, 0); |
98 | FcPatternDestroy (pattern); |
202 | FcPatternDestroy (pattern); |
99 | pango_context_set_font_description (context, font); |
203 | pango_context_set_font_description (context, font); |
100 | } |
204 | } |
|
|
205 | |
|
|
206 | void |
|
|
207 | load_image_inline (SV *image_) |
|
|
208 | ALIAS: |
|
|
209 | load_image_file = 1 |
|
|
210 | PPCODE: |
|
|
211 | { |
|
|
212 | STRLEN image_len; |
|
|
213 | char *image = (char *)SvPVbyte (image_, image_len); |
|
|
214 | SDL_Surface *surface, *surface2; |
|
|
215 | SDL_PixelFormat fmt; |
|
|
216 | SDL_RWops *rw = ix |
|
|
217 | ? SDL_RWFromFile (image, "r") |
|
|
218 | : SDL_RWFromConstMem (image, image_len); |
|
|
219 | |
|
|
220 | if (!rw) |
|
|
221 | croak ("load_image: unable to open file"); |
|
|
222 | |
|
|
223 | surface = IMG_Load_RW (rw, 1); |
|
|
224 | if (!surface) |
|
|
225 | croak ("load_image: unable to read file"); |
|
|
226 | |
|
|
227 | fmt.palette = NULL; |
|
|
228 | fmt.BitsPerPixel = 32; |
|
|
229 | fmt.BytesPerPixel = 4; |
|
|
230 | fmt.Rmask = 0x000000ff; |
|
|
231 | fmt.Gmask = 0x0000ff00; |
|
|
232 | fmt.Bmask = 0x00ff0000; |
|
|
233 | fmt.Amask = 0xff000000; |
|
|
234 | fmt.Rloss = 0; |
|
|
235 | fmt.Gloss = 0; |
|
|
236 | fmt.Bloss = 0; |
|
|
237 | fmt.Aloss = 0; |
|
|
238 | fmt.Rshift = 0; |
|
|
239 | fmt.Gshift = 8; |
|
|
240 | fmt.Bshift = 16; |
|
|
241 | fmt.Ashift = 24; |
|
|
242 | fmt.colorkey = 0; |
|
|
243 | fmt.alpha = 0; |
|
|
244 | |
|
|
245 | surface2 = SDL_ConvertSurface (surface, &fmt, SDL_SWSURFACE); |
|
|
246 | |
|
|
247 | EXTEND (SP, 5); |
|
|
248 | PUSHs (sv_2mortal (newSViv (surface2->w))); |
|
|
249 | PUSHs (sv_2mortal (newSViv (surface2->h))); |
|
|
250 | SDL_LockSurface (surface2); |
|
|
251 | PUSHs (sv_2mortal (newSVpvn (surface2->pixels, surface2->h * surface2->pitch))); |
|
|
252 | SDL_UnlockSurface (surface2); |
|
|
253 | PUSHs (sv_2mortal (newSViv (surface->flags & (SDL_SRCCOLORKEY | SDL_SRCALPHA) ? GL_RGBA : GL_RGB))); |
|
|
254 | PUSHs (sv_2mortal (newSViv (GL_RGBA))); |
|
|
255 | PUSHs (sv_2mortal (newSViv (GL_UNSIGNED_INT_8_8_8_8_REV))); |
|
|
256 | |
|
|
257 | SDL_FreeSurface (surface); |
|
|
258 | SDL_FreeSurface (surface2); |
|
|
259 | } |
|
|
260 | |
|
|
261 | void |
|
|
262 | fatal (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); |
101 | |
270 | |
102 | MODULE = CFClient PACKAGE = CFClient::Layout |
271 | MODULE = CFClient PACKAGE = CFClient::Layout |
103 | |
272 | |
104 | CFClient::Layout |
273 | CFClient::Layout |
105 | new (SV *class, int base_height = 10) |
274 | new (SV *class, int base_height = 10) |
… | |
… | |
170 | PPCODE: |
339 | PPCODE: |
171 | { |
340 | { |
172 | PangoRectangle strong_pos; |
341 | PangoRectangle strong_pos; |
173 | layout_update (self); |
342 | layout_update (self); |
174 | pango_layout_get_cursor_pos (self->pl, index, &strong_pos, 0); |
343 | pango_layout_get_cursor_pos (self->pl, index, &strong_pos, 0); |
|
|
344 | |
175 | EXTEND (SP, 3); |
345 | EXTEND (SP, 3); |
176 | PUSHs (sv_2mortal (newSViv (strong_pos.x / PANGO_SCALE))); |
346 | PUSHs (sv_2mortal (newSViv (strong_pos.x / PANGO_SCALE))); |
177 | PUSHs (sv_2mortal (newSViv (strong_pos.y / PANGO_SCALE))); |
347 | PUSHs (sv_2mortal (newSViv (strong_pos.y / PANGO_SCALE))); |
178 | PUSHs (sv_2mortal (newSViv (strong_pos.height / PANGO_SCALE))); |
348 | PUSHs (sv_2mortal (newSViv (strong_pos.height / PANGO_SCALE))); |
179 | } |
349 | } |
… | |
… | |
234 | glTexCoord2d (0, t); glVertex2d (x , y + h); |
404 | glTexCoord2d (0, t); glVertex2d (x , y + h); |
235 | glTexCoord2d (s, t); glVertex2d (x + w, y + h); |
405 | glTexCoord2d (s, t); glVertex2d (x + w, y + h); |
236 | glTexCoord2d (s, 0); glVertex2d (x + w, y ); |
406 | glTexCoord2d (s, 0); glVertex2d (x + w, y ); |
237 | glEnd (); |
407 | glEnd (); |
238 | } |
408 | } |
|
|
409 | |
|
|
410 | MODULE = CFClient PACKAGE = CFClient::Map |
|
|
411 | |
|
|
412 | CFClient::Map |
|
|
413 | new (SV *class, int map_width, int map_height) |
|
|
414 | CODE: |
|
|
415 | New (0, RETVAL, 1, struct map); |
|
|
416 | RETVAL->x = 0; |
|
|
417 | RETVAL->y = 0; |
|
|
418 | RETVAL->w = map_width; |
|
|
419 | RETVAL->h = map_height; |
|
|
420 | RETVAL->vx = 0; |
|
|
421 | RETVAL->vy = 0; |
|
|
422 | RETVAL->faces = 8192; |
|
|
423 | Newz (0, RETVAL->face, RETVAL->faces, mapface); |
|
|
424 | RETVAL->rows = 0; |
|
|
425 | RETVAL->row = 0; |
|
|
426 | OUTPUT: |
|
|
427 | RETVAL |
|
|
428 | |
|
|
429 | void |
|
|
430 | DESTROY (CFClient::Map self) |
|
|
431 | CODE: |
|
|
432 | { |
|
|
433 | map_clear (self); |
|
|
434 | Safefree (self->face); |
|
|
435 | Safefree (self); |
|
|
436 | } |
|
|
437 | |
|
|
438 | void |
|
|
439 | clear (CFClient::Map self) |
|
|
440 | CODE: |
|
|
441 | map_clear (self); |
|
|
442 | |
|
|
443 | void |
|
|
444 | set_texture (CFClient::Map self, int face, int name, int w, int h, float s, float t) |
|
|
445 | CODE: |
|
|
446 | { |
|
|
447 | while (self->faces < face) |
|
|
448 | { |
|
|
449 | Append (mapface, self->face, self->faces, self->faces); |
|
|
450 | self->faces *= 2; |
|
|
451 | } |
|
|
452 | |
|
|
453 | self->face [face].name = name; |
|
|
454 | self->face [face].w = w; |
|
|
455 | self->face [face].h = h; |
|
|
456 | self->face [face].s = s; |
|
|
457 | self->face [face].t = t; |
|
|
458 | } |
|
|
459 | |
|
|
460 | void |
|
|
461 | scroll (CFClient::Map self, int dx, int dy) |
|
|
462 | CODE: |
|
|
463 | { |
|
|
464 | if (dx > 0) |
|
|
465 | map_blank (self, self->x, self->y, dx - 1, self->h); |
|
|
466 | else if (dx < 0) |
|
|
467 | map_blank (self, self->x + self->w + dx + 1, self->y, 1 - dx, self->h); |
|
|
468 | |
|
|
469 | if (dy > 0) |
|
|
470 | map_blank (self, self->x, self->y, self->w, dy - 1); |
|
|
471 | else if (dy < 0) |
|
|
472 | map_blank (self, self->x, self->y + self->h + dy + 1, self->w, 1 - dy); |
|
|
473 | |
|
|
474 | self->x += dx; |
|
|
475 | self->y += dy; |
|
|
476 | |
|
|
477 | while (self->y < 0) |
|
|
478 | { |
|
|
479 | Prepend (maprow, self->row, self->rows, MAP_EXTEND_Y); |
|
|
480 | |
|
|
481 | self->rows += MAP_EXTEND_Y; |
|
|
482 | self->y += MAP_EXTEND_Y; |
|
|
483 | } |
|
|
484 | } |
|
|
485 | |
|
|
486 | void |
|
|
487 | map1a_update (CFClient::Map self, SV *data_) |
|
|
488 | CODE: |
|
|
489 | { |
|
|
490 | uint8_t *data = (uint8_t *)SvPVbyte_nolen (data_); |
|
|
491 | uint8_t *data_end = (uint8_t *)SvEND (data_); |
|
|
492 | |
|
|
493 | while (data < data_end) |
|
|
494 | { |
|
|
495 | int flags = (data [0] << 8) + data [1]; data += 2; |
|
|
496 | |
|
|
497 | int x = ((flags >> 10) & 63) + self->x; |
|
|
498 | int y = ((flags >> 4) & 63) + self->y; |
|
|
499 | |
|
|
500 | while (y >= self->rows) |
|
|
501 | { |
|
|
502 | Prepend (maprow, self->row, self->rows, MAP_EXTEND_Y); |
|
|
503 | |
|
|
504 | self->rows += MAP_EXTEND_Y; |
|
|
505 | } |
|
|
506 | |
|
|
507 | maprow *row = self->row + y; |
|
|
508 | |
|
|
509 | if (!row->col) |
|
|
510 | { |
|
|
511 | Newz (0, row->col, MAP_EXTEND_X, mapcell); |
|
|
512 | row->c0 = self->x - MAP_EXTEND_X / 4; |
|
|
513 | row->c1 = row->c0 + MAP_EXTEND_X; |
|
|
514 | } |
|
|
515 | |
|
|
516 | if (row->c0 > x) |
|
|
517 | { |
|
|
518 | int extend = row->c0 - x + MAP_EXTEND_X; |
|
|
519 | Prepend (mapcell, row->col, row->c1 - row->c0, extend); |
|
|
520 | row->c0 -= extend; |
|
|
521 | } |
|
|
522 | else if (x >= row->c1) |
|
|
523 | { |
|
|
524 | int extend = x - row->c1 + MAP_EXTEND_X; |
|
|
525 | Append (mapcell, row->col, row->c1 - row->c0, extend); |
|
|
526 | row->c1 += extend; |
|
|
527 | } |
|
|
528 | |
|
|
529 | assert (y >= 0); |
|
|
530 | assert (y < self->rows); |
|
|
531 | assert (x >= row->c0); |
|
|
532 | assert (x < row->c1); |
|
|
533 | mapcell *cell = row->col + (x - row->c0); |
|
|
534 | |
|
|
535 | if (flags & 15) |
|
|
536 | { |
|
|
537 | if (cell->darkness < 0 && x < self->w && y < self->h) |
|
|
538 | { |
|
|
539 | cell->darkness = 0; |
|
|
540 | cell->face [0] = 0; |
|
|
541 | cell->face [1] = 0; |
|
|
542 | cell->face [2] = 0; |
|
|
543 | } |
|
|
544 | |
|
|
545 | cell->darkness = flags & 8 ? *data++ : 255; |
|
|
546 | |
|
|
547 | if (flags & 4) |
|
|
548 | { |
|
|
549 | cell->face [0] = (data [0] << 8) + data [1]; data += 2; |
|
|
550 | } |
|
|
551 | |
|
|
552 | if (flags & 2) |
|
|
553 | { |
|
|
554 | cell->face [1] = (data [0] << 8) + data [1]; data += 2; |
|
|
555 | } |
|
|
556 | |
|
|
557 | if (flags & 1) |
|
|
558 | { |
|
|
559 | cell->face [2] = (data [0] << 8) + data [1]; data += 2; |
|
|
560 | } |
|
|
561 | } |
|
|
562 | else |
|
|
563 | cell->darkness = -1; |
|
|
564 | } |
|
|
565 | } |
|
|
566 | |
|
|
567 | void |
|
|
568 | draw (CFClient::Map self, int x0, int y0, int sw, int sh) |
|
|
569 | PPCODE: |
|
|
570 | { |
|
|
571 | int sw4 = (sw + 3) & ~3; |
|
|
572 | SV *darkness_sv = sv_2mortal (newSV (sw4 * sh)); |
|
|
573 | uint8_t *darkness = SvPVX (darkness_sv); |
|
|
574 | |
|
|
575 | SvPOK_only (darkness_sv); |
|
|
576 | SvCUR_set (darkness_sv, sw4 * sh); |
|
|
577 | |
|
|
578 | int vx = self->vx = self->w >= sw |
|
|
579 | ? self->x + (self->w - sw) / 2 |
|
|
580 | : MIN (self->x, MAX (self->x + self->w - sw + 1, self->vx)); |
|
|
581 | |
|
|
582 | int vy = self->vy = self->h >= sh |
|
|
583 | ? self->y + (self->h - sh) / 2 |
|
|
584 | : MIN (self->y, MAX (self->y + self->h - sh + 1, self->vy)); |
|
|
585 | |
|
|
586 | glColor4ub (255, 255, 255, 255); |
|
|
587 | |
|
|
588 | glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
|
|
589 | glEnable (GL_BLEND); |
|
|
590 | glEnable (GL_TEXTURE_2D); |
|
|
591 | glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); |
|
|
592 | |
|
|
593 | int x, y, z; |
|
|
594 | |
|
|
595 | int last_name = 0; |
|
|
596 | |
|
|
597 | glBegin (GL_QUADS); |
|
|
598 | |
|
|
599 | for (z = 0; z < 3; z++) |
|
|
600 | for (y = 0; y < sh; y++) |
|
|
601 | if (0 <= y + vy && y + vy < self->rows) |
|
|
602 | { |
|
|
603 | maprow *row = self->row + (y + vy); |
|
|
604 | |
|
|
605 | for (x = 0; x < sw; x++) |
|
|
606 | if (row->c0 <= x + vx && x + vx < row->c1) |
|
|
607 | { |
|
|
608 | mapcell *cell = row->col + (x + vx - row->c0); |
|
|
609 | |
|
|
610 | darkness[y * sw4 + x] = cell->darkness < 0 |
|
|
611 | ? 224 |
|
|
612 | : 255 - cell->darkness; |
|
|
613 | |
|
|
614 | uint16_t face = cell->face [z]; |
|
|
615 | |
|
|
616 | if (face) |
|
|
617 | { |
|
|
618 | mapface tex = self->face [face]; |
|
|
619 | |
|
|
620 | int px = (x + 1) * 32 - tex.w; |
|
|
621 | int py = (y + 1) * 32 - tex.h; |
|
|
622 | |
|
|
623 | if (last_name != tex.name) |
|
|
624 | { |
|
|
625 | glEnd (); |
|
|
626 | last_name = tex.name; |
|
|
627 | glBindTexture (GL_TEXTURE_2D, last_name); |
|
|
628 | glBegin (GL_QUADS); |
|
|
629 | } |
|
|
630 | |
|
|
631 | glTexCoord2f (0 , 0 ); glVertex2f (px , py ); |
|
|
632 | glTexCoord2f (0 , tex.t); glVertex2f (px , py + tex.h); |
|
|
633 | glTexCoord2f (tex.s, tex.t); glVertex2f (px + tex.w, py + tex.h); |
|
|
634 | glTexCoord2f (tex.s, 0 ); glVertex2f (px + tex.w, py ); |
|
|
635 | } |
|
|
636 | } |
|
|
637 | } |
|
|
638 | |
|
|
639 | glEnd (); |
|
|
640 | |
|
|
641 | EXTEND (SP, 3); |
|
|
642 | PUSHs (sv_2mortal (newSViv (sw4))); |
|
|
643 | PUSHs (sv_2mortal (newSViv (sh))); |
|
|
644 | PUSHs (darkness_sv); |
|
|
645 | |
|
|
646 | glDisable (GL_TEXTURE_2D); |
|
|
647 | glDisable (GL_BLEND); |
|
|
648 | } |
|
|
649 | |