| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323 |
- #ifndef PACKAGE_H_
- #define PACKAGE_H_
- #include "Mesh.h"
- #include "Font.h"
- #include "Node.h"
- #include "Game.h"
- namespace gameplay
- {
- /**
- * Represents a gameplay binary package file (.gpb) that contains a
- * collection of game resources that can be loaded.
- */
- class Package : public Ref
- {
- public:
- /**
- * Returns a Package for the given resource path.
- *
- * The specified path must reference a valid gameplay binary file.
- * If the package is already loaded, the existing package is returned
- * with its reference count incremented. When no longer needed, the
- * release() method must be called. Note that calling release() does
- * NOT free any actual game objects created/returned from the Package
- * instance and those objects must be released separately.
- */
- static Package* create(const char* path);
- /**
- * Loads the scene with the specified ID from the package.
- * If id is NULL then the first scene found is loaded.
- *
- * @param id The ID of the scene to load (NULL to load the first scene).
- *
- * @return The loaded scene, or NULL if the scene could not be loaded.
- */
- Scene* loadScene(const char* id = NULL);
- /**
- * Loads a node with the specified ID from the package.
- *
- * @param id The ID of the node to load in the package.
- *
- * @return The loaded node, or NULL if the node could not be loaded.
- */
- Node* loadNode(const char* id);
- /**
- * Loads a mesh with the specified ID from the package.
- *
- * @param id The ID of the mesh to load.
- *
- * @return The loaded mesh, or NULL if the mesh could not be loaded.
- */
- Mesh* loadMesh(const char* id);
- /**
- * Loads a font with the specified ID from the package.
- *
- * @param id The ID of the font to load.
- *
- * @return The loaded font, or NULL if the font could not be loaded.
- */
- Font* loadFont(const char* id);
- /**
- * Determines if this package contains a top-level object with the given ID.
- *
- * This method performs a case-sensitive comparison.
- *
- * @param id The ID of the object to search for.
- */
- bool contains(const char* id) const;
- /**
- * Returns the number of top-level objects in this package.
- */
- unsigned int getObjectCount() const;
- /**
- * Returns the unique identifier of the top-level object at the specified index in this package.
- *
- * @param index The index of the object.
- *
- * @return The ID of the object at the given index, or NULL if index is invalid.
- */
- const char* getObjectID(unsigned int index) const;
- private:
- class Reference
- {
- public:
- std::string id;
- unsigned int type;
- unsigned int offset;
- /**
- * Constructor.
- */
- Reference();
- /**
- * Destructor.
- */
- ~Reference();
- };
- class MeshSkinData
- {
- public:
- MeshSkin* skin;
- std::vector<std::string> joints;
- std::vector<Matrix> inverseBindPoseMatrices;
- };
- Package(const char* path);
- /**
- * Destructor.
- */
- ~Package();
- /**
- * Finds a reference by ID.
- */
- Reference* find(const char* id) const;
- /**
- * Resets any load session specific state for the package.
- */
- void clearLoadSession();
- /**
- * Returns the ID of the object at the current file position.
- * Returns NULL if not found.
- *
- * @return The ID string or NULL if not found.
- */
- const char* getIdFromOffset() const;
- /**
- * Returns the ID of the object at the given file offset by searching through the reference table.
- * Returns NULL if not found.
- *
- * @param offset The file offset.
- *
- * @return The ID string or NULL if not found.
- */
- const char* getIdFromOffset(unsigned int offset) const;
- /**
- * Seeks the file pointer to the object with the given ID and type
- * and returns the relevant Reference.
- *
- * @param id The ID string to search for.
- * @param type The object type.
- *
- * @return The reference object or NULL if there was an error.
- */
- Reference* seekTo(const char* id, unsigned int type);
- /**
- * Seeks the file pointer to the first object that matches the given type.
- *
- * @param type The object type.
- *
- * @return The reference object or NULL if there was an error.
- */
- Reference* seekToFirstType(unsigned int type);
- /**
- * Internal method to load a node.
- *
- * Only one of node or scene should be passed as non-NULL (or neither).
- */
- Node* loadNode(const char* id, Scene* sceneContext, Node* nodeContext);
- /**
- * Reads an unsigned int from the current file position.
- *
- * @param ptr A pointer to load the value into.
- *
- * @return True if successful, false if an error occurred.
- */
- bool read(unsigned int* ptr);
- /**
- * Reads an unsigned char from the current file position.
- *
- * @param ptr A pointer to load the value into.
- *
- * @return True if successful, false if an error occurred.
- */
- bool read(unsigned char* ptr);
- /**
- * Reads a float from the current file position.
- *
- * @param ptr A pointer to load the value into.
- *
- * @return True if successful, false if an error occurred.
- */
- bool read(float* ptr);
- /**
- * Reads an array of values and the array length from the current file position.
- *
- * @param length A pointer to where the length of the array will be copied to.
- * @param ptr A pointer to the array where the data will be copied to.
- *
- * @return True if successful, false if an error occurred.
- */
- template <class T>
- bool readArray(unsigned int* length, T** ptr);
- /**
- * Reads 16 floats from the current file position.
- *
- * @param m A pointer to float array of size 16.
- *
- * @return True if successful, false if an error occurred.
- */
- bool readMatrix(float* m);
- /**
- * Reads an xref string from the current file position.
- *
- * @param id The string to load the ID string into.
- *
- * @return True if successful, false if an error occurred.
- */
- bool readXref(std::string& id);
- /**
- * Recursively reads nodes from the current file position.
- * This method will load cameras, lights and models in the nodes.
- *
- * @return A pointer to new node or NULL if there was an error.
- */
- Node* readNode(Scene* sceneContext, Node* nodeContext);
- /**
- * Reads a camera from the current file position.
- *
- * @return A pointer to a new camera or NULL if there was an error.
- */
- Camera* readCamera();
- /**
- * Reads a light from the current file position.
- *
- * @return A pointer to a new light or NULL if there was an error.
- */
- Light* readLight();
- /**
- * Reads a model from the current file position.
- *
- * @return A pointer to a new model or NULL if there was an error.
- */
- Model* readModel(Scene* sceneContext, Node* nodeContext);
- /**
- * Reads a mesh skin from the current file position.
- *
- * @return A pointer to a new mesh skin or NULL if there was an error.
- */
- MeshSkin* readMeshSkin(Scene* sceneContext, Node* nodeContext);
- /**
- * Reads an animation from the current file position.
- *
- * @param scene The scene to load the animations into.
- */
- void readAnimation(Scene* scene);
- /**
- * Reads an "animations" object from the current file position and all of the animations contained in it.
- *
- * @param scene The scene to load the animations into.
- */
- void readAnimations(Scene* scene);
- /**
- * Reads an animation channel at the current file position into the given animation.
- *
- * @param scene The scene that the animation is in.
- * @param animation The animation to the load channel into.
- * @param animationId The ID of the animation that this channel is loaded into.
- *
- * @return The animation that the channel was loaded into.
- */
- Animation* readAnimationChannel(Scene* scene, Animation* animation, const char* animationId);
- /**
- * Sets the transformation matrix.
- *
- * @param values A pointer to array of 16 floats.
- * @param transform The transform to set the values in.
- */
- void setTransform(const float* values, Transform* transform);
- /**
- * Resolves joint references for all pending mesh skins.
- */
- void resolveJointReferences(Scene* sceneContext, Node* nodeContext);
- private:
- std::string _path;
- unsigned int _referenceCount;
- Reference* _references;
- FILE* _file;
- std::vector<MeshSkinData*> _meshSkins;
- };
- }
- #endif
|