GPBFile.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #ifndef GPBFILE_H_
  2. #define GPBFILE_H_
  3. #include "FileIO.h"
  4. #include "Object.h"
  5. #include "Scene.h"
  6. #include "Node.h"
  7. #include "Camera.h"
  8. #include "Light.h"
  9. #include "Mesh.h"
  10. #include "Reference.h"
  11. #include "ReferenceTable.h"
  12. #include "Animations.h"
  13. #include "Animation.h"
  14. #include "AnimationChannel.h"
  15. namespace gameplay
  16. {
  17. /**
  18. * Increment the version number when making a change that break binary compatibility.
  19. * [0] is major, [1] is minor.
  20. */
  21. const unsigned char GPB_VERSION[2] = {1, 2};
  22. /**
  23. * The GamePlay Binary file class handles writing the GamePlay Binary file.
  24. */
  25. class GPBFile
  26. {
  27. public:
  28. /**
  29. * Constructor.
  30. */
  31. GPBFile(void);
  32. /**
  33. * Destructor.
  34. */
  35. ~GPBFile(void);
  36. /**
  37. * Returns the GPBFile instance.
  38. */
  39. static GPBFile* getInstance();
  40. /**
  41. * Saves the GPBFile as a binary file at filepath.
  42. *
  43. * @param filepath The file name and path to save to.
  44. *
  45. * @return True if successful, false if error.
  46. */
  47. bool saveBinary(const std::string& filepath);
  48. /**
  49. * Saves the GPBFile as a text file at filepath. Useful for debugging.
  50. *
  51. * @param filepath The file name and path to save to.
  52. *
  53. * @return True if successful, false if error.
  54. */
  55. bool saveText(const std::string& filepath);
  56. void add(Object* obj);
  57. void addScene(Scene* scene);
  58. void addCamera(Camera* camera);
  59. void addLight(Light* light);
  60. void addMesh(Mesh* mesh);
  61. void addNode(Node* node);
  62. /**
  63. * Adds a node that does not belong to a scene.
  64. */
  65. void addScenelessNode(Node* node);
  66. void addAnimation(Animation* animation);
  67. /**
  68. * Adds the given object to the ref table.
  69. */
  70. void addToRefTable(Object* obj);
  71. /**
  72. * Returns the object with the given id. Returns NULL if not found.
  73. */
  74. Object* getFromRefTable(const std::string& id);
  75. /**
  76. * Returns true if the id was found in the ref table.
  77. */
  78. bool idExists(const std::string& id);
  79. Camera* getCamera(const char* id);
  80. Light* getLight(const char* id);
  81. Mesh* getMesh(const char* id);
  82. Node* getNode(const char* id);
  83. Animations* getAnimations();
  84. /**
  85. * Adjusts the game play binary file before it is written.
  86. */
  87. void adjust();
  88. /**
  89. * Groups the animations of all mesh skins to be under one animation per mesh skin.
  90. */
  91. void groupMeshSkinAnimations();
  92. /**
  93. * Renames the animations in the list of animation ids to the new animation id.
  94. *
  95. * @param animationIds The list of animations to rename.
  96. * @param newId The new animation id.
  97. */
  98. void renameAnimations(std::vector<std::string>& animationIds, const char* newId);
  99. private:
  100. /**
  101. * Computes the bounds of all meshes in the node hierarchy.
  102. */
  103. void computeBounds(Node* node);
  104. void optimizeTransformAnimations();
  105. /**
  106. * Decomposes an ANIMATE_SCALE_ROTATE_TRANSLATE channel into 3 new channels. (Scale, Rotate and Translate)
  107. *
  108. * @param animation The animation that the channel belongs to.
  109. * @param channel The animation channel to decompose.
  110. */
  111. void decomposeTransformAnimationChannel(Animation* animation, const AnimationChannel* channel);
  112. /**
  113. * Moves the animation channels that target the given node and its children to be under the given animation.
  114. *
  115. * @param node The node to recursively search from.
  116. * @param animation The animation to move the channels to.
  117. */
  118. void moveAnimationChannels(Node* node, Animation* animation);
  119. private:
  120. FILE* _file;
  121. std::list<Object*> _objects;
  122. std::list<Camera*> _cameras;
  123. std::list<Light*> _lights;
  124. std::list<Mesh*> _geometry;
  125. /**
  126. * The flat list of all nodes.
  127. */
  128. std::list<Node*> _nodes;
  129. Animations _animations;
  130. bool _animationsAdded;
  131. ReferenceTable _refTable;
  132. };
  133. }
  134. #endif