Package.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. #ifndef PACKAGE_H_
  2. #define PACKAGE_H_
  3. #include "Mesh.h"
  4. #include "Font.h"
  5. #include "Node.h"
  6. #include "Game.h"
  7. namespace gameplay
  8. {
  9. /**
  10. * Represents a gameplay binary package file (.gpb) that contains a
  11. * collection of game resources that can be loaded.
  12. */
  13. class Package : public Ref
  14. {
  15. public:
  16. /**
  17. * Returns a Package for the given resource path.
  18. *
  19. * The specified path must reference a valid gameplay binary file.
  20. * If the package is already loaded, the existing package is returned
  21. * with its reference count incremented. When no longer needed, the
  22. * release() method must be called. Note that calling release() does
  23. * NOT free any actual game objects created/returned from the Package
  24. * instance and those objects must be released separately.
  25. */
  26. static Package* create(const char* path);
  27. /**
  28. * Loads the scene with the specified ID from the package.
  29. * If id is NULL then the first scene found is loaded.
  30. *
  31. * @param id The ID of the scene to load (NULL to load the first scene).
  32. *
  33. * @return The loaded scene, or NULL if the scene could not be loaded.
  34. */
  35. Scene* loadScene(const char* id = NULL);
  36. /**
  37. * Loads a node with the specified ID from the package.
  38. *
  39. * @param id The ID of the node to load in the package.
  40. *
  41. * @return The loaded node, or NULL if the node could not be loaded.
  42. */
  43. Node* loadNode(const char* id);
  44. /**
  45. * Loads a mesh with the specified ID from the package.
  46. *
  47. * @param id The ID of the mesh to load.
  48. *
  49. * @return The loaded mesh, or NULL if the mesh could not be loaded.
  50. */
  51. Mesh* loadMesh(const char* id);
  52. /**
  53. * Loads a font with the specified ID from the package.
  54. *
  55. * @param id The ID of the font to load.
  56. *
  57. * @return The loaded font, or NULL if the font could not be loaded.
  58. */
  59. Font* loadFont(const char* id);
  60. /**
  61. * Determines if this package contains a top-level object with the given ID.
  62. *
  63. * This method performs a case-sensitive comparison.
  64. *
  65. * @param id The ID of the object to search for.
  66. */
  67. bool contains(const char* id) const;
  68. /**
  69. * Returns the number of top-level objects in this package.
  70. */
  71. unsigned int getObjectCount() const;
  72. /**
  73. * Returns the unique identifier of the top-level object at the specified index in this package.
  74. *
  75. * @param index The index of the object.
  76. *
  77. * @return The ID of the object at the given index, or NULL if index is invalid.
  78. */
  79. const char* getObjectID(unsigned int index) const;
  80. private:
  81. class Reference
  82. {
  83. public:
  84. std::string id;
  85. unsigned int type;
  86. unsigned int offset;
  87. /**
  88. * Constructor.
  89. */
  90. Reference();
  91. /**
  92. * Destructor.
  93. */
  94. ~Reference();
  95. };
  96. class MeshSkinData
  97. {
  98. public:
  99. MeshSkin* skin;
  100. std::vector<std::string> joints;
  101. std::vector<Matrix> inverseBindPoseMatrices;
  102. };
  103. Package(const char* path);
  104. /**
  105. * Destructor.
  106. */
  107. ~Package();
  108. /**
  109. * Finds a reference by ID.
  110. */
  111. Reference* find(const char* id) const;
  112. /**
  113. * Resets any load session specific state for the package.
  114. */
  115. void clearLoadSession();
  116. /**
  117. * Returns the ID of the object at the current file position.
  118. * Returns NULL if not found.
  119. *
  120. * @return The ID string or NULL if not found.
  121. */
  122. const char* getIdFromOffset() const;
  123. /**
  124. * Returns the ID of the object at the given file offset by searching through the reference table.
  125. * Returns NULL if not found.
  126. *
  127. * @param offset The file offset.
  128. *
  129. * @return The ID string or NULL if not found.
  130. */
  131. const char* getIdFromOffset(unsigned int offset) const;
  132. /**
  133. * Seeks the file pointer to the object with the given ID and type
  134. * and returns the relevant Reference.
  135. *
  136. * @param id The ID string to search for.
  137. * @param type The object type.
  138. *
  139. * @return The reference object or NULL if there was an error.
  140. */
  141. Reference* seekTo(const char* id, unsigned int type);
  142. /**
  143. * Seeks the file pointer to the first object that matches the given type.
  144. *
  145. * @param type The object type.
  146. *
  147. * @return The reference object or NULL if there was an error.
  148. */
  149. Reference* seekToFirstType(unsigned int type);
  150. /**
  151. * Internal method to load a node.
  152. *
  153. * Only one of node or scene should be passed as non-NULL (or neither).
  154. */
  155. Node* loadNode(const char* id, Scene* sceneContext, Node* nodeContext);
  156. /**
  157. * Reads an unsigned int from the current file position.
  158. *
  159. * @param ptr A pointer to load the value into.
  160. *
  161. * @return True if successful, false if an error occurred.
  162. */
  163. bool read(unsigned int* ptr);
  164. /**
  165. * Reads an unsigned char from the current file position.
  166. *
  167. * @param ptr A pointer to load the value into.
  168. *
  169. * @return True if successful, false if an error occurred.
  170. */
  171. bool read(unsigned char* ptr);
  172. /**
  173. * Reads a float from the current file position.
  174. *
  175. * @param ptr A pointer to load the value into.
  176. *
  177. * @return True if successful, false if an error occurred.
  178. */
  179. bool read(float* ptr);
  180. /**
  181. * Reads an array of values and the array length from the current file position.
  182. *
  183. * @param length A pointer to where the length of the array will be copied to.
  184. * @param ptr A pointer to the array where the data will be copied to.
  185. *
  186. * @return True if successful, false if an error occurred.
  187. */
  188. template <class T>
  189. bool readArray(unsigned int* length, T** ptr);
  190. /**
  191. * Reads 16 floats from the current file position.
  192. *
  193. * @param m A pointer to float array of size 16.
  194. *
  195. * @return True if successful, false if an error occurred.
  196. */
  197. bool readMatrix(float* m);
  198. /**
  199. * Reads an xref string from the current file position.
  200. *
  201. * @param id The string to load the ID string into.
  202. *
  203. * @return True if successful, false if an error occurred.
  204. */
  205. bool readXref(std::string& id);
  206. /**
  207. * Recursively reads nodes from the current file position.
  208. * This method will load cameras, lights and models in the nodes.
  209. *
  210. * @return A pointer to new node or NULL if there was an error.
  211. */
  212. Node* readNode(Scene* sceneContext, Node* nodeContext);
  213. /**
  214. * Reads a camera from the current file position.
  215. *
  216. * @return A pointer to a new camera or NULL if there was an error.
  217. */
  218. Camera* readCamera();
  219. /**
  220. * Reads a light from the current file position.
  221. *
  222. * @return A pointer to a new light or NULL if there was an error.
  223. */
  224. Light* readLight();
  225. /**
  226. * Reads a model from the current file position.
  227. *
  228. * @return A pointer to a new model or NULL if there was an error.
  229. */
  230. Model* readModel(Scene* sceneContext, Node* nodeContext);
  231. /**
  232. * Reads a mesh skin from the current file position.
  233. *
  234. * @return A pointer to a new mesh skin or NULL if there was an error.
  235. */
  236. MeshSkin* readMeshSkin(Scene* sceneContext, Node* nodeContext);
  237. /**
  238. * Reads an animation from the current file position.
  239. *
  240. * @param scene The scene to load the animations into.
  241. */
  242. void readAnimation(Scene* scene);
  243. /**
  244. * Reads an "animations" object from the current file position and all of the animations contained in it.
  245. *
  246. * @param scene The scene to load the animations into.
  247. */
  248. void readAnimations(Scene* scene);
  249. /**
  250. * Reads an animation channel at the current file position into the given animation.
  251. *
  252. * @param scene The scene that the animation is in.
  253. * @param animation The animation to the load channel into.
  254. * @param animationId The ID of the animation that this channel is loaded into.
  255. *
  256. * @return The animation that the channel was loaded into.
  257. */
  258. Animation* readAnimationChannel(Scene* scene, Animation* animation, const char* animationId);
  259. /**
  260. * Sets the transformation matrix.
  261. *
  262. * @param values A pointer to array of 16 floats.
  263. * @param transform The transform to set the values in.
  264. */
  265. void setTransform(const float* values, Transform* transform);
  266. /**
  267. * Resolves joint references for all pending mesh skins.
  268. */
  269. void resolveJointReferences(Scene* sceneContext, Node* nodeContext);
  270. private:
  271. std::string _path;
  272. unsigned int _referenceCount;
  273. Reference* _references;
  274. FILE* _file;
  275. std::vector<MeshSkinData*> _meshSkins;
  276. };
  277. }
  278. #endif