diff --git a/client/composite_image.hpp b/client/composite_image.hpp new file mode 100644 index 0000000..3b27d57 --- /dev/null +++ b/client/composite_image.hpp @@ -0,0 +1,138 @@ +/* Copyright: (c) Kayne Ruse 2013-2016 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#pragma once + +#include "image.hpp" + +#include +#include +#include + +template +class CompositeImage { +public: + CompositeImage() = default; + ~CompositeImage() = default; + + void Load(std::string spriteDir, std::list nameList); + void SetImageTextures(std::map& templateImages); + void Free(); + + void DrawTo(SDL_Renderer* const, Sint16 x, Sint16 y, double scaleX = 1.0, double scaleY = 1.0); + + //accessors & mutators + T* Find(std::string name); + + bool Enable(std::string name); + bool Disable(std::string name); + void EnableAll(); + void DisableAll(); + +private: + std::map > imageMap; +}; + + +template +void CompositeImage::Load(std::string spriteDir, std::list nameList) { + for (auto& it : nameList) { + imageMap[it].first = true; + imageMap[it].second.Load(spriteDir + it); + } +} + +template +void CompositeImage::SetImageTextures(std::map& templateImages) { + for (auto& it : templateImages) { + imageMap[it.first].first = true; + imageMap[it.first].second.SetTexture(it.second.GetTexture()); + } +} + +template +void CompositeImage::Free() { + imageMap.clear(); +} + +template +void CompositeImage::DrawTo(SDL_Renderer* const dest, Sint16 x, Sint16 y, double scaleX, double scaleY) { + //draw all members, regardless of internal ordering + for (auto& it : imageMap) { + if (it.first) { + it.second.DrawTo(dest, x, y, scaleX, scaleY); + } + } +} + + +//------------------------- +//accessors & mutators +//------------------------- + +template +T* CompositeImage::Find(std::string name) { + auto it = imageMap.find(name); + if (it == imageMap.end()) { + return nullptr; + } + else { + return &it.second; + } +} + +template +bool CompositeImage::Enable(std::string name) { + auto it = imageMap.find(name); + if (it == imageMap.end()) { + return false; + } + else { + imageMap[name].first = true; + return true; + } +} + +template +bool CompositeImage::Disable(std::string name) { + auto it = imageMap.find(name); + if (it == imageMap.end()) { + return false; + } + else { + imageMap[name].first = false; + return true; + } +} + +template +void CompositeImage::EnableAll() { + for (auto& it : imageMap) { + it.first = true; + } +} + +template +void CompositeImage::DisableAll() { + for (auto& it : imageMap) { + it.first = false; + } +} diff --git a/client/entities/base_barrier.cpp b/client/entities/base_barrier.cpp index ae94f60..380a812 100644 --- a/client/entities/base_barrier.cpp +++ b/client/entities/base_barrier.cpp @@ -25,8 +25,17 @@ #include +BaseBarrier::BaseBarrier(Image argBaseImage, std::map& templateImages) { + baseImage = argBaseImage; + composite.SetImageTextures(templateImages); +} + +BaseBarrier::~BaseBarrier() { + // +} + void BaseBarrier::CorrectSprite() { - //TODO: (9) BaseBarrier::CorrectSprite() + //TODO: link status to sprite } int BaseBarrier::SetStatus(int k, int v) { @@ -51,3 +60,7 @@ int* BaseBarrier::SetStatusArray(int* ptr) { int* BaseBarrier::GetStatusArray() { return status; } + +CompositeImage<>* BaseBarrier::GetComposite() { + return &composite; +} \ No newline at end of file diff --git a/client/entities/base_barrier.hpp b/client/entities/base_barrier.hpp index 26a0146..bbac84f 100644 --- a/client/entities/base_barrier.hpp +++ b/client/entities/base_barrier.hpp @@ -23,10 +23,13 @@ #include "entity.hpp" +#include "composite_image.hpp" + class BaseBarrier: public Entity { public: - BaseBarrier() = default; - virtual ~BaseBarrier() = default; + BaseBarrier() = delete; + BaseBarrier(Image baseImage, std::map& templateImages); + virtual ~BaseBarrier(); void CorrectSprite(); @@ -36,8 +39,12 @@ public: int* SetStatusArray(int*); int* GetStatusArray(); + CompositeImage<>* GetComposite(); protected: //metadata int status[8]; + + Image baseImage; + CompositeImage<> composite; };