Implement animated flash tiles in Tilemap

Flash tiles start with an alpha of 60, which ramps up to 120
in 16 frames and 4 steps, then ramps down again over the same period.
This commit is contained in:
Jonas Kulla 2013-09-23 08:27:28 +02:00
parent 9e63fb6b64
commit 7a6c05bba0
7 changed files with 70 additions and 6 deletions

View File

@ -96,6 +96,7 @@ EMBED = shader/transSimple.frag \
shader/simple.frag \
shader/simpleColor.frag \
shader/simpleAlpha.frag \
shader/flashMap.frag \
shader/simple.vert \
shader/simpleColor.vert \
shader/sprite.vert \

13
shader/flashMap.frag Normal file
View File

@ -0,0 +1,13 @@
uniform float alpha;
varying vec4 v_color;
void main()
{
vec4 frag = vec4(0, 0, 0, 1);
frag.rgb = mix(vec3(0, 0, 0), v_color.rgb, alpha);
gl_FragColor = frag;
}

View File

@ -71,6 +71,7 @@ struct GlobalStatePrivate
SimpleSpriteShader simpleSpriteShader;
SpriteShader spriteShader;
PlaneShader planeShader;
FlashMapShader flashMapShader;
TransShader transShader;
SimpleTransShader sTransShader;
HueShader hueShader;
@ -187,6 +188,7 @@ GSATT(SimpleAlphaShader&, simpleAlphaShader)
GSATT(SimpleSpriteShader&, simpleSpriteShader)
GSATT(SpriteShader&, spriteShader)
GSATT(PlaneShader&, planeShader)
GSATT(FlashMapShader&, flashMapShader)
GSATT(TransShader&, transShader)
GSATT(SimpleTransShader&, sTransShader)
GSATT(HueShader&, hueShader)

View File

@ -48,6 +48,7 @@ class SimpleAlphaShader;
class SimpleSpriteShader;
class SpriteShader;
class PlaneShader;
class FlashMapShader;
class TransShader;
class SimpleTransShader;
class HueShader;
@ -87,6 +88,7 @@ struct GlobalState
SimpleSpriteShader &simpleSpriteShader();
SpriteShader &spriteShader();
PlaneShader &planeShader();
FlashMapShader &flashMapShader();
TransShader &transShader();
SimpleTransShader &sTransShader();
HueShader &hueShader();

View File

@ -36,6 +36,7 @@
#include "../simple.frag.xxd"
#include "../simpleColor.frag.xxd"
#include "../simpleAlpha.frag.xxd"
#include "../flashMap.frag.xxd"
#include "../simple.vert.xxd"
#include "../simpleColor.vert.xxd"
#include "../sprite.vert.xxd"
@ -369,6 +370,21 @@ void PlaneShader::setOpacity(float value)
}
FlashMapShader::FlashMapShader()
{
INIT_SHADER(simpleColor, flashMap);
ShaderBase::init();
GET_U(alpha);
}
void FlashMapShader::setAlpha(float value)
{
glUniform1f(u_alpha, value);
}
HueShader::HueShader()
{
INIT_SHADER(simple, hue);

View File

@ -176,6 +176,17 @@ private:
GLint u_tone, u_color, u_flash, u_opacity;
};
class FlashMapShader : public ShaderBase
{
public:
FlashMapShader();
void setAlpha(float value);
private:
GLint u_alpha;
};
class HueShader : public ShaderBase
{
public:

View File

@ -144,6 +144,18 @@ static const Position positions[] =
static elementsN(positions);
static const uint8_t flashAlpha[] =
{
/* Fade in */
0x3C, 0x3C, 0x3C, 0x3C, 0x4B, 0x4B, 0x4B, 0x4B,
0x5A, 0x5A, 0x5A, 0x5A, 0x69, 0x69, 0x69, 0x69,
/* Fade out */
0x78, 0x78, 0x78, 0x78, 0x69, 0x69, 0x69, 0x69,
0x5A, 0x5A, 0x5A, 0x5A, 0x4B, 0x4B, 0x4B, 0x4B
};
static elementsN(flashAlpha);
struct GroundLayer : public ViewportElement
{
GLsizei vboCount;
@ -233,6 +245,7 @@ struct TilemapPrivate
VAO::ID vao;
VBO::ID vbo;
int quadCount;
uint8_t alphaIdx;
} flash;
/* Scene elements */
@ -319,6 +332,7 @@ struct TilemapPrivate
flash.vbo = VBO::gen();
flash.vao = VAO::gen();
flash.quadCount = 0;
flash.alphaIdx = 0;
VAO::bind(flash.vao);
@ -751,12 +765,11 @@ struct TilemapPrivate
if (packed == 0)
return false;
const float max = 60.0 / 255.0;
const float step = max / 0xF;
const float max = 0xF;
float b = ((packed & 0x000F) >> 0) * step;
float g = ((packed & 0x00F0) >> 4) * step;
float r = ((packed & 0x0F00) >> 8) * step;
float b = ((packed & 0x000F) >> 0) / max;
float g = ((packed & 0x00F0) >> 4) / max;
float r = ((packed & 0x0F00) >> 8) / max;
out = Vec4(r, g, b, 1);
@ -923,9 +936,10 @@ void GroundLayer::draw()
glState.blendMode.pushSet(BlendAddition);
glState.texture2D.pushSet(false);
SimpleColorShader &shader = gState->simpleColorShader();
FlashMapShader &shader = gState->flashMapShader();
shader.bind();
shader.applyViewportProj();
shader.setAlpha(flashAlpha[p->flash.alphaIdx] / 255.f);
for (int i = 0; i < positionsN; ++i)
{
@ -1062,6 +1076,11 @@ static const uchar atAnimation[16*4] =
void Tilemap::update()
{
/* Animate flash */
if (++p->flash.alphaIdx > flashAlphaN-1)
p->flash.alphaIdx = 0;
/* Animate autotiles */
if (!p->atlas.animated)
return;