-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpriteComponent.cpp
48 lines (41 loc) · 1.12 KB
/
SpriteComponent.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
#include "SpriteComponent.h"
#include "Game.h"
#include "Actor.h"
SpriteComponent::SpriteComponent(Actor* owner, int drawOrder) :Component(owner),
mDrawOrder(drawOrder),
mTexture(nullptr),
mTexWidth(0),
mTexHeight(0)
{
mOwner->GetGame()->AddSprite(this);
}
SpriteComponent::~SpriteComponent()
{
mOwner->GetGame()->RemoveSprite(this);
}
void SpriteComponent::Draw(SDL_Renderer* renderer)
{
if (mTexture)
{
SDL_Rect r;
// Scale the width and height by owner scale
r.w = static_cast<int>(mTexWidth * mOwner->GetScale());
r.h = static_cast<int>(mTexHeight * mOwner->GetScale());
// Center the rectangle around the position of the owner
r.x = static_cast<int>(mOwner->GetPosition().x - r.w / 2);
r.y = static_cast<int>(mOwner->GetPosition().y - r.h / 2);
// Draw
SDL_RenderCopyEx(renderer,
mTexture,
nullptr,
&r,
-Math::ToDegrees(mOwner->GetRotation()),
nullptr,
SDL_FLIP_NONE);
}
}
void SpriteComponent::setTexture(SDL_Texture* texture)
{
mTexture = texture;
SDL_QueryTexture(texture, nullptr, nullptr, &mTexWidth, &mTexHeight);
}