Debian Codegraph @

Contrib/CL_FlexibleMap/CL_FlexibleMap.cpp

raw
1 /*
2 ** CL_FlexibleMap
3 ** Copyright (c) 2005 Erik Ylvisaker
4 **
5 ** This software is provided 'as-is', without any express or implied
6 ** warranty. In no event will the authors be held liable for any damages
7 ** arising from the use of this software.
8 **
9 ** Permission is granted to anyone to use this software for any purpose,
10 ** including commercial applications, and to alter it and redistribute it
11 ** freely, subject to the following restrictions:
12 **
13 ** 1. The origin of this software must not be misrepresented; you must not
14 ** claim that you wrote the original software. If you use this software
15 ** in a product, an acknowledgment in the product documentation would be
16 ** appreciated but is not required.
17 ** 2. Altered source versions must be plainly marked as such, and must not be
18 ** misrepresented as being the original software.
19 ** 3. This notice may not be removed or altered from any source distribution.
20 **
21 ** Note: Some of the libraries ClanLib may link to may have additional
22 ** requirements or restrictions.
23 **
24 ** File Author(s):
25 **
26 ** Erik Ylvisaker
27 ** (if your name is missing here, please add it)
28 */
29
30
31 #include "CL_FlexibleMap.h"
32
33 #include <algorithm>
34
35 CL_FlexibleMap::CL_FlexibleMap(void)
36 {
37 block_data = NULL;
38 anim_data = NULL;
39
40 bgfx_data = NULL;
41
42 tiles = NULL;
43
44 palette = NULL;
45
46 last_error = CL_FlexibleMap::ok;
47
48 use_stagger = false;
49
50 current_layer = 0;
51
52 clear();
53
54 }
55
56 CL_FlexibleMap::~CL_FlexibleMap(void)
57 {
58 clear();
59 }
60
61 void CL_FlexibleMap::clear()
62 {
63 delete [] block_data;
64 block_data = NULL;
65
66 delete [] anim_data;
67 anim_data = NULL;
68 anim_data_count = 0;
69
70 delete [] anim_frames;
71 anim_frames = NULL;
72
73 delete [] bgfx_data;
74 bgfx_data = NULL;
75
76 delete [] tiles;
77 tiles = NULL;
78
79 delete [] palette;
80 palette = NULL;
81 palette_size = 0;
82
83 last_error = ok;
84
85 map_version = 0;
86 current_layer = 0;
87
88 use_stagger = false;
89 map_source = NULL;
90
91
92 last_anim_update = 0;
93 frame_rate_independent_anims = false;
94
95 memset(&MPHD, 0, sizeof(MPHD));
96
97 for (int i = 0; i < (signed) layers.size(); i++)
98 {
99 delete layers[i];
100 }
101
102 layers.clear();
103
104 // free extra chunks
105 for (std::map<std::string, CL_FlexibleMapChunk*>::iterator i = unknown_chunks.begin();
106 i != unknown_chunks.end(); i++)
107 {
108 delete i->second;
109 }
110
111 unknown_chunks.clear();
112
113
114 for (int i = 0; i < 4; i++)
115 author_info[i] = "";
116
117
118
119 }
120
121 void CL_FlexibleMap::read_chars(std::string &dest, int count)
122 {
123 dest = "";
124
125 for (int i = 0; i < count; i++)
126 dest += map_source->read_char8();
127 }
128
129 int CL_FlexibleMap::read_zero_terminated_string(std::string &dest, int maxlen)
130 {
131 dest = "";
132
133 for (int i = 0; i < maxlen; i++)
134 {
135 char c = map_source->read_char8();
136
137 dest += c;
138
139 if (c == 0)
140 break;
141 }
142
143 return i;
144 }
145
146
147 bool CL_FlexibleMap::load(const std::string &filename)
148 {
149 clear();
150
151 CL_InputSource_File file(filename);
152
153 map_source = &file;
154
155 bool retval = _load();
156
157 map_source = NULL;
158
159 return retval;
160
161 }
162
163
164
165 bool CL_FlexibleMap::_load()
166 {
167
168 std::string buffer;
169 cl_uint32 filesize; // size of the file - header (which is 8 bytes)
170
171 // mappy stores the first part in motorola format
172 map_source->set_big_endian_mode();
173
174 // read beginning of file
175 read_chars(buffer, 4);
176
177 if (buffer != "FORM")
178 {
179 last_error = notfmap;
180 return false;
181 }
182
183 // Size of data is next
184 filesize = map_source->read_uint32();
185
186 // now we have FMAP
187 read_chars(buffer, 4);
188
189 if (buffer != "FMAP")
190 {
191 last_error = notfmap;
192 return false;
193 }
194
195
196 // ok, now we read in the chunks
197 // we probably should do something where we read in the MPHD chunk first.
198
199
200 while (map_source->tell() < map_source->size())
201 {
202 bool chunk_check;
203
204 read_chars(buffer, 4); // read in chunk name
205 unsigned int size = map_source->read_uint32();
206
207 // sometimes mappy will store data in little-endian mode. However,
208 // chunk sizes will still be in big-endian mode. So take that into account.
209 if (map_source->is_little_endian())
210 {
211 SWAP_IF_LITTLE(size);
212 }
213
214
215 // store beginning of chunk
216 int pos = map_source->tell();
217
218 // parse reading of chunk
219 if (buffer == "ATHR") { chunk_check = parse_ATHR(buffer, size); }
220 else if (buffer == "MPHD") { chunk_check = parse_MPHD(buffer, size); }
221 else if (buffer == "EDHD") { chunk_check = parse_EDHD(buffer, size); }
222 else if (buffer == "CMAP") { chunk_check = parse_CMAP(buffer, size); }
223 else if (buffer == "BKDT") { chunk_check = parse_BKDT(buffer, size); }
224 else if (buffer == "ANDT") { chunk_check = parse_ANDT(buffer, size); }
225 else if (buffer == "BGFX") { chunk_check = parse_BGFX(buffer, size); }
226 else if (buffer == "BODY") { chunk_check = parse_BODY_LYR(buffer, size); }
227 else if (buffer.substr(0, 3) == "LYR") { chunk_check = parse_BODY_LYR(buffer, size); }
228 else { chunk_check = parse_custom_chunk(buffer, size); }
229
230 if (!chunk_check)
231 return false;
232
233 // navigate to next chunk
234 map_source->seek(pos + size, CL_InputSource::seek_set);
235
236 }
237
238 // from the mappy 1.2 docs:
239 //These are the chunks as of V1.2:
240 //ATHR - Up to 4 ASCII strings of author information, separated by 0 values, always an even size.
241 //MPHD - Map header, see struct in the editor source download
242 //EDHD - Editor information, see struct in mappy.c
243 //CMAP - Colour palette for 8bit maps, red byte, green byte, blue byte for however many colours are needed (so usually 256*3 bytes).
244 //BKDT - Block data. Contains BLKSTR structures for however many block structures were made.
245 //ANDT - Animation data. Contains ANISTR structures for however many animation structures were made, and also animation data.
246 //BGFX - The raw graphics in whatever format the map is in. Examples: 8bit: mapwidth*mapheight bytes per block, in forward format *numblocks 16bit: mapwidth*mapheight*2 bytes per block, each word contains 5 bits red, 6 bits green, 5 bits blue.
247 //BODY - An array of short ints containing positive offsets into BKDT, and negative offsets into ANDT.
248 //LYR? - Where ? is an ASCII number form 1 to 7. These are the same size and format as BODY, and allow object layers to be used.
249 //You can add your own chunks to a map file, if you load it into mappy, when you save it, those additional chunks will be saved in the file, but not necessarily in the same place as before.
250
251
252 /////////////////////
253 //
254 // Ok, all the chunk data is loaded. Now we need to convert the BGFX data
255 // into an actual ClanLib surface.
256
257 if (!load_tiles())
258 return false;
259
260 // sort layers
261 std::sort(layers.begin(), layers.end(), CL_Layer::compare);
262
263 // set the animation time
264 last_anim_update = CL_System::get_time();
265
266 return true;
267
268 }
269
270 int CL_FlexibleMap::get_FMP0_5_bytes_per_pixel()
271 {
272 int pixelbytes;
273
274 switch(MPHD.blockdepth)
275 {
276
277 // 8-bit paletted maps must be converted to non-paletted.
278 case 8: pixelbytes = 1; break;
279
280 case 15: pixelbytes = 2; break;
281 case 16: pixelbytes = 2; break;
282 case 24: pixelbytes = 3; break;
283 case 32: pixelbytes = 4; break;
284
285 default: last_error = could_not_convert_tiles; return 0;
286
287 }
288
289 return pixelbytes * MPHD.blockheight * MPHD.blockwidth;
290 }
291
292
293 bool CL_FlexibleMap::load_tiles()
294 {
295 CL_PixelFormat format;
296
297 int pitch;
298 int width = MPHD.blockwidth;
299 int height = MPHD.blockheight;
300 bool palette8 = false;
301
302 if (CL_Endian::is_system_big())
303 format = CL_PixelFormat::rgba8888;
304 else
305 format = CL_PixelFormat::abgr8888;
306
307 pitch = 4 * width;
308
309
310 // ClanLib doesn't support 8-bit paletted data, for obvious reasons.
311 // It also doesn't seem to implement color-keying right. The best thing to do
312 // is manually convert all input images to rgba8888 format.
313 unsigned int *data = new unsigned int[MPHD.blockwidth * MPHD.blockheight];
314
315 // calculate how to write colors to the memory
316 int redshift, blueshift, greenshift, alphashift;
317
318 redshift = get_color_shift_from_mask(format.get_red_mask());
319 blueshift = get_color_shift_from_mask(format.get_blue_mask());
320 greenshift = get_color_shift_from_mask(format.get_green_mask());
321 alphashift = get_color_shift_from_mask(format.get_alpha_mask());
322
323 // allocate the surfaces
324 tiles = new CL_Surface[MPHD.numblockgfx];
325
326 if (MPHD.blockdepth == 8) // we need to convert 8-bit
327 {
328 for (int n = 0; n < MPHD.numblockgfx; n++)
329 {
330 unsigned char *tile_data = &bgfx_data[n * MPHD.blockwidth * MPHD.blockheight];
331
332 for (int i = 0; i < MPHD.blockwidth * MPHD.blockheight; i++)
333 {
334 unsigned int color = palette[tile_data[i]];
335 unsigned int result;
336
337 if (tile_data[i] == MPHD.ckey8bit)// && map_version > 0.35)
338 // colorkeying was apparently added in version 0.4
339 // but some 0.3 maps seem to need it. How to handle this?????
340 {
341 result = 0;
342 }
343 else
344 {
345 result = ((color & 0x000000FF) ) << blueshift;
346 result |= ((color & 0x0000FF00) >> 8) << greenshift;
347 result |= ((color & 0x00FF0000) >> 16) << redshift;
348 result |= 0xFF << alphashift;
349 }
350
351
352 data[i] = result;
353 }
354
355 CL_PixelBuffer px(MPHD.blockwidth, MPHD.blockheight, pitch, format, data);
356
357 tiles[n] = CL_Surface(px.to_format(format), CL_Surface::flag_none);
358
359 }
360
361
362 }
363 else if (MPHD.blockdepth == 15 || MPHD.blockdepth == 16)
364 {
365 /* for (int i = 0; i < MPHD.blockwidth * MPHD.blockheight * MPHD.numblockgfx; i++)
366 {
367 unsigned short color = ((short*)bgfx_data)[i];
368
369
370 }
371 */
372 unsigned int color_key = get_map_colorkey();
373
374 unsigned short red_mask, green_mask, blue_mask;
375
376 int src_red_shift, src_green_shift, src_blue_shift;
377 int dest_red_shift = 0, dest_green_shift = 0, dest_blue_shift = 0;
378
379
380 if (MPHD.blockdepth == 15)
381 {
382 red_mask = 0x7C00;
383 green_mask = 0x03E0;
384 blue_mask = 0x001F;
385
386 src_red_shift = 10; dest_red_shift = 3;
387 src_green_shift = 5; dest_green_shift = 3;
388 src_blue_shift = 0; dest_blue_shift = 3;
389
390 }
391 else if (MPHD.blockdepth == 16)
392 {
393 red_mask = 0xF800;
394 green_mask = 0x07E0;
395 blue_mask = 0x001F;
396
397 src_red_shift = 11; dest_red_shift = 3;
398 src_green_shift = 5; dest_green_shift = 2;
399 src_blue_shift = 0; dest_blue_shift = 3;
400
401 }
402
403
404 for (int n = 0; n < MPHD.numblockgfx; n++)
405 {
406 unsigned char *tile_data = &bgfx_data[n * 2 * MPHD.blockwidth * MPHD.blockheight];
407
408 for (int i = 0; i < MPHD.blockwidth * MPHD.blockheight; i++)
409 {
410 unsigned short src_color = ((short*)tile_data)[i];
411
412 // I'm not sure about this...
413 if (!CL_Endian::is_system_big())
414 {
415 unsigned int lo = src_color & 0xff;
416 unsigned int hi = (src_color & 0xff00) >> 8;
417
418 src_color = (lo << 8) | hi;
419 }
420
421 unsigned int red = (((src_color & red_mask) >> src_red_shift) << dest_red_shift);
422 unsigned int green = (((src_color & green_mask) >> src_green_shift) << dest_green_shift);
423 unsigned int blue = (((src_color & blue_mask) >> src_blue_shift) << dest_blue_shift);
424
425 if (red > 255 || green > 255 || blue > 255)
426 int j = 4;
427
428 unsigned int color =
429 red << redshift |
430 green << greenshift |
431 blue << blueshift ;
432
433 if (color != color_key)
434 color |= 0xFF << alphashift;
435
436 data[i] = color;
437
438 }
439
440 CL_PixelBuffer px(MPHD.blockwidth, MPHD.blockheight, pitch, format, data);
441
442 tiles[n] = CL_Surface(px.to_format(format), CL_Surface::flag_none);
443
444 }
445 }
446 else if (MPHD.blockdepth == 24)
447 {
448 unsigned int color_key = get_map_colorkey();
449
450 for (int n = 0; n < MPHD.numblockgfx; n++)
451 {
452 unsigned char *tile_data = &bgfx_data[n * 3 * MPHD.blockwidth * MPHD.blockheight];
453
454 for (int i = 0; i < MPHD.blockwidth * MPHD.blockheight; i++)
455 {
456 unsigned int color =
457 tile_data[i*3+0] << redshift |
458 tile_data[i*3+1] << greenshift |
459 tile_data[i*3+2] << blueshift ;
460
461 if (color != color_key)
462 color |= 0xFF << alphashift;
463
464 data[i] = color;
465
466 }
467
468 CL_PixelBuffer px(MPHD.blockwidth, MPHD.blockheight, pitch, format, data);
469
470 tiles[n] = CL_Surface(px.to_format(format), CL_Surface::flag_none);
471
472 }
473 }
474 else if (MPHD.blockdepth == 32)
475 {
476 unsigned int color_key = get_map_colorkey();
477
478 unsigned int alpha_mask = 0xff << redshift |
479 0xff << blueshift |
480 0xff << greenshift;
481
482 for (int n = 0; n < MPHD.numblockgfx; n++)
483 {
484 unsigned char *tile_data = &bgfx_data[n * 4 * MPHD.blockwidth * MPHD.blockheight];
485
486 for (int i = 0; i < MPHD.blockwidth * MPHD.blockheight; i++)
487 {
488 unsigned int color =
489 tile_data[i*4+0] << alphashift |
490 tile_data[i*4+1] << redshift |
491 tile_data[i*4+2] << greenshift |
492 tile_data[i*4+3] << blueshift ;
493
494 if ((color & alpha_mask) != color_key)
495 data[i] = color;
496 else
497 data[i] = 0;
498
499 }
500
501 CL_PixelBuffer px(MPHD.blockwidth, MPHD.blockheight, pitch, format, data);
502
503 tiles[n] = CL_Surface(px.to_format(format), CL_Surface::flag_none);
504
505 }
506 }
507 else
508 {
509 last_error = could_not_convert_tiles;
510 return false;
511
512 }
513
514 // now transfer the data
515 delete [] bgfx_data;
516 bgfx_data = NULL;
517
518
519 delete [] data;
520
521 // create the surface from the data we found in the BGFX chunk.
522 //tile_pixels = CL_PixelBuffer(width, height, pitch, format, bgfx_data);
523
524 // convert to display format
525 //tile_pixels = tile_pixels.to_format(CL_Display::get_current_window()->get_back_buffer().get_format());
526
527
528 //tiles = new CL_Surface(tile_pixels, CL_Surface::flag_keep_pixelbuffer);
529
530
531 if (tiles)
532 {
533
534
535 return true;
536 }
537 else
538 {
539 last_error = could_not_convert_tiles;
540
541 return false;
542 }
543 }
544
545 int CL_FlexibleMap::get_color_shift_from_mask(unsigned int color_mask)
546 {
547 for (int sh = 0; sh < 32; sh += 8)
548 {
549 if (color_mask & (255 << sh))
550 {
551 return sh;
552 }
553 }
554
555 return 0;
556 }
557
558
559 unsigned int CL_FlexibleMap::get_map_colorkey()
560 {
561
562 unsigned int colorkey;
563
564
565 colorkey = MPHD.ckeyred << 16 |
566 MPHD.ckeygreen << 8 |
567 MPHD.ckeyblue ;
568
569 return colorkey;
570 }
571
572 bool CL_FlexibleMap::parse_ATHR(const std::string& blockname, int size)
573 {
574 int bytes_read = 0;
575
576 for (int i = 0; i < 4; i++)
577 {
578 bytes_read += read_zero_terminated_string(author_info[i], size - bytes_read);
579 }
580
581 // make sure we didn't go over
582 if (bytes_read <= size)
583 return true;
584 else
585 {
586 last_error = bad_ATHR;
587
588 return false;
589 }
590
591 }
592
593 bool CL_FlexibleMap::parse_MPHD(const std::string& blockname, int size)
594 {
595
596 //char mapverhigh; /* map version number to left of . (ie X.0). */
597 //char mapverlow; /* map version number to right of . (ie 0.X). */
598 //char lsb; /* if 1, data stored LSB first, otherwise MSB first. */
599 //char reserved;
600 //short int mapwidth; /* width in blocks. */
601 //short int mapheight; /* height in blocks. */
602 //short int reserved1;
603 //short int reserved2;
604 //short int blockwidth; /* width of a block (tile) in pixels. */
605 //short int blockheight; /* height of a block (tile) in pixels. */
606 //short int blockdepth; /* depth of a block (tile) in planes (ie. 256 colours is 8) */
607 //short int blockstrsize; /* size of a block data structure */
608 //short int numblockstr; /* Number of block structures in BKDT */
609 //short int numblockgfx; /* Number of 'blocks' in graphics (BODY) */
610
611 MPHD.mapverhigh = map_source->read_char8();
612 MPHD.mapverlow = map_source->read_char8();
613 MPHD.lsb = map_source->read_char8();
614
615 // exactly one of these should be uncommented.
616 //MPHD.reserved = map_source->read_char8();
617 MPHD.maptype = map_source->read_char8();
618
619
620 // check to see if data is little-endian format
621 if (MPHD.lsb == 1)
622 map_source->set_little_endian_mode();
623
624 MPHD.mapwidth = map_source->read_int16();
625 MPHD.mapheight = map_source->read_int16();
626 MPHD.reserved1 = map_source->read_int16();
627 MPHD.reserved2 = map_source->read_int16();
628 MPHD.blockwidth = map_source->read_int16();
629 MPHD.blockheight = map_source->read_int16();
630 MPHD.blockdepth = map_source->read_int16();
631 MPHD.blockstrsize = map_source->read_int16();
632 MPHD.numblockstr = map_source->read_int16();
633 MPHD.numblockgfx = map_source->read_int16();
634
635 map_version = MPHD.mapverhigh + MPHD.mapverlow / 10.0;
636
637 // version 0.4
638 if (size > 24)
639 {
640 MPHD.ckey8bit = map_source->read_char8();
641 MPHD.ckeyred = map_source->read_char8();
642 MPHD.ckeygreen = map_source->read_char8();
643 MPHD.ckeyblue = map_source->read_char8();
644 }
645 else
646 {
647 MPHD.ckey8bit = 0;
648 MPHD.ckeyblue = 0;
649 MPHD.ckeygreen = 0;
650 MPHD.ckeyred = 0;
651 }
652
653
654
655 // version 0.5
656 if (size > 28)
657 {
658 MPHD.blockgapx = map_source->read_int16();
659 MPHD.blockgapy = map_source->read_int16();
660 MPHD.blockstaggerx = map_source->read_int16();
661 MPHD.blockstaggery = map_source->read_int16();
662 }
663 else
664 {
665 MPHD.blockgapx = MPHD.blockwidth;
666 MPHD.blockgapy = MPHD.blockheight;
667 MPHD.blockstaggerx = 0;
668 MPHD.blockstaggery = 0;
669 }
670
671 if (size > 36)
672 {
673 MPHD.clickmask = map_source->read_int16();
674 MPHD.pillar = map_source->read_int16();
675 }
676 else
677 {
678 MPHD.clickmask = 0;
679 MPHD.pillar = 0;
680 }
681
682 if (MPHD.blockstaggerx || MPHD.blockstaggery)
683 use_stagger = true;
684
685 return true;
686 }
687
688 bool CL_FlexibleMap::parse_EDHD(const std::string& blockname, int size)
689 {
690 // I think we can skip reading this, because this class is not for using the editor.
691 return true;
692 }
693
694 bool CL_FlexibleMap::parse_CMAP(const std::string& blockname, int size)
695 {
696 // here we would read in the color map for 8-bit displays.
697 palette_size = size / 3;
698
699 palette = new unsigned int[palette_size];
700
701 for (int i = 0; i < palette_size; i++)
702 {
703 unsigned char red = map_source->read_uchar8();
704 unsigned char green = map_source->read_uchar8();
705 unsigned char blue = map_source->read_uchar8();
706
707 palette[i] = (red << 16) |
708 (green << 8) |
709 (blue ) ;
710 }
711
712 return true;
713 }
714
715
716 bool CL_FlexibleMap::parse_BKDT(const std::string& blockname, int size)
717 {
718 // read in block data stuff. This assumes the MPHD has been read in
719 block_data = new CL_BlockData[MPHD.numblockstr];
720
721 if (sizeof(CL_BlockData) < MPHD.blockstrsize)
722 {
723 // we've got a problem here. Either the map version is newer, or
724 // the map header is corrupted. Bail out.
725 last_error = CL_FlexibleMap::bad_MPHD;
726
727 return false;
728 }
729
730
731 for (int i = 0; i < MPHD.numblockstr; i++)
732 {
733 CL_BlockData &current = block_data[i];
734
735 //map_source->read(&current, MPHD.blockstrsize );
736 current.bg_offset = map_source->read_int32();
737 current.fg_offset = map_source->read_int32();
738 current.fg2_offset = map_source->read_int32();
739 current.fg3_offset = map_source->read_int32();
740
741 current.user1 = map_source->read_uint32();
742 current.user2 = map_source->read_uint32();
743
744 current.user3 = map_source->read_uint16();
745 current.user4 = map_source->read_uint16();
746
747 current.user5 = map_source->read_char8();
748 current.user6 = map_source->read_char8();
749 current.user7 = map_source->read_char8();
750
751 // if the mappy version is low, convert offsets to indices
752 if (MPHD.mapverhigh == 0)
753 {
754 int pixel_size = get_FMP0_5_bytes_per_pixel();
755
756 current.bg_offset /= pixel_size;
757 current.fg_offset /= pixel_size;
758 current.fg2_offset /= pixel_size;
759 current.fg3_offset /= pixel_size;
760
761 }
762
763
764 // interpret the bit flags;
765 unsigned char bit = map_source->read_uchar8();
766
767
768
769
770 current.tl = (bit & 0x01) ? 1 : 0;
771 current.tr = (bit & 0x02) ? 1 : 0;
772 current.bl = (bit & 0x04) ? 1 : 0;
773 current.br = (bit & 0x08) ? 1 : 0;
774
775 current.trigger = (bit & 0x10) ? 1 : 0;
776 current.u1_attach_next = (bit & 0x20) ? 1 : 0;
777 current.u2_left_pillar = (bit & 0x40) ? 1 : 0;
778 current.u3_right_pillar = (bit & 0x80) ? 1 : 0;
779
780
781
782 //long int bgoff, fgoff; /* offsets from start of graphic blocks */
783 //long int fgoff2, fgoff3; /* more overlay blocks */
784 //unsigned long int user1, user2; /* user long data */
785 //unsigned short int user3, user4; /* user short data */
786 //unsigned char user5, user6, user7; /* user byte data */
787 //unsigned char tl : 1; /* bits for collision detection */
788 //unsigned char tr : 1;
789 //unsigned char bl : 1;
790 //unsigned char br : 1;
791 //unsigned char trigger : 1; /* bit to trigger an event */
792 //unsigned char unused1 : 1;
793 //unsigned char unused2 : 1;
794 //unsigned char unused3 : 1;
795
796
797 }
798
799
800 return true;
801 }
802
803
804 bool CL_FlexibleMap::parse_ANDT(const std::string& blockname, int size)
805 {
806 // read in animation data stuff. This assumes the MPHD has been read in
807 // apparently mappy has a bizarre way of storing this information.
808 unsigned char *data = new unsigned char[size];
809
810 map_source->read(data, size);
811
812 // we need to count the animation structures present, and they are last.
813 CL_AnimData *end_pt = reinterpret_cast<CL_AnimData*>(data + size);
814 CL_AnimData *anim_ptr = end_pt;
815
816
817 do
818 {
819 anim_ptr--;
820 anim_data_count++;
821
822 } while (anim_ptr->anim_type != CL_FlexibleMap::anim_end);
823
824 // no need to store the end structure
825 anim_data_count--;
826
827 anim_data = new CL_AnimData[anim_data_count];
828
829 // allocate memory for the actual frames
830 int frame_count = (size - anim_data_count * anim_data_size) / sizeof(int);
831 anim_frames = new int[frame_count];
832
833 memcpy(anim_frames, data, sizeof(int) * frame_count);
834
835 if (MPHD.mapverhigh == 0)
836 {
837 // these need to be divided by block sizes
838 for (int i = 0; i < frame_count; i++)
839 anim_frames[i] /= block_data_size;
840 }
841
842
843 // ok, read in the control structures.
844 anim_ptr = end_pt;
845
846 for (int i = 0; i < anim_data_count; i++)
847 {
848 anim_ptr--;
849
850 anim_data[i] = *anim_ptr;
851
852 // make sure once through animations are frozen to begin with
853 if (anim_data[i].anim_type == anim_once)
854 anim_data[i].anim_type = anim_onces;
855
856 if (anim_data[i].anim_type == anim_onceh)
857 anim_data[i].anim_type = anim_oncehs;
858
859 // some of the fields need to be modified based on maptype
860 if (MPHD.mapverhigh == 0)
861 {
862 anim_data[i].anim_current_offset += size;
863 anim_data[i].anim_start_offset += size;
864 anim_data[i].anim_end_offset += size;
865 }
866 else
867 {
868 // do nothing?
869 /*
870 anim_data[i].anim_current_offset += size;
871 anim_data[i].anim_start_offset += size;
872 anim_data[i].anim_end_offset += size;
873 */
874 }
875
876 if (MPHD.mapverhigh == 0)
877 {
878 // anim offsets need to be divided by 4 or sizeof(int)
879 anim_data[i].anim_start_offset /= 4;
880 anim_data[i].anim_current_offset /= 4;
881 anim_data[i].anim_end_offset /= 4;
882 }
883
884 int j = 4;
885 }
886
887 return true;
888 }
889
890
891 bool CL_FlexibleMap::parse_BGFX(const std::string& blockname, int size)
892 {
893 // here we need to actually read in the graphics image
894 // graphics data stored here is raw?
895
896 bgfx_data = new unsigned char[size];
897
898 if (!bgfx_data)
899 {
900 last_error = CL_FlexibleMap::out_of_memory;
901 return false;
902 }
903
904 // read the raw data
905 if (map_source->read(bgfx_data, size) == size)
906 return true;
907 else
908 {
909 last_error = CL_FlexibleMap::bad_BGFX;
910 return false;
911 }
912
913 }
914
915 bool CL_FlexibleMap::parse_BODY_LYR(const std::string& blockname, int size)
916 {
917 int layer_id = 0;
918 bool retval = false;
919
920 // assign appropriate ID
921 if (blockname.substr(0, 3) == "LYR")
922 layer_id = atoi(blockname.substr(3, -1).c_str());
923
924 CL_Layer *current = new CL_Layer(this);
925
926 current->layer_id = layer_id;
927
928 current->set_display_area(
929 CL_Rect(0, 0,
930 CL_Display::get_current_window()->get_width(),
931 CL_Display::get_current_window()->get_height() ) );
932
933 // allocate memory for it
934 short int *data = new short int[size / sizeof(short int)];
935
936 if (map_source->read(data, size) != size)
937 {
938 last_error = CL_FlexibleMap::bad_BODY_LYR;
939
940 delete [] data;
941 delete current;
942
943 return false;
944 }
945
946
947
948 // allocate the final storage
949 current->size = MPHD.mapheight * MPHD.mapwidth;
950 current->data = new short int[current->size];
951
952 // make sure allocation was successful
953 if (!current->data)
954 {
955 last_error = CL_FlexibleMap::out_of_memory;
956
957 delete [] data;
958 delete current;
959
960 return false;
961 }
962
963 // well, as an undocumented feature, mappy apparently will store the maptype in a field
964 // normally called "reserved", and the actual storage of map data will be arranged differently
965 // depending on that field.
966
967 if (MPHD.maptype == 0 || MPHD.maptype == 1)
968 {
969 // both of these store the tile data directly.
970 delete [] current->data;
971 current->data = data;
972
973 data = NULL;
974
975 // The oldest versions wrote out actuall byte offsets instead of indices.
976 // So convert offsets into tile indices
977 if (MPHD.mapverhigh == 0)
978 {
979 for (int i = 0; i < (signed) current->size; i++)
980 {
981 if (current->data[i] > 0)
982 current->data[i] /= MPHD.blockstrsize;
983 else if (current->data[i] < 0)
984 current->data[i] /= CL_FlexibleMap::anim_data_size;
985 }
986 }
987
988
989
990 retval = true;
991
992 }
993 else if (MPHD.maptype == 2)
994 {
995 // this seems to be the format of hexagonal and isometric maps
996 int d_index = 0;
997 int i = 0;
998
999 while (i < (signed) current->size)
1000 {
1001 // we have a short indicating how many times to repeat the
1002 // next tile
1003 int repeat = data[d_index];
1004 d_index++;
1005
1006 if (repeat > 0)
1007 {
1008 // in this case, we actually read tiles until we're done
1009 for (int n = 0; n < repeat; n++)
1010 {
1011 current->data[i] = data[d_index];
1012
1013 d_index++;
1014 i++;
1015
1016 }
1017 }
1018 else if (repeat < 0)
1019 {
1020 // here, we read the next tile and repeat it (-repeat) times.
1021 short int tile = data[d_index];
1022
1023 d_index++;
1024
1025 for (int n = 0; n < -repeat; n++, i++)
1026 current->data[i] = tile;
1027 }
1028
1029
1030 }
1031
1032 retval = true;
1033
1034
1035 }
1036 else
1037 {
1038 last_error = CL_FlexibleMap::unsupported_map_type;
1039 delete [] current->data;
1040
1041 retval = false;
1042 }
1043
1044 delete [] data;
1045
1046 // save the layer
1047 if (retval)
1048 layers.push_back(current);
1049
1050 return retval;
1051
1052 }
1053
1054
1055 bool CL_FlexibleMap::parse_custom_chunk(const std::string &blockname, int size)
1056 {
1057 CL_FlexibleMapChunk *chunk = new CL_FlexibleMapChunk;
1058
1059 chunk->name = blockname;
1060 chunk->size = size;
1061
1062 chunk->data = new unsigned char[size];
1063
1064 if (map_source->read(chunk->data, size) == size)
1065 {
1066 unknown_chunks[blockname] = chunk;
1067
1068 return true;
1069 }
1070 else
1071 {
1072 delete [] chunk->data;
1073 delete chunk;
1074
1075 return false;
1076 }
1077
1078 }
1079
1080 int CL_FlexibleMap::get_extra_chunk_count()
1081 {
1082 return (signed) unknown_chunks.size();
1083 }
1084
1085
1086 CL_FlexibleMapChunk* CL_FlexibleMap::get_chunk(const std::string &identifier)
1087 {
1088 if (unknown_chunks.find(identifier) != unknown_chunks.end())
1089 return unknown_chunks[identifier];
1090 else
1091 return NULL;
1092 }
1093
1094 CL_FlexibleMapChunk* CL_FlexibleMap::get_chunk(int index)
1095 {
1096 if (index < (signed) unknown_chunks.size() && index > 0)
1097 {
1098 std::map<std::string, CL_FlexibleMapChunk* >::iterator i = unknown_chunks.begin();
1099
1100 // iterate through
1101 for (int j = 0; j < index; j++, i++);
1102
1103 return i->second;
1104 }
1105 else
1106 return NULL;
1107
1108 }
1109
1110
1111 ////////////////////////////////
1112 // Drawing functions
1113 bool CL_FlexibleMap::done_drawing(CL_FlexibleMapDrawParams &params)
1114 {
1115 if (params.draw.y < params.start.y + params.draw_size.height)
1116 return false;
1117 else
1118 return true;
1119 }
1120
1121
1122 void CL_FlexibleMap::draw_block_layer(int block_layer)
1123 {
1124 CL_FlexibleMapDrawParams params(get_current_layer(), block_layer);
1125
1126 begin_draw_block_layer(params);
1127
1128
1129 // begin drawing loop
1130 while (!done_drawing(params))
1131 {
1132
1133 draw_next_line(params);
1134
1135 }
1136
1137 end_draw_block_layer(params);
1138
1139 }
1140
1141 void CL_FlexibleMap::draw_next_line(CL_FlexibleMapDrawParams &params)
1142 {
1143
1144 int eff_draw_y = params.draw.y;
1145
1146 // whether or not the row is staggered.
1147 if (params.tile.y % 2 != 0 && use_stagger)
1148 params.stagger_this_row = true;
1149 else
1150 params.stagger_this_row = false;
1151
1152 // store drawing location
1153 params.last_row_start.x = params.draw.x;
1154 params.last_row_start.y = params.draw.y;
1155
1156 params.last_row_start_tile.x = params.tile.x;
1157 params.last_row_start_tile.y = params.tile.y;
1158
1159 if (params.stagger_this_row)
1160 {
1161 params.last_row_start.x = params.draw.x + MPHD.blockstaggerx;
1162 params.last_row_start.y = params.draw.y + MPHD.blockstaggery;
1163 }
1164
1165
1166
1167 while (params.draw.x < params.start.x + params.draw_size.width)
1168 {
1169 int eff_draw_x = params.draw.x;
1170
1171 // use_stagger is true for non-rectangular tiles
1172 if (params.stagger_this_row)
1173 {
1174 eff_draw_y = params.draw.y + MPHD.blockstaggery;
1175 eff_draw_x = params.draw.x + MPHD.blockstaggerx;
1176 }
1177
1178 // read from the map
1179 int block_index = params.layer.get_map_data(params.tile.x, params.tile.y);
1180
1181 // draw the block in the correctly calculated place
1182 draw_block(eff_draw_x, eff_draw_y, params.layer.get_alpha(),
1183 block_index, params.block_layer, params.draw_attached);
1184
1185 // increment
1186 params.draw.x += MPHD.blockgapx;
1187 params.tile.x++;
1188
1189 }
1190
1191 // start the next row
1192 params.draw.x = params.start.x;
1193
1194 // the gap is only added for stagger maps every other row. The
1195 // stagger variable will alternate each row.
1196 if (params.stagger_this_row || !use_stagger)
1197 params.draw.y += MPHD.blockgapy;
1198
1199 params.tile.x = params.start_tile.x;
1200 params.tile.y++;
1201
1202 }
1203
1204
1205
1206 void CL_FlexibleMap::begin_draw_block_layer(CL_FlexibleMapDrawParams &params)
1207 {
1208 CL_Layer &layer = params.layer;
1209
1210 params.stagger_this_row = false;
1211
1212
1213 // save the clipping region, and set the layer clip region
1214 params.old_clip_rect = CL_Display::get_cliprect();
1215 params.clip_rect = layer.get_display_area();
1216
1217 // for debugging the clip_rect stuff.. comment out top line and add bottom
1218 //CL_Display::draw_rect(params.clip_rect, CL_Color::white);
1219 CL_Display::set_cliprect(params.clip_rect);
1220
1221 params.eff_stagger.x = use_stagger ? MPHD.blockstaggerx : 0;
1222 params.eff_stagger.y = use_stagger ? MPHD.blockstaggery : 0;
1223
1224 // pixel point at which to use for the top left of the display
1225 params.source.x = layer.draw_point.x - params.clip_rect.left;
1226 params.source.y = layer.draw_point.y - params.clip_rect.top;
1227
1228 // include the choice of origin
1229 CL_Point origin = calc_origin(layer.origin, params.clip_rect.get_size());
1230
1231 params.source.x -= origin.x;
1232 params.source.y -= origin.y;
1233
1234 // tile at which to start drawing
1235 params.start_tile.x = (layer.draw_point.x - origin.x) / MPHD.blockgapx - params.block_border;
1236 params.start_tile.y = (layer.draw_point.y - origin.y) / MPHD.blockgapy - params.block_border;
1237
1238 // incorporate smooth scrolling
1239 params.offset.x = (layer.draw_point.x - origin.x) % MPHD.blockgapx;
1240 params.offset.y = (layer.draw_point.y - origin.y) % MPHD.blockgapy;
1241
1242 // height and width minimum to draw
1243 params.draw_size.width = params.clip_rect.get_width()
1244 + (MPHD.blockgapx + params.eff_stagger.x) * params.block_border * 2 + abs(params.offset.x) * 2;
1245
1246 params.draw_size.height = params.clip_rect.get_height()
1247 + (MPHD.blockgapy + params.eff_stagger.y) * params.block_border * 2 + abs(params.offset.y) * 2;
1248
1249 // for non-rectangular tiles
1250 if (use_stagger)
1251 {
1252 params.start_tile.y = (params.source.y / (MPHD.blockgapy)) * 2 - 2;
1253 params.offset.y = (params.source.y % (MPHD.blockgapy));
1254
1255 }
1256
1257 if (params.block_layer > 1 && MPHD.pillar)
1258 params.offset.y += MPHD.blockheight * (params.block_layer - 1);
1259
1260 // point at which to start drawing
1261 params.start.x = -MPHD.blockgapx * params.block_border - params.offset.x + params.clip_rect.left;
1262 params.start.y = -MPHD.blockgapy * params.block_border - params.offset.y + params.clip_rect.top;
1263
1264 // hack for strange bug where one tile too few is drawn on the top and left
1265 // of hexagonal maps which are wrapping. Turn this off and look at hextst.fmp
1266 // to see what I mean.
1267 if (params.source.x < MPHD.blockgapx + MPHD.blockstaggerx)
1268 {
1269 params.start.x -= MPHD.blockgapx;
1270 params.start_tile.x -= 1;
1271 }
1272 if (params.source.y < MPHD.blockgapy + MPHD.blockstaggery)
1273 {
1274 params.start.y -= MPHD.blockgapy;
1275 params.start_tile.y -= 1;
1276 }
1277
1278 // iteration variables
1279 params.draw.x = params.start.x;
1280 params.draw.y = params.start.y;
1281
1282 params.tile.x = params.start_tile.x;
1283 params.tile.y = params.start_tile.y;
1284
1285 // whether or not to draw isometric pillars
1286 params.draw_attached = false;
1287
1288 if (MPHD.pillar && params.block_layer == 3)
1289 params.draw_attached = true;
1290
1291 if (MPHD.pillar)
1292 {
1293 // extend drawing past bottom to make sure that
1294 // tall pillars that are below the map show up
1295 params.draw_size.height += MPHD.blockgapy * 4;
1296 }
1297
1298 // draw all tiles, for debugging purposes
1299 //for(int i = 0; i < MPHD.numblockgfx; i++)
1300 // tiles[i].draw(MPHD.blockwidth * (i % 10), MPHD.blockwidth * (i / 10));
1301 //
1302 //return;
1303
1304 src_rect = get_src_rect(0);
1305
1306
1307
1308 }
1309
1310
1311 void CL_FlexibleMap::end_draw_block_layer(CL_FlexibleMapDrawParams &params)
1312 {
1313
1314 // restore the clipping area
1315 CL_Display::set_cliprect(params.old_clip_rect);
1316
1317
1318 }
1319
1320
1321 void CL_FlexibleMap::draw_block(int draw_x, int draw_y, int alpha, int block_index,
1322 int block_layer, bool do_attached, bool is_attached)
1323 {
1324
1325 int display_index = 0;
1326 bool has_attached = false;
1327 bool left_pillar = false,
1328 right_pillar = false;
1329
1330 // look for animations first
1331 if (block_index < 0)
1332 {
1333 const CL_AnimData *anim = get_anim(block_index);
1334
1335 if (anim)
1336 {
1337 block_index = anim_frames[anim->anim_current_offset];
1338 }
1339 }
1340
1341 // now look for static blocks
1342 if (block_index > 0)
1343 {
1344 const CL_BlockData *tile = get_block(block_index);
1345
1346 if (MPHD.pillar)
1347 {
1348 if (tile->u1_attach_next) has_attached = true;
1349 if (tile->u2_left_pillar) left_pillar = true;
1350 if (tile->u3_right_pillar) right_pillar = true;
1351
1352 // if both are true, it acts as if both are false.
1353 if (left_pillar && right_pillar) left_pillar = right_pillar = false;
1354
1355 // we draw the whole tile if it's the very bottom one.
1356 if (block_layer == 0 && !is_attached) left_pillar = right_pillar = false;
1357
1358 }
1359 // make sure we have a structure
1360 if (tile)
1361 {
1362 switch(block_layer)
1363 {
1364 case 0: display_index = tile->bg_offset; break;
1365 case 1: display_index = tile->fg_offset; break;
1366 case 2: display_index = tile->fg2_offset; break;
1367 case 3: display_index = tile->fg3_offset; break;
1368
1369 default: int j = 4;
1370 }
1371
1372 }
1373
1374
1375 }
1376
1377
1378
1379 if (display_index) // zero tile is transparent
1380 {
1381 // calculate up the source and destination rects
1382 CL_Rect eff_src_rect = src_rect;
1383 CL_Rect dest_rect (draw_x,
1384 draw_y,
1385 draw_x + MPHD.blockwidth,
1386 draw_y + MPHD.blockheight);
1387
1388 // they're different if we're doing isometric pillars
1389 if (left_pillar)
1390 {
1391 dest_rect = CL_Rect(draw_x,
1392 draw_y,
1393 draw_x + MPHD.blockwidth / 2,
1394 draw_y + MPHD.blockheight);
1395
1396 eff_src_rect = CL_Rect(0, 0, MPHD.blockwidth / 2, MPHD.blockheight);
1397 }
1398 else if (right_pillar)
1399 {
1400 dest_rect = CL_Rect(draw_x + MPHD.blockwidth / 2,
1401 draw_y,
1402 draw_x + MPHD.blockwidth,
1403 draw_y + MPHD.blockheight);
1404
1405 eff_src_rect = CL_Rect(MPHD.blockwidth / 2, 0,
1406 MPHD.blockwidth, MPHD.blockheight);
1407 }
1408
1409 // give the appropriate alpha blending value
1410 tiles[display_index].set_alpha(alpha / 255.0f);
1411
1412 // actually do the drawing
1413 tiles[display_index].draw(eff_src_rect, dest_rect);
1414
1415 }
1416
1417 // check for attached blocks
1418 if (has_attached && do_attached && block_layer == 3)
1419 {
1420 draw_block(draw_x, draw_y - MPHD.blockheight*1, alpha, block_index + 1, 0, do_attached, true);
1421 draw_block(draw_x, draw_y - MPHD.blockheight*2, alpha, block_index + 1, 1, do_attached, true);
1422 draw_block(draw_x, draw_y - MPHD.blockheight*3, alpha, block_index + 1, 2, do_attached, true);
1423 draw_block(draw_x, draw_y - MPHD.blockheight*4, alpha, block_index + 1, 3, do_attached, true);
1424
1425 }
1426 }
1427
1428 short int CL_FlexibleMap::get_map_data(int x, int y, int layer)
1429 {
1430 if (layer < 0 || layer >= get_layer_count())
1431 layer = current_layer;
1432
1433 CL_Layer &current = get_current_layer();
1434
1435 return current.get_map_data(x, y);
1436 }
1437
1438
1439 CL_Rect CL_FlexibleMap::get_src_rect(int tile)
1440 {
1441 return CL_Rect(0, 0, MPHD.blockwidth, MPHD.blockheight);
1442 }
1443
1444 void CL_FlexibleMap::set_frame_independent_animation(int increment_time, bool use_fia)
1445 {
1446 frame_rate_independent_anims = use_fia;
1447
1448 if (frame_rate_independent_anims)
1449 frame_increment_time = increment_time;
1450 else
1451 frame_increment_time = 0;
1452 }
1453
1454 int CL_FlexibleMap::get_frame_increment_time()
1455 {
1456 if (frame_rate_independent_anims)
1457 return frame_increment_time;
1458
1459 else
1460 return 0;
1461
1462
1463 }
1464
1465 void CL_FlexibleMap::start_animate_once(int anim_index, bool start_now)
1466 {
1467 std::vector<int> anim_indices;
1468
1469 // determine which animations to take care of
1470 if (anim_index != 0)
1471 {
1472 if (anim_index < 0) anim_index = -anim_index;
1473
1474 anim_indices.push_back(anim_index - 1);
1475 }
1476 else
1477 {
1478 for (int i = 0; i < anim_data_count; i++)
1479 anim_indices.push_back(i);
1480 }
1481
1482 // cycle through the tiles to update
1483 for (int i = 0; i < (signed)anim_indices.size(); i++)
1484 {
1485 anim_index = anim_indices[i];
1486
1487 CL_AnimData &anim = anim_data[anim_index];
1488
1489 bool update = false;
1490 bool allow_skip = true;
1491
1492 // check if we need to reset this one
1493 switch(anim.anim_type)
1494 {
1495 case anim_once:
1496 case anim_onces:
1497
1498 anim.anim_type = anim_once;
1499 update = true;
1500
1501 break;
1502
1503 case anim_onceh:
1504 case anim_oncehs:
1505
1506 anim.anim_type = anim_onceh;
1507 update = true;
1508
1509 // don't skip the first tile if we are starting this animation over
1510 if (anim.anim_current_offset != anim.anim_start_offset)
1511 allow_skip = false;
1512
1513 break;
1514
1515 }
1516
1517
1518 if (update)
1519 {
1520 // reset the timer
1521 anim.anim_count = anim.anim_delay;
1522
1523 // start the animation over
1524 anim.anim_current_offset = anim.anim_start_offset;
1525
1526 if (start_now && allow_skip)
1527 anim.anim_current_offset++;
1528
1529 }
1530
1531 }
1532
1533 }
1534
1535
1536 void CL_FlexibleMap::update_anim()
1537 {
1538 unsigned int delta_t;
1539 unsigned int current_t = CL_System::get_time();
1540 int decrement_size = 1;
1541
1542 delta_t = current_t - last_anim_update;
1543
1544 if (get_frame_independent_animation())
1545 {
1546 // definitely want integer arithmetic on this one
1547 decrement_size = delta_t / get_frame_increment_time();
1548
1549 // ok, we're not there yet, so don't do any updating.
1550 if (delta_t == 0)
1551 return;
1552
1553 // now only update to the time when the last decrement should have occured
1554 last_anim_update += decrement_size * get_frame_increment_time();
1555 }
1556
1557
1558 for (int i = 0; i < anim_data_count; i++)
1559 {
1560 anim_data[i].anim_count -= decrement_size;
1561
1562
1563 if (anim_data[i].anim_count <= 0)
1564 {
1565 // reset the counter
1566 anim_data[i].anim_count = anim_data[i].anim_delay;
1567
1568 // make sure there is more than one frame to the animation before we start changing stuff
1569 if (anim_data[i].anim_start_offset == anim_data[i].anim_end_offset - 1)
1570 continue;
1571
1572 switch(anim_data[i].anim_type)
1573 {
1574 case anim_loopf:
1575 // increment forward
1576 anim_data[i].anim_current_offset++;
1577
1578 if (anim_data[i].anim_current_offset >= anim_data[i].anim_end_offset)
1579 {
1580 anim_data[i].anim_current_offset = anim_data[i].anim_start_offset;
1581 }
1582
1583 break;
1584
1585 case anim_loopr:
1586 // increment backwards
1587 anim_data[i].anim_current_offset--;
1588
1589 if (anim_data[i].anim_current_offset < anim_data[i].anim_start_offset)
1590 {
1591 anim_data[i].anim_current_offset = anim_data[i].anim_end_offset - 1;
1592 }
1593
1594 break;
1595
1596 case anim_ppff:
1597 case anim_ppfr:
1598 // increment forward
1599 anim_data[i].anim_current_offset++;
1600
1601 if (anim_data[i].anim_current_offset >= anim_data[i].anim_end_offset)
1602 {
1603 // switch type to move backwards
1604 anim_data[i].anim_type = anim_pprf;
1605 anim_data[i].anim_current_offset = anim_data[i].anim_end_offset - 2;
1606 }
1607
1608 break;
1609 case anim_pprr:
1610 case anim_pprf:
1611 // increment backwards
1612 anim_data[i].anim_current_offset--;
1613
1614 if (anim_data[i].anim_current_offset < anim_data[i].anim_start_offset)
1615 {
1616 // switch type to move forwards
1617 anim_data[i].anim_type = anim_ppfr;
1618 anim_data[i].anim_current_offset = anim_data[i].anim_start_offset + 1;
1619 }
1620
1621 break;
1622 case anim_once:
1623 // increment forward
1624 anim_data[i].anim_current_offset++;
1625
1626 if (anim_data[i].anim_current_offset >= anim_data[i].anim_end_offset)
1627 {
1628 anim_data[i].anim_type = anim_onces;
1629 anim_data[i].anim_current_offset = anim_data[i].anim_start_offset;
1630 }
1631
1632 break;
1633
1634 case anim_onceh:
1635 // increment forward
1636 anim_data[i].anim_current_offset++;
1637
1638 if (anim_data[i].anim_current_offset >= anim_data[i].anim_end_offset)
1639 {
1640 anim_data[i].anim_type = anim_oncehs;
1641 anim_data[i].anim_current_offset = anim_data[i].anim_end_offset - 1;
1642 }
1643
1644 break;
1645
1646 case anim_onces:
1647 case anim_oncehs:
1648 // do nothing here
1649 break;
1650
1651 case anim_end:
1652 case anim_none:
1653 default:
1654
1655 // do nothing
1656 break;
1657
1658 }
1659 }
1660 }
1661
1662 }
1663
1664
1665 CL_FlexibleMap::CL_BlockData *CL_FlexibleMap::get_block(short index)
1666 {
1667
1668 if (index > 0)
1669 return &block_data[index];
1670 else if (index < 0)
1671 return NULL;
1672 else
1673 return NULL;
1674 }
1675
1676
1677 CL_FlexibleMap::CL_AnimData *CL_FlexibleMap::get_anim(short index)
1678 {
1679
1680 if (index < 0)
1681 return &anim_data[(-index) - 1];
1682 else
1683 return NULL;
1684
1685 }
1686
1687
1688
1689 bool CL_FlexibleMap::set_current_layer(int layer)
1690 {
1691 if (layer < 0 || layer >= get_layer_count())
1692 return false;
1693
1694 current_layer = layer;
1695
1696 return true;
1697 }
1698
1699
1700
1701 int CL_FlexibleMap::get_layer_count() const
1702 {
1703 return (signed)layers.size();
1704 }
1705
1706
1707 void CL_FlexibleMap::set_display_area(const CL_Rect &area)
1708 {
1709 for (int i = 0; i < (signed) layers.size(); i++)
1710 layers[i]->set_display_area(area);
1711 }
1712
1713 const CL_Rect &CL_FlexibleMap::get_display_area(int layer_id)
1714 {
1715 if (layer_id < 0 || layer_id >= (signed) layers.size())
1716 layer_id = current_layer;
1717
1718 return layers[current_layer]->get_display_area();
1719 }
1720
1721
1722
1723 void CL_FlexibleMap::set_anchor_point(const CL_Origin &origin)
1724 {
1725 for (int i = 0; i < (signed) layers.size(); i++)
1726 layers[i]->set_anchor_point(origin);
1727 }
1728
1729 void CL_FlexibleMap::get_anchor_point(CL_Origin &origin, int layer_id)
1730 {
1731 if (layer_id < 0 || layer_id >= (signed) layers.size())
1732 layer_id = current_layer;
1733
1734 layers[current_layer]->set_anchor_point(origin);
1735 }
1736
1737
1738
1739 ///////////////////////////////////////////////////////////////////////////
1740 //
1741 // CL_FlexibleMap::CL_Layer functions
1742 //
1743 CL_FlexibleMap::CL_Layer::CL_Layer(CL_FlexibleMap *mappy_owner) :
1744 owner(*mappy_owner), size(0), data(NULL), draw_point(0, 0),
1745 layer_id(-1), alpha(255), default_tile(0), border_style(border_none),
1746 origin(origin_top_left)
1747 {
1748
1749 }
1750
1751 CL_FlexibleMap::CL_Layer::CL_Layer(const CL_Layer &copy) : owner(copy.owner)
1752 {
1753 size = copy.size;
1754 draw_point = copy.draw_point;
1755 layer_id = copy.layer_id;
1756 alpha = copy.alpha;
1757 default_tile = copy.default_tile;
1758 border_style = copy.border_style;
1759 origin = copy.origin;
1760 display_area = copy.display_area;
1761
1762 data = new short[size];
1763
1764 memcpy(data, copy.data, size * sizeof(short));
1765 }
1766
1767 CL_FlexibleMap::CL_Layer::~CL_Layer()
1768 {
1769 delete [] data;
1770 data = NULL;
1771 }
1772
1773
1774 void CL_FlexibleMap::CL_Layer::set_draw_point(int x, int y)
1775 {
1776 draw_point.x = x;
1777 draw_point.y = y;
1778 }
1779
1780 void CL_FlexibleMap::CL_Layer::set_draw_point(const CL_Point &pt)
1781 {
1782 draw_point = pt;
1783 }
1784
1785 void CL_FlexibleMap::CL_Layer::set_alpha(int alp)
1786 {
1787 if (alp < 0)
1788 alp = 0;
1789 else if (alp > 255)
1790 alp = 255;
1791
1792 alpha = alp;
1793 }
1794
1795 void CL_FlexibleMap::CL_Layer::set_border_style(CL_FlexibleMap::BorderStyle value)
1796 {
1797 if (value >= 0 && value < 4)
1798 border_style = value;
1799 }
1800
1801 short int CL_FlexibleMap::CL_Layer::get_map_data(int x, int y)
1802 {
1803 int width = owner.MPHD.mapwidth;
1804
1805
1806 if (x < 0 || y < 0 || x >= owner.MPHD.mapwidth || y >= owner.MPHD.mapheight)
1807 {
1808 // now we need to see how the layer should handle it
1809 switch(get_border_style())
1810 {
1811 case border_none:
1812 return NULL;
1813
1814 case border_wrap:
1815 {
1816 while (x < 0) x += owner.MPHD.mapwidth;
1817 while (x >= owner.MPHD.mapwidth) x -= owner.MPHD.mapwidth;
1818 while (y < 0) y += owner.MPHD.mapheight;
1819 while (y >= owner.MPHD.mapheight) y -= owner.MPHD.mapheight;
1820 }
1821
1822 break;
1823
1824 case border_default_tile:
1825 return default_tile;
1826
1827 }
1828
1829
1830 }
1831
1832 return data[y * width + x];
1833 }
1834
1835 void CL_FlexibleMap::CL_Layer::set_map_data(int x, int y, int value)
1836 {
1837 int width = owner.MPHD.mapwidth;
1838
1839 data[y * width + x] = value;
1840 }
1841
1842 void CL_FlexibleMap::CL_Layer::set_display_area(const CL_Rect &area)
1843 {
1844 display_area = area;
1845 }
1846
1847
1848 void CL_FlexibleMap::CL_Layer::set_anchor_point(const CL_Origin &origin)
1849 {
1850 this->origin = origin;
1851 }