From 9f92cab6b884711ca8be050b500a2880a955f001 Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Sat, 23 Jun 2018 15:49:21 +0100 Subject: Switching to east const This is compared to const west, which is not very consistent, as const always acts on what is on its left, and if there isn't anything on the left, it will act on what is on its right. By always placing const on the right of what it should act on, the rule becomes more consistent. --- yage/core/core.h | 2 +- yage/core/exception.cpp | 2 +- yage/core/exception.h | 2 +- yage/core/imageloader.cpp | 2 +- yage/core/imageloader.h | 2 +- yage/core/iomanager.cpp | 2 +- yage/core/iomanager.h | 2 +- yage/core/logger.cpp | 24 ++++++++++++------------ yage/core/logger.h | 42 +++++++++++++++++++++--------------------- yage/core/resourcemanager.cpp | 2 +- yage/core/resourcemanager.h | 2 +- yage/core/texturecache.cpp | 2 +- yage/core/texturecache.h | 2 +- yage/core/window.h | 4 ++-- yage/render/shader.cpp | 12 ++++++------ yage/render/shader.h | 12 ++++++------ 16 files changed, 58 insertions(+), 58 deletions(-) diff --git a/yage/core/core.h b/yage/core/core.h index 1237d9d0..5411eaac 100644 --- a/yage/core/core.h +++ b/yage/core/core.h @@ -20,7 +20,7 @@ namespace yage /** * */ -extern void glfwErrorCallback(int, const char *); +extern void glfwErrorCallback(int, char const *); /** * Initializes YAGE. diff --git a/yage/core/exception.cpp b/yage/core/exception.cpp index 6001213c..82bbe6c3 100644 --- a/yage/core/exception.cpp +++ b/yage/core/exception.cpp @@ -22,7 +22,7 @@ FileLoadException::FileLoadException(std::string err) err_msg = msg.str(); } -const char *FileLoadException::what() const throw() +char const *FileLoadException::what() const throw() { return err_msg.c_str(); } diff --git a/yage/core/exception.h b/yage/core/exception.h index 70042e02..0c53aba3 100644 --- a/yage/core/exception.h +++ b/yage/core/exception.h @@ -20,7 +20,7 @@ class FileLoadException : public std::runtime_error public: FileLoadException(std::string err); - virtual const char *what() const throw(); + virtual char const *what() const throw(); private: std::string err_msg; diff --git a/yage/core/imageloader.cpp b/yage/core/imageloader.cpp index d3c978c1..fd458717 100644 --- a/yage/core/imageloader.cpp +++ b/yage/core/imageloader.cpp @@ -19,7 +19,7 @@ namespace yage { -Texture ImageLoader::loadPng(const std::string &file_path) +Texture ImageLoader::loadPng(std::string const &file_path) { int width, height, num_channels; yLogDebug << "Loading image from disk: " << file_path; diff --git a/yage/core/imageloader.h b/yage/core/imageloader.h index f4c0d90d..0c78ae93 100644 --- a/yage/core/imageloader.h +++ b/yage/core/imageloader.h @@ -19,7 +19,7 @@ class Texture; class ImageLoader { public: - static Texture loadPng(const std::string &file_path); + static Texture loadPng(std::string const &file_path); }; } // namespace yage diff --git a/yage/core/iomanager.cpp b/yage/core/iomanager.cpp index ff920f97..1b2aba02 100644 --- a/yage/core/iomanager.cpp +++ b/yage/core/iomanager.cpp @@ -19,7 +19,7 @@ namespace yage namespace IoManager { -bool readFileToBuffer(const std::string &file_path, +bool readFileToBuffer(std::string const &file_path, std::vector &buffer) { std::ifstream file(file_path, std::ios::binary); diff --git a/yage/core/iomanager.h b/yage/core/iomanager.h index 9d796914..69436364 100644 --- a/yage/core/iomanager.h +++ b/yage/core/iomanager.h @@ -18,7 +18,7 @@ namespace yage namespace IoManager { -extern bool readFileToBuffer(const std::string &file_path, +extern bool readFileToBuffer(std::string const &file_path, std::vector &buffer); } diff --git a/yage/core/logger.cpp b/yage/core/logger.cpp index f420502d..261d8bca 100644 --- a/yage/core/logger.cpp +++ b/yage/core/logger.cpp @@ -24,7 +24,7 @@ namespace yage { LogMessage::LogMessage(Logger *owner, LogLevel level, - const std::string &file_name, int line_num) + std::string const &file_name, int line_num) : owner_(owner), meta_({level, file_name, line_num}) { } @@ -44,11 +44,11 @@ LogMessage &LogMessage::operator<<(std::ostream &(*fn)(std::ostream &os)) return *this; } -LogSink::LogSink(const LogSink &sink) : wrapper_(sink.wrapper_->clone()) {} +LogSink::LogSink(LogSink const &sink) : wrapper_(sink.wrapper_->clone()) {} LogSink::LogSink(LogSink &&sink) : wrapper_(std::move(sink.wrapper_)) {} -LogSink &LogSink::operator=(const LogSink &sink) +LogSink &LogSink::operator=(LogSink const &sink) { wrapper_.reset(sink.wrapper_->clone()); return *this; @@ -60,19 +60,19 @@ LogSink &LogSink::operator=(LogSink &&sink) return *this; } -bool LogSink::operator==(const LogSink &sink) +bool LogSink::operator==(LogSink const &sink) { return (wrapper_.get() == sink.wrapper_.get()); } -void LogSink::write(const LogMessage::Meta &meta, const std::string &msg) const +void LogSink::write(LogMessage::Meta const &meta, std::string const &msg) const { wrapper_->write(meta, msg); } LogSink makeConsoleSink() { - return [](const LogMessage::Meta &meta, const std::string &msg) { + return [](LogMessage::Meta const &meta, std::string const &msg) { std::cout << msg << "\n"; }; } @@ -91,7 +91,7 @@ public: } } - FileSink(const std::string filename) + FileSink(std::string const filename) : fileHandle_(std::make_shared(filename)) { if (!fileHandle_->good()) { @@ -101,7 +101,7 @@ public: ~FileSink() = default; - void operator()(const LogMessage::Meta &meta, const std::string &msg) const + void operator()(const LogMessage::Meta &meta, std::string const &msg) const { using namespace std::chrono; @@ -148,7 +148,7 @@ private: } // namespace -LogSink makeFileSink(const std::string &filename) +LogSink makeFileSink(std::string const &filename) { return FileSink(filename); } @@ -163,7 +163,7 @@ Logger::Logger() : active_(Active::create()), min_level_(LogLevel::INFO) add(makeConsoleSink()); } -Logger::Logger(const std::string &file_path) +Logger::Logger(std::string const &file_path) : active_(Active::create()), min_level_(LogLevel::INFO) { add(makeConsoleSink()); @@ -176,14 +176,14 @@ Logger::Logger(LogLevel min_level) add(makeConsoleSink()); } -Logger::Logger(LogLevel min_level, const std::string &file_path) +Logger::Logger(LogLevel min_level, std::string const &file_path) : active_(Active::create()), min_level_(min_level) { add(makeConsoleSink()); add(makeFileSink(file_path)); } -LogMessage Logger::operator()(LogLevel level, const std::string &fileName, +LogMessage Logger::operator()(LogLevel level, std::string const &fileName, int lineNum) { return LogMessage(this, level, fileName, lineNum); diff --git a/yage/core/logger.h b/yage/core/logger.h index 3da959c6..81b0f556 100644 --- a/yage/core/logger.h +++ b/yage/core/logger.h @@ -64,13 +64,13 @@ class LogMessage public: ~LogMessage(); - LogMessage(const LogMessage &msg) = delete; + LogMessage(LogMessage const &msg) = delete; - LogMessage &operator=(const LogMessage &msg) = delete; + LogMessage &operator=(LogMessage const &msg) = delete; LogMessage &operator=(LogMessage &&msg) = delete; template - LogMessage &operator<<(const T &value); + LogMessage &operator<<(T const &value); LogMessage &operator<<(std::ostream &(*fn)(std::ostream &os)); @@ -87,7 +87,7 @@ private: Logger *owner_; LogMessage::Meta meta_; - LogMessage(Logger *owner, LogLevel level, const std::string &file_name, + LogMessage(Logger *owner, LogLevel level, std::string const &file_name, int line_num); LogMessage(LogMessage &&msg); }; @@ -98,22 +98,22 @@ public: template LogSink(T impl); - LogSink(const LogSink &sink); + LogSink(LogSink const &sink); LogSink(LogSink &&sink); - LogSink &operator=(const LogSink &sink); + LogSink &operator=(LogSink const &sink); LogSink &operator=(LogSink &&sink); - bool operator==(const LogSink &sink); + bool operator==(LogSink const &sink); - void write(const LogMessage::Meta &meta, const std::string &msg) const; + void write(LogMessage::Meta const &meta, std::string const &msg) const; private: struct Concept { virtual ~Concept() = default; virtual Concept *clone() const = 0; - virtual void write(const LogMessage::Meta &meta, - const std::string &msg) const = 0; + virtual void write(LogMessage::Meta const &meta, + std::string const &msg) const = 0; }; template @@ -121,7 +121,7 @@ private: Model(T impl_i); virtual Concept *clone() const override; virtual void write(const LogMessage::Meta &meta, - const std::string &msg) const override; + std::string const &msg) const override; T impl; }; @@ -133,16 +133,16 @@ class Logger { public: Logger(); - explicit Logger(const std::string &file_path); + explicit Logger(std::string const &file_path); explicit Logger(LogLevel min_level); - Logger(LogLevel min_level, const std::string &file_path); + Logger(LogLevel min_level, std::string const &file_path); LogMessage operator()(LogLevel level = LogLevel::INFO, - const std::string &fileName = "", int lineNum = -1); + std::string const &fileName = "", int lineNum = -1); - void flush(const LogMessage *msg); - void add(const LogSink &sink); - void remove(const LogSink &sink); + void flush(LogMessage const *msg); + void add(LogSink const &sink); + void remove(LogSink const &sink); void clear(); static Logger &instance(); @@ -158,7 +158,7 @@ private: LogSink makeConsoleSink(); -LogSink makeFileSink(const std::string &filename); +LogSink makeFileSink(std::string const &filename); LogSink makeFileSink(std::string &&filename); /* ----------------------------------------------------------------------------- @@ -183,14 +183,14 @@ LogSink::Concept *LogSink::Model::clone() const } template -void LogSink::Model::write(const LogMessage::Meta &meta, - const std::string &msg) const +void LogSink::Model::write(LogMessage::Meta const &meta, + std::string const &msg) const { impl(meta, msg); } template -LogMessage &LogMessage::operator<<(const T &value) +LogMessage &LogMessage::operator<<(T const &value) { buffer_ << value; return *this; diff --git a/yage/core/resourcemanager.cpp b/yage/core/resourcemanager.cpp index 984b381c..f41a658c 100644 --- a/yage/core/resourcemanager.cpp +++ b/yage/core/resourcemanager.cpp @@ -15,7 +15,7 @@ namespace yage TextureCache ResourceManager::texture_cache_; -Texture ResourceManager::getTexture(const std::string &texture_path, int x, +Texture ResourceManager::getTexture(std::string const &texture_path, int x, int y) { return texture_cache_.getTexture(texture_path, x, y); diff --git a/yage/core/resourcemanager.h b/yage/core/resourcemanager.h index dcc77ab7..0e57f227 100644 --- a/yage/core/resourcemanager.h +++ b/yage/core/resourcemanager.h @@ -24,7 +24,7 @@ private: static TextureCache texture_cache_; public: - static Texture getTexture(const std::string &texture_path, int x = 1, + static Texture getTexture(std::string const &texture_path, int x = 1, int y = 1); }; diff --git a/yage/core/texturecache.cpp b/yage/core/texturecache.cpp index 5f78a37e..a9ff169c 100644 --- a/yage/core/texturecache.cpp +++ b/yage/core/texturecache.cpp @@ -14,7 +14,7 @@ namespace yage { -Texture TextureCache::getTexture(const std::string &texture_path, int x, int y) +Texture TextureCache::getTexture(std::string const &texture_path, int x, int y) { auto itr = texture_map_.find(texture_path); diff --git a/yage/core/texturecache.h b/yage/core/texturecache.h index 45cc0a9d..1a0b3e0d 100644 --- a/yage/core/texturecache.h +++ b/yage/core/texturecache.h @@ -24,7 +24,7 @@ private: public: TextureCache() = default; - Texture getTexture(const std::string &texture_path, int x = 1, int y = 1); + Texture getTexture(std::string const &texture_path, int x = 1, int y = 1); }; } // namespace yage diff --git a/yage/core/window.h b/yage/core/window.h index 6b0e7fed..f633f3f7 100644 --- a/yage/core/window.h +++ b/yage/core/window.h @@ -35,12 +35,12 @@ private: public: Window() = default; - Window(const Window &) = delete; + Window(Window const &) = delete; Window(Window &&) = delete; /// destroys the window handle ~Window(); - Window &operator=(const Window &) = delete; + Window &operator=(Window const &) = delete; Window &operator=(Window &&) = delete; /// create the window, initialize the handle and update the width and height diff --git a/yage/render/shader.cpp b/yage/render/shader.cpp index e35c5099..b3aca539 100644 --- a/yage/render/shader.cpp +++ b/yage/render/shader.cpp @@ -20,7 +20,7 @@ using std::runtime_error; namespace yage { -Shader::Shader(const std::string &vertex_path, const std::string &fragment_path) +Shader::Shader(std::string const &vertex_path, std::string const &fragment_path) { std::string vertex_source, fragment_source; std::ifstream vertex_file, fragment_file; @@ -85,22 +85,22 @@ void Shader::use() const glUseProgram(program_id_); } -void Shader::setUniform(const std::string &name, int value) const +void Shader::setUniform(std::string const &name, int value) const { glUniform1i(getUniformLocation(name), static_cast(value)); } -void Shader::setUniform(const std::string &name, float value) const +void Shader::setUniform(std::string const &name, float value) const { glUniform1f(getUniformLocation(name), static_cast(value)); } -void Shader::setUniform(const std::string &name, const glm::mat4 &matrix) const +void Shader::setUniform(std::string const &name, const glm::mat4 &matrix) const { glUniformMatrix4fv(getUniformLocation(name), 1, GL_FALSE, &(matrix[0][0])); } -GLint Shader::getUniformLocation(const std::string &uniform_name) const +GLint Shader::getUniformLocation(std::string const &uniform_name) const { GLuint location = glGetUniformLocation(program_id_, uniform_name.c_str()); if (location == GL_INVALID_INDEX) { @@ -109,7 +109,7 @@ GLint Shader::getUniformLocation(const std::string &uniform_name) const return location; } -void Shader::errorCheck(GLuint shader, const std::string &shader_type) const +void Shader::errorCheck(GLuint shader, std::string const &shader_type) const { int success; char info_log[1024]; diff --git a/yage/render/shader.h b/yage/render/shader.h index d91753cb..75a24c6d 100644 --- a/yage/render/shader.h +++ b/yage/render/shader.h @@ -20,7 +20,7 @@ namespace yage class Shader { public: - Shader(const std::string &vertex_path, const std::string &fragment_path); + Shader(std::string const &vertex_path, std::string const &fragment_path); Shader(const Shader &) = delete; Shader(Shader &&) = delete; ~Shader(); @@ -32,16 +32,16 @@ public: void use() const; /// set uniforms of different type - void setUniform(const std::string &name, int value) const; - void setUniform(const std::string &name, float value) const; - void setUniform(const std::string &name, const glm::mat4 &matrix) const; + void setUniform(std::string const &name, int value) const; + void setUniform(std::string const &name, float value) const; + void setUniform(std::string const &name, const glm::mat4 &matrix) const; private: /// compiled shader program id GLuint program_id_ = 0; - GLint getUniformLocation(const std::string &uniform_name) const; - void errorCheck(GLuint shader, const std::string &shader_type) const; + GLint getUniformLocation(std::string const &uniform_name) const; + void errorCheck(GLuint shader, std::string const &shader_type) const; }; } // namespace yage -- cgit From 083fcf24c50753b6f5fa04c5358be783be64407d Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Sat, 23 Jun 2018 15:51:19 +0100 Subject: Some more component implementations --- yage/entity/component.cpp | 16 ++++++++++++++++ yage/entity/component.h | 10 +++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/yage/entity/component.cpp b/yage/entity/component.cpp index eba2ad0a..4c0eae82 100644 --- a/yage/entity/component.cpp +++ b/yage/entity/component.cpp @@ -1,8 +1,24 @@ #include "component.h" +#include + namespace yage { GroupId BaseComponent::group_id_counter_ = 0; +ComponentGroup &ComponentGroup::add(std::unique_ptr &&component) +{ + components_.push_back(std::move(component)); + return *this; +} + +ComponentGroup::Container::iterator ComponentGroup::begin() { + return components_.begin(); +} + +ComponentGroup::Container::iterator ComponentGroup::end() { + return components_.end(); +} + } // namespace yage diff --git a/yage/entity/component.h b/yage/entity/component.h index a21409ff..3f2b3a81 100644 --- a/yage/entity/component.h +++ b/yage/entity/component.h @@ -28,13 +28,21 @@ private: template class Component : public BaseComponent { +private: GroupId getGroup() override; }; class ComponentGroup { public: - std::vector> components_; + typedef std::vector> Container; + + ComponentGroup &add(std::unique_ptr &&component); + Container::iterator begin(); + Container::iterator end(); + +private: + Container components_; }; template -- cgit From 801299f3175fccb940550cba48903decd7882822 Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Sat, 23 Jun 2018 15:51:37 +0100 Subject: Entity implementation with added 'each' function --- yage/entity/entity.cpp | 11 +++++++++-- yage/entity/entity.h | 29 +++++++++++++++++++++++++---- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/yage/entity/entity.cpp b/yage/entity/entity.cpp index 25e8e2d0..c20d15d6 100644 --- a/yage/entity/entity.cpp +++ b/yage/entity/entity.cpp @@ -3,6 +3,8 @@ #include "component.h" #include +#include +#include namespace yage { @@ -29,12 +31,17 @@ bool EntityManager::is_valid(Entity entity) const return false; } -EntityManager &EntityManager::add_component(Entity entity, - BaseComponent *component) +EntityManager & +EntityManager::add_component(Entity entity, + std::unique_ptr &&component) { auto id = component->getGroup(); component_masks_[entity] = component_masks_[entity] | ComponentMask(1 << id); + if (id+1 > component_group_.size()) { + component_group_.resize(id+1); + } + component_group_[id].add(std::move(component)); return *this; } diff --git a/yage/entity/entity.h b/yage/entity/entity.h index 2aa31a66..93bea8fb 100644 --- a/yage/entity/entity.h +++ b/yage/entity/entity.h @@ -1,9 +1,12 @@ #pragma once -#include - #include "component.h" +#include +#include +#include +#include + namespace yage { @@ -24,17 +27,35 @@ public: Entity create_entity(); EntityManager &delete_entity(Entity entity); bool is_valid(Entity entity) const; - EntityManager &add_component(Entity entity, BaseComponent *component); + EntityManager &add_component(Entity entity, + std::unique_ptr &&component); + template + EntityManager &each(std::function update); private: Entity update_next_entity(); Entity next_entity_ = 0; -public: std::vector component_group_; std::vector component_masks_; std::vector deleted_; }; +template +EntityManager &EntityManager::each(std::function update) +{ + T c; + auto id = static_cast(&c)->getGroup(); + for (auto it = component_group_[id].begin(); + it != component_group_[id].end(); ++it) { + auto iteration = it - component_group_[id].begin(); + if (is_valid(iteration) && component_masks_[iteration][id]) { + update(*static_cast((*it).get())); + } + } + + return *this; +} + } // namespace yage -- cgit From 8dfc67bd3d2297410a39bf907d9a59589c6c7d9d Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Sat, 23 Jun 2018 15:51:52 +0100 Subject: Updating readme --- yage/entity/README.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/yage/entity/README.md b/yage/entity/README.md index 6a7704c6..18ac16cf 100644 --- a/yage/entity/README.md +++ b/yage/entity/README.md @@ -1,9 +1,10 @@ # Entity Component System (ECS) -This ECS is heavily inspired from the [Entityx component system](__fix -link__). It is a much simpler and less efficient implementation, as it does not -support dedicated pools for the different components, and only stores them on -the heap and are directed to by pointers. +This ECS is heavily inspired from the [Entityx component +system](https://github.com/alecthomas/entityx). It is a much simpler and less +efficient implementation, as it does not support dedicated pools for the +different components, and only stores them on the heap and are directed to by +pointers. A future improvement would be to store the components in a contiguous area in memory, so that the iteration through them can be more efficient. -- cgit From 93c8bfea8b5bcfc0d6513d93cec0eafe82dc465a Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Sat, 23 Jun 2018 15:51:59 +0100 Subject: Updating example to test out the new entity system --- tests/entity_test.cpp | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/tests/entity_test.cpp b/tests/entity_test.cpp index 0e40d3ea..578f1671 100644 --- a/tests/entity_test.cpp +++ b/tests/entity_test.cpp @@ -11,6 +11,7 @@ struct Position : public Component { double x; double y; + Position() = default; Position(double x_, double y_) : x(x_), y(y_) {} }; @@ -18,7 +19,8 @@ struct Size : public Component { double width; double height; - Size(double w, double h) : width(w), height(h) {} + Size() = default; + Size(double w_, double h_) : width(w_), height(h_) {} }; class MovementSystem : public System @@ -26,27 +28,36 @@ class MovementSystem : public System public: void update(double dt, EntityManager &em) override { - for (auto &&x : em.component_masks_) { - if(x[1] == 1) { - std::cout << "Found size: "; - } - } + std::cout << "New Iteration\n"; + std::cout << " Position\n"; + em.each([](Position &pos) { + std::cout << " X: " << pos.x << ", Y: " << pos.y << "\n"; + ++pos.x, ++pos.y; + }); + std::cout << " Size\n"; + em.each([](Size &size) { + std::cout << " WIDTH: " << size.width + << ", HEIGHT: " << size.height << "\n"; + size.width += 7; + size.height += 10; + }); } }; int main() { EntityManager em; - Position p1(1, 2); - Position p2(2, 1); - Size s1(5, 5); Entity e1 = em.create_entity(); Entity e2 = em.create_entity(); - std::cout << "e1: " << e1 << ", e2: " << e2 << "\n"; - MovementSystem s; - em.add_component(e1, &p1).add_component(e2, &p2).add_component(e2, &s1); + std::cout << "e1: " << e1 << "\n"; + em.add_component(e1, std::make_unique(10, 5)) + .add_component(e1, std::make_unique(1, 5)) + .add_component(e2, std::make_unique(5, 2)); + s.update(60, em); + s.update(60, em); + em.delete_entity(e2); s.update(60, em); } -- cgit