Debian Codegraph @

Contrib/CL_FlexibleMap/FMPExample/CL_FlexibleMap.h

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
32 #include <string>
33
34 #include <clanlib/core.h>
35 #include <clanlib/display.h>
36 #include <clanlib/gl.h>
37
38 #include <vector>
39
40 struct CL_FlexibleMapDrawParams;
41
42 //: Structure for unknown chunks of the file.
43 struct CL_FlexibleMapChunk
44 {
45 //! Construction:
46 public:
47 CL_FlexibleMapChunk() : data(NULL), size(0) {}
48 ~CL_FlexibleMapChunk() { free(); }
49
50 void free() { delete [] data; data = NULL; size = 0; }
51
52 //! Implementation:
53 public:
54
55 //: Four-byte chunk identifier
56 std::string name;
57
58 //: Indicates the number of bytes stored in data
59 int size;
60
61 //: Pointer to the actual data. Freed when the structure is deleted.
62 unsigned char *data;
63
64 };
65
66
67 //: Class for loading and displaying a .FMP file
68 //- <p>This class is designed to load in files produced by the program Mappy,
69 //- It will automatically generate CL_Surface data for the tile graphics data
70 //- stored in the FMP file. It supports non-square tiles. So far it works with
71 //- all the sample maps that come with Mappy.</p>
72 class CL_FlexibleMap
73 {
74 //! Enums:
75 public:
76
77 //: Error enumeration to give idea of what the last error was.
78 enum CL_FlexibleMapError
79 {
80 ok = 0,
81 notfmap = 1,
82
83 bad_ATHR,
84 bad_MPHD,
85 bad_EDHD,
86 bad_CMAP,
87 bad_BKDT,
88 bad_ANDT,
89 bad_BGFX,
90 bad_BODY_LYR,
91
92 out_of_memory,
93 could_not_convert_tiles,
94 unsupported_map_type,
95
96
97 };
98
99 //: Enum for layers indicating how tiles outside the map rectangle should be handled.
100 enum BorderStyle
101 {
102 border_none = 0,
103 border_wrap,
104 border_default_tile,
105 };
106
107 //: Enum for the various animation types that can be selected.
108 enum AnimType
109 {
110 anim_end = -1,
111 anim_none = 0,
112 anim_loopf = 1,
113 anim_loopr = 2,
114 anim_once = 3,
115 anim_onceh = 4,
116 anim_ppff = 5,
117 anim_pprr = 6,
118 anim_pprf = 7,
119 anim_ppfr = 8,
120 anim_onces = 9,
121 anim_oncehs = 10,
122 };
123
124
125 //! Construction:
126 public:
127 //: Constructs a single Mappy reader object
128 CL_FlexibleMap(void);
129 CL_FlexibleMap(const CL_FlexibleMap& copy);
130
131 ~CL_FlexibleMap(void);
132
133 //: Frees all memory and erases everything that has to do with the current map.
134 void clear();
135
136 //! Implementation:
137 public:
138
139 //: Class for storing information about a layer
140 class CL_Layer
141 {
142 public:
143 CL_Layer(CL_FlexibleMap *mappy_owner);
144 CL_Layer(const CL_Layer &copy);
145
146 ~CL_Layer();
147
148
149
150 //: Comparison operator for ordering layers. Compares layer_id data.
151 static compare(const CL_Layer *left, const CL_Layer *right) { return left->layer_id < right->layer_id; }
152
153 //: Sets the drawing point for this layer
154 void set_draw_point(int x, int y);
155 void set_draw_point(const CL_Point& pt);
156
157 //: Fills the passed parameters with the current draw point.
158 void get_draw_point(/*out*/ int &x, /*out*/ int &y) const { x = draw_point.x; y = draw_point.y; }
159 void get_draw_point(/*out*/ CL_Point &pt) const { pt = draw_point; }
160
161
162 //: Sets transparency for the layer
163 void set_alpha(int alp);
164
165 //: Returns the overall transparency for this layer.
166 int get_alpha() const { return alpha; }
167
168
169 //: Indicates how we handle tiles that are picked outside the rectangle making up the map
170 BorderStyle get_border_style() { return border_style; }
171
172 //: Sets how we handle tiles that are picked outside the rectangle making up the map
173 void set_border_style(BorderStyle value);
174
175 //: Sets the default tile for when border_style == border_default_tile
176 void set_default_tile(short tile) { default_tile = tile; }
177
178 //: returns map data at the specified point
179 short int get_map_data(int x, int y);
180
181 //: sets map data at a specified point
182 void set_map_data(int x, int y, int value);
183
184 //: Sets the region of the window in which things are actually drawn for this layer.
185 //- It's not recommended to change this for each layer individually, unless you really
186 //- know what you're doing.
187 void set_display_area(const CL_Rect &area);
188
189 //: Returns the region of the window in which things are actually drawn for this layer.
190 const CL_Rect &get_display_area() { return display_area; }
191
192 //: Sets the point within the display area that the drawpoint is interpreted to be
193 //- <p>This function will set the origin draw point for this layer within the
194 //- display area. That is, if you call set_anchor_point(CL_Origin::origin_center), then
195 //- call set_draw_point(0, 0), the pixel point (0, 0) will be drawn in the center
196 //- of the area the map is drawn in. This can be set on each layer
197 //- individually, but you probably want to use CL_FlexibleMap::set_anchor_point.
198 void set_anchor_point(const CL_Origin &origin);
199
200 //: Returns the point within the display area that the drawpoint is interpreted to be
201 void get_anchor_point(CL_Origin &origin) { origin = this->origin; }
202
203 private:
204
205 friend class CL_FlexibleMap;
206 CL_FlexibleMap &owner;
207
208 //: Size of map in tiles
209 unsigned int size;
210
211 //: Data containing indices into BlockData (positive) or AnimData (negative)
212 short int *data;
213
214 //: ID of this layer in the FMP
215 int layer_id;
216
217 //: Position to draw the layer
218 CL_Point draw_point;
219
220 //: Alpha blending of the layer.
221 int alpha;
222
223 //: Handling requests outside of the map
224 BorderStyle border_style;
225
226 //: The block structure (positive) or animation structure(negative) for drawing outside of the map
227 short int default_tile;
228
229 //: The display area in the window in which this layer's contents will be drawn.
230 CL_Rect display_area;
231
232 CL_Origin origin;
233
234 };
235
236 //: Structure for data blocks
237 class CL_BlockData
238 {
239 public:
240
241 //: Offsets from start of graphic blocks for the pixels that are actually drawn
242 long int bg_offset,
243 fg_offset,
244 fg2_offset,
245 fg3_offset;
246
247 //: 32-bit user data
248 unsigned long int user1, user2;
249
250 //: 16-bit user data
251 unsigned short int user3, user4;
252
253 //: 8-bit user data
254 unsigned char user5, user6, user7;
255
256 //: bits for collision detection
257 unsigned char tl;
258 unsigned char tr;
259 unsigned char bl;
260 unsigned char br;
261
262 //: User bit
263 //- Called trigger (or bg transparency) in mappy, but it's not used internally by this class
264 //- So it's really a user bit
265 unsigned char trigger;
266
267 //: More user bits
268 //- these are often called unused1,2,3 in the mappy source. They are user bits, unless
269 //- you are working with an isometric map with pillars. Then they are used to indicate how
270 //- pillars should be displayed.
271 unsigned char u1_attach_next; // Attach next
272 unsigned char u2_left_pillar; // left pillar
273 unsigned char u3_right_pillar; // right pillar
274
275 };
276
277 //: Structure for animation blocks
278 class CL_AnimData
279 {
280 // unfortunately, mappy doesn't output the size of these structures used, as it does
281 // for the BLKSTR or CL_BlockData structures, so for
282 // now I am using a hard-coded 16 bytes in the variable anim_data_size to figure out
283 // how many structures to read in.
284 public:
285 signed char anim_type; /* Type of anim, AN_? */
286 signed char anim_delay; /* Frames to go before next frame */
287 signed char anim_count; /* Counter, decs each frame, till 0, then resets to andelay */
288 signed char user; /* User info */
289 long int anim_current_offset; /* Points to current offset in list */
290 long int anim_start_offset; /* Points to start of blkstr offsets list, AFTER ref. blkstr offset */
291 long int anim_end_offset; /* Points to end of blkstr offsets list */
292
293
294
295 };
296
297
298 //: Size of block data structure used in Mappy
299 static const int block_data_size = 32; //sizeof(CL_BlockData);
300
301 //: Size of animation structure used in Mappy
302 static const int anim_data_size = 16; //sizeof(CL_AnimData);
303
304
305 //! Operations:
306 public:
307 //: Begins the drawing loop
308 void begin_draw_block_layer(CL_FlexibleMapDrawParams &params);
309
310 //: Draws a single horizontal line of tiles
311 void draw_next_line(CL_FlexibleMapDrawParams &params);
312
313 //: Returns true when all lines have been drawn
314 bool done_drawing(CL_FlexibleMapDrawParams &params);
315
316 //; Ends drawing
317 void end_draw_block_layer(CL_FlexibleMapDrawParams &params);
318
319
320
321 //: Draws the background layer
322 //- <p>Tiles have four layers. This function will draw the very bottom
323 //- one for all the visible tiles. There are multiple of these functions
324 //- in order to allow drawing of sprites between background and foreground.</p>
325 void draw_background () { draw_block_layer(0); }
326
327 //: Draws the foreground layer
328 //- <p>Tiles have four layers. This function will draw the second
329 //- one for all the visible tiles.</p>
330 void draw_foreground () { draw_block_layer(1); }
331
332 //: Draws the foreground layer
333 //- <p>Tiles have four layers. This function will draw the third
334 //- one for all the visible tiles.</p>
335 void draw_foreground2() { draw_block_layer(2); }
336
337 //: Draws the foreground layer
338 //- <p>Tiles have four layers. This function will draw the fourth
339 //- one for all the visible tiles.</p>
340 void draw_foreground3() { draw_block_layer(3); }
341
342 //: Draws a given block layer on the screen
343 //- An alternative to the draw_background(), draw_foreground(), etc. functions,
344 //- which can be used in a loop.
345 void draw_block_layer(int block_layer);
346
347 //: Draws a single block, at the specified screen point.
348 //- param draw_x: Back buffer x-position to draw at
349 //- param draw_y: Back buffer y-position to draw at
350 //- param alpha: Alpha blending value to use, in the range of (0-255)
351 //- param block_index: Which block structure (positive) or animation structure (negative) to draw.
352 //- param block_layer: 0-3 are valid inputs only. Selects background(0) or one of the three foreground layers
353 //- param do_attached: For pillars, if we are to draw blocks attached above the pillar (only for block_layer == 3)
354 //- param is_attached: Used internally for recursive calls to the function. Leave as the default.
355 void draw_block(int draw_x, int draw_y, int alpha, int block_index,
356 int block_layer, bool do_attached, bool is_attached = false);
357
358
359 //: Updates all the animation structures
360 void update_anim();
361
362 //: Load the FMP data
363 //- Returns true if loading of the map was successful. Otherwise, check
364 //- get_last_error().
365 //- param filename: Relative path and file name of FMP file to load
366 bool load(const std::string& filename);
367
368 //! Attributes:
369 public:
370 //: If the load method returns false, this will give a basic description of the error.
371 CL_FlexibleMapError get_last_error() { return last_error; }
372
373 //: Sets the current layer for drawing
374 //- Returns false if failed (ie. parameter is >= get_layer_count())
375 bool set_current_layer(int layer);
376
377 //: Returns the index of the current layer
378 int get_current_layer_index() const { return current_layer; }
379
380
381 //: Returns a reference to the current layer
382 //- If you are storing this in a variable, make sure it is a reference or pointer
383 //- variable to avoid invoking a costly copy constructor.
384 CL_Layer &get_current_layer() { return *layers[current_layer]; }
385
386
387 //: Returns the total number of layers in the map
388 int get_layer_count() const;
389
390
391 //: Returns the tile indicated at the position on the map
392 //- This function will return the actual map data. If it is positive or zero, it indicates
393 //- an index in the CL_BlockData array. If it's negative, it's an index into the CL_AnimData array.
394 //- param x: x position in tiles on the map (between 0 and map_width - 1)
395 //- param y: y position in tiles on the map (between 0 and map_height - 1)
396 //- param layer: a shortcut to using layer pointers. The default value will look up the tile on the current layer.
397 short int get_map_data(int x, int y, int layer = -1);
398
399
400 //: Sets frame-rate independent animations
401 //- This function will set a flag indicating to the map to use frame-rate independent tile
402 //- animation.
403 //- param increment_time: The number of milliseconds to pass for each value of anim_delay that is set in Mappy.
404 //- param use_fia: flag to indicate whether to use frame-rate independent animation or not
405 void set_frame_independent_animation(int increment_time, bool use_fia = true);
406
407 //: Returns the characteristic time length for frame-rate independent animations.
408 //- Returns zero if frame-rate independent animations are off. Otherwise, returns the number
409 //- of milliseconds between each decrement of the anim_delay values.
410 int get_frame_increment_time();
411
412 //: Returns the flag indicating whether or not frame-rate independent animation is in use.
413 bool get_frame_independent_animation() { return frame_rate_independent_anims; }
414
415 //: Start up animate once tiles
416 //- Starts up tiles which run their animation once.
417 //- param anim_index: The index (either positive or negative) of the animation structure to run. The default value of zero will do all once run tiles.
418 //- param start_now: Indicates whether to immediately change the visible frame to the next one in sequence (true), or wait for the count to decrease (false).
419 void start_animate_once(int anim_index = 0, bool start_now = true);
420
421 //: Returns a CL_BlockData structure from the index given.
422 //- param index: A non-negative number indicating the CL_BlockData structure to return.
423 //- The zero tile is always transparent.
424 CL_BlockData *get_block(short index);
425
426 //: Returns a CL_AnimData structure from the index given.
427 //- param index: A negative number indicating the CL_BlockData structure to return.
428 CL_AnimData *get_anim(short index);
429
430
431 //: Gets the color key that is provided by the map.
432 //- On loading the map, colors matching the color key are automatically replaced with
433 //- a color with zero alpha.
434 unsigned int get_map_colorkey();
435
436
437 //: Returns the source rectangle for drawing a particular tile.
438 //- This should return (0, 0, MPHD.blockwidth, MPHD.blockheight)
439 CL_Rect get_src_rect(int tile);
440
441
442 //: Sets the region of the window in which things are actually drawn for all layers.
443 void set_display_area(const CL_Rect &area);
444
445
446 //: Returns the region of the window in which things are actually drawn for this layer.
447 //- param layer_id: a shortcut to using layer pointers. The default value will return the value for the current layer.
448 const CL_Rect &get_display_area(int layer_id = -1);
449
450
451 //: Sets the point within the display area that the drawpoint is interpreted to be
452 //- <p>This function will set the origin draw point for all layers within the
453 //- display area. That is, if you call set_anchor_point(CL_Origin::origin_center), then
454 //- call set_draw_point(0, 0), the pixel point (0, 0) will be drawn in the center
455 //- of the area the map is drawn in.
456 void set_anchor_point(const CL_Origin &origin);
457
458 //: Returns the point within the display area that the drawpoint is interpreted to be
459 void get_anchor_point(CL_Origin &origin, int layer_id = -1);
460
461
462 //: Returns the number of extra chunks in the file, that load didn't parse.
463 int get_extra_chunk_count();
464
465 //: Returns the specified chunk from Mappy
466 //- param identifier: String identifing which chunk to get (prefered access method)
467 //- param index: Integer from 0 to (get_extra_chunk_count() - 1) indexing which chunk to return. Good for enumeration, but chunks will not be in a specified order in a given file.
468 CL_FlexibleMapChunk* get_chunk(const std::string &identifier);
469 CL_FlexibleMapChunk* get_chunk(int index);
470
471
472 private:
473
474
475 CL_InputSource *map_source; // Source of map data
476 CL_FlexibleMapError last_error; // record of the last error
477
478 CL_Surface *tiles; // surface of image data for drawing tiles.
479
480 // actually does the reading, once the input source is selected
481 bool _load();
482
483 //: Returns how many bits to shift for a particular mask (888 type colors only)
484 //- This is included to help endian compatibility, but it needs to be tested
485 //- in a big-endian environment.
486 static int get_color_shift_from_mask(unsigned int color_mask);
487
488 //: Converts the raw image data in BGFX to a CL_Surface
489 bool load_tiles();
490
491 //: Reads a specified number of chars (usually 4)
492 void read_chars(std::string& dest, int count);
493
494 //: Reads a null terminated string. returns number of characters read.
495 int read_zero_terminated_string(std::string &dest, int maxlen);
496
497 // from the mappy 1.2 docs:
498 //These are the chunks as of V1.2:
499 //ATHR - Up to 4 ASCII strings of author information, separated by 0 values, always an even size.
500 //MPHD - Map header, see struct in the editor source download
501 //EDHD - Editor information, see struct in mappy.c
502 //CMAP - Colour palette for 8bit maps, red byte, green byte, blue byte for however many colours are needed (so usually 256*3 bytes).
503 //BKDT - Block data. Contains BLKSTR structures for however many block structures were made.
504 //ANDT - Animation data. Contains ANISTR structures for however many animation structures were made, and also animation data.
505 //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.
506 //BODY - An array of short ints containing positive offsets into BKDT, and negative offsets into ANDT.
507 //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.
508 //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.
509
510 bool parse_ATHR(const std::string& blockname, int size);
511 bool parse_MPHD(const std::string& blockname, int size);
512 bool parse_EDHD(const std::string& blockname, int size);
513 bool parse_CMAP(const std::string& blockname, int size);
514 bool parse_BKDT(const std::string& blockname, int size);
515 bool parse_ANDT(const std::string& blockname, int size);
516 bool parse_BGFX(const std::string& blockname, int size);
517 bool parse_BODY_LYR(const std::string& blockname, int size);
518 bool parse_custom_chunk(const std::string& blockname, int size);
519
520
521 ///////////////////////////////////////
522 //: ATHR data
523 std::string author_info[4];
524
525
526 ///////////////////////////////////////
527 // MPHD data - This struct is taken directly from the mappy source code,
528 // which is copyright Robin Burrows 1997-2005
529 //
530 struct { /* Map header structure */
531 /* char M,P,H,D; 4 byte chunk identification. */
532 /* long int mphdsize; size of map header. */
533 char mapverhigh; /* map version number to left of . (ie X.0). */
534 char mapverlow; /* map version number to right of . (ie 0.X). */
535 char lsb; /* if 1, data stored LSB first, otherwise MSB first. */
536
537 // documentation calls this reserved.. however a look at the playback
538 // library suggests this indicates 2D, Hex, or Isometric.
539 //char reserved;
540 char maptype;
541
542 short int mapwidth; /* width in blocks. */
543 short int mapheight; /* height in blocks. */
544 short int reserved1;
545 short int reserved2;
546 short int blockwidth; /* width of a block (tile) in pixels. */
547 short int blockheight; /* height of a block (tile) in pixels. */
548 short int blockdepth; /* depth of a block (tile) in planes (ie. 256 colours is 8) */
549 short int blockstrsize; /* size of a block data structure */
550 short int numblockstr; /* Number of block structures in BKDT */
551 short int numblockgfx; /* Number of 'blocks' in graphics (BODY) */
552
553 // added FMP0.4
554 unsigned char ckey8bit, ckeyred, ckeygreen, ckeyblue; /* colour key values added FMP0.4*/
555
556 // added FMP0.5
557 short int blockgapx;
558 short int blockgapy;
559 short int blockstaggerx;
560 short int blockstaggery;
561
562 short int clickmask;
563 short int pillar;
564
565 } MPHD;
566
567 //: Indicates whether or not to pay attention to the blockstaggerx and blockstaggery fields
568 bool use_stagger;
569
570 //: Version number of map loaded in
571 double map_version;
572
573 //: Returns the number of bytes per tile stored in FMP version 0.5
574 //- FMP 0.5, the default file format produced by Mappy, uses units of byte offsets in everything
575 //- rather than storing structure indices.
576 int get_FMP0_5_bytes_per_pixel();
577
578
579 ///////////////////////////////////////
580 // BKDT data
581
582 //: Stores all the BlockData structures for drawing on the screen.
583 //- To get how many total there are, see MPHD.numblockstr
584 CL_BlockData *block_data;
585
586
587 ///////////////////////////////////////
588 // ANDT data
589 CL_AnimData *anim_data;
590 int anim_data_count;
591 int *anim_frames;
592
593 unsigned int last_anim_update;
594 bool frame_rate_independent_anims;
595 unsigned int frame_increment_time;
596
597
598 ///////////////////////////////////////
599 // CMAP data
600 unsigned int *palette;
601 int palette_size;
602
603
604 ///////////////////////////////////////
605 // BGFX data
606 unsigned char *bgfx_data;
607
608
609 ///////////////////////////////////////
610 // BODY, LYR? data
611 std::vector<CL_Layer*> layers;
612
613 int current_layer;
614
615 CL_Rect src_rect; // source rect for drawing.. hardly ever changes
616
617
618 ///////////////////////////////////////
619 // Other data
620 std::map<std::string, CL_FlexibleMapChunk*> unknown_chunks;
621
622 };
623
624
625
626 //: Structure passed around in drawing the map.
627 struct CL_FlexibleMapDrawParams
628 {
629 //! Construction:
630 public:
631 CL_FlexibleMapDrawParams(CL_FlexibleMap::CL_Layer &source_layer, int blocklayer)
632 : layer(source_layer), block_border(1), block_layer(blocklayer) {}
633 ~CL_FlexibleMapDrawParams() {}
634
635 //! Implementation
636 public:
637
638 CL_FlexibleMap::CL_Layer &layer;
639
640 //: Which block layer to draw
641 int block_layer;
642
643 //: Blocks to draw outside the clipping rectangle to insure smooth scrolling
644 int block_border;
645
646 //: Indicates whether or not the row drawn was staggered
647 bool stagger_this_row;
648
649 //: Number of pixels to stagger drawing
650 CL_Point eff_stagger;
651
652 //: pixel point on the layer at which to use for the top left of the display
653 CL_Point source;
654
655 //: tile at which to start drawing
656 CL_Point start_tile;
657
658 //: pixel point in window at which to start drawing
659 CL_Point start;
660
661 //: pixel offset to incorporate smooth scrolling
662 CL_Point offset;
663
664 //: height and width minimum to draw
665 CL_Size draw_size;
666
667
668 //: Draw point iterated through
669 CL_Point draw;
670
671 //: tiles iterated through
672 CL_Point tile;
673
674 //: whether or not to draw isometric pillars
675 bool draw_attached;
676
677 //: The on-screen point at which drawing of the last row started
678 CL_Point last_row_start;
679
680 //; The point in tiles on the map at which drawing of the last row started
681 CL_Point last_row_start_tile;
682
683
684
685 private:
686 //: The old saved clipping region
687 CL_Rect old_clip_rect;
688
689 //: The clip region being used.
690 CL_Rect clip_rect;
691
692
693 friend class CL_FlexibleMap;
694
695 };