forked from gameprogcpp/code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpriteComponent.cpp
More file actions
44 lines (39 loc) · 1.45 KB
/
SpriteComponent.cpp
File metadata and controls
44 lines (39 loc) · 1.45 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
// ----------------------------------------------------------------
// 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), pTexture(nullptr), mDrawOrder(drawOrder), mTexWidth(0), mTexHeight(0)
{
pOwner->GetGame()->AddSprite(this);
}
SpriteComponent::~SpriteComponent()
{
pOwner->GetGame()->RemoveSprite(this);
}
void SpriteComponent::Draw(SDL_Renderer* renderer)
{
if (pTexture)
{
SDL_Rect r;
// Scale the width/height by owner's scale
r.w = static_cast<int>(mTexWidth * pOwner->GetScale());
r.h = static_cast<int>(mTexHeight * pOwner->GetScale());
// Center the rectangle around the position of the owner
r.x = static_cast<int>(pOwner->GetPosition().x - r.w / 2);
r.y = static_cast<int>(pOwner->GetPosition().y - r.h / 2);
// Draw (have to convert angle from radians to degrees, and clockwise to counter)
SDL_RenderCopyEx(renderer, pTexture, nullptr, &r, -Math::ToDegrees(pOwner->GetRotation()), nullptr, SDL_FLIP_NONE);
}
}
void SpriteComponent::SetTexture(SDL_Texture* texture)
{
pTexture = texture;
// Set width/height
SDL_QueryTexture(texture, nullptr, nullptr, &mTexWidth, &mTexHeight);
}