Sprites.cpp 5.0 KB

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