SceneLoader.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #ifndef SCENELOADER_H_
  2. #define SCENELOADER_H_
  3. #include "Base.h"
  4. #include "Mesh.h"
  5. #include "PhysicsRigidBody.h"
  6. #include "Properties.h"
  7. #include "Scene.h"
  8. namespace gameplay
  9. {
  10. /**
  11. * Helper class for loading scenes from .scene files.
  12. * @script{ignore}
  13. */
  14. class SceneLoader
  15. {
  16. friend class Scene;
  17. private:
  18. /**
  19. * Loads a scene using the data from the Properties object defined at the specified URL,
  20. * where the URL is of the format "<file-path>.<extension>#<namespace-id>/<namespace-id>/.../<namespace-id>"
  21. * (and "#<namespace-id>/<namespace-id>/.../<namespace-id>" is optional).
  22. *
  23. * @param url The URL pointing to the Properties object defining the scene.
  24. */
  25. static Scene* load(const char* url);
  26. /**
  27. * Helper structures and functions for SceneLoader::load(const char*).
  28. */
  29. struct SceneAnimation
  30. {
  31. SceneAnimation(const char* animationID, const char* targetID, std::string url)
  32. : _animationID(animationID), _targetID(targetID), _url(url) {}
  33. const char* _animationID;
  34. const char* _targetID;
  35. std::string _url;
  36. };
  37. struct SceneNodeProperty
  38. {
  39. enum Type
  40. {
  41. AUDIO = 1,
  42. MATERIAL = 2,
  43. PARTICLE = 4,
  44. COLLISION_OBJECT = 8,
  45. TRANSLATE = 16,
  46. ROTATE = 32,
  47. SCALE = 64,
  48. URL = 128
  49. };
  50. SceneNodeProperty(Type type, const std::string& url, int index);
  51. Type _type;
  52. std::string _url;
  53. int _index;
  54. };
  55. struct SceneNode
  56. {
  57. SceneNode();
  58. const char* _nodeID;
  59. bool _exactMatch;
  60. std::vector<Node*> _nodes;
  61. std::vector<SceneNodeProperty> _properties;
  62. std::map<std::string, std::string> _tags;
  63. };
  64. Scene* loadInternal(const char* url);
  65. void addSceneAnimation(const char* animationID, const char* targetID, const char* url);
  66. void addSceneNodeProperty(SceneNode& sceneNode, SceneNodeProperty::Type type, const char* url = NULL, int index = 0);
  67. void applyNodeProperties(const Scene* scene, const Properties* sceneProperties, unsigned int typeFlags);
  68. void applyNodeProperty(SceneNode& sceneNode, Node* node, const Properties* sceneProperties, const SceneNodeProperty& snp, const Scene* scene);
  69. void applyNodeUrls(Scene* scene);
  70. void buildReferenceTables(Properties* sceneProperties);
  71. void calculateNodesWithMeshRigidBodies(const Properties* sceneProperties);
  72. void createAnimations(const Scene* scene);
  73. PhysicsConstraint* loadGenericConstraint(const Properties* constraint, PhysicsRigidBody* rbA, PhysicsRigidBody* rbB);
  74. PhysicsConstraint* loadHingeConstraint(const Properties* constraint, PhysicsRigidBody* rbA, PhysicsRigidBody* rbB);
  75. Scene* loadMainSceneData(const Properties* sceneProperties);
  76. void loadPhysics(Properties* physics, Scene* scene);
  77. void loadReferencedFiles();
  78. PhysicsConstraint* loadSocketConstraint(const Properties* constraint, PhysicsRigidBody* rbA, PhysicsRigidBody* rbB);
  79. PhysicsConstraint* loadSpringConstraint(const Properties* constraint, PhysicsRigidBody* rbA, PhysicsRigidBody* rbB);
  80. std::map<std::string, Properties*> _propertiesFromFile; // Holds the properties object for a given file.
  81. std::map<std::string, Properties*> _properties; // Holds the properties object for a given URL.
  82. std::vector<SceneAnimation> _animations; // Holds the animations declared in the .scene file.
  83. std::vector<SceneNode> _sceneNodes; // Holds all the nodes+properties declared in the .scene file.
  84. std::string _gpbPath; // The path of the main GPB for the scene being loaded.
  85. std::string _path; // The path of the scene file being loaded.
  86. };
  87. /**
  88. * Utility function for splitting up a URL of the form 'file#id', where one or both of file and id may be empty.
  89. *
  90. * @param url The url to split.
  91. * @param file The out parameter containing the file part of the url.
  92. * @param id The out parameter containing the id part of the url.
  93. * @script{ignore}
  94. */
  95. void splitURL(const std::string& url, std::string* file, std::string* id);
  96. }
  97. #endif