// ---------------------------------------------------------------- // 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(mTexWidth * mOwner->GetScale()); r.h = static_cast(mTexHeight * mOwner->GetScale()); // Center the rectangle around the position of the owner r.x = static_cast(mOwner->GetPosition().x - r.w / 2); r.y = static_cast(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); }