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