From 67a7f9c3f2050c1681ee07e1804fa4a259db3487 Mon Sep 17 00:00:00 2001 From: frank <420@shampoo.ooo> Date: Tue, 26 Oct 2021 16:37:05 -0400 Subject: [PATCH] moved model class into separate file --- Makefile | 5 ++- src/Item.hpp | 1 + src/Model.cpp | 116 ++++++++++++++++++++++++++++++++++++++++++++++++ src/Model.hpp | 76 +++++++++++++++++++++++++++++++ src/Pudding.cpp | 101 ----------------------------------------- src/Pudding.hpp | 51 +-------------------- 6 files changed, 199 insertions(+), 151 deletions(-) create mode 100644 src/Model.cpp create mode 100644 src/Model.hpp diff --git a/Makefile b/Makefile index 16aeef5..32a954b 100644 --- a/Makefile +++ b/Makefile @@ -8,6 +8,8 @@ # //_________________________\\ `----------------------------------------------------------------' # # This Makefile should build a Linux binary from `make linux`. Other platform targets have not been tested yet. +# It requires the [SPACE BOX] framework to be located in the paths that can be configured in the code block +# below. ####################### # Location parameters # @@ -73,7 +75,8 @@ $(SB_SRC_DIR)GLObject.o : $(addprefix $(SB_SRC_DIR),Log.hpp) $(SB_SRC_DIR)Texture.o : $(addprefix $(SB_SRC_DIR),GLObject.hpp filesystem.hpp Log.hpp) $(SB_SRC_DIR)VBO.o : $(addprefix $(SB_SRC_DIR),Log.hpp GLObject.hpp Attributes.hpp extension.hpp) $(SB_SRC_DIR)Attributes.o : $(addprefix $(SB_SRC_DIR),Log.hpp extension.hpp) -$(SRC_DIR)Item.o : $(addprefix $(SB_SRC_DIR),extension.hpp Node.hpp Texture.hpp Log.hpp) +$(SRC_DIR)Model.o : $(addprefix $(SB_SRC_DIR),extension.hpp Attributes.hpp Texture.hpp) +$(SRC_DIR)Item.o : $(addprefix $(SB_SRC_DIR),extension.hpp Node.hpp Texture.hpp Log.hpp) $(SRC_DIR)Model.hpp $(SRC_DIR)Pudding.o : $(SRC_H_FILES) $(SB_H_FILES) %.o : %.cpp %.hpp $(CPPC) $(CPP_FLAGS) $< -c -o $@ diff --git a/src/Item.hpp b/src/Item.hpp index 4df1d00..c3b5323 100644 --- a/src/Item.hpp +++ b/src/Item.hpp @@ -28,6 +28,7 @@ #include "Node.hpp" #include "Texture.hpp" #include "Log.hpp" +#include "Model.hpp" class Item : public Node { diff --git a/src/Model.cpp b/src/Model.cpp new file mode 100644 index 0000000..599a5a0 --- /dev/null +++ b/src/Model.cpp @@ -0,0 +1,116 @@ +/* _______________ ,----------------------------------------------------------------. + //`````````````\\ \ \ + //~~~~~~~~~~~~~~~\\ \ 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); + } +} + +/* 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. Use + * this 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); +} + +/* 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; +} diff --git a/src/Model.hpp b/src/Model.hpp new file mode 100644 index 0000000..758da2a --- /dev/null +++ b/src/Model.hpp @@ -0,0 +1,76 @@ +/* _______________ ,----------------------------------------------------------------. + //`````````````\\ \ \ + //~~~~~~~~~~~~~~~\\ \ by @ohsqueezy & @sleepin \ + //=================\\ \ [ohsqueezy.itch.io] [sleepin.itch.io] \ + // \\ \ \ + // \\ \ code released under the zlib license [git.nugget.fun/pudding] \ + // ☆ GUNKISS ☆ \\ \ \ +//_________________________\\ `---------------------------------------------------------------*/ + +#ifndef MODEL_H_ +#define MODEL_H_ + +/* GL functions */ +#if defined(__EMSCRIPTEN__) +#include +#else +#include "glew/glew.h" +#endif + +#include +#include +#include +#include +#include "glm/glm.hpp" +#include "Attributes.hpp" +#include "Texture.hpp" + +class Model +{ + +private: + + std::map> model_attributes; + std::map model_texture; + glm::mat4 model_transformation = glm::mat4(1); + +public: + + Model(); + Model(const std::map>&); + Model(const std::map&); + std::map> attributes(); + std::shared_ptr attributes(const std::string&); + void attributes(const std::string&, const sb::Attributes&); + void attributes(const std::string&, const std::shared_ptr&); + void enable(); + void disable(); + sb::Texture texture(const std::string&); + void texture(const std::string&, const sb::Texture&); + void transformation(const glm::mat4&); + std::size_t size(); + operator glm::mat4() const; + +}; + +class Plane : public Model +{ + +public: + + inline const static std::shared_ptr position = std::make_shared(sb::Attributes{ + {-1.0f, 1.0f}, {1.0f, 1.0f}, {-1.0f, -1.0f}, + {1.0f, 1.0f}, {1.0f, -1.0f}, {-1.0f, -1.0f} + }); + inline const static std::shared_ptr uv = std::make_shared(sb::Attributes{ + {0.0f, 1.0f}, {1.0f, 1.0f}, {0.0f, 0.0f}, + {1.0f, 1.0f}, {1.0f, 0.0f}, {0.0f, 0.0f} + }); + +public: + + Plane() : Model(std::map>({{"position", position}, {"uv", uv}})) {} + +}; + +#endif diff --git a/src/Pudding.cpp b/src/Pudding.cpp index 7f1f111..6a7182c 100644 --- a/src/Pudding.cpp +++ b/src/Pudding.cpp @@ -852,104 +852,3 @@ void Pudding::update() previous_barcode = current_barcode; } } - -/* Default constructor for Model */ -Model::Model() {}; - -Model::Model(const std::map>& attributes_pack) -{ - for (auto attributes : attributes_pack) - { - this->attributes(attributes.first, attributes.second); - } -} - -Model::Model(const std::map& attributes_pack) -{ - for (auto attributes : attributes_pack) - { - this->attributes(attributes.first, attributes.second); - } -} - -/* 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. Use - * this 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); -} - -/* 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; -} diff --git a/src/Pudding.hpp b/src/Pudding.hpp index b8b9f4d..984ac86 100644 --- a/src/Pudding.hpp +++ b/src/Pudding.hpp @@ -32,61 +32,14 @@ #include "Color.hpp" #include "extension.hpp" #include "filesystem.hpp" -#include "Item.hpp" #include "Animation.hpp" #include "Texture.hpp" #include "GLObject.hpp" #include "Log.hpp" #include "Attributes.hpp" #include "VBO.hpp" - -class Model -{ - -private: - - std::map> model_attributes; - std::map model_texture; - glm::mat4 model_transformation = glm::mat4(1); - -public: - - Model(); - Model(const std::map>&); - Model(const std::map&); - std::map> attributes(); - std::shared_ptr attributes(const std::string&); - void attributes(const std::string&, const sb::Attributes&); - void attributes(const std::string&, const std::shared_ptr&); - void enable(); - void disable(); - sb::Texture texture(const std::string&); - void texture(const std::string&, const sb::Texture&); - void transformation(const glm::mat4&); - std::size_t size(); - operator glm::mat4() const; - -}; - -class Plane : public Model -{ - -public: - - inline const static std::shared_ptr position = std::make_shared(sb::Attributes{ - {-1.0f, 1.0f}, {1.0f, 1.0f}, {-1.0f, -1.0f}, - {1.0f, 1.0f}, {1.0f, -1.0f}, {-1.0f, -1.0f} - }); - inline const static std::shared_ptr uv = std::make_shared(sb::Attributes{ - {0.0f, 1.0f}, {1.0f, 1.0f}, {0.0f, 0.0f}, - {1.0f, 1.0f}, {1.0f, 0.0f}, {0.0f, 0.0f} - }); - -public: - - Plane() : Model(std::map>({{"position", position}, {"uv", uv}})) {} - -}; +#include "Item.hpp" +#include "Model.hpp" class Pudding : public Game {