Player.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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/Graphics.h>
  26. #include <Atomic/Graphics/Renderer.h>
  27. #include <Atomic/Graphics/Camera.h>
  28. #include <Atomic/Graphics/Texture2D.h>
  29. #include "Player.h"
  30. namespace AtomicPlayer
  31. {
  32. Player::Player(Context* context) :
  33. Object(context),
  34. renderWidth_(0),
  35. renderHeight_(0)
  36. {
  37. viewport_ = new Viewport(context_);
  38. SubscribeToEvent(E_EXITREQUESTED, HANDLER(Player, HandleExitRequested));
  39. }
  40. Player::~Player()
  41. {
  42. }
  43. void Player::HandleExitRequested(StringHash eventType, VariantMap& eventData)
  44. {
  45. currentScene_ = 0;
  46. viewport_ = 0;
  47. }
  48. void Player::SetRenderToTexture(unsigned width, unsigned height)
  49. {
  50. renderWidth_ = width;
  51. renderHeight_ = height;
  52. renderTexture_ = new Texture2D(context_);
  53. depthTexture_ = new Texture2D(context_);
  54. renderTexture_->SetGPUShared(true);
  55. viewport_->SetRect(IntRect(0, 0, width, height));
  56. renderTexture_->SetSize(width, height, Graphics::GetRGBFormat(), TEXTURE_RENDERTARGET);
  57. depthTexture_->SetSize(width, height, Graphics::GetDepthStencilFormat(), TEXTURE_DEPTHSTENCIL);
  58. RenderSurface* surface = renderTexture_->GetRenderSurface();
  59. surface->SetViewport(0, viewport_);
  60. surface->SetUpdateMode(SURFACE_UPDATEALWAYS );
  61. surface->SetLinkedDepthStencil(depthTexture_->GetRenderSurface());
  62. }
  63. Scene* Player::LoadScene(const String& filename, Camera *camera)
  64. {
  65. ResourceCache* cache = GetSubsystem<ResourceCache>();
  66. SharedPtr<File> file = cache->GetFile(filename);
  67. // If we're not rendering to texture and we haven't set the viewport, set it
  68. if (renderTexture_.Null() && GetSubsystem<Renderer>()->GetViewport(0) != viewport_)
  69. {
  70. GetSubsystem<Renderer>()->SetViewport(0, viewport_);
  71. }
  72. if (!file->IsOpen())
  73. {
  74. return 0;
  75. }
  76. Scene* scene = new Scene(context_);
  77. if (!scene->LoadXML(*file))
  78. {
  79. scene->ReleaseRef();
  80. return 0;
  81. }
  82. if (currentScene_.Null())
  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. return scene;
  99. }
  100. }