-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSprite.cpp
More file actions
62 lines (49 loc) · 1.29 KB
/
Sprite.cpp
File metadata and controls
62 lines (49 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include "Sprite.h"
#include "Scene.h"
Sprite::Sprite()
{
m_index = 0;
m_delay = 0;
}
void Sprite::update(const int delta)
{
const float f = (delta / 1000.0f) * (1000.0f / m_delay);
const float c = m_index + f;
const float index = c < m_frames.count() ? c : 0;
m_index = index;
}
QSGTexture *Sprite::currentFrame(Scene *scene, const bool flipped)
{
const int i = m_index;
const int count = m_frames.count();
const int index = i <= count ? i : 0;
const int foo = index < 0 ? count - 1 : index;
const int identifier = foo + count * flipped;
return m_textures.value(identifier) ?: createTexture(scene, foo, flipped);
}
float Sprite::delay() const
{
return m_delay;
}
int Sprite::frameCount() const
{
return m_frames.count();
}
QSGTexture *Sprite::createTexture(Scene *scene, const int index, const bool flipped)
{
QImage image = m_frames[index];
QImage adjusted = image.mirrored(flipped, false);
QSGTexture *texture = scene->createTexture(adjusted);
const int count = m_frames.count();
const int identifier = index + count * flipped;
qDebug() << "Creating new texture with id" << identifier;
return m_textures
.insert(identifier, texture)
.value();
}
QDataStream &operator >>(QDataStream &stream, Sprite &sprite)
{
stream >> sprite.m_delay;
stream >> sprite.m_frames;
return stream;
}