Sprites.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #include <Urho3D/Core/CoreEvents.h>
  4. #include <Urho3D/Engine/Engine.h>
  5. #include <Urho3D/Graphics/Graphics.h>
  6. #include <Urho3D/GraphicsAPI/Texture2D.h>
  7. #include <Urho3D/UI/Sprite.h>
  8. #include <Urho3D/UI/UI.h>
  9. #include "Sprites.h"
  10. #include <Urho3D/DebugNew.h>
  11. // Number of sprites to draw
  12. static const unsigned NUM_SPRITES = 100;
  13. // Custom variable identifier for storing sprite velocity within the UI element
  14. static constexpr StringHash VAR_VELOCITY = "Velocity"_hash;
  15. URHO3D_DEFINE_APPLICATION_MAIN(Sprites)
  16. Sprites::Sprites(Context* context) :
  17. Sample(context)
  18. {
  19. }
  20. void Sprites::Start()
  21. {
  22. // Execute base class startup
  23. Sample::Start();
  24. // Create the sprites to the user interface
  25. CreateSprites();
  26. // Hook up to the frame update events
  27. SubscribeToEvents();
  28. // Set the mouse mode to use in the sample
  29. Sample::InitMouseMode(MM_FREE);
  30. }
  31. void Sprites::CreateSprites()
  32. {
  33. auto* cache = GetSubsystem<ResourceCache>();
  34. auto* graphics = GetSubsystem<Graphics>();
  35. auto* ui = GetSubsystem<UI>();
  36. // Get rendering window size as floats
  37. auto width = (float)graphics->GetWidth();
  38. auto height = (float)graphics->GetHeight();
  39. // Get the Urho3D fish texture
  40. auto* decalTex = cache->GetResource<Texture2D>("Textures/UrhoDecal.dds");
  41. for (unsigned i = 0; i < NUM_SPRITES; ++i)
  42. {
  43. // Create a new sprite, set it to use the texture
  44. SharedPtr<Sprite> sprite(new Sprite(context_));
  45. sprite->SetTexture(decalTex);
  46. // The UI root element is as big as the rendering window, set random position within it
  47. sprite->SetPosition(Vector2(Random() * width, Random() * height));
  48. // Set sprite size & hotspot in its center
  49. sprite->SetSize(IntVector2(128, 128));
  50. sprite->SetHotSpot(IntVector2(64, 64));
  51. // Set random rotation in degrees and random scale
  52. sprite->SetRotation(Random() * 360.0f);
  53. sprite->SetScale(Random(1.0f) + 0.5f);
  54. // Set random color and additive blending mode
  55. sprite->SetColor(Color(Random(0.5f) + 0.5f, Random(0.5f) + 0.5f, Random(0.5f) + 0.5f));
  56. sprite->SetBlendMode(BLEND_ADD);
  57. // Add as a child of the root UI element
  58. ui->GetRoot()->AddChild(sprite);
  59. // Store sprite's velocity as a custom variable
  60. sprite->SetVar(VAR_VELOCITY, Vector2(Random(200.0f) - 100.0f, Random(200.0f) - 100.0f));
  61. // Store sprites to our own container for easy movement update iteration
  62. sprites_.Push(sprite);
  63. }
  64. }
  65. void Sprites::MoveSprites(float timeStep)
  66. {
  67. auto* graphics = GetSubsystem<Graphics>();
  68. auto width = (float)graphics->GetWidth();
  69. auto height = (float)graphics->GetHeight();
  70. // Go through all sprites
  71. for (const SharedPtr<Sprite>& sprite : sprites_)
  72. {
  73. // Rotate
  74. float newRot = sprite->GetRotation() + timeStep * 30.0f;
  75. sprite->SetRotation(newRot);
  76. // Move, wrap around rendering window edges
  77. Vector2 newPos = sprite->GetPosition() + sprite->GetVar(VAR_VELOCITY).GetVector2() * timeStep;
  78. if (newPos.x_ < 0.0f)
  79. newPos.x_ += width;
  80. if (newPos.x_ >= width)
  81. newPos.x_ -= width;
  82. if (newPos.y_ < 0.0f)
  83. newPos.y_ += height;
  84. if (newPos.y_ >= height)
  85. newPos.y_ -= height;
  86. sprite->SetPosition(newPos);
  87. }
  88. }
  89. void Sprites::SubscribeToEvents()
  90. {
  91. // Subscribe HandleUpdate() function for processing update events
  92. SubscribeToEvent(E_UPDATE, URHO3D_HANDLER(Sprites, HandleUpdate));
  93. }
  94. void Sprites::HandleUpdate(StringHash eventType, VariantMap& eventData)
  95. {
  96. using namespace Update;
  97. // Take the frame time step, which is stored as a float
  98. float timeStep = eventData[P_TIMESTEP].GetFloat();
  99. // Move sprites, scale movement with time step
  100. MoveSprites(timeStep);
  101. }