gcsx_layer.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /* GCSx
  2. ** LAYER.H
  3. **
  4. ** Layer storage format and layer-related functions
  5. ** Doesn't include any editor-only functionality
  6. */
  7. /*****************************************************************************
  8. ** Copyright (C) 2003-2006 Janson
  9. **
  10. ** This program is free software; you can redistribute it and/or modify
  11. ** it under the terms of the GNU General Public License as published by
  12. ** the Free Software Foundation; either version 2 of the License, or
  13. ** (at your option) any later version.
  14. **
  15. ** This program is distributed in the hope that it will be useful,
  16. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ** GNU General Public License for more details.
  19. **
  20. ** You should have received a copy of the GNU General Public License
  21. ** along with this program; if not, write to the Free Software
  22. ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
  23. *****************************************************************************/
  24. #ifndef __GCSx_LAYER_H_
  25. #define __GCSx_LAYER_H_
  26. // (should never be used as a LoadOnly item- always called from a scene;
  27. // doesn't follow spec exactly, ie LoadContent doesn't kill FileRead ptr, etc.)
  28. class Layer /* : virtual public LoadOnly */ {
  29. public:
  30. // Types of layers available
  31. enum LayerType {
  32. LAYER_EMPTY,
  33. LAYER_TILE,
  34. LAYER_IMAGE,
  35. LAYER_FONT,
  36. };
  37. // Bitmasks for layer data
  38. enum {
  39. // Layer data
  40. LAYER_TILE_INDEX = 0x0000FFFF,
  41. LAYER_TILE_COLOR = 0x0FFF0000,
  42. LAYER_TILE_COLOR_R = 0x000F0000,
  43. LAYER_TILE_COLOR_G = 0x00F00000,
  44. LAYER_TILE_COLOR_B = 0x0F000000,
  45. LAYER_TILE_COLOR_BITDEPTH = 4,
  46. LAYER_TILE_COLOR_SHIFT = 16,
  47. LAYER_TILE_FLIP = 0x10000000,
  48. LAYER_TILE_MIRROR = 0x20000000,
  49. LAYER_TILE_ROTATE = 0x40000000,
  50. LAYER_TILE_ORIENTATION = 0x70000000,
  51. LAYER_TILE_COLLISION = 0x80000000,
  52. // Extended data
  53. LAYER_EXT_COLL = 0x000007FF,
  54. LAYER_EXT_COLLTYPE = 0x0000F800,
  55. LAYER_EXT_ALPHA = 0x000F0000,
  56. LAYER_EXT_ALPHA_BITDEPTH = 4,
  57. LAYER_EXT_ALPHA_SHIFT = 16,
  58. LAYER_EXT_ANIMON = 0x00100000,
  59. LAYER_EXT_ANIMREV = 0x00200000,
  60. LAYER_EXT_UNUSED = 0xFFC00000,
  61. // Effects data
  62. LAYER_FX_UNDEFINED = 0xFFFFFFFF,
  63. // Default layer and effects when empty/clearing (no collision, for optimized)
  64. LAYER_TILE_DEFAULT = LAYER_TILE_COLOR,
  65. LAYER_EXT_DEFAULT = LAYER_EXT_ALPHA,
  66. LAYER_FX_DEFAULT = 0,
  67. // Default mask used when flood filling/wand selecting
  68. LAYER_TILE_FILL_MASK= LAYER_TILE_INDEX | LAYER_TILE_COLOR | LAYER_TILE_FLIP | LAYER_TILE_MIRROR | LAYER_TILE_ROTATE,
  69. };
  70. protected:
  71. std::string name;
  72. std::string nameL;
  73. class World* world;
  74. class Scene* scene;
  75. int cached; // If we haven't been asked to loadContent yet
  76. int lockCount;
  77. int lockCountPlay;
  78. // Numeric ID- computer-generated- unique to world- nonzero
  79. // Most functions refer to layers by their scene and z position,
  80. // but this is used by undo and a few other obscure things
  81. int id;
  82. // Type of layer (only LAYER_EMPTY and LAYER_TILE supported at this time)
  83. LayerType layerType;
  84. // Layer size in tiles for LAYER_TILE, in pixels for _FONT and _IMAGE
  85. int xSize;
  86. int ySize;
  87. // Spawns on this layer- ptrs owned by us; no NULL ptrs allowed
  88. // Vector, for quick traversal (initial spawn/display) and quick searching/sorting
  89. std::vector<class Spawn*> spawns; // part of "header"
  90. // LAYER_TILE layers
  91. // Tile set being used
  92. // These should remain static (other than being cached/uncached) during
  93. // normal gameplay; LayerEdit class knows how to handle them changing
  94. // and will be kept updated by appropriate methods; tileset cannot be
  95. // deleted if in use by any layer anywhere
  96. int tilesetId; // If 0, no tileset selected yet
  97. class TileSet* tileset; // Not owned by us
  98. // Layer data-
  99. // 16bits - tile index
  100. // 12bits - tile color (RGB 4bits each)
  101. // 3bits - orientation- flip, mirror, rotate90 (affects default collision maps too)
  102. // 1bit - collisions- default enabled (default map, type 0)
  103. // (this bit is ignored if collision data present)
  104. Uint32* tileData; // CACHEABLE
  105. // Extended data- (optional)
  106. // 11bits - collision map- 0 all off, 7FF all on, other user-defined
  107. // 5bits - collision type (0 to 31)
  108. // 4bits - translucency (alpha)
  109. // 2bits - animation (enabled, reverse)
  110. // 10bits - (unused currently)
  111. Uint32* extendedData; // CACHEABLE
  112. int usesExtended;
  113. // Effects data- (optional) (unused currently)
  114. Uint32* effectsData; // CACHEABLE
  115. int usesEffects;
  116. enum {
  117. // Default tile layer size in each direction
  118. LAYER_TILE_DEFAULT_SIZE = 64,
  119. };
  120. virtual int doLock(int play) throw_File;
  121. virtual int doUnlock(int play);
  122. void loadHeaderNonSpawns(class FileRead* file);
  123. // NON-EDITOR, GAMEPLAY DATA (not saved by editor)
  124. // Sprite lookup by name- this layer only
  125. SpriteIndex spritesById;
  126. // Sprites in display order, for this layer only
  127. SpriteOrder spritesByOrder;
  128. // Sub-functions for draw routine
  129. void drawTile(int viewX, int viewY);
  130. void drawSprites(int viewX, int viewY);
  131. public:
  132. // Creates a default properties (empty) layer
  133. // Id must be unique; use ID of 0 if we're going to load anyways
  134. Layer(class World* myWorld, class Scene* myScene, int myId = 0);
  135. virtual ~Layer();
  136. // Accessors
  137. const std::string& getName() const { return name; }
  138. const std::string& getNameL() const { return nameL; }
  139. const class World* getWorld() const { return world; }
  140. class World* getWorld() { return world; }
  141. const class Scene* getScene() const { return scene; }
  142. class Scene* getScene() { return scene; }
  143. LayerType getType() const { return layerType; }
  144. int getXSize() const;
  145. int getYSize() const;
  146. int getId() const { return id; }
  147. int getBlockType() const { return WorldFileLoad::BLOCKTYPE_UNUSED; }
  148. // LAYER_TILE accessors
  149. const class TileSet* getTileSet() const { return tileset; }
  150. class TileSet* getTileSet() { return tileset; }
  151. int getUsesExt() const { return usesExtended; }
  152. int getUsesFx() const { return usesEffects; }
  153. // Load data from file- only Scene should call these
  154. // Layer assumes scene will call us with these when appropriate
  155. // loadHeader sets cached to 1, loadContent changes it back to 0
  156. virtual void loadHeader(class FileRead* file) throw_File;
  157. void loadContent(class FileRead* file) throw_File;
  158. void cacheLoad();
  159. // Recache data (just content) set cached back to 1
  160. // Layer assumes scene will call us with this only when we're not in use
  161. // and assumes scene won't call this if we're modified
  162. void recacheContent();
  163. // Marks tileset or any other resources we use as locked/unlocked
  164. // 'play' versions don't retain source tileset data- only textures
  165. int markLock() throw_File;
  166. int markLockPlay() throw_File;
  167. int markUnlock();
  168. int markUnlockPlay();
  169. int isContentCached() const;
  170. int isLocked() const;
  171. // GAMEPLAY
  172. // working with sprites
  173. // First places in all indices including world's- call on create/delete
  174. // Also activates/deactivates if we're locked
  175. void indexSprite(Sprite* addSprite);
  176. void deindexSprite(Sprite* remSprite);
  177. // Second modifies priority order only- use only when just changing priority
  178. void orderSprite(Sprite* addSprite);
  179. void deorderSprite(Sprite* remSprite);
  180. // do initial spawning- each spawn becomes an object/sprite
  181. void spawnLayer();
  182. // draws onscreen, over anything existing
  183. // draws sprites as well
  184. void draw(int viewX, int viewY);
  185. };
  186. #endif