ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/Client.xs
Revision: 1.41
Committed: Sat Apr 15 01:52:53 2006 UTC (18 years, 1 month ago) by root
Branch: MAIN
Changes since 1.40: +2 -2 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4
5 #include <string.h>
6 #include <stdio.h>
7
8 #include <SDL.h>
9 #include <SDL_image.h>
10 #include <SDL_opengl.h>
11
12 #include <glib/gmacros.h>
13
14 #include <pango/pango.h>
15 #include <pango/pangofc-fontmap.h>
16 #include <pango/pangoft2.h>
17
18 #include <sys/types.h>
19 #include <sys/socket.h>
20 #include <netinet/in.h>
21 #include <netinet/tcp.h>
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
30 static PangoContext *context;
31 static PangoFontMap *fontmap;
32
33 typedef struct cf_layout {
34 PangoLayout *pl;
35 int base_height;
36 } *CFClient__Layout;
37
38 static void
39 substitute_func (FcPattern *pattern, gpointer data)
40 {
41 FcPatternAddBool (pattern, FC_HINTING , 1);
42 FcPatternAddBool (pattern, FC_AUTOHINT, 0);
43 }
44
45 static void
46 layout_update (CFClient__Layout self)
47 {
48 /* use a random scale factor to account for unknown descenders, 0.8 works
49 * reasonably well with bitstream vera
50 */
51 PangoFontDescription *font = pango_context_get_font_description (context);
52 pango_font_description_set_absolute_size (font, self->base_height * (PANGO_SCALE * 8 / 10));
53 }
54
55 static void
56 layout_get_pixel_size (CFClient__Layout self, int *w, int *h)
57 {
58 layout_update (self);
59
60 pango_layout_get_pixel_size (self->pl, w, h);
61
62 *w = (*w + 3) & ~3;
63 if (!*w) *w = 1;
64 if (!*h) *h = 1;
65 }
66
67 typedef struct {
68 GLint name;
69 int w, h;
70 float s, t;
71 uint8_t r, g, b, a;
72 } mapface;
73
74 typedef struct {
75 int16_t darkness;
76 uint16_t face[3];
77 } mapcell;
78
79 typedef struct {
80 int32_t c0, c1;
81 mapcell *col;
82 } maprow;
83
84 typedef struct map {
85 int x, y, w, h;
86 int vx, vy;
87 int faces;
88 mapface *face;
89
90 uint32_t rows;
91 maprow *row;
92 } *CFClient__Map;
93
94 static char *
95 prepend (char *ptr, int sze, int inc)
96 {
97 char *p;
98
99 New (0, p, sze + inc, char);
100 Zero (p, inc, char);
101 Move (ptr, p + inc, sze, char);
102 Safefree (ptr);
103
104 return p;
105 }
106
107 static char *
108 append (char *ptr, int sze, int inc)
109 {
110 Renew (ptr, sze + inc, char);
111 Zero (ptr + sze, inc, char);
112
113 return ptr;
114 }
115
116 #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))
118
119 static void
120 map_clear (CFClient__Map self)
121 {
122 int r;
123
124 for (r = 0; r < self->rows; r++)
125 Safefree (self->row[r].col);
126
127 Safefree (self->row);
128
129 self->x = 0;
130 self->y = 0;
131 self->vx = 0;
132 self->vy = 0;
133 self->row = 0;
134 self->rows = 0;
135 }
136
137 static void
138 map_blank (CFClient__Map self, int x0, int y0, int w, int h)
139 {
140 int x, y;
141
142 for (y = y0; y < y0 + h; y++)
143 if (y >= 0)
144 {
145 if (y >= self->rows)
146 break;
147
148 maprow *row = self->row + y;
149
150 for (x = x0; x < x0 + w; x++)
151 if (x >= row->c0)
152 {
153 if (x >= row->c1)
154 break;
155
156 row->col[x - row->c0].darkness = -1;
157 }
158 }
159 }
160
161 MODULE = CFClient PACKAGE = CFClient
162
163 PROTOTYPES: ENABLE
164
165 BOOT:
166 {
167 fontmap = pango_ft2_font_map_new ();
168 pango_ft2_font_map_set_default_substitute ((PangoFT2FontMap *)fontmap, substitute_func, 0, 0);
169 context = pango_ft2_font_map_create_context ((PangoFT2FontMap *)fontmap);
170 }
171
172 void
173 lowdelay (int fd, int val = 1)
174 CODE:
175 setsockopt (fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof (val));
176
177 char *
178 gl_version ()
179 CODE:
180 RETVAL = (char *)glGetString (GL_VERSION);
181 OUTPUT:
182 RETVAL
183
184 char *
185 gl_extensions ()
186 CODE:
187 RETVAL = (char *)glGetString (GL_EXTENSIONS);
188 OUTPUT:
189 RETVAL
190
191 void
192 add_font (char *file)
193 CODE:
194 FcConfigAppFontAddFile (0, (const FcChar8 *)file); /* no idea wether this is required */
195
196 void
197 set_font (char *file)
198 CODE:
199 {
200 int count;
201 FcPattern *pattern = FcFreeTypeQuery ((const FcChar8 *)file, 0, 0, &count);
202 PangoFontDescription *font = pango_fc_font_description_from_pattern (pattern, 0);
203 FcPatternDestroy (pattern);
204 pango_context_set_font_description (context, font);
205 }
206
207 void
208 load_image_inline (SV *image_)
209 ALIAS:
210 load_image_file = 1
211 PPCODE:
212 {
213 STRLEN image_len;
214 char *image = (char *)SvPVbyte (image_, image_len);
215 SDL_Surface *surface, *surface2;
216 SDL_PixelFormat fmt;
217 SDL_RWops *rw = ix
218 ? SDL_RWFromFile (image, "r")
219 : SDL_RWFromConstMem (image, image_len);
220
221 if (!rw)
222 croak ("load_image: %s", SDL_GetError ());
223
224 surface = IMG_Load_RW (rw, 1);
225 if (!surface)
226 croak ("load_image: %s", SDL_GetError ());
227
228 fmt.palette = NULL;
229 fmt.BitsPerPixel = 32;
230 fmt.BytesPerPixel = 4;
231 fmt.Rmask = 0x000000ff;
232 fmt.Gmask = 0x0000ff00;
233 fmt.Bmask = 0x00ff0000;
234 fmt.Amask = 0xff000000;
235 fmt.Rloss = 0;
236 fmt.Gloss = 0;
237 fmt.Bloss = 0;
238 fmt.Aloss = 0;
239 fmt.Rshift = 0;
240 fmt.Gshift = 8;
241 fmt.Bshift = 16;
242 fmt.Ashift = 24;
243 fmt.colorkey = 0;
244 fmt.alpha = 0;
245
246 surface2 = SDL_ConvertSurface (surface, &fmt, SDL_SWSURFACE);
247
248 assert (surface2->pitch == surface2->w * 4);
249
250 EXTEND (SP, 5);
251 PUSHs (sv_2mortal (newSViv (surface2->w)));
252 PUSHs (sv_2mortal (newSViv (surface2->h)));
253 SDL_LockSurface (surface2);
254 PUSHs (sv_2mortal (newSVpvn (surface2->pixels, surface2->h * surface2->pitch)));
255 SDL_UnlockSurface (surface2);
256 PUSHs (sv_2mortal (newSViv (surface->flags & (SDL_SRCCOLORKEY | SDL_SRCALPHA) ? GL_RGBA : GL_RGB)));
257 PUSHs (sv_2mortal (newSViv (GL_RGBA)));
258 PUSHs (sv_2mortal (newSViv (GL_UNSIGNED_INT_8_8_8_8_REV)));
259
260 SDL_FreeSurface (surface);
261 SDL_FreeSurface (surface2);
262 }
263
264 void
265 average (int x, int y, uint32_t *data)
266 PPCODE:
267 {
268 uint32_t r = 0, g = 0, b = 0, a = 0;
269
270 x = y = x * y;
271
272 while (x--)
273 {
274 uint32_t p = *data++;
275
276 r += (p ) & 255;
277 g += (p >> 8) & 255;
278 b += (p >> 16) & 255;
279 a += (p >> 24) & 255;
280 }
281
282 EXTEND (SP, 4);
283 PUSHs (sv_2mortal (newSViv (r / y)));
284 PUSHs (sv_2mortal (newSViv (g / y)));
285 PUSHs (sv_2mortal (newSViv (b / y)));
286 PUSHs (sv_2mortal (newSViv (a / y)));
287 }
288
289 void
290 fatal (char *message)
291 CODE:
292 #ifdef WIN32
293 MessageBox (0, message, "Crossfire+ Fatal Error", MB_OK | MB_ICONERROR | MB_SETFOREGROUND);
294 #else
295 fprintf (stderr, "FATAL: %s\n", message);
296 #endif
297 exit (1);
298
299 MODULE = CFClient PACKAGE = CFClient::Layout
300
301 CFClient::Layout
302 new (SV *class, int base_height = 10)
303 CODE:
304 New (0, RETVAL, 1, struct cf_layout);
305 RETVAL->base_height = base_height;
306 RETVAL->pl = pango_layout_new (context);
307 pango_layout_set_wrap (RETVAL->pl, PANGO_WRAP_WORD_CHAR);
308 OUTPUT:
309 RETVAL
310
311 void
312 DESTROY (CFClient::Layout self)
313 CODE:
314 g_object_unref (self->pl);
315 Safefree (self);
316
317 void
318 set_text (CFClient::Layout self, SV *text_)
319 CODE:
320 {
321 STRLEN textlen;
322 char *text = SvPVutf8 (text_, textlen);
323
324 pango_layout_set_text (self->pl, text, textlen);
325 }
326
327 void
328 set_markup (CFClient::Layout self, SV *text_)
329 CODE:
330 {
331 STRLEN textlen;
332 char *text = SvPVutf8 (text_, textlen);
333
334 pango_layout_set_markup (self->pl, text, textlen);
335 }
336
337 void
338 set_height (CFClient::Layout self, int base_height)
339 CODE:
340 self->base_height = base_height;
341
342 void
343 set_width (CFClient::Layout self, int max_width = -1)
344 CODE:
345 pango_layout_set_width (self->pl, max_width < 0 ? max_width : max_width * PANGO_SCALE);
346
347 void
348 size (CFClient::Layout self)
349 PPCODE:
350 {
351 int w, h;
352
353 layout_update (self);
354 layout_get_pixel_size (self, &w, &h);
355
356 EXTEND (SP, 2);
357 PUSHs (sv_2mortal (newSViv (w)));
358 PUSHs (sv_2mortal (newSViv (h)));
359 }
360
361 int
362 xy_to_index (CFClient::Layout self, int x, int y)
363 CODE:
364 {
365 int index, trailing;
366
367 layout_update (self);
368 pango_layout_xy_to_index (self->pl, x * PANGO_SCALE, y * PANGO_SCALE, &index, &trailing);
369
370 RETVAL = index;
371 }
372 OUTPUT:
373 RETVAL
374
375 void
376 cursor_pos (CFClient::Layout self, int index)
377 PPCODE:
378 {
379 PangoRectangle strong_pos;
380 layout_update (self);
381 pango_layout_get_cursor_pos (self->pl, index, &strong_pos, 0);
382
383 EXTEND (SP, 3);
384 PUSHs (sv_2mortal (newSViv (strong_pos.x / PANGO_SCALE)));
385 PUSHs (sv_2mortal (newSViv (strong_pos.y / PANGO_SCALE)));
386 PUSHs (sv_2mortal (newSViv (strong_pos.height / PANGO_SCALE)));
387 }
388
389 void
390 render (CFClient::Layout self)
391 PPCODE:
392 {
393 SV *retval;
394 int w, h;
395 FT_Bitmap bitmap;
396
397 layout_update (self);
398 layout_get_pixel_size (self, &w, &h);
399
400 retval = newSV (w * h);
401 SvPOK_only (retval);
402 SvCUR_set (retval, w * h);
403
404 bitmap.rows = h;
405 bitmap.width = w;
406 bitmap.pitch = w;
407 bitmap.buffer = (unsigned char*)SvPVX (retval);
408 bitmap.num_grays = 256;
409 bitmap.pixel_mode = FT_PIXEL_MODE_GRAY;
410
411 memset (bitmap.buffer, 0, w * h);
412
413 pango_ft2_render_layout (&bitmap, self->pl, 0 * PANGO_SCALE, 0 * PANGO_SCALE);
414
415 EXTEND (SP, 3);
416 PUSHs (sv_2mortal (newSViv (w)));
417 PUSHs (sv_2mortal (newSViv (h)));
418 PUSHs (sv_2mortal (retval));
419 }
420
421 MODULE = CFClient PACKAGE = CFClient::Texture
422
423 void
424 draw_quad (SV *self, float x, float y, float w = 0, float h = 0)
425 PROTOTYPE: $$$;$$
426 CODE:
427 {
428 HV *hv = (HV *)SvRV (self);
429 float s = SvNV (*hv_fetch (hv, "s", 1, 1));
430 float t = SvNV (*hv_fetch (hv, "t", 1, 1));
431 int name = SvIV (*hv_fetch (hv, "name", 4, 1));
432 int wrap_mode = SvIV (*hv_fetch (hv, "wrap_mode", 9, 1));
433
434 if (items < 5)
435 {
436 w = SvNV (*hv_fetch (hv, "w", 1, 1));
437 h = SvNV (*hv_fetch (hv, "h", 1, 1));
438 }
439
440 glBindTexture (GL_TEXTURE_2D, name);
441 if (wrap_mode) {
442 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
443 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
444 }
445 glBegin (GL_QUADS);
446 glTexCoord2f (0, 0); glVertex2f (x , y );
447 glTexCoord2f (0, t); glVertex2f (x , y + h);
448 glTexCoord2f (s, t); glVertex2f (x + w, y + h);
449 glTexCoord2f (s, 0); glVertex2f (x + w, y );
450 glEnd ();
451 }
452
453 MODULE = CFClient PACKAGE = CFClient::Map
454
455 CFClient::Map
456 new (SV *class, int map_width, int map_height)
457 CODE:
458 New (0, RETVAL, 1, struct map);
459 RETVAL->x = 0;
460 RETVAL->y = 0;
461 RETVAL->w = map_width;
462 RETVAL->h = map_height;
463 RETVAL->vx = 0;
464 RETVAL->vy = 0;
465 RETVAL->faces = 8192;
466 Newz (0, RETVAL->face, RETVAL->faces, mapface);
467 RETVAL->rows = 0;
468 RETVAL->row = 0;
469 OUTPUT:
470 RETVAL
471
472 void
473 DESTROY (CFClient::Map self)
474 CODE:
475 {
476 map_clear (self);
477 Safefree (self->face);
478 Safefree (self);
479 }
480
481 void
482 clear (CFClient::Map self)
483 CODE:
484 map_clear (self);
485
486 void
487 set_texture (CFClient::Map self, int face, int name, int w, int h, float s, float t, int r, int g, int b, int a)
488 CODE:
489 {
490 while (self->faces < face)
491 {
492 Append (mapface, self->face, self->faces, self->faces);
493 self->faces *= 2;
494 }
495
496 mapface *f = self->face + face;
497
498 f->name = name;
499 f->w = w;
500 f->h = h;
501 f->s = s;
502 f->t = t;
503 f->r = r;
504 f->g = g;
505 f->b = b;
506 f->a = a;
507 }
508
509 void
510 scroll (CFClient::Map self, int dx, int dy)
511 CODE:
512 {
513 if (dx > 0)
514 map_blank (self, self->x, self->y, dx - 1, self->h);
515 else if (dx < 0)
516 map_blank (self, self->x + self->w + dx + 1, self->y, 1 - dx, self->h);
517
518 if (dy > 0)
519 map_blank (self, self->x, self->y, self->w, dy - 1);
520 else if (dy < 0)
521 map_blank (self, self->x, self->y + self->h + dy + 1, self->w, 1 - dy);
522
523 self->x += dx;
524 self->y += dy;
525
526 while (self->y < 0)
527 {
528 Prepend (maprow, self->row, self->rows, MAP_EXTEND_Y);
529
530 self->rows += MAP_EXTEND_Y;
531 self->y += MAP_EXTEND_Y;
532 }
533 }
534
535 void
536 map1a_update (CFClient::Map self, SV *data_)
537 CODE:
538 {
539 uint8_t *data = (uint8_t *)SvPVbyte_nolen (data_);
540 uint8_t *data_end = (uint8_t *)SvEND (data_);
541
542 while (data < data_end)
543 {
544 int flags = (data [0] << 8) + data [1]; data += 2;
545
546 int x = ((flags >> 10) & 63) + self->x;
547 int y = ((flags >> 4) & 63) + self->y;
548
549 while (y >= self->rows)
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
584 if (flags & 15)
585 {
586 if (cell->darkness < 0 && x < self->w && y < self->h)
587 {
588 cell->darkness = 0;
589 cell->face [0] = 0;
590 cell->face [1] = 0;
591 cell->face [2] = 0;
592 }
593
594 cell->darkness = flags & 8 ? *data++ : 255;
595
596 if (flags & 4)
597 {
598 cell->face [0] = (data [0] << 8) + data [1]; data += 2;
599 }
600
601 if (flags & 2)
602 {
603 cell->face [1] = (data [0] << 8) + data [1]; data += 2;
604 }
605
606 if (flags & 1)
607 {
608 cell->face [2] = (data [0] << 8) + data [1]; data += 2;
609 }
610 }
611 else
612 cell->darkness = -1;
613 }
614 }
615
616 SV *
617 mapmap (CFClient::Map self, int w, int h)
618 CODE:
619 {
620 int x0, x1, x;
621 int y0, y1, y;
622 int z;
623 SV *map_sv = newSV (w * h * sizeof (uint32_t));
624 uint32_t *map = (uint32_t *)SvPVX (map_sv);
625
626 SvPOK_only (map_sv);
627 SvCUR_set (map_sv, w * h * sizeof (uint32_t));
628
629 x0 = self->x - w / 2; x1 = x0 + w;
630 y0 = self->y - h / 2; y1 = y0 + h;
631
632 for (y = y0; y < y1; y++)
633 {
634 maprow *row = 0 <= y && y < self->rows
635 ? self->row + y
636 : 0;
637
638 for (x = x0; x < x1; x++)
639 {
640 int r = 32, g = 32, b = 32, a = 192;
641
642 if (row && row->c0 <= x && x < row->c1)
643 {
644 mapcell *cell = row->col + (x - row->c0);
645
646 for (z = 0; z <= 0; z++)
647 {
648 uint16_t face = cell->face [z];
649
650 if (face)
651 {
652 mapface tex = self->face [face];
653 int a0 = 255 - tex.a;
654 int a1 = tex.a;
655
656 r = (r * a0 + tex.r * a1) / 255;
657 g = (g * a0 + tex.g * a1) / 255;
658 b = (b * a0 + tex.b * a1) / 255;
659 a = (a * a0 + tex.a * a1) / 255;
660 }
661 }
662 }
663
664 *map++ = (r )
665 | (g << 8)
666 | (b << 16)
667 | (a << 24);
668 }
669 }
670
671 RETVAL = map_sv;
672 }
673 OUTPUT:
674 RETVAL
675
676 void
677 draw (CFClient::Map self, int shift_x, int shift_y, int x0, int y0, int sw, int sh)
678 PPCODE:
679 {
680 int sw4 = (sw + 3) & ~3;
681 SV *darkness_sv = sv_2mortal (newSV (sw4 * sh));
682 uint8_t *darkness = (uint8_t *)SvPVX (darkness_sv);
683
684 SvPOK_only (darkness_sv);
685 SvCUR_set (darkness_sv, sw4 * sh);
686
687 int vx = self->x + (self->w - sw) / 2 - shift_x;
688 int vy = self->y + (self->h - sh) / 2 - shift_y;
689
690 if (0)
691 {
692 int vx = self->vx = self->w >= sw
693 ? self->x + (self->w - sw) / 2
694 : MIN (self->x, MAX (self->x + self->w - sw + 1, self->vx));
695
696 int vy = self->vy = self->h >= sh
697 ? self->y + (self->h - sh) / 2
698 : MIN (self->y, MAX (self->y + self->h - sh + 1, self->vy));
699 }
700
701 glColor4ub (255, 255, 255, 255);
702
703 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
704 glEnable (GL_BLEND);
705 glEnable (GL_TEXTURE_2D);
706 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
707
708 int x, y, z;
709
710 int last_name = 0;
711
712 glBegin (GL_QUADS);
713
714 for (z = 0; z < 3; z++)
715 for (y = 0; y < sh; y++)
716 if (0 <= y + vy && y + vy < self->rows)
717 {
718 maprow *row = self->row + (y + vy);
719
720 for (x = 0; x < sw; x++)
721 if (row->c0 <= x + vx && x + vx < row->c1)
722 {
723 mapcell *cell = row->col + (x + vx - row->c0);
724
725 darkness[y * sw4 + x] = cell->darkness < 0
726 ? 255 - FOW_DARKNESS
727 : 255 - cell->darkness;
728
729 uint16_t face = cell->face [z];
730
731 if (face)
732 {
733 mapface tex = self->face [face];
734
735 int px = (x + 1) * 32 - tex.w;
736 int py = (y + 1) * 32 - tex.h;
737
738 if (last_name != tex.name)
739 {
740 glEnd ();
741 last_name = tex.name;
742 glBindTexture (GL_TEXTURE_2D, last_name);
743 glBegin (GL_QUADS);
744 }
745
746 glTexCoord2f (0 , 0 ); glVertex2f (px , py );
747 glTexCoord2f (0 , tex.t); glVertex2f (px , py + tex.h);
748 glTexCoord2f (tex.s, tex.t); glVertex2f (px + tex.w, py + tex.h);
749 glTexCoord2f (tex.s, 0 ); glVertex2f (px + tex.w, py );
750 }
751 }
752 }
753
754 glEnd ();
755
756 glDisable (GL_TEXTURE_2D);
757 glDisable (GL_BLEND);
758
759 EXTEND (SP, 3);
760 PUSHs (sv_2mortal (newSViv (sw4)));
761 PUSHs (sv_2mortal (newSViv (sh)));
762 PUSHs (darkness_sv);
763 }
764