Sprites.cpp 5.0 KB

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