AppState_Base.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #include "AppState_Base.h"
  4. #include <Urho3D/Graphics/Camera.h>
  5. #include <Urho3D/Graphics/Renderer.h>
  6. #include <Urho3D/Resource/ResourceCache.h>
  7. #include <Urho3D/UI/Text.h>
  8. #include <Urho3D/UI/UI.h>
  9. #include <Urho3D/DebugNew.h>
  10. using namespace Urho3D;
  11. void AppState_Base::LoadSceneXml(const String& path)
  12. {
  13. assert(!scene_);
  14. scene_ = MakeShared<Scene>(context_);
  15. SharedPtr<File> file = GetSubsystem<ResourceCache>()->GetFile(path);
  16. scene_->LoadXML(*file);
  17. #ifndef NDEBUG
  18. Node* cameraNode = scene_->GetChild("Camera");
  19. assert(cameraNode);
  20. Camera* camera = cameraNode->GetComponent<Camera>();
  21. assert(camera);
  22. #endif
  23. }
  24. void AppState_Base::UpdateCurrentFpsElement()
  25. {
  26. String fpsStr = fpsCounter_.GetCurrentFps() == -1 ? "?" : String(fpsCounter_.GetCurrentFps());
  27. Text* fpsElement = GetSubsystem<UI>()->GetRoot()->GetChildStaticCast<Text>(CURRENT_FPS_STR);
  28. fpsElement->SetText("FPS: " + fpsStr);
  29. }
  30. void AppState_Base::SetupViewport()
  31. {
  32. Node* cameraNode = scene_->GetChild("Camera");
  33. Camera* camera = cameraNode->GetComponent<Camera>();
  34. SharedPtr<Viewport> viewport(new Viewport(context_, scene_, camera));
  35. Renderer* renderer = GetSubsystem<Renderer>();
  36. renderer->SetViewport(0, viewport);
  37. }
  38. void AppState_Base::DestroyViewport()
  39. {
  40. GetSubsystem<Renderer>()->SetViewport(0, nullptr);
  41. }