Main.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #include "AppStateManager.h"
  4. #include <Urho3D/Core/CoreEvents.h>
  5. #include <Urho3D/Engine/Application.h>
  6. #include <Urho3D/Engine/EngineDefs.h>
  7. #include <Urho3D/Input/Input.h>
  8. #include <Urho3D/IO/FileSystem.h>
  9. #include <Urho3D/Resource/ResourceCache.h>
  10. #include <Urho3D/UI/Text.h>
  11. #include <Urho3D/UI/UI.h>
  12. using namespace Urho3D;
  13. class App : public Application
  14. {
  15. URHO3D_OBJECT(App, Application);
  16. public:
  17. App(Context* context)
  18. : Application(context)
  19. {
  20. // The first handler for the first event in each frame.
  21. // To prevent a crash, we can only change the current scene at the start of a frame,
  22. // before any scene events are processed
  23. SubscribeToEvent(E_BEGINFRAME, URHO3D_HANDLER(App, ApplyAppState));
  24. }
  25. void Setup() override
  26. {
  27. engineParameters_[EP_WINDOW_TITLE] = "Urho3D Benchmark";
  28. engineParameters_[EP_LOG_NAME] = GetSubsystem<FileSystem>()->GetAppPreferencesDir("urho3d", "logs") + "99_Benchmark.log";
  29. engineParameters_[EP_FULL_SCREEN] = false;
  30. engineParameters_[EP_WINDOW_WIDTH] = 960;
  31. engineParameters_[EP_WINDOW_HEIGHT] = 720;
  32. engineParameters_[EP_FRAME_LIMITER] = false;
  33. }
  34. // This elements can be used anywhere in the program
  35. void CreateCurrentFpsUiElement()
  36. {
  37. UIElement* root = GetSubsystem<UI>()->GetRoot();
  38. root->SetDefaultStyle(GetSubsystem<ResourceCache>()->GetResource<XMLFile>("UI/DefaultStyle.xml"));
  39. Text* fpsElement = root->CreateChild<Text>(CURRENT_FPS_STR);
  40. fpsElement->SetStyleAuto();
  41. fpsElement->SetTextEffect(TE_SHADOW);
  42. fpsElement->SetPosition(10, 10);
  43. fpsElement->SetText("FPS");
  44. }
  45. void Start() override
  46. {
  47. context_->RegisterSubsystem(new AppStateManager(context_));
  48. AppStateManager* appStateManager = context_->GetSubsystem<AppStateManager>();
  49. appStateManager->SetRequiredAppStateId(APPSTATEID_MAINSCREEN);
  50. GetSubsystem<Input>()->SetToggleFullscreen(false); // Block Alt+Enter
  51. CreateCurrentFpsUiElement();
  52. }
  53. void ApplyAppState(StringHash eventType, VariantMap& eventData)
  54. {
  55. context_->GetSubsystem<AppStateManager>()->Apply();
  56. }
  57. };
  58. URHO3D_DEFINE_APPLICATION_MAIN(App);