-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathrepeatedquad.cpp
More file actions
85 lines (70 loc) · 2.44 KB
/
repeatedquad.cpp
File metadata and controls
85 lines (70 loc) · 2.44 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <fea/rendering/repeatedquad.hpp>
#include <fea/assert.hpp>
namespace fea
{
RepeatedQuad::RepeatedQuad() : AnimatedQuad()
{
mTileSize = getSize();
updateConstraints();
}
RepeatedQuad::RepeatedQuad(const glm::vec2& size) : AnimatedQuad(size)
{
mTileSize = getSize();
updateConstraints();
}
void RepeatedQuad::setSize(const glm::vec2& size)
{
Quad::setSize(size);
updateConstraints();
}
void RepeatedQuad::setTileSize(const glm::ivec2& size)
{
FEA_ASSERT(size.x > 0 && size.y > 0, "Size must be greater than zero in all dimensions when calling RepeatedQuad::setTileSize! " + std::to_string(size.x) + " " + std::to_string(size.y) + " provided.");
mTileSize = size;
updateConstraints();
}
const glm::ivec2& RepeatedQuad::getTileSize()
{
return mTileSize;
}
void RepeatedQuad::setScrollSpeed(const glm::vec2& speed)
{
mScrollSpeed = speed;
}
const glm::vec2& RepeatedQuad::getScrollSpeed() const
{
return mScrollSpeed;
}
void RepeatedQuad::updateConstraints()
{
glm::vec2 scaleFactor = getSize() / static_cast<glm::vec2>(mTileSize);
glm::vec2 texCoordsX = glm::vec2(0.0f, 1.0f) * mHFlip - glm::vec2(mCurrentScroll.x, mCurrentScroll.x);
glm::vec2 texCoordsY = glm::vec2(0.0f, 1.0f) * mVFlip - glm::vec2(mCurrentScroll.y, mCurrentScroll.y);
mTexCoords = {texCoordsX[0], texCoordsY[0],
texCoordsX[0], texCoordsY[1],
texCoordsX[1], texCoordsY[0],
texCoordsX[1], texCoordsY[0],
texCoordsX[0], texCoordsY[1],
texCoordsX[1], texCoordsY[1]};
for(uint32_t i = 0; i < 12; i += 2)
{
glm::vec2 newCoords = glm::vec2(mTexCoords[i], mTexCoords[i+1]) * scaleFactor;
mTexCoords[i] = newCoords.x;
mTexCoords[i+1] = newCoords.y;
}
}
void RepeatedQuad::tick()
{
AnimatedQuad::tick();
mCurrentScroll += mScrollSpeed;
updateConstraints();
if(mCurrentScroll.x > 1000.0f)
mCurrentScroll.x -= 1000.0f;
if(mCurrentScroll.y > 1000.0f)
mCurrentScroll.y -= 1000.0f;
if(mCurrentScroll.x < -1000.0f)
mCurrentScroll.x += 1000.0f;
if(mCurrentScroll.y < -1000.0f)
mCurrentScroll.y += 1000.0f;
}
}