PolySceneSprite.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. Copyright (C) 2013 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #pragma once
  20. #include "PolyGlobals.h"
  21. #include "PolyScenePrimitive.h"
  22. #include "PolyCore.h"
  23. #include "PolyResourceManager.h"
  24. #include <vector>
  25. namespace Polycode {
  26. class SpriteFrame {
  27. public:
  28. Polycode::Rectangle coordinates;
  29. Vector2 anchorPoint;
  30. unsigned int frameID;
  31. };
  32. class SpriteSet;
  33. class SpriteState {
  34. public:
  35. SpriteState(SpriteSet *spriteSet, String name);
  36. void setName(String name);
  37. String getName() const;
  38. void POLYIGNORE appendFrames(std::vector<unsigned int> newFrameIDs);
  39. unsigned int getNumFrameIDs();
  40. unsigned int getFrameIDAtIndex(unsigned int index);
  41. Mesh *getMeshForFrameIndex(unsigned int index);
  42. void insertFrame(unsigned int index, unsigned int frameID);
  43. void POLYIGNORE setNewFrameIDs(std::vector<unsigned int> newIDs);
  44. void removeFrameByIndex(unsigned int frameIndex);
  45. void POLYIGNORE removeFrameIndices(std::vector<unsigned int> indices);
  46. void clearFrames();
  47. void setPixelsPerUnit(Number ppu);
  48. Number getPixelsPerUnit();
  49. void rebuildStateMeshes();
  50. void setStateFPS(Number fps);
  51. Number getStateFPS();
  52. Vector3 getLargestFrameBoundingBox() const;
  53. void setBoundingBox(Vector2 boundingBox);
  54. Vector2 getBoundingBox();
  55. Vector2 getSpriteOffset();
  56. void setSpriteOffset(const Vector2 &offset);
  57. protected:
  58. Vector3 largestFrameBoundingBox;
  59. Vector2 boundingBox;
  60. Vector2 spriteOffset;
  61. Number pixelsPerUnit;
  62. Number stateFPS;
  63. SpriteSet *spriteSet;
  64. String name;
  65. std::vector<unsigned int> frameIDs;
  66. std::vector<Mesh*> frameMeshes;
  67. };
  68. class SpriteSet;
  69. class Sprite : public Resource {
  70. public:
  71. Sprite(String name);
  72. ~Sprite();
  73. String getName();
  74. void setName(String name);
  75. void addSpriteState(SpriteState *state);
  76. void removeSpriteState(SpriteState *state);
  77. unsigned int getNumStates();
  78. SpriteState *getState(unsigned int index);
  79. SpriteState *getStateByName(const String &name);
  80. void setParentSpritSet(SpriteSet *spriteSet);
  81. SpriteSet *getParentSpriteSet();
  82. protected:
  83. String name;
  84. SpriteSet *parentSpriteSet;
  85. std::vector<SpriteState*> states;
  86. };
  87. class SpriteSet : public ResourcePool {
  88. public:
  89. SpriteSet(const String &fileName, ResourcePool *parentPool = CoreServices::getInstance()->getResourceManager()->getGlobalPool());
  90. ~SpriteSet();
  91. void setTexture(Texture *texture);
  92. Texture *getTexture();
  93. Texture *loadTexture(String imageFileName);
  94. void addSpriteEntry(Sprite *newEntry);
  95. unsigned int getNumSpriteEntries() const;
  96. Sprite *getSpriteEntry(unsigned int index) const;
  97. void removeSprite(Sprite *sprite);
  98. void loadSpriteSet(String fileName);
  99. // frame manipulation
  100. void addSpriteFrame(const SpriteFrame &frame, bool assignID = true);
  101. unsigned int getNumFrames() const;
  102. SpriteFrame getSpriteFrame(unsigned int index) const;
  103. SpriteFrame getSpriteFrameByID(unsigned int frameID) const;
  104. void removeFrameByID(unsigned int frameID);
  105. void setSpriteFrame(const SpriteFrame &frame);
  106. void clearFrames();
  107. // automatic frame generation
  108. void createGridFrames(Number width, Number height, const Vector2 &defaultAnchor);
  109. void createFramesFromIslands(unsigned int minDistance, const Vector2 &defaultAnchor);
  110. Sprite *getSpriteByName(String spriteName);
  111. protected:
  112. unsigned int nextFrameIDIndex;
  113. Texture *spriteTexture;
  114. std::vector<SpriteFrame> frames;
  115. std::vector<Sprite*> sprites;
  116. };
  117. class SceneSprite : public SceneMesh {
  118. public:
  119. SceneSprite(SpriteSet *spriteSet);
  120. ~SceneSprite();
  121. Entity *Clone(bool deepClone, bool ignoreEditorOnly) const;
  122. void applyClone(Entity *clone, bool deepClone, bool ignoreEditorOnly) const;
  123. SpriteSet *getSpriteSet();
  124. Sprite *getCurrentSprite();
  125. void handleEvent(Event *event);
  126. void setSpriteSet(SpriteSet *spriteSet);
  127. void setSpriteByName(String spriteName);
  128. void setCurrentFrame(unsigned int frameIndex);
  129. unsigned int getCurrentFrame();
  130. void Update();
  131. void Render();
  132. Vector3 getSpriteBoundingBox() const;
  133. void setPaused(bool val);
  134. bool isPaused();
  135. void setSprite(Sprite *spriteEntry);
  136. void setSpriteState(SpriteState *spriteState, unsigned int startingFrame, bool playOnce);
  137. void setSpriteStateByName(String name, unsigned int startingFrame, bool playOnce);
  138. SpriteState *getCurrentSpriteState();
  139. bool getStartOnRandomFrame();
  140. void setStartOnRandomFrame(bool val);
  141. protected:
  142. Vector3 spriteBoundingBox;
  143. bool startOnRandomFrame;
  144. bool playOnce;
  145. bool paused;
  146. Core *core;
  147. unsigned int currentFrame;
  148. Mesh *defaultMesh;
  149. Sprite *currentSprite;
  150. SpriteState *currentSpriteState;
  151. SpriteSet *spriteSet;
  152. Number spriteTimer;
  153. Number spriteTimerVal;
  154. };
  155. }