Main.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //
  2. // Copyright (c) 2008-2022 the Urho3D project.
  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 "AppStateManager.h"
  23. #include <Urho3D/Core/CoreEvents.h>
  24. #include <Urho3D/Engine/Application.h>
  25. #include <Urho3D/Engine/EngineDefs.h>
  26. #include <Urho3D/Input/Input.h>
  27. #include <Urho3D/IO/FileSystem.h>
  28. #include <Urho3D/Resource/ResourceCache.h>
  29. #include <Urho3D/UI/Text.h>
  30. #include <Urho3D/UI/UI.h>
  31. using namespace Urho3D;
  32. class App : public Application
  33. {
  34. URHO3D_OBJECT(App, Application);
  35. public:
  36. App(Context* context)
  37. : Application(context)
  38. {
  39. // The first handler for the first event in each frame.
  40. // To prevent a crash, we can only change the current scene at the start of a frame,
  41. // before any scene events are processed
  42. SubscribeToEvent(E_BEGINFRAME, URHO3D_HANDLER(App, ApplyAppState));
  43. }
  44. void Setup() override
  45. {
  46. engineParameters_[EP_WINDOW_TITLE] = "Urho3D Benchmark";
  47. engineParameters_[EP_LOG_NAME] = GetSubsystem<FileSystem>()->GetAppPreferencesDir("urho3d", "logs") + "99_Benchmark.log";
  48. engineParameters_[EP_FULL_SCREEN] = false;
  49. engineParameters_[EP_WINDOW_WIDTH] = 960;
  50. engineParameters_[EP_WINDOW_HEIGHT] = 720;
  51. engineParameters_[EP_FRAME_LIMITER] = false;
  52. }
  53. // This elements can be used anywhere in the program
  54. void CreateCurrentFpsUiElement()
  55. {
  56. UIElement* root = GetSubsystem<UI>()->GetRoot();
  57. root->SetDefaultStyle(GetSubsystem<ResourceCache>()->GetResource<XMLFile>("UI/DefaultStyle.xml"));
  58. Text* fpsElement = root->CreateChild<Text>(CURRENT_FPS_STR);
  59. fpsElement->SetStyleAuto();
  60. fpsElement->SetTextEffect(TE_SHADOW);
  61. fpsElement->SetPosition(10, 10);
  62. fpsElement->SetText("FPS");
  63. }
  64. void Start() override
  65. {
  66. context_->RegisterSubsystem(new AppStateManager(context_));
  67. AppStateManager* appStateManager = context_->GetSubsystem<AppStateManager>();
  68. appStateManager->SetRequiredAppStateId(APPSTATEID_MAINSCREEN);
  69. GetSubsystem<Input>()->SetToggleFullscreen(false); // Block Alt+Enter
  70. CreateCurrentFpsUiElement();
  71. }
  72. void ApplyAppState(StringHash eventType, VariantMap& eventData)
  73. {
  74. context_->GetSubsystem<AppStateManager>()->Apply();
  75. }
  76. };
  77. URHO3D_DEFINE_APPLICATION_MAIN(App);