-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBGSpriteComponent.cpp
66 lines (55 loc) · 1.47 KB
/
BGSpriteComponent.cpp
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
#include "BGSpriteComponent.h"
#include "Actor.h"
BGSpriteComponent::BGSpriteComponent(Actor* owner, int drawOrder)
:SpriteComponent(owner, drawOrder)
,mScrollSpeed(0.0f)
{
}
void BGSpriteComponent::SetBGTextures(const std::vector<SDL_Texture*>& textures)
{
// need to start by initializing the offsets for the textures
int count = 0;
for (auto tex : textures)
{
BGTexture temp;
temp.mTexture = tex;
// Each texture is a screen width in offset
temp.mOffset.x = count * mScreenSize.x;
temp.mOffset.y = 0;
mBGTextures.emplace_back(temp);
count++;
}
}
void BGSpriteComponent::Update(float deltaTime)
{
SpriteComponent::Update(deltaTime);
for (auto& bg : mBGTextures)
{
// update the x offset
bg.mOffset.x += mScrollSpeed * deltaTime;
// if this is completely off the screen, reset offset to
// the right of the last bg texture
if (bg.mOffset.x < -mScreenSize.x)
{
bg.mOffset.x = (mBGTextures.size() - 1) * mScreenSize.x - 1;
}
}
}
void BGSpriteComponent::Draw(SDL_Renderer* renderer)
{
for (auto& bg : mBGTextures)
{
SDL_Rect r;
// Scale the width and height by owner scale
r.w = static_cast<int>(mScreenSize.x);
r.h = static_cast<int>(mScreenSize.y);
// Center the rectangle around the position of the owner
r.x = static_cast<int>(mOwner->GetPosition().x - r.w / 2 + bg.mOffset.x);
r.y = static_cast<int>(mOwner->GetPosition().y - r.h / 2 + bg.mOffset.y);
// Draw
SDL_RenderCopy(renderer,
bg.mTexture,
nullptr,
&r);
}
}