aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYann Herklotz <ymherklotz@gmail.com>2018-06-23 16:06:42 +0100
committerGitHub <noreply@github.com>2018-06-23 16:06:42 +0100
commit11a6d07652c98b13be6d8097e389480453a7c568 (patch)
tree3f06a7d35e8ec647041fccea90d5d763ebe6849f
parentb1673de1b46bd2e566b7c66197ad989d0323f061 (diff)
parent93c8bfea8b5bcfc0d6513d93cec0eafe82dc465a (diff)
downloadYAGE-11a6d07652c98b13be6d8097e389480453a7c568.tar.gz
YAGE-11a6d07652c98b13be6d8097e389480453a7c568.zip
Merge pull request #20 from ymherklotz/developHEADmaster
Develop
-rw-r--r--tests/entity_test.cpp35
-rw-r--r--yage/core/core.h2
-rw-r--r--yage/core/exception.cpp2
-rw-r--r--yage/core/exception.h2
-rw-r--r--yage/core/imageloader.cpp2
-rw-r--r--yage/core/imageloader.h2
-rw-r--r--yage/core/iomanager.cpp2
-rw-r--r--yage/core/iomanager.h2
-rw-r--r--yage/core/logger.cpp24
-rw-r--r--yage/core/logger.h42
-rw-r--r--yage/core/resourcemanager.cpp2
-rw-r--r--yage/core/resourcemanager.h2
-rw-r--r--yage/core/texturecache.cpp2
-rw-r--r--yage/core/texturecache.h2
-rw-r--r--yage/core/window.h4
-rw-r--r--yage/entity/README.md9
-rw-r--r--yage/entity/component.cpp16
-rw-r--r--yage/entity/component.h10
-rw-r--r--yage/entity/entity.cpp11
-rw-r--r--yage/entity/entity.h29
-rw-r--r--yage/render/shader.cpp12
-rw-r--r--yage/render/shader.h12
22 files changed, 145 insertions, 81 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<Position> {
double x;
double y;
+ Position() = default;
Position(double x_, double y_) : x(x_), y(y_) {}
};
@@ -18,7 +19,8 @@ struct Size : public Component<Size> {
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<MovementSystem>
@@ -26,27 +28,36 @@ class MovementSystem : public System<MovementSystem>
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>([](Position &pos) {
+ std::cout << " X: " << pos.x << ", Y: " << pos.y << "\n";
+ ++pos.x, ++pos.y;
+ });
+ std::cout << " Size\n";
+ em.each<Size>([](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<Size>(10, 5))
+ .add_component(e1, std::make_unique<Position>(1, 5))
+ .add_component(e2, std::make_unique<Position>(5, 2));
+ s.update(60, em);
+ s.update(60, em);
+ em.delete_entity(e2);
s.update(60, em);
}
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<unsigned char> &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<unsigned char> &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<std::ofstream>(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 <typename T>
- 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 <typename T>
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 <typename T>
@@ -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<T>::clone() const
}
template <typename T>
-void LogSink::Model<T>::write(const LogMessage::Meta &meta,
- const std::string &msg) const
+void LogSink::Model<T>::write(LogMessage::Meta const &meta,
+ std::string const &msg) const
{
impl(meta, msg);
}
template <typename T>
-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/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.
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 <memory>
+
namespace yage
{
GroupId BaseComponent::group_id_counter_ = 0;
+ComponentGroup &ComponentGroup::add(std::unique_ptr<BaseComponent> &&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 <typename T>
class Component : public BaseComponent
{
+private:
GroupId getGroup() override;
};
class ComponentGroup
{
public:
- std::vector<std::unique_ptr<BaseComponent>> components_;
+ typedef std::vector<std::unique_ptr<BaseComponent>> Container;
+
+ ComponentGroup &add(std::unique_ptr<BaseComponent> &&component);
+ Container::iterator begin();
+ Container::iterator end();
+
+private:
+ Container components_;
};
template <typename T>
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 <algorithm>
+#include <iostream>
+#include <memory>
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<BaseComponent> &&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 <vector>
-
#include "component.h"
+#include <functional>
+#include <iostream>
+#include <memory>
+#include <vector>
+
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<BaseComponent> &&component);
+ template <typename T>
+ EntityManager &each(std::function<void(T &)> update);
private:
Entity update_next_entity();
Entity next_entity_ = 0;
-public:
std::vector<ComponentGroup> component_group_;
std::vector<ComponentMask> component_masks_;
std::vector<Entity> deleted_;
};
+template <typename T>
+EntityManager &EntityManager::each(std::function<void(T &)> update)
+{
+ T c;
+ auto id = static_cast<BaseComponent *>(&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<T *>((*it).get()));
+ }
+ }
+
+ return *this;
+}
+
} // namespace yage
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<GLint>(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<GLfloat>(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