// ---------------------------------------------------------------- // 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 "Texture.h" #include "Shader.h" #include "Actor.h" #include "Game.h" #include "Renderer.h" SpriteComponent::SpriteComponent(Actor* owner, int drawOrder): Component(owner), pTexture(nullptr), mDrawOrder(drawOrder), mTexWidth(0), mTexHeight(0) { pOwner->GetGame()->GetRenderer()->AddSprite(this); } SpriteComponent::~SpriteComponent() { pOwner->GetGame()->GetRenderer()->RemoveSprite(this); } void SpriteComponent::Draw(Shader* shader) { if (pTexture) { // Scale the quad by the width/height of texture Matrix4 scaleMat = Matrix4::CreateScale(static_cast(mTexWidth), static_cast(mTexHeight), 1.0f); Matrix4 world = scaleMat * pOwner->GetWorldTransform(); // Since all sprites use the same shader/vertices, // the game first sets them active before any sprite draws // Set world transform shader->SetMatrixUniform("uWorldTransform", world); // Set current texture pTexture->SetActive(); // Draw quad glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr); } } void SpriteComponent::SetTexture(Texture* texture) { pTexture = texture; // Set width/height mTexWidth = texture->GetWidth(); mTexHeight = texture->GetHeight(); }