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.35 by root, Fri Apr 14 02:03:10 2006 UTC vs.
Revision 1.51 by root, Wed Apr 19 20:46:44 2006 UTC

1#ifdef _WIN32
2# include <malloc.h>
3#endif
4
1#include "EXTERN.h" 5#include "EXTERN.h"
2#include "perl.h" 6#include "perl.h"
3#include "XSUB.h" 7#include "XSUB.h"
4 8
5#include <string.h> 9#include <string.h>
13 17
14#include <pango/pango.h> 18#include <pango/pango.h>
15#include <pango/pangofc-fontmap.h> 19#include <pango/pangofc-fontmap.h>
16#include <pango/pangoft2.h> 20#include <pango/pangoft2.h>
17 21
22#ifndef _WIN32
18#include <sys/types.h> 23# include <sys/types.h>
19#include <sys/socket.h> 24# include <sys/socket.h>
20#include <netinet/in.h> 25# include <netinet/in.h>
21#include <netinet/tcp.h> 26# include <netinet/tcp.h>
22
23#include <inttypes.h> 27# include <inttypes.h>
28#else
29 typedef unsigned char uint8_t;
30 typedef unsigned short uint16_t;
31 typedef unsigned int uint32_t;
32 typedef signed char int8_t;
33 typedef signed short int16_t;
34 typedef signed int int32_t;
35#endif
24 36
25#define FOW_DARKNESS 32 37#define FOW_DARKNESS 32
26 38
27#define MAP_EXTEND_X 32 39#define MAP_EXTEND_X 32
28#define MAP_EXTEND_Y 512 40#define MAP_EXTEND_Y 512
47{ 59{
48 /* use a random scale factor to account for unknown descenders, 0.8 works 60 /* use a random scale factor to account for unknown descenders, 0.8 works
49 * reasonably well with bitstream vera 61 * reasonably well with bitstream vera
50 */ 62 */
51 PangoFontDescription *font = pango_context_get_font_description (context); 63 PangoFontDescription *font = pango_context_get_font_description (context);
52 pango_font_description_set_absolute_size (font, self->base_height * (PANGO_SCALE * 8 / 10)); 64
65 int height = self->base_height * (PANGO_SCALE * 8 / 10);
66
67 if (pango_font_description_get_size (font) != height)
68 {
69 pango_font_description_set_absolute_size (font, height);
70 pango_layout_context_changed (self->pl);
71 }
53} 72}
54 73
55static void 74static void
56layout_get_pixel_size (CFClient__Layout self, int *w, int *h) 75layout_get_pixel_size (CFClient__Layout self, int *w, int *h)
57{ 76{
61 80
62 *w = (*w + 3) & ~3; 81 *w = (*w + 3) & ~3;
63 if (!*w) *w = 1; 82 if (!*w) *w = 1;
64 if (!*h) *h = 1; 83 if (!*h) *h = 1;
65} 84}
85
86typedef uint16_t mapface;
66 87
67typedef struct { 88typedef struct {
68 GLint name; 89 GLint name;
69 int w, h; 90 int w, h;
70 float s, t; 91 float s, t;
71} mapface; 92 uint8_t r, g, b, a;
93} maptex;
72 94
73typedef struct { 95typedef struct {
74 int16_t darkness; 96 int16_t darkness;
75 uint16_t face[3]; 97 mapface face[3];
76} mapcell; 98} mapcell;
77 99
78typedef struct { 100typedef struct {
79 int32_t c0, c1; 101 int32_t c0, c1;
80 mapcell *col; 102 mapcell *col;
81} maprow; 103} maprow;
82 104
83typedef struct map { 105typedef struct map {
84 int x, y, w, h; 106 int x, y, w, h;
85 int vx, vy; 107 int ox, oy; /* offset to virtual global coordinate system */
86 int faces; 108 int faces;
87 mapface *face; 109 mapface *face;
88 110
111 int texs;
112 maptex *tex;
113
89 uint32_t rows; 114 int32_t rows;
90 maprow *row; 115 maprow *row;
91} *CFClient__Map; 116} *CFClient__Map;
92 117
93static char * 118static char *
94prepend (char *ptr, int sze, int inc) 119prepend (char *ptr, int sze, int inc)
113} 138}
114 139
115#define Append(type,ptr,sze,inc) (ptr) = (type *)append ((char *)ptr, (sze) * sizeof (type), (inc) * sizeof (type)) 140#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)) 141#define Prepend(type,ptr,sze,inc) (ptr) = (type *)prepend ((char *)ptr, (sze) * sizeof (type), (inc) * sizeof (type))
117 142
143static maprow *
144map_get_row (CFClient__Map self, int y)
145{
146 if (0 > y)
147 {
148 int extend = - y + MAP_EXTEND_Y;
149 Prepend (maprow, self->row, self->rows, extend);
150
151 self->rows += extend;
152 self->y += extend;
153 y += extend;
154 }
155 else if (y >= self->rows)
156 {
157 int extend = y - self->rows + MAP_EXTEND_Y;
158 Append (maprow, self->row, self->rows, extend);
159 self->rows += extend;
160 }
161
162 return self->row + y;
163}
164
165static mapcell *
166row_get_cell (maprow *row, int x)
167{
168 if (!row->col)
169 {
170 Newz (0, row->col, MAP_EXTEND_X, mapcell);
171 row->c0 = x - MAP_EXTEND_X / 4;
172 row->c1 = row->c0 + MAP_EXTEND_X;
173 }
174
175 if (row->c0 > x)
176 {
177 int extend = row->c0 - x + MAP_EXTEND_X;
178 Prepend (mapcell, row->col, row->c1 - row->c0, extend);
179 row->c0 -= extend;
180 }
181 else if (x >= row->c1)
182 {
183 int extend = x - row->c1 + MAP_EXTEND_X;
184 Append (mapcell, row->col, row->c1 - row->c0, extend);
185 row->c1 += extend;
186 }
187
188 return row->col + (x - row->c0);
189}
190
191static mapcell *
192map_get_cell (CFClient__Map self, int x, int y)
193{
194 return row_get_cell (map_get_row (self, y), x);
195}
196
118static void 197static void
119map_clear (CFClient__Map self) 198map_clear (CFClient__Map self)
120{ 199{
121 int r; 200 int r;
122 201
125 204
126 Safefree (self->row); 205 Safefree (self->row);
127 206
128 self->x = 0; 207 self->x = 0;
129 self->y = 0; 208 self->y = 0;
130 self->vx = 0; 209 self->ox = 0;
131 self->vy = 0; 210 self->oy = 0;
132 self->row = 0; 211 self->row = 0;
133 self->rows = 0; 212 self->rows = 0;
134} 213}
135 214
136static void 215static void
137map_blank (CFClient__Map self, int x0, int y0, int w, int h) 216map_blank (CFClient__Map self, int x0, int y0, int w, int h)
138{ 217{
139 int x, y; 218 int x, y;
219 maprow *row;
140 220
141 for (y = y0; y < y0 + h; y++) 221 for (y = y0; y < y0 + h; y++)
142 if (y >= 0) 222 if (y >= 0)
143 { 223 {
144 if (y >= self->rows) 224 if (y >= self->rows)
145 break; 225 break;
146 226
147 maprow *row = self->row + y; 227 row = self->row + y;
148 228
149 for (x = x0; x < x0 + w; x++) 229 for (x = x0; x < x0 + w; x++)
150 if (x >= row->c0) 230 if (x >= row->c0)
151 { 231 {
152 if (x >= row->c1) 232 if (x >= row->c1)
161 241
162PROTOTYPES: ENABLE 242PROTOTYPES: ENABLE
163 243
164BOOT: 244BOOT:
165{ 245{
246 HV *stash = gv_stashpv ("CFClient", 1);
247 static const struct {
248 const char *name;
249 IV iv;
250 } *civ, const_iv[] = {
251# define const_iv(name) { # name, (IV)name }
252 const_iv (SDL_ACTIVEEVENT),
253 const_iv (SDL_KEYDOWN),
254 const_iv (SDL_KEYUP),
255 const_iv (SDL_MOUSEMOTION),
256 const_iv (SDL_MOUSEBUTTONDOWN),
257 const_iv (SDL_MOUSEBUTTONUP),
258 const_iv (SDL_JOYAXISMOTION),
259 const_iv (SDL_JOYBALLMOTION),
260 const_iv (SDL_JOYHATMOTION),
261 const_iv (SDL_JOYBUTTONDOWN),
262 const_iv (SDL_JOYBUTTONUP),
263 const_iv (SDL_QUIT),
264 const_iv (SDL_SYSWMEVENT),
265 const_iv (SDL_EVENT_RESERVEDA),
266 const_iv (SDL_EVENT_RESERVEDB),
267 const_iv (SDL_VIDEORESIZE),
268 const_iv (SDL_VIDEOEXPOSE),
269 const_iv (SDL_USEREVENT),
270 const_iv (SDLK_KP0),
271 const_iv (SDLK_KP1),
272 const_iv (SDLK_KP2),
273 const_iv (SDLK_KP3),
274 const_iv (SDLK_KP4),
275 const_iv (SDLK_KP5),
276 const_iv (SDLK_KP6),
277 const_iv (SDLK_KP7),
278 const_iv (SDLK_KP8),
279 const_iv (SDLK_KP9),
280 const_iv (SDLK_KP_PERIOD),
281 const_iv (SDLK_KP_DIVIDE),
282 const_iv (SDLK_KP_MULTIPLY),
283 const_iv (SDLK_KP_MINUS),
284 const_iv (SDLK_KP_PLUS),
285 const_iv (SDLK_KP_ENTER),
286 const_iv (SDLK_KP_EQUALS),
287 const_iv (SDLK_UP),
288 const_iv (SDLK_DOWN),
289 const_iv (SDLK_RIGHT),
290 const_iv (SDLK_LEFT),
291 const_iv (SDLK_INSERT),
292 const_iv (SDLK_HOME),
293 const_iv (SDLK_END),
294 const_iv (SDLK_PAGEUP),
295 const_iv (SDLK_PAGEDOWN),
296 const_iv (SDLK_F1),
297 const_iv (SDLK_F2),
298 const_iv (SDLK_F3),
299 const_iv (SDLK_F4),
300 const_iv (SDLK_F5),
301 const_iv (SDLK_F6),
302 const_iv (SDLK_F7),
303 const_iv (SDLK_F8),
304 const_iv (SDLK_F9),
305 const_iv (SDLK_F10),
306 const_iv (SDLK_F11),
307 const_iv (SDLK_F12),
308 const_iv (SDLK_F13),
309 const_iv (SDLK_F14),
310 const_iv (SDLK_F15),
311 const_iv (SDLK_NUMLOCK),
312 const_iv (SDLK_CAPSLOCK),
313 const_iv (SDLK_SCROLLOCK),
314 const_iv (SDLK_RSHIFT),
315 const_iv (SDLK_LSHIFT),
316 const_iv (SDLK_RCTRL),
317 const_iv (SDLK_LCTRL),
318 const_iv (SDLK_RALT),
319 const_iv (SDLK_LALT),
320 const_iv (SDLK_RMETA),
321 const_iv (SDLK_LMETA),
322 const_iv (SDLK_LSUPER),
323 const_iv (SDLK_RSUPER),
324 const_iv (SDLK_MODE),
325 const_iv (SDLK_COMPOSE),
326 const_iv (SDLK_HELP),
327 const_iv (SDLK_PRINT),
328 const_iv (SDLK_SYSREQ),
329 const_iv (SDLK_BREAK),
330 const_iv (SDLK_MENU),
331 const_iv (SDLK_POWER),
332 const_iv (SDLK_EURO),
333 const_iv (SDLK_UNDO),
334 const_iv (KMOD_NONE),
335 const_iv (KMOD_LSHIFT),
336 const_iv (KMOD_RSHIFT),
337 const_iv (KMOD_LCTRL),
338 const_iv (KMOD_RCTRL),
339 const_iv (KMOD_LALT),
340 const_iv (KMOD_RALT),
341 const_iv (KMOD_LMETA),
342 const_iv (KMOD_RMETA),
343 const_iv (KMOD_NUM),
344 const_iv (KMOD_CAPS),
345 const_iv (KMOD_MODE),
346 const_iv (KMOD_CTRL),
347 const_iv (KMOD_SHIFT),
348 const_iv (KMOD_ALT),
349 const_iv (KMOD_META)
350# undef const_iv
351 };
352
353 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; )
354 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv));
355
166 fontmap = pango_ft2_font_map_new (); 356 fontmap = pango_ft2_font_map_new ();
167 pango_ft2_font_map_set_default_substitute ((PangoFT2FontMap *)fontmap, substitute_func, 0, 0); 357 pango_ft2_font_map_set_default_substitute ((PangoFT2FontMap *)fontmap, substitute_func, 0, 0);
168 context = pango_ft2_font_map_create_context ((PangoFT2FontMap *)fontmap); 358 context = pango_ft2_font_map_create_context ((PangoFT2FontMap *)fontmap);
169} 359}
360
361int
362SDL_Init (U32 flags = SDL_INIT_VIDEO | SDL_INIT_AUDIO)
363
364void
365SDL_Quit ()
366
367void
368SDL_ListModes ()
369 PPCODE:
370{
371 SDL_Rect **m;
372
373 SDL_GL_SetAttribute (SDL_GL_RED_SIZE, 5);
374 SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, 5);
375 SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE, 5);
376 SDL_GL_SetAttribute (SDL_GL_ALPHA_SIZE, 1);
377
378 SDL_GL_SetAttribute (SDL_GL_ACCUM_RED_SIZE, 0);
379 SDL_GL_SetAttribute (SDL_GL_ACCUM_GREEN_SIZE, 0);
380 SDL_GL_SetAttribute (SDL_GL_ACCUM_BLUE_SIZE, 0);
381 SDL_GL_SetAttribute (SDL_GL_ACCUM_ALPHA_SIZE, 0);
382
383 SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1);
384 SDL_GL_SetAttribute (SDL_GL_BUFFER_SIZE, 15);
385 SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 0);
386
387 m = SDL_ListModes (0, SDL_FULLSCREEN | SDL_OPENGL);
388
389 if (m && m != (SDL_Rect **)-1)
390 while (*m)
391 {
392 AV *av = newAV ();
393 av_push (av, newSViv ((*m)->w));
394 av_push (av, newSViv ((*m)->h));
395 XPUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
396
397 ++m;
398 }
399}
400
401int
402SDL_SetVideoMode (int w, int h, int fullscreen)
403 CODE:
404 RETVAL = !!SDL_SetVideoMode (
405 w, h, 0, SDL_OPENGL | (fullscreen ? SDL_FULLSCREEN : 0)
406 );
407 SDL_WM_SetCaption ("Crossfire+ Client " VERSION, "Crossfire+");
408 OUTPUT:
409 RETVAL
170 410
171void 411void
172lowdelay (int fd, int val = 1) 412lowdelay (int fd, int val = 1)
173 CODE: 413 CODE:
414#ifndef _WIN32
174 setsockopt (fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof (val)); 415 setsockopt (fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof (val));
416#endif
175 417
176char * 418char *
177gl_version () 419gl_version ()
178 CODE: 420 CODE:
179 RETVAL = (char *)glGetString (GL_VERSION); 421 RETVAL = (char *)glGetString (GL_VERSION);
216 SDL_RWops *rw = ix 458 SDL_RWops *rw = ix
217 ? SDL_RWFromFile (image, "r") 459 ? SDL_RWFromFile (image, "r")
218 : SDL_RWFromConstMem (image, image_len); 460 : SDL_RWFromConstMem (image, image_len);
219 461
220 if (!rw) 462 if (!rw)
221 croak ("load_image: unable to open file"); 463 croak ("load_image: %s", SDL_GetError ());
222 464
223 surface = IMG_Load_RW (rw, 1); 465 surface = IMG_Load_RW (rw, 1);
224 if (!surface) 466 if (!surface)
225 croak ("load_image: unable to read file"); 467 croak ("load_image: %s", SDL_GetError ());
226 468
227 fmt.palette = NULL; 469 fmt.palette = NULL;
228 fmt.BitsPerPixel = 32; 470 fmt.BitsPerPixel = 32;
229 fmt.BytesPerPixel = 4; 471 fmt.BytesPerPixel = 4;
472#if SDL_BYTEORDER == SDL_LIL_ENDIAN
230 fmt.Rmask = 0x000000ff; 473 fmt.Rmask = 0x000000ff;
231 fmt.Gmask = 0x0000ff00; 474 fmt.Gmask = 0x0000ff00;
232 fmt.Bmask = 0x00ff0000; 475 fmt.Bmask = 0x00ff0000;
233 fmt.Amask = 0xff000000; 476 fmt.Amask = 0xff000000;
477#else
478 fmt.Rmask = 0xff000000;
479 fmt.Gmask = 0x00ff0000;
480 fmt.Bmask = 0x0000ff00;
481 fmt.Amask = 0x000000ff;
482#endif
234 fmt.Rloss = 0; 483 fmt.Rloss = 0;
235 fmt.Gloss = 0; 484 fmt.Gloss = 0;
236 fmt.Bloss = 0; 485 fmt.Bloss = 0;
237 fmt.Aloss = 0; 486 fmt.Aloss = 0;
238 fmt.Rshift = 0; 487 fmt.Rshift = 0;
242 fmt.colorkey = 0; 491 fmt.colorkey = 0;
243 fmt.alpha = 0; 492 fmt.alpha = 0;
244 493
245 surface2 = SDL_ConvertSurface (surface, &fmt, SDL_SWSURFACE); 494 surface2 = SDL_ConvertSurface (surface, &fmt, SDL_SWSURFACE);
246 495
496 assert (surface2->pitch == surface2->w * 4);
497
247 EXTEND (SP, 5); 498 EXTEND (SP, 5);
248 PUSHs (sv_2mortal (newSViv (surface2->w))); 499 PUSHs (sv_2mortal (newSViv (surface2->w)));
249 PUSHs (sv_2mortal (newSViv (surface2->h))); 500 PUSHs (sv_2mortal (newSViv (surface2->h)));
250 SDL_LockSurface (surface2); 501 SDL_LockSurface (surface2);
251 PUSHs (sv_2mortal (newSVpvn (surface2->pixels, surface2->h * surface2->pitch))); 502 PUSHs (sv_2mortal (newSVpvn (surface2->pixels, surface2->h * surface2->pitch)));
252 SDL_UnlockSurface (surface2); 503 SDL_UnlockSurface (surface2);
253 PUSHs (sv_2mortal (newSViv (surface->flags & (SDL_SRCCOLORKEY | SDL_SRCALPHA) ? GL_RGBA : GL_RGB))); 504 PUSHs (sv_2mortal (newSViv (surface->flags & (SDL_SRCCOLORKEY | SDL_SRCALPHA) ? GL_RGBA : GL_RGB)));
254 PUSHs (sv_2mortal (newSViv (GL_RGBA))); 505 PUSHs (sv_2mortal (newSViv (GL_RGBA)));
255 PUSHs (sv_2mortal (newSViv (GL_UNSIGNED_INT_8_8_8_8_REV))); 506 PUSHs (sv_2mortal (newSViv (GL_UNSIGNED_BYTE)));
256 507
257 SDL_FreeSurface (surface); 508 SDL_FreeSurface (surface);
258 SDL_FreeSurface (surface2); 509 SDL_FreeSurface (surface2);
259} 510}
260 511
261void 512void
513average (int x, int y, uint32_t *data)
514 PPCODE:
515{
516 uint32_t r = 0, g = 0, b = 0, a = 0;
517
518 x = y = x * y;
519
520 while (x--)
521 {
522 uint32_t p = *data++;
523
524 r += (p ) & 255;
525 g += (p >> 8) & 255;
526 b += (p >> 16) & 255;
527 a += (p >> 24) & 255;
528 }
529
530 EXTEND (SP, 4);
531 PUSHs (sv_2mortal (newSViv (r / y)));
532 PUSHs (sv_2mortal (newSViv (g / y)));
533 PUSHs (sv_2mortal (newSViv (b / y)));
534 PUSHs (sv_2mortal (newSViv (a / y)));
535}
536
537void
262fatal (char *message) 538fatal (char *message)
263 CODE: 539 CODE:
264#ifdef WIN32 540#ifdef _WIN32
265 MessageBox (0, message, "Crossfire+ Fatal Error", MB_OK | MB_ICONERROR | MB_SETFOREGROUND); 541 MessageBox (0, message, "Crossfire+ Fatal Error", MB_OK | MB_ICONERROR | MB_SETFOREGROUND);
266#else 542#else
267 fprintf (stderr, "%s\n", message); 543 fprintf (stderr, "FATAL: %s\n", message);
268#endif 544#endif
269 exit (1); 545 exit (1);
270 546
271MODULE = CFClient PACKAGE = CFClient::Layout 547MODULE = CFClient PACKAGE = CFClient::Layout
272 548
304 char *text = SvPVutf8 (text_, textlen); 580 char *text = SvPVutf8 (text_, textlen);
305 581
306 pango_layout_set_markup (self->pl, text, textlen); 582 pango_layout_set_markup (self->pl, text, textlen);
307} 583}
308 584
585SV *
586get_text (CFClient::Layout self)
587 CODE:
588 RETVAL = newSVpv (pango_layout_get_text (self->pl), 0);
589 SvUTF8_on (RETVAL);
590 OUTPUT:
591 RETVAL
592
309void 593void
310set_height (CFClient::Layout self, int base_height) 594set_height (CFClient::Layout self, int base_height)
311 CODE: 595 CODE:
312 self->base_height = base_height; 596 self->base_height = base_height;
313 597
391} 675}
392 676
393MODULE = CFClient PACKAGE = CFClient::Texture 677MODULE = CFClient PACKAGE = CFClient::Texture
394 678
395void 679void
396draw_quad (SV *self, double x, double y, double w = 0, double h = 0) 680draw_quad (SV *self, float x, float y, float w = 0, float h = 0)
397 PROTOTYPE: $$$;$$ 681 PROTOTYPE: $$$;$$
398 CODE: 682 CODE:
399{ 683{
400 HV *hv = (HV *)SvRV (self); 684 HV *hv = (HV *)SvRV (self);
401 double s = SvNV (*hv_fetch (hv, "s", 1, 1)); 685 float s = SvNV (*hv_fetch (hv, "s", 1, 1));
402 double t = SvNV (*hv_fetch (hv, "t", 1, 1)); 686 float t = SvNV (*hv_fetch (hv, "t", 1, 1));
403 int name = SvIV (*hv_fetch (hv, "name", 4, 1)); 687 int name = SvIV (*hv_fetch (hv, "name", 4, 1));
688 int wrap_mode = SvIV (*hv_fetch (hv, "wrap_mode", 9, 1));
404 689
405 if (items < 5) 690 if (items < 5)
406 { 691 {
407 w = SvNV (*hv_fetch (hv, "w", 1, 1)); 692 w = SvNV (*hv_fetch (hv, "w", 1, 1));
408 h = SvNV (*hv_fetch (hv, "h", 1, 1)); 693 h = SvNV (*hv_fetch (hv, "h", 1, 1));
409 } 694 }
410 695
411 glBindTexture (GL_TEXTURE_2D, name); 696 glBindTexture (GL_TEXTURE_2D, name);
697 if (wrap_mode) {
698 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
699 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
700 }
412 glBegin (GL_QUADS); 701 glBegin (GL_QUADS);
413 glTexCoord2d (0, 0); glVertex2d (x , y ); 702 glTexCoord2f (0, 0); glVertex2f (x , y );
414 glTexCoord2d (0, t); glVertex2d (x , y + h); 703 glTexCoord2f (0, t); glVertex2f (x , y + h);
415 glTexCoord2d (s, t); glVertex2d (x + w, y + h); 704 glTexCoord2f (s, t); glVertex2f (x + w, y + h);
416 glTexCoord2d (s, 0); glVertex2d (x + w, y ); 705 glTexCoord2f (s, 0); glVertex2f (x + w, y );
417 glEnd (); 706 glEnd ();
418} 707}
419 708
420MODULE = CFClient PACKAGE = CFClient::Map 709MODULE = CFClient PACKAGE = CFClient::Map
421 710
422CFClient::Map 711CFClient::Map
423new (SV *class, int map_width, int map_height) 712new (SV *class, int map_width, int map_height)
424 CODE: 713 CODE:
425 New (0, RETVAL, 1, struct map); 714 New (0, RETVAL, 1, struct map);
426 RETVAL->x = 0; 715 RETVAL->x = 0;
427 RETVAL->y = 0; 716 RETVAL->y = 0;
428 RETVAL->w = map_width; 717 RETVAL->w = map_width;
429 RETVAL->h = map_height; 718 RETVAL->h = map_height;
430 RETVAL->vx = 0; 719 RETVAL->ox = 0;
431 RETVAL->vy = 0; 720 RETVAL->oy = 0;
432 RETVAL->faces = 8192; 721 RETVAL->faces = 8192;
433 Newz (0, RETVAL->face, RETVAL->faces, mapface); 722 Newz (0, RETVAL->face, RETVAL->faces, mapface);
723 RETVAL->texs = 8192;
724 Newz (0, RETVAL->tex, RETVAL->texs, maptex);
434 RETVAL->rows = 0; 725 RETVAL->rows = 0;
435 RETVAL->row = 0; 726 RETVAL->row = 0;
436 OUTPUT: 727 OUTPUT:
437 RETVAL 728 RETVAL
438 729
449clear (CFClient::Map self) 740clear (CFClient::Map self)
450 CODE: 741 CODE:
451 map_clear (self); 742 map_clear (self);
452 743
453void 744void
454set_texture (CFClient::Map self, int face, int name, int w, int h, float s, float t) 745set_face (CFClient::Map self, int face, int texid)
455 CODE: 746 CODE:
456{ 747{
457 while (self->faces < face) 748 while (self->faces <= face)
458 { 749 {
459 Append (mapface, self->face, self->faces, self->faces); 750 Append (mapface, self->face, self->faces, self->faces);
460 self->faces *= 2; 751 self->faces *= 2;
461 } 752 }
462 753
463 self->face [face].name = name;
464 self->face [face].w = w; 754 self->face [face] = texid;
465 self->face [face].h = h;
466 self->face [face].s = s;
467 self->face [face].t = t;
468} 755}
756
757void
758set_texture (CFClient::Map self, int texid, int name, int w, int h, float s, float t, int r, int g, int b, int a)
759 CODE:
760{
761 while (self->texs <= texid)
762 {
763 Append (maptex, self->tex, self->texs, self->texs);
764 self->texs *= 2;
765 }
766
767 {
768 maptex *tex = self->tex + texid;
769
770 tex->name = name;
771 tex->w = w;
772 tex->h = h;
773 tex->s = s;
774 tex->t = t;
775 tex->r = r;
776 tex->g = g;
777 tex->b = b;
778 tex->a = a;
779 }
780}
781
782int
783ox (CFClient::Map self)
784 ALIAS:
785 oy = 1
786 CODE:
787 switch (ix)
788 {
789 case 0: RETVAL = self->ox; break;
790 case 1: RETVAL = self->oy; break;
791 }
792 OUTPUT:
793 RETVAL
469 794
470void 795void
471scroll (CFClient::Map self, int dx, int dy) 796scroll (CFClient::Map self, int dx, int dy)
472 CODE: 797 CODE:
473{ 798{
479 if (dy > 0) 804 if (dy > 0)
480 map_blank (self, self->x, self->y, self->w, dy - 1); 805 map_blank (self, self->x, self->y, self->w, dy - 1);
481 else if (dy < 0) 806 else if (dy < 0)
482 map_blank (self, self->x, self->y + self->h + dy + 1, self->w, 1 - dy); 807 map_blank (self, self->x, self->y + self->h + dy + 1, self->w, 1 - dy);
483 808
484 self->x += dx; 809 self->ox += dx; self->x += dx;
485 self->y += dy; 810 self->oy += dy; self->y += dy;
486 811
487 while (self->y < 0) 812 while (self->y < 0)
488 { 813 {
489 Prepend (maprow, self->row, self->rows, MAP_EXTEND_Y); 814 Prepend (maprow, self->row, self->rows, MAP_EXTEND_Y);
490 815
497map1a_update (CFClient::Map self, SV *data_) 822map1a_update (CFClient::Map self, SV *data_)
498 CODE: 823 CODE:
499{ 824{
500 uint8_t *data = (uint8_t *)SvPVbyte_nolen (data_); 825 uint8_t *data = (uint8_t *)SvPVbyte_nolen (data_);
501 uint8_t *data_end = (uint8_t *)SvEND (data_); 826 uint8_t *data_end = (uint8_t *)SvEND (data_);
502 827 mapcell *cell;
828 int x, y, flags;
829
503 while (data < data_end) 830 while (data < data_end)
504 { 831 {
505 int flags = (data [0] << 8) + data [1]; data += 2; 832 flags = (data [0] << 8) + data [1]; data += 2;
506 833
507 int x = ((flags >> 10) & 63) + self->x; 834 x = ((flags >> 10) & 63) + self->x;
508 int y = ((flags >> 4) & 63) + self->y; 835 y = ((flags >> 4) & 63) + self->y;
509 836
510 while (y >= self->rows) 837 cell = map_get_cell (self, x, y);
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 838
545 if (flags & 15) 839 if (flags & 15)
546 { 840 {
547 if (cell->darkness < 0 && x < self->w && y < self->h) 841 if (cell->darkness < 0)
548 { 842 {
549 cell->darkness = 0; 843 cell->darkness = 0;
550 cell->face [0] = 0; 844 cell->face [0] = 0;
551 cell->face [1] = 0; 845 cell->face [1] = 0;
552 cell->face [2] = 0; 846 cell->face [2] = 0;
553 } 847 }
554 848
555 cell->darkness = flags & 8 ? *data++ : 255; 849 cell->darkness = flags & 8 ? *data++ : 255;
850
851 //TODO: don't trust server data to be in-range(!)
556 852
557 if (flags & 4) 853 if (flags & 4)
558 { 854 {
559 cell->face [0] = (data [0] << 8) + data [1]; data += 2; 855 cell->face [0] = self->face [(data [0] << 8) + data [1]]; data += 2;
560 } 856 }
561 857
562 if (flags & 2) 858 if (flags & 2)
563 { 859 {
564 cell->face [1] = (data [0] << 8) + data [1]; data += 2; 860 cell->face [1] = self->face [(data [0] << 8) + data [1]]; data += 2;
565 } 861 }
566 862
567 if (flags & 1) 863 if (flags & 1)
568 { 864 {
569 cell->face [2] = (data [0] << 8) + data [1]; data += 2; 865 cell->face [2] = self->face [(data [0] << 8) + data [1]]; data += 2;
570 } 866 }
571 } 867 }
572 else 868 else
573 cell->darkness = -1; 869 cell->darkness = -1;
574 } 870 }
575} 871}
576 872
873SV *
874mapmap (CFClient::Map self, int w, int h)
875 CODE:
876{
877 int x0, x1, x;
878 int y0, y1, y;
879 int z;
880 SV *map_sv = newSV (w * h * sizeof (uint32_t));
881 uint32_t *map = (uint32_t *)SvPVX (map_sv);
882
883 SvPOK_only (map_sv);
884 SvCUR_set (map_sv, w * h * sizeof (uint32_t));
885
886 x0 = self->x - w / 2; x1 = x0 + w;
887 y0 = self->y - h / 2; y1 = y0 + h;
888
889 for (y = y0; y < y1; y++)
890 {
891 maprow *row = 0 <= y && y < self->rows
892 ? self->row + y
893 : 0;
894
895 for (x = x0; x < x1; x++)
896 {
897 int r = 32, g = 32, b = 32, a = 192;
898
899 if (row && row->c0 <= x && x < row->c1)
900 {
901 mapcell *cell = row->col + (x - row->c0);
902
903 for (z = 0; z <= 0; z++)
904 {
905 mapface face = cell->face [z];
906
907 if (face)
908 {
909 maptex tex = self->tex [face];
910 int a0 = 255 - tex.a;
911 int a1 = tex.a;
912
913 r = (r * a0 + tex.r * a1) / 255;
914 g = (g * a0 + tex.g * a1) / 255;
915 b = (b * a0 + tex.b * a1) / 255;
916 a = (a * a0 + tex.a * a1) / 255;
917 }
918 }
919 }
920
921 *map++ = (r )
922 | (g << 8)
923 | (b << 16)
924 | (a << 24);
925 }
926 }
927
928 RETVAL = map_sv;
929}
930 OUTPUT:
931 RETVAL
932
577void 933void
578draw (CFClient::Map self, int x0, int y0, int sw, int sh) 934draw (CFClient::Map self, int shift_x, int shift_y, int x0, int y0, int sw, int sh)
579 PPCODE: 935 PPCODE:
580{ 936{
937 int vx, vy;
938 int x, y, z;
939 int last_name;
940 mapface face;
581 int sw4 = (sw + 3) & ~3; 941 int sw4 = (sw + 3) & ~3;
582 SV *darkness_sv = sv_2mortal (newSV (sw4 * sh)); 942 SV *darkness_sv = sv_2mortal (newSV (sw4 * sh));
583 uint8_t *darkness = (uint8_t *)SvPVX (darkness_sv); 943 uint8_t *darkness = (uint8_t *)SvPVX (darkness_sv);
584 944
945 memset (darkness, 255, sw4 * sh);
585 SvPOK_only (darkness_sv); 946 SvPOK_only (darkness_sv);
586 SvCUR_set (darkness_sv, sw4 * sh); 947 SvCUR_set (darkness_sv, sw4 * sh);
587 948
949 vx = self->x + (self->w - sw) / 2 - shift_x;
950 vy = self->y + (self->h - sh) / 2 - shift_y;
951
952 /*
588 int vx = self->vx = self->w >= sw 953 int vx = self->vx = self->w >= sw
589 ? self->x + (self->w - sw) / 2 954 ? self->x + (self->w - sw) / 2
590 : MIN (self->x, MAX (self->x + self->w - sw + 1, self->vx)); 955 : MIN (self->x, MAX (self->x + self->w - sw + 1, self->vx));
591 956
592 int vy = self->vy = self->h >= sh 957 int vy = self->vy = self->h >= sh
593 ? self->y + (self->h - sh) / 2 958 ? self->y + (self->h - sh) / 2
594 : MIN (self->y, MAX (self->y + self->h - sh + 1, self->vy)); 959 : MIN (self->y, MAX (self->y + self->h - sh + 1, self->vy));
960 */
595 961
596 glColor4ub (255, 255, 255, 255); 962 glColor4ub (255, 255, 255, 255);
597 963
598 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 964 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
599 glEnable (GL_BLEND); 965 glEnable (GL_BLEND);
600 glEnable (GL_TEXTURE_2D); 966 glEnable (GL_TEXTURE_2D);
601 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); 967 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
602 968
603 int x, y, z;
604
605 int last_name = 0;
606
607 glBegin (GL_QUADS); 969 glBegin (GL_QUADS);
970
971 last_name = 0;
608 972
609 for (z = 0; z < 3; z++) 973 for (z = 0; z < 3; z++)
610 for (y = 0; y < sh; y++) 974 for (y = 0; y < sh; y++)
611 if (0 <= y + vy && y + vy < self->rows) 975 if (0 <= y + vy && y + vy < self->rows)
612 { 976 {
619 983
620 darkness[y * sw4 + x] = cell->darkness < 0 984 darkness[y * sw4 + x] = cell->darkness < 0
621 ? 255 - FOW_DARKNESS 985 ? 255 - FOW_DARKNESS
622 : 255 - cell->darkness; 986 : 255 - cell->darkness;
623 987
624 uint16_t face = cell->face [z]; 988 face = cell->face [z];
625 989
626 if (face) 990 if (face)
627 { 991 {
628 mapface tex = self->face [face]; 992 maptex tex = self->tex [face];
629 993
630 int px = (x + 1) * 32 - tex.w; 994 int px = (x + 1) * 32 - tex.w;
631 int py = (y + 1) * 32 - tex.h; 995 int py = (y + 1) * 32 - tex.h;
632 996
633 if (last_name != tex.name) 997 if (last_name != tex.name)
655 PUSHs (sv_2mortal (newSViv (sw4))); 1019 PUSHs (sv_2mortal (newSViv (sw4)));
656 PUSHs (sv_2mortal (newSViv (sh))); 1020 PUSHs (sv_2mortal (newSViv (sh)));
657 PUSHs (darkness_sv); 1021 PUSHs (darkness_sv);
658} 1022}
659 1023
1024SV *
1025get_rect (CFClient::Map self, int x0, int y0, int w, int h)
1026 CODE:
1027{
1028 int x, y, x1, y1;
1029 SV *data_sv = newSV (w * h * 7 + 5);
1030 uint8_t *data = (uint8_t *)SvPVX (data_sv);
1031
1032 *data++ = 0; /* version 0 format */
1033 *data++ = w >> 8; *data++ = w;
1034 *data++ = h >> 8; *data++ = h;
1035
1036 // we need to do this 'cause we don't keep an absolute coord system for rows
1037 // TODO: treat rows as we treat
1038 map_get_row (self, y0 + self->y - self->oy);//D
1039 map_get_row (self, y0 + self->y - self->oy + h - 1);//D
1040
1041 x0 += self->x - self->ox;
1042 y0 += self->y - self->oy;
1043
1044 x1 = x0 + w;
1045 y1 = y0 + h;
1046
1047 for (y = y0; y < y1; y++)
1048 {
1049 maprow *row = 0 <= y && y < self->rows
1050 ? self->row + y
1051 : 0;
1052
1053 for (x = x0; x < x1; x++)
1054 {
1055 if (row && row->c0 <= x && x < row->c1)
1056 {
1057 mapcell *cell = row->col + (x - row->c0);
1058 uint8_t flags = 0;
1059
1060 if (cell->face [0]) flags |= 1;
1061 if (cell->face [1]) flags |= 2;
1062 if (cell->face [2]) flags |= 4;
1063
1064 *data++ = flags;
1065
1066 if (flags & 1)
1067 {
1068 *data++ = cell->face [0] >> 8;
1069 *data++ = cell->face [0];
1070 }
1071
1072 if (flags & 2)
1073 {
1074 *data++ = cell->face [1] >> 8;
1075 *data++ = cell->face [1];
1076 }
1077
1078 if (flags & 4)
1079 {
1080 *data++ = cell->face [2] >> 8;
1081 *data++ = cell->face [2];
1082 }
1083 }
1084 else
1085 *data++ = 0;
1086 }
1087 }
1088
1089 SvPOK_only (data_sv);
1090 SvCUR_set (data_sv, data - (uint8_t *)SvPVX (data_sv));
1091 RETVAL = data_sv;
1092}
1093 OUTPUT:
1094 RETVAL
1095
1096void
1097set_rect (CFClient::Map self, int x0, int y0, uint8_t *data)
1098 PPCODE:
1099{
1100 int x, y, z;
1101 int w, h;
1102 int x1, y1;
1103
1104 if (*data++ != 0)
1105 return; /* version mismatch */
1106
1107 w = *data++ << 8; w |= *data++;
1108 h = *data++ << 8; h |= *data++;
1109
1110 // we need to do this 'cause we don't keep an absolute coord system for rows
1111 // TODO: treat rows as we treat
1112 map_get_row (self, y0 + self->y - self->oy);//D
1113 map_get_row (self, y0 + self->y - self->oy + h - 1);//D
1114
1115 x0 += self->x - self->ox;
1116 y0 += self->y - self->oy;
1117
1118 x1 = x0 + w;
1119 y1 = y0 + h;
1120
1121 for (y = y0; y < y1; y++)
1122 {
1123 maprow *row = map_get_row (self, y);
1124
1125 for (x = x0; x < x1; x++)
1126 {
1127 uint8_t flags = *data++;
1128
1129 if (flags)
1130 {
1131 mapface face[3] = { 0, 0, 0 };
1132
1133 mapcell *cell = row_get_cell (row, x);
1134
1135 if (flags & 1) { face[0] = *data++ << 8; face[0] |= *data++; }
1136 if (flags & 2) { face[1] = *data++ << 8; face[1] |= *data++; }
1137 if (flags & 4) { face[2] = *data++ << 8; face[2] |= *data++; }
1138
1139 if (cell->darkness <= 0)
1140 {
1141 cell->darkness = -1;
1142
1143 for (z = 0; z <= 2; z++)
1144 {
1145 cell->face[z] = face[z];
1146
1147 if (face[z] && (face[z] >= self->texs || !self->tex[face [z]].name))
1148 XPUSHs (sv_2mortal (newSViv (face[z])));
1149 }
1150 }
1151 }
1152 }
1153 }
1154}
1155

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines