Slightly improves minimap flares, makes animation more smooth via alpha fade in/out.

Tested By: Langbart
Differential Revision: https://code.wildfiregames.com/D4351
This was SVN commit r26001.
This commit is contained in:
Vladislav Belov 2021-11-16 16:58:32 +00:00
parent 0c4f59d0a7
commit 9b3dcd2610
11 changed files with 69 additions and 27 deletions

View File

@ -242,7 +242,7 @@
</optional> </optional>
<optional> <optional>
<attribute name="flare_animation_speed"> <attribute name="flare_animation_speed">
<data type="nonNegativeInteger"/> <data type="decimal"/>
</attribute> </attribute>
</optional> </optional>
<optional> <optional>
@ -252,7 +252,17 @@
</optional> </optional>
<optional> <optional>
<attribute name="flare_lifetime_seconds"> <attribute name="flare_lifetime_seconds">
<data type="nonNegativeInteger"/> <data type="decimal"/>
</attribute>
</optional>
<optional>
<attribute name="flare_start_fade_seconds">
<data type="decimal"/>
</attribute>
</optional>
<optional>
<attribute name="flare_stop_fade_seconds">
<data type="decimal"/>
</attribute> </attribute>
</optional> </optional>
<optional> <optional>

View File

@ -63,8 +63,10 @@
mask="true" mask="true"
flare_texture_count="16" flare_texture_count="16"
flare_render_size="32" flare_render_size="32"
flare_animation_speed="11" flare_animation_speed="10.67"
flare_interleave="true" flare_interleave="true"
flare_lifetime_seconds="6" flare_lifetime_seconds="6"
flare_start_fade_seconds="0.25"
flare_stop_fade_seconds="0.5"
/> />
</object> </object>

View File

@ -36,6 +36,7 @@
#include "lib/external_libraries/libsdl.h" #include "lib/external_libraries/libsdl.h"
#include "lib/ogl.h" #include "lib/ogl.h"
#include "lib/timer.h" #include "lib/timer.h"
#include "maths/MathUtil.h"
#include "ps/CLogger.h" #include "ps/CLogger.h"
#include "ps/ConfigDB.h" #include "ps/ConfigDB.h"
#include "ps/CStrInternStatic.h" #include "ps/CStrInternStatic.h"
@ -136,7 +137,9 @@ CMiniMap::CMiniMap(CGUI& pGUI) :
m_MapSize(0), m_MapScale(1.f), m_Mask(this, "mask", false), m_MapSize(0), m_MapScale(1.f), m_Mask(this, "mask", false),
m_FlareTextureCount(this, "flare_texture_count", 0), m_FlareRenderSize(this, "flare_render_size", 0), m_FlareTextureCount(this, "flare_texture_count", 0), m_FlareRenderSize(this, "flare_render_size", 0),
m_FlareInterleave(this, "flare_interleave", false), m_FlareAnimationSpeed(this, "flare_animation_speed", 0.0f), m_FlareInterleave(this, "flare_interleave", false), m_FlareAnimationSpeed(this, "flare_animation_speed", 0.0f),
m_FlareLifetimeSeconds(this, "flare_lifetime_seconds", 0.0f) m_FlareLifetimeSeconds(this, "flare_lifetime_seconds", 0.0f),
m_FlareStartFadeSeconds(this, "flare_start_fade_seconds", 0.0f),
m_FlareStopFadeSeconds(this, "flare_stop_fade_seconds", 0.0f)
{ {
m_Clicking = false; m_Clicking = false;
m_MouseHovering = false; m_MouseHovering = false;
@ -341,21 +344,38 @@ void CMiniMap::DrawFlare(CCanvas2D& canvas, const MapFlare& flare, double curren
flareCenter.X - m_FlareRenderSize, flareCenter.Y - m_FlareRenderSize, flareCenter.X - m_FlareRenderSize, flareCenter.Y - m_FlareRenderSize,
flareCenter.X + m_FlareRenderSize, flareCenter.Y + m_FlareRenderSize); flareCenter.X + m_FlareRenderSize, flareCenter.Y + m_FlareRenderSize);
const u32 flooredStep = floor((currentTime - flare.time) * m_FlareAnimationSpeed); const double deltaTime = currentTime - flare.time;
const double remainingTime = m_FlareLifetimeSeconds - deltaTime;
const u32 flooredStep = floor(deltaTime * m_FlareAnimationSpeed);
CTexturePtr texture = m_FlareTextures[flooredStep % m_FlareTextures.size()]; const float startFadeAlpha = m_FlareStartFadeSeconds > 0.0f ? deltaTime / m_FlareStartFadeSeconds : 1.0f;
// TODO: Only draw inside the minimap circle. const float stopFadeAlpha = m_FlareStopFadeSeconds > 0.0f ? remainingTime / m_FlareStopFadeSeconds : 1.0f;
canvas.DrawTexture(texture, destination, CRect(0, 0, texture->GetWidth(), texture->GetHeight()), flare.color, CColor(0.0f, 0.0f, 0.0f, 0.0f), 0.0f); const float alpha = Clamp(std::min(
SmoothStep(0.0f, 1.0f, startFadeAlpha), SmoothStep(0.0f, 1.0f, stopFadeAlpha)),
0.0f, 1.0f);
// Draw a second circle if the first has reached half of the animation DrawFlareFrame(canvas, flooredStep % m_FlareTextures.size(), destination, flare.color, alpha);
// Draw a second circle if the first has reached half of the animation.
if (m_FlareInterleave && flooredStep >= m_FlareTextures.size() / 2) if (m_FlareInterleave && flooredStep >= m_FlareTextures.size() / 2)
{ {
texture = m_FlareTextures[(flooredStep - m_FlareTextures.size() / 2) % m_FlareTextures.size()]; DrawFlareFrame(canvas, (flooredStep - m_FlareTextures.size() / 2) % m_FlareTextures.size(),
// TODO: Only draw inside the minimap circle. destination, flare.color, alpha);
canvas.DrawTexture(texture, destination, CRect(0, 0, texture->GetWidth(), texture->GetHeight()), flare.color, CColor(0.0f, 0.0f, 0.0f, 0.0f), 0.0f);
} }
} }
void CMiniMap::DrawFlareFrame(CCanvas2D& canvas, const u32 frameIndex,
const CRect& destination, const CColor& color, float alpha) const
{
// TODO: Only draw inside the minimap circle.
CTexturePtr texture = m_FlareTextures[frameIndex % m_FlareTextures.size()];
CColor finalColor = color;
finalColor.a *= alpha;
canvas.DrawTexture(texture, destination,
CRect(0, 0, texture->GetWidth(), texture->GetHeight()), finalColor,
CColor(0.0f, 0.0f, 0.0f, 0.0f), 0.0f);
}
void CMiniMap::Draw(CCanvas2D& canvas) void CMiniMap::Draw(CCanvas2D& canvas)
{ {
PROFILE3("render minimap"); PROFILE3("render minimap");

View File

@ -80,6 +80,8 @@ private:
CGUISimpleSetting<bool> m_FlareInterleave; CGUISimpleSetting<bool> m_FlareInterleave;
CGUISimpleSetting<float> m_FlareAnimationSpeed; CGUISimpleSetting<float> m_FlareAnimationSpeed;
CGUISimpleSetting<float> m_FlareLifetimeSeconds; CGUISimpleSetting<float> m_FlareLifetimeSeconds;
CGUISimpleSetting<float> m_FlareStartFadeSeconds;
CGUISimpleSetting<float> m_FlareStopFadeSeconds;
// Whether to draw a black square around and under the minimap. // Whether to draw a black square around and under the minimap.
CGUISimpleSetting<bool> m_Mask; CGUISimpleSetting<bool> m_Mask;
@ -94,7 +96,8 @@ private:
void DrawViewRect(CCanvas2D& canvas) const; void DrawViewRect(CCanvas2D& canvas) const;
void DrawFlare(CCanvas2D& canvas, const MapFlare& flare, double curentTime) const; void DrawFlare(CCanvas2D& canvas, const MapFlare& flare, double currentTime) const;
void DrawFlareFrame(CCanvas2D& canvas, const u32 frameIndex, const CRect& destination, const CColor& color, float alpha) const;
void GetMouseWorldCoordinates(float& x, float& z) const; void GetMouseWorldCoordinates(float& x, float& z) const;

View File

@ -22,13 +22,13 @@
#define RADTODEG(a) ((a) * (180.0f/(float)M_PI)) #define RADTODEG(a) ((a) * (180.0f/(float)M_PI))
#define SQR(x) ((x) * (x)) #define SQR(x) ((x) * (x))
template <typename T> template<typename T>
inline T Interpolate(const T& a, const T& b, float t) inline T Interpolate(const T& a, const T& b, float t)
{ {
return a + (b - a) * t; return a + (b - a) * t;
} }
template <typename T> template<typename T>
inline T Clamp(T value, T min, T max) inline T Clamp(T value, T min, T max)
{ {
if (value <= min) if (value <= min)
@ -38,6 +38,13 @@ inline T Clamp(T value, T min, T max)
return value; return value;
} }
template<typename T>
inline T SmoothStep(T edge0, T edge1, T value)
{
value = Clamp<T>((value - edge0) / (edge1 - edge0), 0, 1);
return value * value * (3 - 2 * value);
}
inline float sgn(float a) inline float sgn(float a)
{ {
if (a > 0.0f) if (a > 0.0f)