Bundle.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. #ifndef BUNDLE_H_
  2. #define BUNDLE_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 bundle file (.gpb) that contains a
  11. * collection of binary game assets that can be loaded.
  12. */
  13. class Bundle : public Ref
  14. {
  15. friend class PhysicsController;
  16. friend class SceneLoader;
  17. public:
  18. /**
  19. * Returns a Bundle for the given resource path.
  20. *
  21. * The specified path must reference a valid gameplay bundle file.
  22. * If the bundle is already loaded, the existing bundle is returned
  23. * with its reference count incremented. When no longer needed, the
  24. * release() method must be called. Note that calling release() does
  25. * NOT free any actual game objects created/returned from the Bundle
  26. * instance and those objects must be released separately.
  27. * @script{create}
  28. */
  29. static Bundle* create(const char* path);
  30. /**
  31. * Loads the scene with the specified ID from the bundle.
  32. * If id is NULL then the first scene found is loaded.
  33. *
  34. * @param id The ID of the scene to load (NULL to load the first scene).
  35. *
  36. * @return The loaded scene, or NULL if the scene could not be loaded.
  37. * @script{create}
  38. */
  39. Scene* loadScene(const char* id = NULL);
  40. /**
  41. * Loads a node with the specified ID from the bundle.
  42. *
  43. * @param id The ID of the node to load in the bundle.
  44. *
  45. * @return The loaded node, or NULL if the node could not be loaded.
  46. * @script{create}
  47. */
  48. Node* loadNode(const char* id);
  49. /**
  50. * Loads a mesh with the specified ID from the bundle.
  51. *
  52. * @param id The ID of the mesh to load.
  53. *
  54. * @return The loaded mesh, or NULL if the mesh could not be loaded.
  55. * @script{create}
  56. */
  57. Mesh* loadMesh(const char* id);
  58. /**
  59. * Loads a font with the specified ID from the bundle.
  60. *
  61. * @param id The ID of the font to load.
  62. *
  63. * @return The loaded font, or NULL if the font could not be loaded.
  64. * @script{create}
  65. */
  66. Font* loadFont(const char* id);
  67. /**
  68. * Determines if this bundle contains a top-level object with the given ID.
  69. *
  70. * This method performs a case-sensitive comparison.
  71. *
  72. * @param id The ID of the object to search for.
  73. */
  74. bool contains(const char* id) const;
  75. /**
  76. * Returns the number of top-level objects in this bundle.
  77. */
  78. unsigned int getObjectCount() const;
  79. /**
  80. * Returns the unique identifier of the top-level object at the specified index in this bundle.
  81. *
  82. * @param index The index of the object.
  83. *
  84. * @return The ID of the object at the given index, or NULL if index is invalid.
  85. */
  86. const char* getObjectId(unsigned int index) const;
  87. private:
  88. class Reference
  89. {
  90. public:
  91. std::string id;
  92. unsigned int type;
  93. unsigned int offset;
  94. /**
  95. * Constructor.
  96. */
  97. Reference();
  98. /**
  99. * Destructor.
  100. */
  101. ~Reference();
  102. };
  103. struct MeshSkinData
  104. {
  105. MeshSkin* skin;
  106. std::vector<std::string> joints;
  107. std::vector<Matrix> inverseBindPoseMatrices;
  108. };
  109. struct MeshPartData
  110. {
  111. MeshPartData();
  112. ~MeshPartData();
  113. Mesh::PrimitiveType primitiveType;
  114. Mesh::IndexFormat indexFormat;
  115. unsigned int indexCount;
  116. unsigned char* indexData;
  117. };
  118. struct MeshData
  119. {
  120. MeshData(const VertexFormat& vertexFormat);
  121. ~MeshData();
  122. VertexFormat vertexFormat;
  123. unsigned int vertexCount;
  124. unsigned char* vertexData;
  125. BoundingBox boundingBox;
  126. BoundingSphere boundingSphere;
  127. Mesh::PrimitiveType primitiveType;
  128. std::vector<MeshPartData*> parts;
  129. };
  130. Bundle(const char* path);
  131. /**
  132. * Destructor.
  133. */
  134. ~Bundle();
  135. /**
  136. * Hidden copy assignment operator.
  137. */
  138. Bundle& operator=(const Bundle&);
  139. /**
  140. * Finds a reference by ID.
  141. */
  142. Reference* find(const char* id) const;
  143. /**
  144. * Resets any load session specific state for the bundle.
  145. */
  146. void clearLoadSession();
  147. /**
  148. * Returns the ID of the object at the current file position.
  149. * Returns NULL if not found.
  150. *
  151. * @return The ID string or NULL if not found.
  152. */
  153. const char* getIdFromOffset() const;
  154. /**
  155. * Returns the ID of the object at the given file offset by searching through the reference table.
  156. * Returns NULL if not found.
  157. *
  158. * @param offset The file offset.
  159. *
  160. * @return The ID string or NULL if not found.
  161. */
  162. const char* getIdFromOffset(unsigned int offset) const;
  163. /**
  164. * Seeks the file pointer to the object with the given ID and type
  165. * and returns the relevant Reference.
  166. *
  167. * @param id The ID string to search for.
  168. * @param type The object type.
  169. *
  170. * @return The reference object or NULL if there was an error.
  171. */
  172. Reference* seekTo(const char* id, unsigned int type);
  173. /**
  174. * Seeks the file pointer to the first object that matches the given type.
  175. *
  176. * @param type The object type.
  177. *
  178. * @return The reference object or NULL if there was an error.
  179. */
  180. Reference* seekToFirstType(unsigned int type);
  181. /**
  182. * Internal method to load a node.
  183. *
  184. * Only one of node or scene should be passed as non-NULL (or neither).
  185. */
  186. Node* loadNode(const char* id, Scene* sceneContext, Node* nodeContext);
  187. /**
  188. * Internal method for SceneLoader to load a node into a scene.
  189. */
  190. Node* loadNode(const char* id, Scene* sceneContext);
  191. /**
  192. * Loads a mesh with the specified ID from the bundle.
  193. *
  194. * @param id The ID of the mesh to load.
  195. * @param nodeId The id of the mesh's model's parent node.
  196. *
  197. * @return The loaded mesh, or NULL if the mesh could not be loaded.
  198. */
  199. Mesh* loadMesh(const char* id, const char* nodeId);
  200. /**
  201. * Reads an unsigned int from the current file position.
  202. *
  203. * @param ptr A pointer to load the value into.
  204. *
  205. * @return True if successful, false if an error occurred.
  206. */
  207. bool read(unsigned int* ptr);
  208. /**
  209. * Reads an unsigned char from the current file position.
  210. *
  211. * @param ptr A pointer to load the value into.
  212. *
  213. * @return True if successful, false if an error occurred.
  214. */
  215. bool read(unsigned char* ptr);
  216. /**
  217. * Reads a float from the current file position.
  218. *
  219. * @param ptr A pointer to load the value into.
  220. *
  221. * @return True if successful, false if an error occurred.
  222. */
  223. bool read(float* ptr);
  224. /**
  225. * Reads an array of values and the array length from the current file position.
  226. *
  227. * @param length A pointer to where the length of the array will be copied to.
  228. * @param ptr A pointer to the array where the data will be copied to.
  229. *
  230. * @return True if successful, false if an error occurred.
  231. */
  232. template <class T>
  233. bool readArray(unsigned int* length, T** ptr);
  234. /**
  235. * Reads an array of values and the array length from the current file position.
  236. *
  237. * @param length A pointer to where the length of the array will be copied to.
  238. * @param values A pointer to the vector to copy the values to. The vector will be resized if it is smaller than length.
  239. *
  240. * @return True if successful, false if an error occurred.
  241. */
  242. template <class T>
  243. bool readArray(unsigned int* length, std::vector<T>* values);
  244. /**
  245. * Reads an array of values and the array length from the current file position.
  246. *
  247. * @param length A pointer to where the length of the array will be copied to.
  248. * @param values A pointer to the vector to copy the values to. The vector will be resized if it is smaller than length.
  249. * @param readSize The size that reads will be preformed at, size must be the same as or smaller then the sizeof(T)
  250. *
  251. * @return True if successful, false if an error occurred.
  252. */
  253. template <class T>
  254. bool readArray(unsigned int* length, std::vector<T>* values, unsigned int readSize);
  255. /**
  256. * Reads 16 floats from the current file position.
  257. *
  258. * @param m A pointer to float array of size 16.
  259. *
  260. * @return True if successful, false if an error occurred.
  261. */
  262. bool readMatrix(float* m);
  263. /**
  264. * Reads an xref string from the current file position.
  265. *
  266. * @param id The string to load the ID string into.
  267. *
  268. * @return True if successful, false if an error occurred.
  269. */
  270. bool readXref(std::string& id);
  271. /**
  272. * Recursively reads nodes from the current file position.
  273. * This method will load cameras, lights and models in the nodes.
  274. *
  275. * @return A pointer to new node or NULL if there was an error.
  276. */
  277. Node* readNode(Scene* sceneContext, Node* nodeContext);
  278. /**
  279. * Reads a camera from the current file position.
  280. *
  281. * @return A pointer to a new camera or NULL if there was an error.
  282. */
  283. Camera* readCamera();
  284. /**
  285. * Reads a light from the current file position.
  286. *
  287. * @return A pointer to a new light or NULL if there was an error.
  288. */
  289. Light* readLight();
  290. /**
  291. * Reads a model from the current file position.
  292. *
  293. * @return A pointer to a new model or NULL if there was an error.
  294. */
  295. Model* readModel(const char* nodeId);
  296. /**
  297. * Reads mesh data from the current file position.
  298. */
  299. MeshData* readMeshData();
  300. /**
  301. * Reads mesh data for the specified URL.
  302. *
  303. * The specified URL should be formatted as 'bundle#id', where
  304. * 'bundle' is the bundle file containing the mesh and 'id' is the ID
  305. * of the mesh to read data for.
  306. *
  307. * @param url The URL to read mesh data from.
  308. *
  309. * @return The mesh rigid body data.
  310. */
  311. static MeshData* readMeshData(const char* url);
  312. /**
  313. * Reads a mesh skin from the current file position.
  314. *
  315. * @return A pointer to a new mesh skin or NULL if there was an error.
  316. */
  317. MeshSkin* readMeshSkin();
  318. /**
  319. * Reads an animation from the current file position.
  320. *
  321. * @param scene The scene to load the animations into.
  322. */
  323. void readAnimation(Scene* scene);
  324. /**
  325. * Reads an "animations" object from the current file position and all of the animations contained in it.
  326. *
  327. * @param scene The scene to load the animations into.
  328. */
  329. void readAnimations(Scene* scene);
  330. /**
  331. * Reads an animation channel at the current file position into the given animation.
  332. *
  333. * @param scene The scene that the animation is in.
  334. * @param animation The animation to the load channel into.
  335. * @param animationId The ID of the animation that this channel is loaded into.
  336. *
  337. * @return The animation that the channel was loaded into.
  338. */
  339. Animation* readAnimationChannel(Scene* scene, Animation* animation, const char* animationId);
  340. /**
  341. * Reads the animation channel data at the current file position into the given animation
  342. * (with the given animation target and target attribute).
  343. *
  344. * Note: this is used by Bundle::loadNode(const char*, Scene*) and Bundle::readAnimationChannel(Scene*, Animation*, const char*).
  345. *
  346. * @param animation The animation to the load channel into.
  347. * @param id The ID of the animation that this channel is loaded into.
  348. * @param target The animation target.
  349. * @param targetAttribute The target attribute being animated.
  350. *
  351. * @return The animation that the channel was loaded into.
  352. */
  353. Animation* readAnimationChannelData(Animation* animation, const char* id, AnimationTarget* target, unsigned int targetAttribute);
  354. /**
  355. * Sets the transformation matrix.
  356. *
  357. * @param values A pointer to array of 16 floats.
  358. * @param transform The transform to set the values in.
  359. */
  360. void setTransform(const float* values, Transform* transform);
  361. /**
  362. * Resolves joint references for all pending mesh skins.
  363. */
  364. void resolveJointReferences(Scene* sceneContext, Node* nodeContext);
  365. private:
  366. /**
  367. * Skips over a Node's data within a bundle.
  368. *
  369. * @return True if the Node was successfully skipped; false otherwise.
  370. */
  371. bool skipNode();
  372. std::string _path;
  373. unsigned int _referenceCount;
  374. Reference* _references;
  375. FILE* _file;
  376. std::vector<MeshSkinData*> _meshSkins;
  377. std::map<std::string, Node*>* _trackedNodes;
  378. };
  379. }
  380. #endif