/* _______________ ,----------------------------------------------------------------. //`````````````\\ \ \ //~~~~~~~~~~~~~~~\\ \ by @ohsqueezy & @sleepin \ //=================\\ \ [ohsqueezy.itch.io] [sleepin.itch.io] \ // \\ \ \ // \\ \ code released under the zlib license [git.nugget.fun/pudding] \ // ☆ GUNKISS ☆ \\ \ \ //_________________________\\ `---------------------------------------------------------------*/ #include "Model.hpp" /* Default constructor for Model */ Model::Model() {}; /* Construct a Model, adding Attributes each already wrapped in a shared pointer. The attributes should * be passed as a map with each key being a name and each value being a shared pointer to attributes. */ Model::Model(const std::map>& attributes_pack) { for (auto attributes : attributes_pack) { this->attributes(attributes.first, attributes.second); } } /* Construct a Model, adding Attributes, which will each be wrapped in a shared pointer and stored in the * created object. The attributes should be passed as a map with each key being a name and each value being * an attributes object. */ Model::Model(const std::map& attributes_pack) { for (auto attributes : attributes_pack) { this->attributes(attributes.first, attributes.second); } } /* Construct a new model object by passing a list of names which will be used to initialize * empty attributes objects with the given names */ Model::Model(const std::initializer_list& names) { for (const std::string& name : names) { this->attributes(name, sb::Attributes()); } } /* Get the entire map of attributes, each wrapped in its shared pointer held by this object. * Can be used to iterate through the attributes. */ std::map>& Model::attributes() { return model_attributes; } /* Get the attributes under name, wrapped in the shared pointer held by this object. This * function uses the at method of std::map, so name must refer to attributes already * stored in this model. Use this function to share ownership of the attributes or to gain * access to the public interface of the attributes. */ std::shared_ptr& Model::attributes(const std::string& name) { return model_attributes.at(name); } /* Get the attributes under name, wrapped in the shared pointer held by this object. This * function uses operator[] or std::map, so this can be used to add new attributes to the * object if they are wrapped in a shared pointer. */ std::shared_ptr& Model::operator[](const std::string& name) { auto element = model_attributes.find(name); if (element == model_attributes.end()) { attributes(name, sb::Attributes{}); } return model_attributes[name]; } /* Assign name to attributes, copy and wrap in a shared pointer. The model can share * ownership of the created attribute memory with callers that request it. */ void Model::attributes(const std::string& name, const sb::Attributes& attributes) { this->attributes(name, std::make_shared(attributes)); } /* Assign name to attributes and share ownership. */ void Model::attributes(const std::string& name, const std::shared_ptr& attributes) { model_attributes[name] = attributes; } /* Enable all attributes. */ void Model::enable() { for (const auto& attributes : this->attributes()) { attributes.second->enable(); } } /* Disable all attributes. */ void Model::disable() { for (const auto& attributes : this->attributes()) { attributes.second->disable(); } } /* Get the texture at name. This can be used to read the texture memory, share ownership of it, or * anything else a Texture object can be used for with direct calls to GL functions. */ sb::Texture& Model::texture(const std::string& name) { return model_texture.at(name); } /* Assign name to texture and share ownership. */ void Model::texture(const std::string& name, const sb::Texture& texture) { model_texture[name] = texture; } /* Set the transformation matrix. */ void Model::transformation(const glm::mat4& transformation) { model_transformation = transformation; } /* Return the size in bytes of the sum of the attributes. */ std::size_t Model::size() { std::size_t sum = 0; for (const auto& attributes : this->attributes()) { sum += attributes.second->size(); } return sum; } /* Return the transformation matrix. */ Model::operator glm::mat4() const { return model_transformation; } /* Increment offset that indicates which tile texture is active. If offset reaches the end of the container, * start over from the beginning. */ void Background::next() { offset = ++offset % model_texture.size(); } /* Return the currently active tile texture. */ const sb::Texture& Background::current() const { auto position = model_texture.begin(); std::advance(position, offset); return position->second; } CameraView::CameraView() : Plane() { texture("front", sb::Texture()); texture("back", sb::Texture()); } void CameraView::generate(const glm::vec2& size) { for (sb::Texture* buffer : {&texture("front"), &texture("back")}) { buffer->generate(size); } } sb::Texture& CameraView::current() { return swapped ? texture("back") : texture("front"); } sb::Texture& CameraView::free() { return swapped ? texture("front") : texture("back"); } void CameraView::swap() { swapped = !swapped; }