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; |
24 | int base_height; |
35 | int base_height; |
25 | } *Crossfire__Client__Layout; |
36 | } *CFClient__Layout; |
26 | |
37 | |
27 | static void |
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 |
28 | layout_get_pixel_size (Crossfire__Client__Layout self, int *w, int *h) |
56 | layout_get_pixel_size (CFClient__Layout self, int *w, int *h) |
29 | { |
57 | { |
|
|
58 | layout_update (self); |
|
|
59 | |
30 | pango_layout_get_pixel_size (self->pl, w, h); |
60 | pango_layout_get_pixel_size (self->pl, w, h); |
31 | |
61 | |
32 | *w = (*w + 3) & ~3; |
62 | *w = (*w + 3) & ~3; |
33 | if (!*w) *w = 1; |
63 | if (!*w) *w = 1; |
34 | if (!*h) *h = 1; |
64 | if (!*h) *h = 1; |
35 | } |
65 | } |
36 | |
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 | |
37 | MODULE = Crossfire::Client PACKAGE = Crossfire::Client |
160 | MODULE = CFClient PACKAGE = CFClient |
38 | |
161 | |
39 | PROTOTYPES: ENABLE |
162 | PROTOTYPES: ENABLE |
40 | |
163 | |
41 | BOOT: |
164 | BOOT: |
42 | { |
165 | { |
43 | fontmap = pango_ft2_font_map_new (); |
166 | fontmap = pango_ft2_font_map_new (); |
|
|
167 | pango_ft2_font_map_set_default_substitute ((PangoFT2FontMap *)fontmap, substitute_func, 0, 0); |
44 | context = pango_ft2_font_map_create_context ((PangoFT2FontMap *)fontmap); |
168 | context = pango_ft2_font_map_create_context ((PangoFT2FontMap *)fontmap); |
45 | } |
169 | } |
46 | |
170 | |
47 | void |
171 | void |
48 | lowdelay (int fd, int val = 1) |
172 | lowdelay (int fd, int val = 1) |
… | |
… | |
77 | PangoFontDescription *font = pango_fc_font_description_from_pattern (pattern, 0); |
201 | PangoFontDescription *font = pango_fc_font_description_from_pattern (pattern, 0); |
78 | FcPatternDestroy (pattern); |
202 | FcPatternDestroy (pattern); |
79 | pango_context_set_font_description (context, font); |
203 | pango_context_set_font_description (context, font); |
80 | } |
204 | } |
81 | |
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); |
|
|
270 | |
82 | MODULE = Crossfire::Client PACKAGE = Crossfire::Client::Layout |
271 | MODULE = CFClient PACKAGE = CFClient::Layout |
83 | |
272 | |
84 | Crossfire::Client::Layout |
273 | CFClient::Layout |
85 | new (SV *class, int base_height = 10) |
274 | new (SV *class, int base_height = 10) |
86 | CODE: |
275 | CODE: |
87 | New (0, RETVAL, 1, struct cf_layout); |
276 | New (0, RETVAL, 1, struct cf_layout); |
88 | RETVAL->base_height = base_height; |
277 | RETVAL->base_height = base_height; |
89 | RETVAL->pl = pango_layout_new (context); |
278 | RETVAL->pl = pango_layout_new (context); |
90 | pango_layout_set_wrap (RETVAL->pl, PANGO_WRAP_WORD_CHAR); |
279 | pango_layout_set_wrap (RETVAL->pl, PANGO_WRAP_WORD_CHAR); |
91 | OUTPUT: |
280 | OUTPUT: |
92 | RETVAL |
281 | RETVAL |
93 | |
282 | |
94 | void |
283 | void |
95 | DESTROY (Crossfire::Client::Layout self) |
284 | DESTROY (CFClient::Layout self) |
96 | CODE: |
285 | CODE: |
97 | g_object_unref (self->pl); |
286 | g_object_unref (self->pl); |
98 | Safefree (self); |
287 | Safefree (self); |
99 | |
288 | |
100 | void |
289 | void |
101 | set_markup (Crossfire::Client::Layout self, SV *text_) |
290 | set_text (CFClient::Layout self, SV *text_) |
102 | CODE: |
291 | CODE: |
103 | { |
292 | { |
104 | STRLEN textlen; |
293 | STRLEN textlen; |
105 | char *text = SvPVutf8 (text_, textlen); |
294 | char *text = SvPVutf8 (text_, textlen); |
106 | |
295 | |
|
|
296 | pango_layout_set_text (self->pl, text, textlen); |
|
|
297 | } |
|
|
298 | |
|
|
299 | void |
|
|
300 | set_markup (CFClient::Layout self, SV *text_) |
|
|
301 | CODE: |
|
|
302 | { |
|
|
303 | STRLEN textlen; |
|
|
304 | char *text = SvPVutf8 (text_, textlen); |
|
|
305 | |
107 | pango_layout_set_markup (self->pl, text, textlen); |
306 | pango_layout_set_markup (self->pl, text, textlen); |
108 | } |
307 | } |
109 | |
308 | |
110 | void |
309 | void |
|
|
310 | set_height (CFClient::Layout self, int base_height) |
|
|
311 | CODE: |
|
|
312 | self->base_height = base_height; |
|
|
313 | |
|
|
314 | void |
111 | set_width (Crossfire::Client::Layout self, int max_width = -1) |
315 | set_width (CFClient::Layout self, int max_width = -1) |
112 | CODE: |
316 | CODE: |
113 | pango_layout_set_width (self->pl, max_width < 0 ? max_width : max_width * PANGO_SCALE); |
317 | pango_layout_set_width (self->pl, max_width < 0 ? max_width : max_width * PANGO_SCALE); |
114 | |
318 | |
115 | void |
319 | void |
116 | size (Crossfire::Client::Layout self) |
320 | size (CFClient::Layout self) |
117 | PPCODE: |
321 | PPCODE: |
118 | { |
322 | { |
119 | int w, h; |
323 | int w, h; |
120 | |
324 | |
121 | PangoFontDescription *font = pango_context_get_font_description (context); |
325 | layout_update (self); |
122 | pango_font_description_set_absolute_size (font, self->base_height * PANGO_SCALE); |
|
|
123 | |
|
|
124 | layout_get_pixel_size (self, &w, &h); |
326 | layout_get_pixel_size (self, &w, &h); |
125 | |
327 | |
126 | EXTEND (SP, 2); |
328 | EXTEND (SP, 2); |
127 | PUSHs (sv_2mortal (newSViv (w))); |
329 | PUSHs (sv_2mortal (newSViv (w))); |
128 | PUSHs (sv_2mortal (newSViv (h))); |
330 | PUSHs (sv_2mortal (newSViv (h))); |
129 | } |
331 | } |
130 | |
332 | |
|
|
333 | int |
|
|
334 | xy_to_index (CFClient::Layout self, int x, int y) |
|
|
335 | CODE: |
|
|
336 | { |
|
|
337 | int index, trailing; |
|
|
338 | |
|
|
339 | layout_update (self); |
|
|
340 | pango_layout_xy_to_index (self->pl, x * PANGO_SCALE, y * PANGO_SCALE, &index, &trailing); |
|
|
341 | |
|
|
342 | RETVAL = index; |
|
|
343 | } |
|
|
344 | OUTPUT: |
|
|
345 | RETVAL |
|
|
346 | |
131 | void |
347 | void |
|
|
348 | cursor_pos (CFClient::Layout self, int index) |
|
|
349 | PPCODE: |
|
|
350 | { |
|
|
351 | PangoRectangle strong_pos; |
|
|
352 | layout_update (self); |
|
|
353 | pango_layout_get_cursor_pos (self->pl, index, &strong_pos, 0); |
|
|
354 | |
|
|
355 | EXTEND (SP, 3); |
|
|
356 | PUSHs (sv_2mortal (newSViv (strong_pos.x / PANGO_SCALE))); |
|
|
357 | PUSHs (sv_2mortal (newSViv (strong_pos.y / PANGO_SCALE))); |
|
|
358 | PUSHs (sv_2mortal (newSViv (strong_pos.height / PANGO_SCALE))); |
|
|
359 | } |
|
|
360 | |
|
|
361 | void |
132 | render (Crossfire::Client::Layout self) |
362 | render (CFClient::Layout self) |
133 | PPCODE: |
363 | PPCODE: |
134 | { |
364 | { |
135 | SV *retval; |
365 | SV *retval; |
136 | int w, h; |
366 | int w, h; |
137 | FT_Bitmap bitmap; |
367 | FT_Bitmap bitmap; |
138 | |
368 | |
|
|
369 | layout_update (self); |
139 | layout_get_pixel_size (self, &w, &h); |
370 | layout_get_pixel_size (self, &w, &h); |
140 | |
371 | |
141 | retval = newSV (w * h); |
372 | retval = newSV (w * h); |
142 | SvPOK_only (retval); |
373 | SvPOK_only (retval); |
143 | SvCUR_set (retval, w * h); |
374 | SvCUR_set (retval, w * h); |
… | |
… | |
157 | PUSHs (sv_2mortal (newSViv (w))); |
388 | PUSHs (sv_2mortal (newSViv (w))); |
158 | PUSHs (sv_2mortal (newSViv (h))); |
389 | PUSHs (sv_2mortal (newSViv (h))); |
159 | PUSHs (sv_2mortal (retval)); |
390 | PUSHs (sv_2mortal (retval)); |
160 | } |
391 | } |
161 | |
392 | |
162 | MODULE = Crossfire::Client PACKAGE = Crossfire::Client::Texture |
393 | MODULE = CFClient PACKAGE = CFClient::Texture |
163 | |
394 | |
164 | void |
395 | void |
165 | draw_quad (SV *self, double x, double y, double w = 0, double h = 0) |
396 | draw_quad (SV *self, double x, double y, double w = 0, double h = 0) |
166 | PROTOTYPE: $$$;$$ |
397 | PROTOTYPE: $$$;$$ |
167 | CODE: |
398 | CODE: |
168 | { |
399 | { |
169 | HV *hv = (HV *)SvRV (self); |
400 | HV *hv = (HV *)SvRV (self); |
170 | double s = SvNV (*hv_fetch (hv, "s", 1, 1)); |
401 | double s = SvNV (*hv_fetch (hv, "s", 1, 1)); |
171 | double t = SvNV (*hv_fetch (hv, "t", 1, 1)); |
402 | double t = SvNV (*hv_fetch (hv, "t", 1, 1)); |
172 | int name = SvIV (*hv_fetch (hv, "name", 4, 1)); |
403 | int name = SvIV (*hv_fetch (hv, "name", 4, 1)); |
|
|
404 | int wrap_mode = SvIV (*hv_fetch (hv, "wrap_mode", 9, 1)); |
173 | |
405 | |
174 | if (items < 5) |
406 | if (items < 5) |
175 | { |
407 | { |
176 | w = SvNV (*hv_fetch (hv, "width", 5, 1)); |
408 | w = SvNV (*hv_fetch (hv, "w", 1, 1)); |
177 | h = SvNV (*hv_fetch (hv, "height", 6, 1)); |
409 | h = SvNV (*hv_fetch (hv, "h", 1, 1)); |
178 | } |
410 | } |
179 | |
411 | |
180 | glBindTexture (GL_TEXTURE_2D, name); |
412 | glBindTexture (GL_TEXTURE_2D, name); |
|
|
413 | if (wrap_mode) { |
|
|
414 | glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); |
|
|
415 | glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); |
|
|
416 | } |
181 | glBegin (GL_QUADS); |
417 | glBegin (GL_QUADS); |
182 | glTexCoord2d (0, 0); glVertex2d (x , y ); |
418 | glTexCoord2d (0, 0); glVertex2d (x , y ); |
183 | glTexCoord2d (0, t); glVertex2d (x , y + h); |
419 | glTexCoord2d (0, t); glVertex2d (x , y + h); |
184 | glTexCoord2d (s, t); glVertex2d (x + w, y + h); |
420 | glTexCoord2d (s, t); glVertex2d (x + w, y + h); |
185 | glTexCoord2d (s, 0); glVertex2d (x + w, y ); |
421 | glTexCoord2d (s, 0); glVertex2d (x + w, y ); |
186 | glEnd (); |
422 | glEnd (); |
187 | } |
423 | } |
|
|
424 | |
|
|
425 | MODULE = CFClient PACKAGE = CFClient::Map |
|
|
426 | |
|
|
427 | CFClient::Map |
|
|
428 | new (SV *class, int map_width, int map_height) |
|
|
429 | CODE: |
|
|
430 | New (0, RETVAL, 1, struct map); |
|
|
431 | RETVAL->x = 0; |
|
|
432 | RETVAL->y = 0; |
|
|
433 | RETVAL->w = map_width; |
|
|
434 | RETVAL->h = map_height; |
|
|
435 | RETVAL->vx = 0; |
|
|
436 | RETVAL->vy = 0; |
|
|
437 | RETVAL->faces = 8192; |
|
|
438 | Newz (0, RETVAL->face, RETVAL->faces, mapface); |
|
|
439 | RETVAL->rows = 0; |
|
|
440 | RETVAL->row = 0; |
|
|
441 | OUTPUT: |
|
|
442 | RETVAL |
|
|
443 | |
|
|
444 | void |
|
|
445 | DESTROY (CFClient::Map self) |
|
|
446 | CODE: |
|
|
447 | { |
|
|
448 | map_clear (self); |
|
|
449 | Safefree (self->face); |
|
|
450 | Safefree (self); |
|
|
451 | } |
|
|
452 | |
|
|
453 | void |
|
|
454 | clear (CFClient::Map self) |
|
|
455 | CODE: |
|
|
456 | map_clear (self); |
|
|
457 | |
|
|
458 | void |
|
|
459 | set_texture (CFClient::Map self, int face, int name, int w, int h, float s, float t) |
|
|
460 | CODE: |
|
|
461 | { |
|
|
462 | while (self->faces < face) |
|
|
463 | { |
|
|
464 | Append (mapface, self->face, self->faces, self->faces); |
|
|
465 | self->faces *= 2; |
|
|
466 | } |
|
|
467 | |
|
|
468 | self->face [face].name = name; |
|
|
469 | self->face [face].w = w; |
|
|
470 | self->face [face].h = h; |
|
|
471 | self->face [face].s = s; |
|
|
472 | self->face [face].t = t; |
|
|
473 | } |
|
|
474 | |
|
|
475 | void |
|
|
476 | scroll (CFClient::Map self, int dx, int dy) |
|
|
477 | CODE: |
|
|
478 | { |
|
|
479 | if (dx > 0) |
|
|
480 | map_blank (self, self->x, self->y, dx - 1, self->h); |
|
|
481 | else if (dx < 0) |
|
|
482 | map_blank (self, self->x + self->w + dx + 1, self->y, 1 - dx, self->h); |
|
|
483 | |
|
|
484 | if (dy > 0) |
|
|
485 | map_blank (self, self->x, self->y, self->w, dy - 1); |
|
|
486 | else if (dy < 0) |
|
|
487 | map_blank (self, self->x, self->y + self->h + dy + 1, self->w, 1 - dy); |
|
|
488 | |
|
|
489 | self->x += dx; |
|
|
490 | self->y += dy; |
|
|
491 | |
|
|
492 | while (self->y < 0) |
|
|
493 | { |
|
|
494 | Prepend (maprow, self->row, self->rows, MAP_EXTEND_Y); |
|
|
495 | |
|
|
496 | self->rows += MAP_EXTEND_Y; |
|
|
497 | self->y += MAP_EXTEND_Y; |
|
|
498 | } |
|
|
499 | } |
|
|
500 | |
|
|
501 | void |
|
|
502 | map1a_update (CFClient::Map self, SV *data_) |
|
|
503 | CODE: |
|
|
504 | { |
|
|
505 | uint8_t *data = (uint8_t *)SvPVbyte_nolen (data_); |
|
|
506 | uint8_t *data_end = (uint8_t *)SvEND (data_); |
|
|
507 | |
|
|
508 | while (data < data_end) |
|
|
509 | { |
|
|
510 | int flags = (data [0] << 8) + data [1]; data += 2; |
|
|
511 | |
|
|
512 | int x = ((flags >> 10) & 63) + self->x; |
|
|
513 | int y = ((flags >> 4) & 63) + self->y; |
|
|
514 | |
|
|
515 | while (y >= self->rows) |
|
|
516 | { |
|
|
517 | Prepend (maprow, self->row, self->rows, MAP_EXTEND_Y); |
|
|
518 | |
|
|
519 | self->rows += MAP_EXTEND_Y; |
|
|
520 | } |
|
|
521 | |
|
|
522 | maprow *row = self->row + y; |
|
|
523 | |
|
|
524 | if (!row->col) |
|
|
525 | { |
|
|
526 | Newz (0, row->col, MAP_EXTEND_X, mapcell); |
|
|
527 | row->c0 = self->x - MAP_EXTEND_X / 4; |
|
|
528 | row->c1 = row->c0 + MAP_EXTEND_X; |
|
|
529 | } |
|
|
530 | |
|
|
531 | if (row->c0 > x) |
|
|
532 | { |
|
|
533 | int extend = row->c0 - x + MAP_EXTEND_X; |
|
|
534 | Prepend (mapcell, row->col, row->c1 - row->c0, extend); |
|
|
535 | row->c0 -= extend; |
|
|
536 | } |
|
|
537 | else if (x >= row->c1) |
|
|
538 | { |
|
|
539 | int extend = x - row->c1 + MAP_EXTEND_X; |
|
|
540 | Append (mapcell, row->col, row->c1 - row->c0, extend); |
|
|
541 | row->c1 += extend; |
|
|
542 | } |
|
|
543 | |
|
|
544 | assert (y >= 0); |
|
|
545 | assert (y < self->rows); |
|
|
546 | assert (x >= row->c0); |
|
|
547 | assert (x < row->c1); |
|
|
548 | mapcell *cell = row->col + (x - row->c0); |
|
|
549 | |
|
|
550 | if (flags & 15) |
|
|
551 | { |
|
|
552 | if (cell->darkness < 0 && x < self->w && y < self->h) |
|
|
553 | { |
|
|
554 | cell->darkness = 0; |
|
|
555 | cell->face [0] = 0; |
|
|
556 | cell->face [1] = 0; |
|
|
557 | cell->face [2] = 0; |
|
|
558 | } |
|
|
559 | |
|
|
560 | cell->darkness = flags & 8 ? *data++ : 255; |
|
|
561 | |
|
|
562 | if (flags & 4) |
|
|
563 | { |
|
|
564 | cell->face [0] = (data [0] << 8) + data [1]; data += 2; |
|
|
565 | } |
|
|
566 | |
|
|
567 | if (flags & 2) |
|
|
568 | { |
|
|
569 | cell->face [1] = (data [0] << 8) + data [1]; data += 2; |
|
|
570 | } |
|
|
571 | |
|
|
572 | if (flags & 1) |
|
|
573 | { |
|
|
574 | cell->face [2] = (data [0] << 8) + data [1]; data += 2; |
|
|
575 | } |
|
|
576 | } |
|
|
577 | else |
|
|
578 | cell->darkness = -1; |
|
|
579 | } |
|
|
580 | } |
|
|
581 | |
|
|
582 | void |
|
|
583 | draw (CFClient::Map self, int x0, int y0, int sw, int sh) |
|
|
584 | PPCODE: |
|
|
585 | { |
|
|
586 | int sw4 = (sw + 3) & ~3; |
|
|
587 | SV *darkness_sv = sv_2mortal (newSV (sw4 * sh)); |
|
|
588 | uint8_t *darkness = (uint8_t *)SvPVX (darkness_sv); |
|
|
589 | |
|
|
590 | SvPOK_only (darkness_sv); |
|
|
591 | SvCUR_set (darkness_sv, sw4 * sh); |
|
|
592 | |
|
|
593 | int vx = self->vx = self->w >= sw |
|
|
594 | ? self->x + (self->w - sw) / 2 |
|
|
595 | : MIN (self->x, MAX (self->x + self->w - sw + 1, self->vx)); |
|
|
596 | |
|
|
597 | int vy = self->vy = self->h >= sh |
|
|
598 | ? self->y + (self->h - sh) / 2 |
|
|
599 | : MIN (self->y, MAX (self->y + self->h - sh + 1, self->vy)); |
|
|
600 | |
|
|
601 | glColor4ub (255, 255, 255, 255); |
|
|
602 | |
|
|
603 | glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
|
|
604 | glEnable (GL_BLEND); |
|
|
605 | glEnable (GL_TEXTURE_2D); |
|
|
606 | glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); |
|
|
607 | |
|
|
608 | int x, y, z; |
|
|
609 | |
|
|
610 | int last_name = 0; |
|
|
611 | |
|
|
612 | glBegin (GL_QUADS); |
|
|
613 | |
|
|
614 | for (z = 0; z < 3; z++) |
|
|
615 | for (y = 0; y < sh; y++) |
|
|
616 | if (0 <= y + vy && y + vy < self->rows) |
|
|
617 | { |
|
|
618 | maprow *row = self->row + (y + vy); |
|
|
619 | |
|
|
620 | for (x = 0; x < sw; x++) |
|
|
621 | if (row->c0 <= x + vx && x + vx < row->c1) |
|
|
622 | { |
|
|
623 | mapcell *cell = row->col + (x + vx - row->c0); |
|
|
624 | |
|
|
625 | darkness[y * sw4 + x] = cell->darkness < 0 |
|
|
626 | ? 255 - FOW_DARKNESS |
|
|
627 | : 255 - cell->darkness; |
|
|
628 | |
|
|
629 | uint16_t face = cell->face [z]; |
|
|
630 | |
|
|
631 | if (face) |
|
|
632 | { |
|
|
633 | mapface tex = self->face [face]; |
|
|
634 | |
|
|
635 | int px = (x + 1) * 32 - tex.w; |
|
|
636 | int py = (y + 1) * 32 - tex.h; |
|
|
637 | |
|
|
638 | if (last_name != tex.name) |
|
|
639 | { |
|
|
640 | glEnd (); |
|
|
641 | last_name = tex.name; |
|
|
642 | glBindTexture (GL_TEXTURE_2D, last_name); |
|
|
643 | glBegin (GL_QUADS); |
|
|
644 | } |
|
|
645 | |
|
|
646 | glTexCoord2f (0 , 0 ); glVertex2f (px , py ); |
|
|
647 | glTexCoord2f (0 , tex.t); glVertex2f (px , py + tex.h); |
|
|
648 | glTexCoord2f (tex.s, tex.t); glVertex2f (px + tex.w, py + tex.h); |
|
|
649 | glTexCoord2f (tex.s, 0 ); glVertex2f (px + tex.w, py ); |
|
|
650 | } |
|
|
651 | } |
|
|
652 | } |
|
|
653 | |
|
|
654 | glEnd (); |
|
|
655 | |
|
|
656 | glDisable (GL_TEXTURE_2D); |
|
|
657 | glDisable (GL_BLEND); |
|
|
658 | |
|
|
659 | EXTEND (SP, 3); |
|
|
660 | PUSHs (sv_2mortal (newSViv (sw4))); |
|
|
661 | PUSHs (sv_2mortal (newSViv (sh))); |
|
|
662 | PUSHs (darkness_sv); |
|
|
663 | } |
|
|
664 | |