SceneLoader.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. * Defines an internal helper class for loading scenes from .scene files.
  12. *
  13. * @script{ignore}
  14. */
  15. class SceneLoader
  16. {
  17. friend class Scene;
  18. private:
  19. /**
  20. * Loads a scene using the data from the Properties object defined at the specified URL,
  21. * where the URL is of the format "<file-path>.<extension>#<namespace-id>/<namespace-id>/.../<namespace-id>"
  22. * (and "#<namespace-id>/<namespace-id>/.../<namespace-id>" is optional).
  23. *
  24. * @param url The URL pointing to the Properties object defining the scene.
  25. */
  26. static Scene* load(const char* url);
  27. /**
  28. * Helper structures and functions for SceneLoader::load(const char*).
  29. */
  30. struct SceneAnimation
  31. {
  32. SceneAnimation(const char* animationID, const char* targetID, std::string url)
  33. : _animationID(animationID), _targetID(targetID), _url(url) {}
  34. const char* _animationID;
  35. const char* _targetID;
  36. std::string _url;
  37. };
  38. struct SceneNodeProperty
  39. {
  40. enum Type
  41. {
  42. AUDIO = 1,
  43. MATERIAL = 2,
  44. PARTICLE = 4,
  45. TERRAIN = 8,
  46. LIGHT = 16,
  47. CAMERA = 32,
  48. COLLISION_OBJECT = 64,
  49. TRANSLATE = 128,
  50. ROTATE = 256,
  51. SCALE = 512,
  52. URL = 1024,
  53. SCRIPT = 2048,
  54. SPRITE = 4096,
  55. TILESET = 8192,
  56. TEXT = 16384
  57. };
  58. SceneNodeProperty(Type type, const std::string& value, int index, bool isUrl);
  59. Type _type;
  60. std::string _value;
  61. bool _isUrl;
  62. int _index;
  63. };
  64. struct SceneNode
  65. {
  66. SceneNode();
  67. const char* _nodeID;
  68. bool _exactMatch;
  69. Properties* _namespace;
  70. std::vector<Node*> _nodes; // list of nodes sharing properties defined in this SceneNode
  71. std::vector<SceneNode> _children; // list of unique child nodes
  72. std::vector<SceneNodeProperty> _properties;
  73. std::map<std::string, std::string> _tags;
  74. };
  75. SceneLoader();
  76. Scene* loadInternal(const char* url);
  77. void applyTags(SceneNode& sceneNode);
  78. void addSceneAnimation(const char* animationID, const char* targetID, const char* url);
  79. void addSceneNodeProperty(SceneNode& sceneNode, SceneNodeProperty::Type type, const char* value = NULL, bool supportsUrl = false, int index = 0);
  80. void applyNodeProperties(const Properties* sceneProperties, unsigned int typeFlags);
  81. void applyNodeProperties(SceneNode& sceneNode, const Properties* sceneProperties, unsigned int typeFlags);
  82. void applyNodeProperty(SceneNode& sceneNode, Node* node, const Properties* sceneProperties, const SceneNodeProperty& snp);
  83. void applyNodeUrls();
  84. void applyNodeUrls(SceneNode& sceneNode, Node* parent);
  85. void buildReferenceTables(Properties* sceneProperties);
  86. void parseNode(Properties* ns, SceneNode* parent, const std::string& path);
  87. void calculateNodesWithMeshRigidBodies(const Properties* sceneProperties);
  88. void createAnimations();
  89. PhysicsConstraint* loadGenericConstraint(const Properties* constraint, PhysicsRigidBody* rbA, PhysicsRigidBody* rbB);
  90. PhysicsConstraint* loadHingeConstraint(const Properties* constraint, PhysicsRigidBody* rbA, PhysicsRigidBody* rbB);
  91. Scene* loadMainSceneData(const Properties* sceneProperties);
  92. void loadPhysics(Properties* physics);
  93. void loadReferencedFiles();
  94. PhysicsConstraint* loadSocketConstraint(const Properties* constraint, PhysicsRigidBody* rbA, PhysicsRigidBody* rbB);
  95. PhysicsConstraint* loadSpringConstraint(const Properties* constraint, PhysicsRigidBody* rbA, PhysicsRigidBody* rbB);
  96. std::map<std::string, Properties*> _propertiesFromFile; // Holds the properties object for a given file.
  97. std::map<std::string, Properties*> _properties; // Holds the properties object for a given URL.
  98. std::vector<SceneAnimation> _animations; // Holds the animations declared in the .scene file.
  99. std::vector<SceneNode> _sceneNodes; // Holds all the nodes+properties declared in the .scene file.
  100. std::string _gpbPath; // The path of the main GPB for the scene being loaded.
  101. std::string _path; // The path of the scene file being loaded.
  102. Scene* _scene; // The scene being loaded
  103. };
  104. /**
  105. * Utility function for splitting up a URL of the form 'file#id', where one or both of file and id may be empty.
  106. *
  107. * @param url The url to split.
  108. * @param file The out parameter containing the file part of the url.
  109. * @param id The out parameter containing the id part of the url.
  110. * @script{ignore}
  111. */
  112. void splitURL(const std::string& url, std::string* file, std::string* id);
  113. }
  114. #endif