PolySceneSprite.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. void setBoundingBox(Vector2 boundingBox);
  53. Vector2 getBoundingBox();
  54. Vector2 getSpriteOffset();
  55. void setSpriteOffset(const Vector2 &offset);
  56. protected:
  57. Vector2 boundingBox;
  58. Vector2 spriteOffset;
  59. Number pixelsPerUnit;
  60. Number stateFPS;
  61. SpriteSet *spriteSet;
  62. String name;
  63. std::vector<unsigned int> frameIDs;
  64. std::vector<Mesh*> frameMeshes;
  65. };
  66. class SpriteSet;
  67. class Sprite : public Resource {
  68. public:
  69. Sprite(String name);
  70. ~Sprite();
  71. String getName();
  72. void setName(String name);
  73. void addSpriteState(SpriteState *state);
  74. void removeSpriteState(SpriteState *state);
  75. unsigned int getNumStates();
  76. SpriteState *getState(unsigned int index);
  77. void setParentSpritSet(SpriteSet *spriteSet);
  78. SpriteSet *getParentSpriteSet();
  79. protected:
  80. String name;
  81. SpriteSet *parentSpriteSet;
  82. std::vector<SpriteState*> states;
  83. };
  84. class SpriteSet : public ResourcePool {
  85. public:
  86. SpriteSet(String imageFileName, ResourcePool *parentPool);
  87. ~SpriteSet();
  88. void setTexture(Texture *texture);
  89. Texture *getTexture();
  90. Texture *loadTexture(String imageFileName);
  91. void addSpriteEntry(Sprite *newEntry);
  92. unsigned int getNumSpriteEntries() const;
  93. Sprite *getSpriteEntry(unsigned int index) const;
  94. void removeSprite(Sprite *sprite);
  95. void loadSpriteSet(String fileName);
  96. // frame manipulation
  97. void addSpriteFrame(const SpriteFrame &frame, bool assignID = true);
  98. unsigned int getNumFrames() const;
  99. SpriteFrame getSpriteFrame(unsigned int index) const;
  100. SpriteFrame getSpriteFrameByID(unsigned int frameID) const;
  101. void removeFrameByID(unsigned int frameID);
  102. void setSpriteFrame(const SpriteFrame &frame);
  103. void clearFrames();
  104. // automatic frame generation
  105. void createGridFrames(Number width, Number height, const Vector2 &defaultAnchor);
  106. void createFramesFromIslands(unsigned int minDistance, const Vector2 &defaultAnchor);
  107. protected:
  108. unsigned int nextFrameIDIndex;
  109. Texture *spriteTexture;
  110. std::vector<SpriteFrame> frames;
  111. std::vector<Sprite*> sprites;
  112. };
  113. class SceneSprite : public SceneMesh {
  114. public:
  115. SceneSprite(SpriteSet *spriteSet);
  116. ~SceneSprite();
  117. SpriteSet *getSpriteSet();
  118. Sprite *getCurrentSprite();
  119. void setCurrentFrame(unsigned int frameIndex);
  120. unsigned int getCurrentFrame();
  121. void Update();
  122. void Render();
  123. void setPaused(bool val);
  124. bool isPaused();
  125. void setSprite(Sprite *spriteEntry);
  126. void setSpriteState(SpriteState *spriteState);
  127. SpriteState *getCurrentSpriteState();
  128. protected:
  129. bool paused;
  130. Core *core;
  131. unsigned int currentFrame;
  132. Mesh *defaultMesh;
  133. Sprite *currentSprite;
  134. SpriteState *currentSpriteState;
  135. SpriteSet *spriteSet;
  136. Number spriteTimer;
  137. Number spriteTimerVal;
  138. };
  139. }