-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpriteComponent.cpp
56 lines (50 loc) · 1.47 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
49
50
51
52
53
54
55
56
// ----------------------------------------------------------------
// From Game Programming in C++ by Sanjay Madhav
// Copyright (C) 2017 Sanjay Madhav. All rights reserved.
//
// Released under the BSD License
// See LICENSE in root directory for full details.
// ----------------------------------------------------------------
#include "SpriteComponent.h"
#include "Actor.h"
#include "Game.h"
SpriteComponent::SpriteComponent(Actor* owner, int drawOrder)
:Component(owner)
,mTexture(nullptr)
,mDrawOrder(drawOrder)
,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/height by owner's 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 (have to convert angle from radians to degrees, and clockwise to counter)
SDL_RenderCopyEx(renderer,
mTexture,
nullptr,
&r,
-Math::ToDegrees(mOwner->GetRotation()),
nullptr,
SDL_FLIP_NONE);
}
}
void SpriteComponent::SetTexture(SDL_Texture* texture)
{
mTexture = texture;
// Set width/height
SDL_QueryTexture(texture, nullptr, nullptr, &mTexWidth, &mTexHeight);
}