ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/Client.xs
Revision: 1.30
Committed: Thu Apr 13 23:07:56 2006 UTC (18 years, 1 month ago) by root
Branch: MAIN
Changes since 1.29: +169 -73 lines
Log Message:
1st version of xs-level map handling

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, 1);
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 } 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 mapcell *cell = row->col + (x - row->c0);
156 cell->darkness = -1;
157 cell->face [2] = 0;
158 }
159 }
160 }
161
162 MODULE = CFClient PACKAGE = CFClient
163
164 PROTOTYPES: ENABLE
165
166 BOOT:
167 {
168 fontmap = pango_ft2_font_map_new ();
169 pango_ft2_font_map_set_default_substitute ((PangoFT2FontMap *)fontmap, substitute_func, 0, 0);
170 context = pango_ft2_font_map_create_context ((PangoFT2FontMap *)fontmap);
171 }
172
173 void
174 lowdelay (int fd, int val = 1)
175 CODE:
176 setsockopt (fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof (val));
177
178 char *
179 gl_version ()
180 CODE:
181 RETVAL = (char *)glGetString (GL_VERSION);
182 OUTPUT:
183 RETVAL
184
185 char *
186 gl_extensions ()
187 CODE:
188 RETVAL = (char *)glGetString (GL_EXTENSIONS);
189 OUTPUT:
190 RETVAL
191
192 void
193 add_font (char *file)
194 CODE:
195 FcConfigAppFontAddFile (0, (const FcChar8 *)file); /* no idea wether this is required */
196
197 void
198 set_font (char *file)
199 CODE:
200 {
201 int count;
202 FcPattern *pattern = FcFreeTypeQuery ((const FcChar8 *)file, 0, 0, &count);
203 PangoFontDescription *font = pango_fc_font_description_from_pattern (pattern, 0);
204 FcPatternDestroy (pattern);
205 pango_context_set_font_description (context, font);
206 }
207
208 void
209 load_image_inline (SV *image_)
210 ALIAS:
211 load_image_file = 1
212 PPCODE:
213 {
214 STRLEN image_len;
215 char *image = (char *)SvPVbyte (image_, image_len);
216 SDL_Surface *surface, *surface2;
217 SDL_PixelFormat fmt;
218 SDL_RWops *rw = ix
219 ? SDL_RWFromFile (image, "r")
220 : SDL_RWFromConstMem (image, image_len);
221
222 if (!rw)
223 croak ("load_image: unable to open file");
224
225 surface = IMG_Load_RW (rw, 1);
226 if (!surface)
227 croak ("load_image: unable to read file");
228
229 fmt.palette = NULL;
230 fmt.BitsPerPixel = 32;
231 fmt.BytesPerPixel = 4;
232 fmt.Rmask = 0x000000ff;
233 fmt.Gmask = 0x0000ff00;
234 fmt.Bmask = 0x00ff0000;
235 fmt.Amask = 0xff000000;
236 fmt.Rloss = 0;
237 fmt.Gloss = 0;
238 fmt.Bloss = 0;
239 fmt.Aloss = 0;
240 fmt.Rshift = 0;
241 fmt.Gshift = 8;
242 fmt.Bshift = 16;
243 fmt.Ashift = 24;
244 fmt.colorkey = 0;
245 fmt.alpha = 0;
246
247 surface2 = SDL_ConvertSurface (surface, &fmt, SDL_SWSURFACE);
248
249 EXTEND (SP, 5);
250 PUSHs (sv_2mortal (newSViv (surface2->w)));
251 PUSHs (sv_2mortal (newSViv (surface2->h)));
252 SDL_LockSurface (surface2);
253 PUSHs (sv_2mortal (newSVpvn (surface2->pixels, surface2->h * surface2->pitch)));
254 SDL_UnlockSurface (surface2);
255 PUSHs (sv_2mortal (newSViv (surface->flags & (SDL_SRCCOLORKEY | SDL_SRCALPHA) ? GL_RGBA : GL_RGB)));
256 PUSHs (sv_2mortal (newSViv (GL_RGBA)));
257 PUSHs (sv_2mortal (newSViv (GL_UNSIGNED_INT_8_8_8_8_REV)));
258
259 SDL_FreeSurface (surface);
260 SDL_FreeSurface (surface2);
261 }
262
263 void
264 fatal (char *message)
265 CODE:
266 #ifdef WIN32
267 MessageBox (0, message, "Crossfire+ Fatal Error", MB_OK | MB_ICONERROR | MB_SETFOREGROUND);
268 #else
269 fprintf (stderr, "%s\n", message);
270 #endif
271 exit (1);
272
273 MODULE = CFClient PACKAGE = CFClient::Layout
274
275 CFClient::Layout
276 new (SV *class, int base_height = 10)
277 CODE:
278 New (0, RETVAL, 1, struct cf_layout);
279 RETVAL->base_height = base_height;
280 RETVAL->pl = pango_layout_new (context);
281 pango_layout_set_wrap (RETVAL->pl, PANGO_WRAP_WORD_CHAR);
282 OUTPUT:
283 RETVAL
284
285 void
286 DESTROY (CFClient::Layout self)
287 CODE:
288 g_object_unref (self->pl);
289 Safefree (self);
290
291 void
292 set_markup (CFClient::Layout self, SV *text_)
293 CODE:
294 {
295 STRLEN textlen;
296 char *text = SvPVutf8 (text_, textlen);
297
298 pango_layout_set_markup (self->pl, text, textlen);
299 }
300
301 void
302 set_height (CFClient::Layout self, int base_height)
303 CODE:
304 self->base_height = base_height;
305
306 void
307 set_width (CFClient::Layout self, int max_width = -1)
308 CODE:
309 pango_layout_set_width (self->pl, max_width < 0 ? max_width : max_width * PANGO_SCALE);
310
311 void
312 size (CFClient::Layout self)
313 PPCODE:
314 {
315 int w, h;
316
317 layout_update (self);
318 layout_get_pixel_size (self, &w, &h);
319
320 EXTEND (SP, 2);
321 PUSHs (sv_2mortal (newSViv (w)));
322 PUSHs (sv_2mortal (newSViv (h)));
323 }
324
325 int
326 xy_to_index (CFClient::Layout self, int x, int y)
327 CODE:
328 {
329 int index, trailing;
330
331 layout_update (self);
332 pango_layout_xy_to_index (self->pl, x * PANGO_SCALE, y * PANGO_SCALE, &index, &trailing);
333
334 RETVAL = index;
335 }
336 OUTPUT:
337 RETVAL
338
339 void
340 cursor_pos (CFClient::Layout self, int index)
341 PPCODE:
342 {
343 PangoRectangle strong_pos;
344 layout_update (self);
345 pango_layout_get_cursor_pos (self->pl, index, &strong_pos, 0);
346
347 EXTEND (SP, 3);
348 PUSHs (sv_2mortal (newSViv (strong_pos.x / PANGO_SCALE)));
349 PUSHs (sv_2mortal (newSViv (strong_pos.y / PANGO_SCALE)));
350 PUSHs (sv_2mortal (newSViv (strong_pos.height / PANGO_SCALE)));
351 }
352
353 void
354 render (CFClient::Layout self)
355 PPCODE:
356 {
357 SV *retval;
358 int w, h;
359 FT_Bitmap bitmap;
360
361 layout_update (self);
362 layout_get_pixel_size (self, &w, &h);
363
364 retval = newSV (w * h);
365 SvPOK_only (retval);
366 SvCUR_set (retval, w * h);
367
368 bitmap.rows = h;
369 bitmap.width = w;
370 bitmap.pitch = w;
371 bitmap.buffer = (unsigned char*)SvPVX (retval);
372 bitmap.num_grays = 256;
373 bitmap.pixel_mode = FT_PIXEL_MODE_GRAY;
374
375 memset (bitmap.buffer, 0, w * h);
376
377 pango_ft2_render_layout (&bitmap, self->pl, 0 * PANGO_SCALE, 0 * PANGO_SCALE);
378
379 EXTEND (SP, 3);
380 PUSHs (sv_2mortal (newSViv (w)));
381 PUSHs (sv_2mortal (newSViv (h)));
382 PUSHs (sv_2mortal (retval));
383 }
384
385 MODULE = CFClient PACKAGE = CFClient::Texture
386
387 void
388 draw_quad (SV *self, double x, double y, double w = 0, double h = 0)
389 PROTOTYPE: $$$;$$
390 CODE:
391 {
392 HV *hv = (HV *)SvRV (self);
393 double s = SvNV (*hv_fetch (hv, "s", 1, 1));
394 double t = SvNV (*hv_fetch (hv, "t", 1, 1));
395 int name = SvIV (*hv_fetch (hv, "name", 4, 1));
396
397 if (items < 5)
398 {
399 w = SvNV (*hv_fetch (hv, "w", 1, 1));
400 h = SvNV (*hv_fetch (hv, "h", 1, 1));
401 }
402
403 glBindTexture (GL_TEXTURE_2D, name);
404 glBegin (GL_QUADS);
405 glTexCoord2d (0, 0); glVertex2d (x , y );
406 glTexCoord2d (0, t); glVertex2d (x , y + h);
407 glTexCoord2d (s, t); glVertex2d (x + w, y + h);
408 glTexCoord2d (s, 0); glVertex2d (x + w, y );
409 glEnd ();
410 }
411
412 MODULE = CFClient PACKAGE = CFClient::Map
413
414 CFClient::Map
415 new (SV *class, int map_width, int map_height)
416 CODE:
417 New (0, RETVAL, 1, struct map);
418 RETVAL->x = 0;
419 RETVAL->y = 0;
420 RETVAL->w = map_width;
421 RETVAL->h = map_height;
422 RETVAL->vx = 0;
423 RETVAL->vy = 0;
424 RETVAL->faces = 8192;
425 Newz (0, RETVAL->face, RETVAL->faces, mapface);
426 RETVAL->rows = 0;
427 RETVAL->row = 0;
428 OUTPUT:
429 RETVAL
430
431 void
432 DESTROY (CFClient::Map self)
433 CODE:
434 {
435 map_clear (self);
436 Safefree (self->face);
437 Safefree (self);
438 }
439
440 void
441 clear (CFClient::Map self)
442 CODE:
443 map_clear (self);
444
445 void
446 set_texture (CFClient::Map self, int face, int name, int w, int h, float s, float t)
447 CODE:
448 {
449 while (self->faces < face)
450 {
451 Append (mapface, self->face, self->faces, self->faces);
452 self->faces *= 2;
453 }
454
455 self->face [face].name = name;
456 self->face [face].w = w;
457 self->face [face].h = h;
458 self->face [face].s = s;
459 self->face [face].t = t;
460 }
461
462 void
463 scroll (CFClient::Map self, int dx, int dy)
464 CODE:
465 {
466 if (dx > 0)
467 map_blank (self, self->x, self->y, dx - 1, self->h);
468 else if (dx < 0)
469 map_blank (self, self->x + self->w + dx + 1, self->y, 1 - dx, self->h);
470
471 if (dy > 0)
472 map_blank (self, self->x, self->y, self->w, dy - 1);
473 else if (dy < 0)
474 map_blank (self, self->x, self->y + self->h + dy + 1, self->w, 1 - dy);
475
476 self->x += dx;
477 self->y += dy;
478
479 while (self->y < 0)
480 {
481 Prepend (maprow, self->row, self->rows, MAP_EXTEND_Y);
482
483 self->rows += MAP_EXTEND_Y;
484 self->y += MAP_EXTEND_Y;
485 }
486 }
487
488 void
489 map1a_update (CFClient::Map self, SV *data_)
490 CODE:
491 {
492 uint8_t *data = (uint8_t *)SvPVbyte_nolen (data_);
493 uint8_t *data_end = (uint8_t *)SvEND (data_);
494
495 while (data < data_end)
496 {
497 int flags = (data [0] << 8) + data [1]; data += 2;
498
499 int x = ((flags >> 10) & 63) + self->x;
500 int y = ((flags >> 4) & 63) + self->y;
501
502 while (y >= self->rows)
503 {
504 Prepend (maprow, self->row, self->rows, MAP_EXTEND_Y);
505
506 self->rows += MAP_EXTEND_Y;
507 }
508
509 maprow *row = self->row + y;
510
511 if (!row->col)
512 {
513 Newz (0, row->col, MAP_EXTEND_X, mapcell);
514 row->c0 = self->x - MAP_EXTEND_X / 4;
515 row->c1 = row->c0 + MAP_EXTEND_X;
516 }
517
518 if (row->c0 > x)
519 {
520 int extend = row->c0 - x + MAP_EXTEND_X;
521 Prepend (mapcell, row->col, row->c1 - row->c0, extend);
522 row->c0 -= extend;
523 }
524 else if (x >= row->c1)
525 {
526 int extend = x - row->c1 + MAP_EXTEND_X;
527 Append (mapcell, row->col, row->c1 - row->c0, extend);
528 row->c1 += extend;
529 }
530
531 assert (y >= 0);
532 assert (y < self->rows);
533 assert (x >= row->c0);
534 assert (x < row->c1);
535 mapcell *cell = row->col + (x - row->c0);
536
537 if (flags & 15)
538 {
539 if (cell->darkness < 0 && x < self->w && y < self->h)
540 {
541 cell->darkness = 0;
542 cell->face [0] = 0;
543 cell->face [1] = 0;
544 cell->face [2] = 0;
545 }
546
547 cell->darkness = flags & 8 ? *data++ : 255;
548
549 if (flags & 4)
550 {
551 cell->face [0] = (data [0] << 8) + data [1]; data += 2;
552 }
553
554 if (flags & 2)
555 {
556 cell->face [1] = (data [0] << 8) + data [1]; data += 2;
557 }
558
559 if (flags & 1)
560 {
561 cell->face [2] = (data [0] << 8) + data [1]; data += 2;
562 }
563 }
564 else
565 {
566 cell->darkness = -1;
567 cell->face [2] = 0;
568 }
569 }
570 }
571
572 void
573 draw (CFClient::Map self, int x0, int y0, int sw, int sh)
574 CODE:
575 {
576 int vx = self->vx = self->w >= sw
577 ? self->x + (self->w - sw) / 2
578 : MIN (self->x, MAX (self->x + self->w - sw + 1, self->vx));
579
580 int vy = self->vy = self->h >= sh
581 ? self->y + (self->h - sh) / 2
582 : MIN (self->y, MAX (self->y + self->h - sh + 1, self->vy));
583
584 glColor4ub (255, 255, 255, 255);
585
586 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
587 glEnable (GL_BLEND);
588 glEnable (GL_TEXTURE_2D);
589 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
590
591 int x, y, z;
592
593 int last_name = 0;
594
595 glBegin (GL_QUADS);
596
597 for (z = 0; z < 3; z++)
598 for (y = 0; y < sh; y++)
599 if (0 <= y + vy && y + vy < self->rows)
600 {
601 maprow *row = self->row + (y + vy);
602
603 for (x = 0; x < sw; x++)
604 if (row->c0 <= x + vx && x + vx < row->c1)
605 {
606 mapcell *cell = row->col + (x + vx - row->c0);
607 uint16_t face = cell->face [z];
608
609 if (face)
610 {
611 mapface tex = self->face [face];
612
613 int px = (x + 1) * 32 - tex.w;
614 int py = (y + 1) * 32 - tex.h;
615
616 if (last_name != tex.name)
617 {
618 glEnd ();
619 last_name = tex.name;
620 glBindTexture (GL_TEXTURE_2D, last_name);
621 glBegin (GL_QUADS);
622 }
623
624 glTexCoord2f (0 , 0 ); glVertex2f (px , py );
625 glTexCoord2f (0 , tex.t); glVertex2f (px , py + tex.h);
626 glTexCoord2f (tex.s, tex.t); glVertex2f (px + tex.w, py + tex.h);
627 glTexCoord2f (tex.s, 0 ); glVertex2f (px + tex.w, py );
628 }
629 }
630 }
631
632 glEnd ();
633 }
634