Player.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <Atomic/IO/Log.h>
  23. #include <Atomic/Input/InputEvents.h>
  24. #include <Atomic/Resource/ResourceCache.h>
  25. #include <Atomic/Graphics/Renderer.h>
  26. #include <Atomic/Graphics/Camera.h>
  27. #include "PlayerEvents.h"
  28. #include "Player.h"
  29. namespace AtomicPlayer
  30. {
  31. Player::Player(Context* context) :
  32. Object(context)
  33. {
  34. viewport_ = new Viewport(context_);
  35. GetSubsystem<Renderer>()->SetViewport(0, viewport_);
  36. SubscribeToEvent(E_EXITREQUESTED, ATOMIC_HANDLER(Player, HandleExitRequested));
  37. }
  38. Player::~Player()
  39. {
  40. }
  41. void Player::HandleExitRequested(StringHash eventType, VariantMap& eventData)
  42. {
  43. currentScene_ = 0;
  44. viewport_ = 0;
  45. }
  46. Scene* Player::LoadScene(const String& filename, Camera *camera)
  47. {
  48. ResourceCache* cache = GetSubsystem<ResourceCache>();
  49. SharedPtr<File> file = cache->GetFile(filename);
  50. if (!file || !file->IsOpen())
  51. {
  52. return 0;
  53. }
  54. Scene* scene = new Scene(context_);
  55. VariantMap eventData;
  56. eventData[PlayerSceneLoadBegin::P_SCENE] = scene;
  57. scene->SendEvent(E_PLAYERSCENELOADBEGIN, eventData);
  58. if (!scene->LoadXML(*file))
  59. {
  60. eventData[PlayerSceneLoadEnd::P_SCENE] = scene;
  61. eventData[PlayerSceneLoadEnd::P_SUCCESS] = false;
  62. scene->SendEvent(E_PLAYERSCENELOADEND, eventData);
  63. scene->ReleaseRef();
  64. return 0;
  65. }
  66. eventData[PlayerSceneLoadEnd::P_SCENE] = scene;
  67. eventData[PlayerSceneLoadEnd::P_SUCCESS] = true;
  68. scene->SendEvent(E_PLAYERSCENELOADEND, eventData);
  69. loadedScenes_.Push(SharedPtr<Scene>(scene));
  70. if (currentScene_.Null())
  71. {
  72. SetCurrentScene(scene, camera);
  73. }
  74. return scene;
  75. }
  76. void Player::SetCurrentScene(Scene* scene, Camera* camera)
  77. {
  78. Vector<SharedPtr<Scene>>::ConstIterator citr = loadedScenes_.Find(SharedPtr<Scene>(scene));
  79. if (citr == loadedScenes_.End())
  80. {
  81. ATOMIC_LOGERROR("Player::UnloadScene - unknown scene");
  82. return;
  83. }
  84. currentScene_ = scene;
  85. if (!camera)
  86. {
  87. PODVector<Node*> cameraNodes;
  88. scene->GetChildrenWithComponent(cameraNodes, Camera::GetTypeStatic(), true);
  89. if (cameraNodes.Size())
  90. {
  91. camera = cameraNodes[0]->GetComponent<Camera>();
  92. }
  93. }
  94. viewport_->SetScene(scene);
  95. if (camera)
  96. viewport_->SetCamera(camera);
  97. }
  98. void Player::UnloadScene(Scene* scene)
  99. {
  100. SharedPtr<Scene> keepalive(scene);
  101. if (currentScene_ == scene)
  102. {
  103. viewport_->SetScene(0);
  104. viewport_->SetCamera(0);
  105. currentScene_ = 0;
  106. }
  107. Vector<SharedPtr<Scene>>::ConstIterator citr = loadedScenes_.Find(keepalive);
  108. if (citr == loadedScenes_.End())
  109. {
  110. ATOMIC_LOGERROR("Player::UnloadScene - unknown scene");
  111. return;
  112. }
  113. VariantMap eventData;
  114. eventData[PlayerSceneUnload::P_SCENE] = scene;
  115. scene->SendEvent(E_PLAYERSCENEUNLOAD, eventData);
  116. loadedScenes_.Remove(keepalive);
  117. }
  118. void Player::UnloadAllScenes()
  119. {
  120. Vector<SharedPtr<Scene>> scenes = loadedScenes_;
  121. for (unsigned i = 0; i < scenes.Size(); i++)
  122. {
  123. UnloadScene(scenes[i]);
  124. }
  125. assert(loadedScenes_.Size() == 0);
  126. }
  127. }