aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYann Herklotz <ymherklotz@gmail.com>2018-06-22 00:14:10 +0100
committerGitHub <noreply@github.com>2018-06-22 00:14:10 +0100
commitb1673de1b46bd2e566b7c66197ad989d0323f061 (patch)
tree2cbfdab552025af7a8772069b9f9935f9c9f387f
parent39ef9c1cbe660a8369a33eb0ba13cc15be4cfa15 (diff)
parent3702e753a5f7b31c31261c968757e19e808a84ec (diff)
downloadYAGE-b1673de1b46bd2e566b7c66197ad989d0323f061.tar.gz
YAGE-b1673de1b46bd2e566b7c66197ad989d0323f061.zip
Merge pull request #19 from ymherklotz/develop
Develop
-rw-r--r--.dir-locals.el2
-rw-r--r--CMakeLists.txt2
-rw-r--r--tests/entity_test.cpp52
-rw-r--r--yage/core/imageloader.cpp5
-rw-r--r--yage/core/window.cpp3
-rw-r--r--yage/core/window.h2
-rw-r--r--yage/entity/component.cpp8
-rw-r--r--yage/entity/component.h47
-rw-r--r--yage/entity/engine.cpp61
-rw-r--r--yage/entity/engine.h58
-rw-r--r--yage/entity/entity.cpp53
-rw-r--r--yage/entity/entity.h125
-rw-r--r--yage/entity/space.cpp29
-rw-r--r--yage/entity/space.h76
-rw-r--r--yage/entity/system.h25
-rw-r--r--yage/math/matrix.h4
-rw-r--r--yage/physics/particlebody.cpp1
-rw-r--r--yage/render/spritebatch.cpp3
-rw-r--r--yage/yage.h6
19 files changed, 172 insertions, 390 deletions
diff --git a/.dir-locals.el b/.dir-locals.el
index 40e6adb0..3ead1dc4 100644
--- a/.dir-locals.el
+++ b/.dir-locals.el
@@ -1,3 +1,3 @@
-((nil . ((projectile-project-compilation-cmd . "mkdir -p build && cd build && cmake -GNinja -DCMAKE_EXPORT_COMPILE_COMMANDS=ON .. && ninja -j4")
+((nil . ((projectile-project-compilation-cmd . "cd build && ninja -j4")
(projectile-project-test-cmd . "cd build/tests && ctest -j4 --schedule-random")
(projectile-project-run-cmd . "./build/bin/simplegame"))))
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 5e55c01d..55539b04 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -10,7 +10,7 @@ option(YAGE_BUILD_DOCS "Build documentation using Doxygen" OFF)
option(YAGE_BUILD_EXAMPLES "Build example executables" OFF)
# set standard
-set(CMAKE_CXX_STANDARD 14)
+set(CMAKE_CXX_STANDARD 17)
# find other libraries from source
set(EXTERNAL_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libs)
diff --git a/tests/entity_test.cpp b/tests/entity_test.cpp
new file mode 100644
index 00000000..0e40d3ea
--- /dev/null
+++ b/tests/entity_test.cpp
@@ -0,0 +1,52 @@
+#include <yage/entity/component.h>
+#include <yage/entity/entity.h>
+#include <yage/entity/system.h>
+
+using namespace yage;
+
+#include <iostream>
+#include <vector>
+
+struct Position : public Component<Position> {
+ double x;
+ double y;
+
+ Position(double x_, double y_) : x(x_), y(y_) {}
+};
+
+struct Size : public Component<Size> {
+ double width;
+ double height;
+
+ Size(double w, double h) : width(w), height(h) {}
+};
+
+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: ";
+ }
+ }
+ }
+};
+
+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);
+
+ s.update(60, em);
+}
diff --git a/yage/core/imageloader.cpp b/yage/core/imageloader.cpp
index 4905bf5e..d3c978c1 100644
--- a/yage/core/imageloader.cpp
+++ b/yage/core/imageloader.cpp
@@ -11,15 +11,11 @@
#include "../data/texture.h"
#include "logger.h"
#include "stb_image.h"
-#include <iostream>
#include <glad/glad.h>
-#include <iostream>
#include <stdexcept>
-using std::cout;
-
namespace yage
{
@@ -32,7 +28,6 @@ Texture ImageLoader::loadPng(const std::string &file_path)
yLogDebug << "Sucessfully loaded file";
Texture texture(0, static_cast<int>(width), static_cast<int>(height));
yLogDebug << "Creating texture";
- cout << "Hello";
glGenTextures(1, &texture.id);
glBindTexture(GL_TEXTURE_2D, texture.id);
diff --git a/yage/core/window.cpp b/yage/core/window.cpp
index 25f9f834..392d4363 100644
--- a/yage/core/window.cpp
+++ b/yage/core/window.cpp
@@ -61,6 +61,7 @@ void Window::create(std::string window_name, int width, int height)
// set key callback
glfwSetKeyCallback(window_, key_callback);
+
// set resize callback
glfwSetFramebufferSizeCallback(window_, framebuffer_size_callback);
@@ -108,7 +109,7 @@ void Window::pollEvents() const
glfwPollEvents();
}
-bool Window::keyPressed(key k)
+bool Window::keyPressed(key k) const
{
if (window_ == nullptr) {
throw runtime_error("Window is not initialized");
diff --git a/yage/core/window.h b/yage/core/window.h
index 2ffafba8..6b0e7fed 100644
--- a/yage/core/window.h
+++ b/yage/core/window.h
@@ -56,7 +56,7 @@ public:
bool shouldClose();
void pollEvents() const;
- bool keyPressed(key k);
+ bool keyPressed(key k) const;
};
} // namespace yage
diff --git a/yage/entity/component.cpp b/yage/entity/component.cpp
new file mode 100644
index 00000000..eba2ad0a
--- /dev/null
+++ b/yage/entity/component.cpp
@@ -0,0 +1,8 @@
+#include "component.h"
+
+namespace yage
+{
+
+GroupId BaseComponent::group_id_counter_ = 0;
+
+} // namespace yage
diff --git a/yage/entity/component.h b/yage/entity/component.h
new file mode 100644
index 00000000..a21409ff
--- /dev/null
+++ b/yage/entity/component.h
@@ -0,0 +1,47 @@
+#pragma once
+
+#include <bitset>
+#include <memory>
+#include <vector>
+
+namespace yage
+{
+
+/**
+ * The component mask represents all the components that the entity is
+ * currently attached to.
+ */
+typedef std::bitset<64> ComponentMask;
+
+typedef unsigned int GroupId;
+
+class BaseComponent
+{
+protected:
+ virtual GroupId getGroup() = 0;
+ static GroupId group_id_counter_;
+
+private:
+ friend class EntityManager;
+};
+
+template <typename T>
+class Component : public BaseComponent
+{
+ GroupId getGroup() override;
+};
+
+class ComponentGroup
+{
+public:
+ std::vector<std::unique_ptr<BaseComponent>> components_;
+};
+
+template <typename T>
+GroupId Component<T>::getGroup()
+{
+ static GroupId group_id = group_id_counter_++;
+ return group_id;
+}
+
+} // namespace yage
diff --git a/yage/entity/engine.cpp b/yage/entity/engine.cpp
deleted file mode 100644
index 1cef4504..00000000
--- a/yage/entity/engine.cpp
+++ /dev/null
@@ -1,61 +0,0 @@
-/** ---------------------------------------------------------------------------
- * -*- c++ -*-
- * @file: engine.cpp
- *
- * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com>
- * MIT License, see LICENSE file for more details.
- * ----------------------------------------------------------------------------
- */
-
-#include "engine.h"
-
-#include "../core/core.h"
-
-#include "space.h"
-
-namespace yage
-{
-
-Engine::~Engine()
-{
- quit();
-}
-
-void Engine::init()
-{
- yage::init();
- window_.create("Game Engine", 800, 640);
-}
-
-void Engine::mainLoop()
-{
- while (!window_.shouldClose()) {
- window_.pollEvents();
- window_.clearBuffer();
-
- update();
-
- window_.swapBuffer();
- }
-}
-
-void Engine::update()
-{
- for (auto &space : spaces_) {
- space->update();
- }
-}
-
-void Engine::addSpace(std::unique_ptr<Space> space)
-{
- spaces_.push_back(std::move(space));
-}
-
-Engine &Engine::instance()
-{
- static Engine engine_instance;
-
- return engine_instance;
-}
-
-} // namespace yage
diff --git a/yage/entity/engine.h b/yage/entity/engine.h
deleted file mode 100644
index 9a1f6d2b..00000000
--- a/yage/entity/engine.h
+++ /dev/null
@@ -1,58 +0,0 @@
-/** ---------------------------------------------------------------------------
- * -*- c++ -*-
- * @file: engine.h
- *
- * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com>
- * MIT License, see LICENSE file for more details.
- * ----------------------------------------------------------------------------
- */
-
-#pragma once
-
-#include "system.h"
-
-#include "../core/window.h"
-#include "../util/noncopyable.h"
-
-#include <memory>
-#include <vector>
-
-namespace yage
-{
-
-class Space;
-
-/**
- * Main engine class that contains a systems, the main loop and the update
- * function that updates all the systems.
- */
-class Engine : public NonCopyable
-{
-public:
- ~Engine();
-
- /// Initialize window and other aspects of the engine.
- void init();
-
- /// Main game loop of the engine.
- void mainLoop();
-
- /// Updates the systems.
- void update();
-
- /// Add spaces to the engine
- void addSpace(std::unique_ptr<Space> space);
-
- /// Returns the instance of the engine, as there is only one instance of the
- /// engine.
- static Engine &instance();
-
-private:
- /// Window
- Window window_;
-
- /// A vector of all the spaces
- std::vector<std::unique_ptr<Space>> spaces_;
-};
-
-} // namespace yage
diff --git a/yage/entity/entity.cpp b/yage/entity/entity.cpp
index 1a2b16ad..25e8e2d0 100644
--- a/yage/entity/entity.cpp
+++ b/yage/entity/entity.cpp
@@ -1,42 +1,51 @@
-/** ---------------------------------------------------------------------------
- * -*- c++ -*-
- * @file: entity.cpp
- *
- * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com>
- * MIT License, see LICENSE file for more details.
- * ----------------------------------------------------------------------------
- */
-
#include "entity.h"
+#include "component.h"
+
#include <algorithm>
namespace yage
{
-BaseComponent::Group BaseComponent::group_id_counter_ = 0;
+Entity EntityManager::create_entity()
+{
+ Entity entity = update_next_entity();
+ component_masks_.push_back(ComponentMask(0));
+ return entity;
+}
-EntityManager::EntityManager(Space *space) : space_(space) {}
+EntityManager &EntityManager::delete_entity(Entity entity)
+{
+ deleted_.push_back(entity);
+ return *this;
+}
-EntityManager::EntityManager(Space *space, std::size_t n) : space_(space)
+bool EntityManager::is_valid(Entity entity) const
{
- entities_.reserve(n);
+ auto it = std::find(deleted_.begin(), deleted_.end(), entity);
+ if (it == deleted_.end()) {
+ return true;
+ }
+ return false;
}
-Entity EntityManager::createEntity()
+EntityManager &EntityManager::add_component(Entity entity,
+ BaseComponent *component)
{
- Entity entity = next_entity_++;
- entities_.push_back(entity);
- return entity;
+ auto id = component->getGroup();
+ component_masks_[entity] =
+ component_masks_[entity] | ComponentMask(1 << id);
+ return *this;
}
-void EntityManager::deleteEntity(Entity entity)
+Entity EntityManager::update_next_entity()
{
- auto index = std::find_if(entities_.begin(), entities_.end(),
- [&](Entity &value) { return value == entity; });
- if (index != entities_.end()) {
- entities_.erase(index);
+ if (deleted_.empty()) {
+ return next_entity_++;
}
+ Entity ent = deleted_.back();
+ deleted_.pop_back();
+ return ent;
}
} // namespace yage
diff --git a/yage/entity/entity.h b/yage/entity/entity.h
index a032ad8a..2aa31a66 100644
--- a/yage/entity/entity.h
+++ b/yage/entity/entity.h
@@ -1,127 +1,40 @@
-/** ---------------------------------------------------------------------------
- * -*- c++ -*-
- * @file: entity.h
- *
- * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com>
- * MIT License, see LICENSE file for more details.
- * ----------------------------------------------------------------------------
- */
-
#pragma once
-#include <memory>
#include <vector>
+#include "component.h"
+
namespace yage
{
-class Space;
-class ComponentGroup;
-
+/**
+ * The entity is currently just an unsigned integer, which may change to a
+ * class in the future.
+ */
typedef unsigned int Entity;
-/** Manages entities in a space.
+/**
+ * Has to keep track of all the different entities and their current state.
+ *
+ * The key actions on an Entity are: deleting, creating.
*/
class EntityManager
{
public:
- /** Default instance of an EntityManager.
- */
- EntityManager() = default;
-
- /** Creates an instance of the entity manager, which refers back to the
- * space it was created in and belongs to.
- *
- * @param space Current space that the EntityManager belongs to.
- */
- EntityManager(Space *space);
-
- /** Creates an instance of the entitiy manager with an initial size.
- *
- * @param space Current space that the EntityManager belongs to. @param n
- * Initial size of the EntityManager.
- */
- EntityManager(Space *space, std::size_t n);
-
- /** Creates an Entity and returns the handle to the entity, which can then
- * be used by the user to do operations on it.
- *
- * @return The handle to the entity that was created in the space.
- */
- Entity createEntity();
-
- /** Delete an entity.
- */
- void deleteEntity(Entity entity);
+ Entity create_entity();
+ EntityManager &delete_entity(Entity entity);
+ bool is_valid(Entity entity) const;
+ EntityManager &add_component(Entity entity, BaseComponent *component);
private:
- /** The next available handle to give to the user.
- */
- Entity next_entity_ = 0;
-
- /** The space that the entity manager belongs to.
- */
- Space *space_ = nullptr;
-
- /** The entities in the current space.
- */
- std::vector<Entity> entities_;
+ Entity update_next_entity();
- /** Component list of different component groups, that then contain the
- * different components for the entities.
- */
- std::vector<std::unique_ptr<ComponentGroup>> components;
-};
+ Entity next_entity_ = 0;
-/** Base component used to store components.
- *
- * Should not be inherited from when declaring a new component. Instead, the
- * Component<Derived> should be used.
- */
-class BaseComponent
-{
public:
- /** Group used to register a specific component internally with the entity
- * manager.
- */
- typedef unsigned Group;
-
-protected:
- static Group group_id_counter_;
-};
-
-/** The main component that is used to make a component from a defined struct.
- *
- * A component should only be declared as a struct and should not contain any
- * data itself.
- */
-template <typename Derived>
-class Component : public BaseComponent
-{
-private:
- friend class EntityManager;
-
- BaseComponent::Group group();
+ std::vector<ComponentGroup> component_group_;
+ std::vector<ComponentMask> component_masks_;
+ std::vector<Entity> deleted_;
};
-/** Contains a list of all components that belong to a sepecific group, these
- * are then stored in the main entity manager.
- */
-class ComponentGroup
-{
-private:
- friend class EntityManager;
-
- std::vector<BaseComponent *> components_;
-};
-
-// Template definitions
-
-template <typename Derived>
-BaseComponent::Group Component<Derived>::group()
-{
- static Group group_id = group_id_counter_++;
- return group_id;
-}
-
} // namespace yage
diff --git a/yage/entity/space.cpp b/yage/entity/space.cpp
deleted file mode 100644
index 530f5b49..00000000
--- a/yage/entity/space.cpp
+++ /dev/null
@@ -1,29 +0,0 @@
-/** ---------------------------------------------------------------------------
- * -*- c++ -*-
- * @file: space.cpp
- *
- * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com>
- * MIT License, see LICENSE file for more details.
- * ----------------------------------------------------------------------------
- */
-
-#include "space.h"
-
-namespace yage
-{
-
-Space::Space() : em_(this) {}
-
-unsigned Space::createEntity()
-{
- return em_.createEntity();
-}
-
-void Space::update()
-{
- for (auto &system : systems_) {
- system->update();
- }
-}
-
-} // namespace yage
diff --git a/yage/entity/space.h b/yage/entity/space.h
deleted file mode 100644
index 0763b189..00000000
--- a/yage/entity/space.h
+++ /dev/null
@@ -1,76 +0,0 @@
-/** ---------------------------------------------------------------------------
- * -*- c++ -*-
- * @file: space.h
- *
- * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com>
- * MIT License, see LICENSE file for more details.
- * ----------------------------------------------------------------------------
- */
-
-#pragma once
-
-#include <memory>
-#include <vector>
-
-#include "entity.h"
-#include "system.h"
-
-struct Movement {
-};
-
-namespace yage
-{
-
-class System;
-
-/**
- * Space that keeps track of all the entities, componenets and runs
- * the systems on the data to update them. There can be multiple
- * instances of a space, which can be used, for example, for different
- * levels in the game that can be loaded separately, or a game menu
- * that can be loaded above the other spaces when the user presses on
- * pause.
- */
-class Space
-{
-public:
- /**
- * Default instance for a space.
- */
- Space();
-
- /**
- * Create an entity that will belong to this space, and return the
- * handle to the user. The Entity class itself should not be
- * visible to the user, as the user only needs to worry about the
- * handle when referring to the Entity and changing it.
- */
- Entity createEntity();
-
- /**
- * Update all the systems.
- */
- void update();
-
-private:
- /**
- * The systems of the Space that act on the data and on their
- * respective component. These are specific to the Space, as other
- * spaces might have different Systems and not act on the same
- * entities.
- */
- std::vector<std::unique_ptr<System>> systems_;
-
- /**
- * Manages all the entities in the system, can create them for the
- * current space.
- */
- EntityManager em_;
-
- /**
- * Manages all the components
- */
- // ComponentManager cm_;
-};
-
-} // namespace yage
diff --git a/yage/entity/system.h b/yage/entity/system.h
index 84536cc9..4f8306a8 100644
--- a/yage/entity/system.h
+++ b/yage/entity/system.h
@@ -1,26 +1,21 @@
-/** ---------------------------------------------------------------------------
- * -*- c++ -*-
- * @file: system.h
- *
- * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com>
- * MIT License, see LICENSE file for more details.
- * ----------------------------------------------------------------------------
- */
-
#pragma once
-#include <cstddef>
-
-#include "../util/noncopyable.h"
+#include "entity.h"
namespace yage
{
-class System : public NonCopyable
+class BaseSystem
{
public:
- virtual void init() = 0;
- virtual void update() = 0;
+ virtual void update(double dt, EntityManager &em) = 0;
+};
+
+template <typename T>
+class System : public BaseSystem
+{
+protected:
+ ComponentMask active_mask_;
};
} // namespace yage
diff --git a/yage/math/matrix.h b/yage/math/matrix.h
index a41c522f..c1a93413 100644
--- a/yage/math/matrix.h
+++ b/yage/math/matrix.h
@@ -7,14 +7,10 @@
* ----------------------------------------------------------------------------
*/
-/** @file
- */
-
#pragma once
#include <algorithm>
#include <exception>
-#include <iostream>
#include <sstream>
#include <string>
#include <vector>
diff --git a/yage/physics/particlebody.cpp b/yage/physics/particlebody.cpp
index 49520558..c6d8f469 100644
--- a/yage/physics/particlebody.cpp
+++ b/yage/physics/particlebody.cpp
@@ -10,7 +10,6 @@
#include <yage/physics/particlebody.h>
#include <cmath>
-#include <iostream>
namespace yage
{
diff --git a/yage/render/spritebatch.cpp b/yage/render/spritebatch.cpp
index fc0103f4..bfce102a 100644
--- a/yage/render/spritebatch.cpp
+++ b/yage/render/spritebatch.cpp
@@ -11,11 +11,8 @@
#include "../core/logger.h"
#include <algorithm>
-#include <iostream>
#include <stdexcept>
-using std::cout;
-
#include <GLFW/glfw3.h>
namespace yage
diff --git a/yage/yage.h b/yage/yage.h
index 089c2797..bff3610c 100644
--- a/yage/yage.h
+++ b/yage/yage.h
@@ -44,9 +44,3 @@
*/
#include "render/shader.h"
#include "render/spritebatch.h"
-
-/**
- * Engine that includes a Entity Component System to organize the data and make
- * it more flexible and efficient.
- */
-#include "entity/engine.h"