AppState_Benchmark04.cpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #include "AppState_Benchmark04.h"
  4. #include "AppStateManager.h"
  5. #include <Urho3D/Core/Timer.h>
  6. #include <Urho3D/Graphics/GraphicsEvents.h>
  7. #include <Urho3D/GraphicsAPI/Texture2D.h>
  8. #include <Urho3D/Input/Input.h>
  9. #include <Urho3D/Resource/ResourceCache.h>
  10. #include <Urho3D/DebugNew.h>
  11. using namespace Urho3D;
  12. void AppState_Benchmark04::OnEnter()
  13. {
  14. assert(!scene_);
  15. // Сцена и вьюпорт не нужны
  16. GetSubsystem<Input>()->SetMouseVisible(false);
  17. SubscribeToEvent(E_ENDALLVIEWSRENDER, URHO3D_HANDLER(AppState_Benchmark04, HandleEndAllViewsRender));
  18. fpsCounter_.Clear();
  19. spriteBatch_ = new SpriteBatch(context_);
  20. }
  21. void AppState_Benchmark04::OnLeave()
  22. {
  23. UnsubscribeFromAllEvents();
  24. spriteBatch_ = nullptr;
  25. }
  26. void AppState_Benchmark04::HandleEndAllViewsRender(StringHash eventType, VariantMap& eventData)
  27. {
  28. float timeStep = GetSubsystem<Time>()->GetTimeStep();
  29. fpsCounter_.Update(timeStep);
  30. UpdateCurrentFpsElement();
  31. if (GetSubsystem<Input>()->GetKeyDown(KEY_ESCAPE))
  32. {
  33. GetSubsystem<AppStateManager>()->SetRequiredAppStateId(APPSTATEID_MAINSCREEN);
  34. return;
  35. }
  36. if (fpsCounter_.GetTotalTime() >= 25.f)
  37. {
  38. GetSubsystem<AppStateManager>()->SetRequiredAppStateId(APPSTATEID_RESULTSCREEN);
  39. return;
  40. }
  41. angle_ += timeStep * 100.0f;
  42. angle_ = fmod(angle_, 360.0f);
  43. scale_ += timeStep;
  44. Graphics* graphics = GetSubsystem<Graphics>();
  45. ResourceCache* cache = GetSubsystem<ResourceCache>();
  46. Texture2D* ball = GetSubsystem<ResourceCache>()->GetResource<Texture2D>("Urho2D/Ball.png");
  47. Texture2D* head = cache->GetResource<Texture2D>("Textures/FishBoneLogo.png");
  48. GetSubsystem<Graphics>()->Clear(CLEAR_COLOR, Color::GREEN);
  49. for (int i = 0; i < 20000; i++)
  50. {
  51. spriteBatch_->DrawSprite(ball,
  52. Vector2(Random(0.0f, (float)(graphics->GetWidth() - ball->GetWidth())), Random(0.0f, (float)graphics->GetHeight() - ball->GetHeight())), nullptr, 0xFFFFFFFF);
  53. }
  54. spriteBatch_->DrawSprite(head, Vector2(200.0f, 200.0f), nullptr, 0xFFFFFFFF, 0.0f, Vector2::ZERO, Vector2::ONE, FlipModes::Both);
  55. float scale = cos(scale_) + 1.0f; // cos возвращает значения в диапазоне [-1, 1], значит scale будет в диапазоне [0, 2].
  56. Vector2 origin = Vector2(head->GetWidth() * 0.5f, head->GetHeight() * 0.5f);
  57. spriteBatch_->DrawSprite(head, Vector2(400.0f, 300.0f), nullptr, 0xFFFFFFFF, angle_, origin, Vector2(scale, scale));
  58. spriteBatch_->DrawString("Отзеркаленный текст", cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 40.0f,
  59. Vector2(250.0f, 200.0f), 0xFF0000FF, 0.0f, Vector2::ZERO, Vector2::ONE, FlipModes::Both);
  60. spriteBatch_->DrawString("Некий текст", cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 40.0f,
  61. Vector2(400.0f, 300.0f), 0xFFFF0000, angle_, Vector2::ZERO, Vector2(scale, scale));
  62. spriteBatch_->Flush();
  63. }