Blamite Game Engine - blam!  00296.01.12.21.0102.blamite
The core library for the Blamite Game Engine.
ColorTransition.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "../animation.h"
4 
5 #include <d2d1.h>
6 #include <string>
7 
9 #include <Strings/components/utils/math/math.h>
12 
13 #ifndef BLAM
14 #define BLAM
15 #endif
16 
17 namespace Blam::Animation
18 {
23  {
24  private:
25  D2D1_COLOR_F evaluated_color;
26 
27  D2D1_COLOR_F color_1;
28  D2D1_COLOR_F color_2;
29 
30  float fade_duration = 1.0f;
31 
32  int ticks_per_fade = 0;
33  int local_ticks = 0;
34  bool swap_fade_direction = false;
35  float elapsed_time = 0.0f;
36 
37  bool loop = false;
38  bool animation_done = false;
39  bool is_started = false;
40  bool pause = false;
41 
50  float GetIntermediaryColor(float subcolor1, float subcolor2)
51  {
52  float blend_amount = fabsf((subcolor1 - subcolor2) * elapsed_time);
53 
54  if (subcolor1 < subcolor2)
55  {
56  return subcolor2 - blend_amount;
57  }
58  else
59  {
60  return subcolor2 + blend_amount;
61  }
62  }
63 
67  void OnTickEvent(TickEvent* event)
68  {
69  if (!pause)
70  {
71  if (loop)
72  {
73  local_ticks++;
74 
75  if (local_ticks == ticks_per_fade)
76  {
77  swap_fade_direction = !swap_fade_direction;
78  local_ticks = 1;
79  }
80 
81  elapsed_time = float(local_ticks) / float(ticks_per_fade);
82 
83  if (swap_fade_direction)
84  {
85  evaluated_color.r = GetIntermediaryColor(color_1.r, color_2.r);
86  evaluated_color.g = GetIntermediaryColor(color_1.g, color_2.g);
87  evaluated_color.b = GetIntermediaryColor(color_1.b, color_2.b);
88  evaluated_color.a = GetIntermediaryColor(color_1.a, color_2.a);
89  }
90  else
91  {
92  evaluated_color.r = GetIntermediaryColor(color_2.r, color_1.r);
93  evaluated_color.g = GetIntermediaryColor(color_2.g, color_1.g);
94  evaluated_color.b = GetIntermediaryColor(color_2.b, color_1.b);
95  evaluated_color.a = GetIntermediaryColor(color_2.a, color_1.a);
96  }
97  }
98  else
99  {
100  if (!animation_done)
101  {
102  local_ticks++;
103 
104  elapsed_time = float(local_ticks) / float(ticks_per_fade);
105 
106  evaluated_color.r = GetIntermediaryColor(color_1.r, color_2.r);
107  evaluated_color.g = GetIntermediaryColor(color_1.g, color_2.g);
108  evaluated_color.b = GetIntermediaryColor(color_1.b, color_2.b);
109  evaluated_color.a = GetIntermediaryColor(color_1.a, color_2.a);
110 
111  if (evaluated_color.r == color_1.r && evaluated_color.g == color_1.g && evaluated_color.b == color_1.b && evaluated_color.a == color_1.a)
112  {
113  animation_done = true;
114  }
115  }
116  else
117  {
119  }
120  }
121  }
122  }
123 
124  public:
133  void Initialize(D2D1_COLOR_F _c1, D2D1_COLOR_F _c2, float fade_duration, bool _loop)
134  {
135  float full_ticks_per_fade = Blam::Tick::MaxTicksPerSecond() * fade_duration;
136 
137  ticks_per_fade = full_ticks_per_fade;
138 
139  if (!BlamStrings::Utils::Math::FloatIsInteger(full_ticks_per_fade))
140  {
141  Blam::Logger::LogEvent("color transition animation was configured to use fade duration of " + std::to_string(fade_duration) +
142  ", which results in a tick per fade of " + std::to_string(full_ticks_per_fade) + " - the animation will not be properly fixed to a per-second timestep and "
143  + "this can cause unforeseen issues!", WSV_WARNING);
144  }
145 
146  color_1 = _c1;
147  color_2 = _c2;
148 
149  loop = _loop;
150 
151  evaluated_color = _c2;
152  }
153 
157  void Start()
158  {
159  if (!is_started)
160  {
161  this->Subscribe(EventType_Tick);
163 
164  is_started = true;
165  }
166  }
167 
171  D2D1_COLOR_F GetColor()
172  {
173  return evaluated_color;
174  }
175 
179  bool HasStarted()
180  {
181  return is_started;
182  }
183 
184  void Pause()
185  {
186  pause = true;
187  }
188 
189  void Resume()
190  {
191  pause = false;
192  }
193  };
194 }
Blam::Events::RegisterListener
BLAM void RegisterListener(BlamEventListener *listener)
Registers a an event listener.
Definition: events.cpp:75
Blam::Logger::LogEvent
BLAM void LogEvent(std::string message)
Logs a message to the log and/or console.
Definition: aliases.cpp:101
Blam::Animation::ColorTransitionAnimation
Animation used to handle an alternating fade between two colors.
Definition: ColorTransition.h:22
logger.h
Blam::Animation
Namespace for animation data.
Definition: animation.h:12
Blam::Tick::MaxTicksPerSecond
BLAM int MaxTicksPerSecond()
Retrieves the maximum number of ticks per second.
Definition: tick.cpp:146
Blam::Animation::ColorTransitionAnimation::GetColor
D2D1_COLOR_F GetColor()
Returns the evaluated color data.
Definition: ColorTransition.h:171
events.h
Blam::Animation::ColorTransitionAnimation::Initialize
void Initialize(D2D1_COLOR_F _c1, D2D1_COLOR_F _c2, float fade_duration, bool _loop)
Prepares data needed for the animation.
Definition: ColorTransition.h:133
tick.h
TickEvent
Class representing an engine tick event.
Definition: TickEvent.h:10
Blam::Animation::ColorTransitionAnimation::Pause
void Pause()
Definition: ColorTransition.h:184
Blam::Animation::ColorTransitionAnimation::Resume
void Resume()
Definition: ColorTransition.h:189
Blam::Animation::ColorTransitionAnimation::Start
void Start()
Instructs the listener to start animating.
Definition: ColorTransition.h:157
BlamEventListener::Subscribe
void Subscribe(BlamEventType type)
Subscribes to an event type.
Definition: BlamEventListener.cpp:3
Blam::Events::UnregisterListener
BLAM void UnregisterListener(BlamEventListener *listener)
Unregisters a an event listener.
Definition: events.cpp:80
EventType_Tick
@ EventType_Tick
Definition: BlamEvent.h:9
BlamEventListener
Class representing an Event Listener.
Definition: events.h:27
WSV_WARNING
#define WSV_WARNING
Macro for 'Warning' log seveirty. Original pre-enum value was 3.
Definition: logger.h:18
Blam::Animation::ColorTransitionAnimation::HasStarted
bool HasStarted()
Determines whether or not the animation has been started.
Definition: ColorTransition.h:179