openspades/Sources/Client/SmokeSpriteEntity.cpp

91 lines
2.2 KiB
C++
Raw Normal View History

2013-08-29 11:45:22 +09:00
/*
Copyright (c) 2013 yvt
2013-08-29 11:45:22 +09:00
This file is part of OpenSpades.
2013-08-29 11:45:22 +09:00
OpenSpades is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
2013-08-29 11:45:22 +09:00
OpenSpades is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
2013-08-29 11:45:22 +09:00
You should have received a copy of the GNU General Public License
along with OpenSpades. If not, see <http://www.gnu.org/licenses/>.
2013-08-29 11:45:22 +09:00
*/
2013-08-18 16:18:06 +09:00
#include <cstdio>
2013-08-18 16:18:06 +09:00
#include "Client.h"
#include "IImage.h"
#include "IRenderer.h"
#include "SmokeSpriteEntity.h"
2013-08-18 16:18:06 +09:00
namespace spades {
namespace client {
2013-08-18 16:18:06 +09:00
static IRenderer *lastRenderer = NULL;
static IImage *lastSeq[180];
static IImage *lastSeq2[48];
// FIXME: add "image manager"?
2013-08-18 16:18:06 +09:00
static void Load(IRenderer *r) {
if (r == lastRenderer)
2013-08-18 16:18:06 +09:00
return;
for (int i = 0; i < 180; i++) {
2013-08-18 16:18:06 +09:00
char buf[256];
sprintf(buf, "Textures/Smoke1/%03d.png", i);
2013-08-18 16:18:06 +09:00
lastSeq[i] = r->RegisterImage(buf);
}
for (int i = 0; i < 48; i++) {
char buf[256];
sprintf(buf, "Textures/Smoke2/%03d.png", i);
lastSeq2[i] = r->RegisterImage(buf);
}
2013-08-18 16:18:06 +09:00
lastRenderer = r;
}
IImage *SmokeSpriteEntity::GetSequence(int i, IRenderer *r, Type type) {
2013-08-18 16:18:06 +09:00
Load(r);
if (type == Type::Steady) {
2014-05-08 02:58:41 +09:00
SPAssert(i >= 0 && i < 180);
return lastSeq[i];
} else {
2014-05-08 02:58:41 +09:00
SPAssert(i >= 0 && i < 48);
return lastSeq2[i];
}
2013-08-18 16:18:06 +09:00
}
SmokeSpriteEntity::SmokeSpriteEntity(Client *c, Vector4 color, float fps, Type type)
: ParticleSpriteEntity(c, GetSequence(0, c->GetRenderer(), type), color),
fps(fps),
type(type) {
2013-08-18 16:18:06 +09:00
frame = 0.f;
}
void SmokeSpriteEntity::Preload(IRenderer *r) { Load(r); }
2013-08-18 16:18:06 +09:00
bool SmokeSpriteEntity::Update(float dt) {
frame += dt * fps;
if (type == Type::Steady) {
frame = fmodf(frame, 180.f);
} else {
if (frame > 47.f) {
frame = 47.f;
return false;
}
}
2013-08-18 16:18:06 +09:00
int fId = (int)floorf(frame);
SetImage(GetSequence(fId, GetRenderer(), type));
2013-08-18 16:18:06 +09:00
return ParticleSpriteEntity::Update(dt);
}
}
}