aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYann Herklotz <ymherklotz@gmail.com>2018-01-10 12:02:04 +0000
committerYann Herklotz <ymherklotz@gmail.com>2018-01-10 12:02:04 +0000
commita62fbea8d40f623ffcd60eced63f295cd55db084 (patch)
tree2374234d4364eb389df98fff0c4c180118331b27
parentcbd6bf4a695370dbfc088bbe0cd6f270e1c112a7 (diff)
downloadYAGE-a62fbea8d40f623ffcd60eced63f295cd55db084.tar.gz
YAGE-a62fbea8d40f623ffcd60eced63f295cd55db084.zip
[Engine] Adding initial components for an ECS.
-rw-r--r--CMakeLists.txt6
-rw-r--r--tests/CMakeLists.txt1
-rw-r--r--tests/engine/test.cpp16
-rw-r--r--yage/core/stb_image.cpp8
-rw-r--r--yage/core/stb_image.h8
-rw-r--r--yage/engine/component.h21
-rw-r--r--yage/engine/engine.cpp21
-rw-r--r--yage/engine/engine.h7
-rw-r--r--yage/engine/entity.cpp23
-rw-r--r--yage/engine/entity.h53
-rw-r--r--yage/engine/entitymanager.cpp34
-rw-r--r--yage/engine/entitymanager.h83
-rw-r--r--yage/engine/space.cpp21
-rw-r--r--yage/engine/space.h61
14 files changed, 338 insertions, 25 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 65b817e1..ea15bb0a 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -28,12 +28,17 @@ add_subdirectory(${EXTERNAL_DIR}/glfw)
# glad
add_subdirectory(${EXTERNAL_DIR}/glad)
+# Box2D
+set(BOX2D_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
+add_subdirectory(${EXTERNAL_DIR}/Box2D/Box2D)
+
# yage
file(GLOB YAGE_CORE_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} yage/core/*.cpp)
file(GLOB YAGE_MATH_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} yage/math/*.cpp)
file(GLOB YAGE_PHYSICS_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} yage/physics/*.cpp)
file(GLOB YAGE_UTIL_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} yage/util/*.cpp)
file(GLOB YAGE_RENDER_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} yage/render/*.cpp)
+file(GLOB YAGE_ENGINE_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} yage/engine/*.cpp)
file(GLOB YAGE_CURRENT_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} yage/*.cpp)
set(YAGE_SOURCES
@@ -42,6 +47,7 @@ set(YAGE_SOURCES
${YAGE_MATH_SOURCES}
${YAGE_UTIL_SOURCES}
${YAGE_RENDER_SOURCES}
+ ${YAGE_ENGINE_SOURCES}
${YAGE_CURRENT_SOURCES})
add_library(yage
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index 36afa574..5b7d6fd3 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -32,3 +32,4 @@ make_test(thread 1)
make_test(syncqueue 1)
make_test(active 1)
make_test(struct ${SIMULATION_RUNS})
+make_test(engine ${SIMULATION_RUNS})
diff --git a/tests/engine/test.cpp b/tests/engine/test.cpp
new file mode 100644
index 00000000..57680b99
--- /dev/null
+++ b/tests/engine/test.cpp
@@ -0,0 +1,16 @@
+#include <yage/engine/space.h>
+
+#include <yage/yage.h>
+
+int main()
+{
+ yage::Space space;
+ auto entity = space.createEntity();
+ auto entity2 = space.createEntity();
+ auto entity3 = space.createEntity();
+
+ yLogInfo << "Entity 1: " << entity;
+ yLogInfo << "Entity 3: " << entity3;
+ yLogInfo << "Entity 2: " << entity2;
+ return 0;
+}
diff --git a/yage/core/stb_image.cpp b/yage/core/stb_image.cpp
index 128d5cca..8ddfd1f5 100644
--- a/yage/core/stb_image.cpp
+++ b/yage/core/stb_image.cpp
@@ -1,10 +1,2 @@
-/** ---------------------------------------------------------------------------
- * @file: stb_image.cpp
- *
- * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com>
- * MIT License, see LICENSE file for more details.
- * ----------------------------------------------------------------------------
- */
-
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
diff --git a/yage/core/stb_image.h b/yage/core/stb_image.h
index d4eecf29..9ec8b93a 100644
--- a/yage/core/stb_image.h
+++ b/yage/core/stb_image.h
@@ -1,11 +1,3 @@
-/** ---------------------------------------------------------------------------
- * @file: stb_image.h
- *
- * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com>
- * MIT License, see LICENSE file for more details.
- * ----------------------------------------------------------------------------
- */
-
/* stb_image - v2.16 - public domain image loader -
http://nothings.org/stb_image.h no warranty implied; use at your own risk
diff --git a/yage/engine/component.h b/yage/engine/component.h
new file mode 100644
index 00000000..9fd85d82
--- /dev/null
+++ b/yage/engine/component.h
@@ -0,0 +1,21 @@
+/** ---------------------------------------------------------------------------
+ * @file: component.h
+ *
+ * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com>
+ * MIT License, see LICENSE file for more details.
+ * ----------------------------------------------------------------------------
+ */
+
+#ifndef YAGE_ENGINE_COMPONENT_H
+#define YAGE_ENGINE_COMPONENT_H
+
+class Component
+{
+};
+
+enum class ComponentEnum {
+ POSITION,
+ NAX_COMPONENTS,
+};
+
+#endif
diff --git a/yage/engine/engine.cpp b/yage/engine/engine.cpp
index 3918e7e9..cf6f73b7 100644
--- a/yage/engine/engine.cpp
+++ b/yage/engine/engine.cpp
@@ -8,23 +8,26 @@
#include "engine.h"
-#include "../core/window.h"
-
namespace yage
{
-void Engine::mainLoop()
+void Engine::init()
{
- Window window;
+ window_.create("Game Engine", 800, 640);
- window.create("Game Engine", 800, 640);
+ for (auto &system : systems_) {
+ system->init();
+ }
+}
- while(!window.shouldClose()) {
- window.clearBuffer();
+void Engine::mainLoop()
+{
+ while (!window_.shouldClose()) {
+ window_.clearBuffer();
update();
- window.swapBuffer();
+ window_.swapBuffer();
}
}
@@ -32,7 +35,7 @@ void Engine::update()
{
const double dt = 1.0 / 60.0;
- for(auto &&system : systems_) {
+ for (auto &system : systems_) {
system->update(dt);
}
}
diff --git a/yage/engine/engine.h b/yage/engine/engine.h
index dabe5596..147769e5 100644
--- a/yage/engine/engine.h
+++ b/yage/engine/engine.h
@@ -9,6 +9,7 @@
#ifndef YAGE_CORE_ENGINE_H
#define YAGE_CORE_ENGINE_H
+#include "../core/window.h"
#include "system.h"
#include <vector>
@@ -21,6 +22,9 @@ namespace yage
class Engine
{
public:
+ /// Initialize window and other aspects of the engine.
+ void init();
+
/// Main game loop of the engine.
void mainLoop();
@@ -37,6 +41,9 @@ public:
private:
/// Vector of all the systems in the engine.
std::vector<System *> systems_;
+
+ /// Window
+ Window window_;
};
} // namespace yage
diff --git a/yage/engine/entity.cpp b/yage/engine/entity.cpp
new file mode 100644
index 00000000..4d9a4b0a
--- /dev/null
+++ b/yage/engine/entity.cpp
@@ -0,0 +1,23 @@
+/** ---------------------------------------------------------------------------
+ * @file: entity.cpp
+ *
+ * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com>
+ * MIT License, see LICENSE file for more details.
+ * ----------------------------------------------------------------------------
+ */
+
+#include "entity.h"
+
+#include "space.h"
+
+namespace yage
+{
+
+Entity::Entity(unsigned handle) : handle_(handle) {}
+
+unsigned Entity::getHandle() const
+{
+ return handle_;
+}
+
+} // naemspace yage
diff --git a/yage/engine/entity.h b/yage/engine/entity.h
new file mode 100644
index 00000000..c31490e5
--- /dev/null
+++ b/yage/engine/entity.h
@@ -0,0 +1,53 @@
+/** ---------------------------------------------------------------------------
+ * @file: entity.h
+ *
+ * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com>
+ * MIT License, see LICENSE file for more details.
+ * ----------------------------------------------------------------------------
+ */
+
+#ifndef YAGE_ENGINE_ENTITY_H
+#define YAGE_ENGINE_ENTITY_H
+
+#include <vector>
+
+namespace yage
+{
+
+class Space;
+
+/**
+ * Entity convenience class. It contains handles to where the entity is in the
+ * entity manager of the space, and a pointer back to the space itself.
+ *
+ * This class cannot be instantiated outside of a Space and should only be
+ * instantiated through an entity manager, as otherwise the handle will not have
+ * a meaning.
+ */
+class Entity
+{
+public:
+ /**
+ * Creates an instance of an Entity with a handle that is associated to it.
+ * This handle refers to the position of he Entity in the list that is held
+ * by the EntityManager, and therefore the id is enough to refer to it.
+ */
+ Entity(unsigned handle);
+
+ /**
+ * Handle getter, as the user will only interact with the id itself. The
+ * handle is the unique identifier that the user can use to refer to the
+ * entity.
+ */
+ unsigned getHandle() const;
+
+private:
+ /**
+ * Entity handle for the entity manager.
+ */
+ unsigned handle_;
+};
+
+} // namespace yage
+
+#endif
diff --git a/yage/engine/entitymanager.cpp b/yage/engine/entitymanager.cpp
new file mode 100644
index 00000000..332ed9b8
--- /dev/null
+++ b/yage/engine/entitymanager.cpp
@@ -0,0 +1,34 @@
+/** ---------------------------------------------------------------------------
+ * @file: entitymanager.cpp
+ *
+ * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com>
+ * MIT License, see LICENSE file for more details.
+ * ----------------------------------------------------------------------------
+ */
+
+#include "entitymanager.h"
+
+namespace yage
+{
+
+EntityManager::EntityManager(Space *space) : next_handle_(0), space_(space) {}
+
+EntityManager::EntityManager(Space *space, std::size_t n)
+ : next_handle_(0), space_(space)
+{
+ entities_.reserve(n);
+}
+
+unsigned EntityManager::createEntity()
+{
+ return createEntityInstance().getHandle();
+}
+
+Entity EntityManager::createEntityInstance()
+{
+ Entity entity(next_handle_++);
+ entities_.push_back(entity);
+ return entity;
+}
+
+} // namespace yage
diff --git a/yage/engine/entitymanager.h b/yage/engine/entitymanager.h
new file mode 100644
index 00000000..da125d94
--- /dev/null
+++ b/yage/engine/entitymanager.h
@@ -0,0 +1,83 @@
+/** ---------------------------------------------------------------------------
+ * @file: entitymanager.h
+ *
+ * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com>
+ * MIT License, see LICENSE file for more details.
+ * ----------------------------------------------------------------------------
+ */
+
+#ifndef YAGE_ENGINE_ENTITYMANAGER_H
+#define YAGE_ENGINE_ENTITYMANAGER_H
+
+#include "entity.h"
+
+#include <vector>
+
+namespace yage
+{
+
+class Space;
+
+/**
+ * Manages entities in a space.
+ */
+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.
+ */
+ unsigned createEntity();
+
+ /**
+ * Creates an Entity and returns it.
+ *
+ * @return The entity that was created by the entity manager in the current
+ * space.
+ */
+ Entity createEntityInstance();
+
+private:
+ /**
+ * The next available handle to give to the user.
+ */
+ unsigned next_handle_;
+
+ /**
+ * The space that the entity manager belongs to.
+ */
+ Space *space_;
+
+ /**
+ * The entities in the current space.
+ */
+ std::vector<Entity> entities_;
+};
+
+} // namespace yage
+
+#endif
diff --git a/yage/engine/space.cpp b/yage/engine/space.cpp
new file mode 100644
index 00000000..f3e343b5
--- /dev/null
+++ b/yage/engine/space.cpp
@@ -0,0 +1,21 @@
+/** ---------------------------------------------------------------------------
+ * @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();
+}
+
+} // namespace yage
diff --git a/yage/engine/space.h b/yage/engine/space.h
new file mode 100644
index 00000000..e69df37a
--- /dev/null
+++ b/yage/engine/space.h
@@ -0,0 +1,61 @@
+/** ---------------------------------------------------------------------------
+ * @file: space.h
+ *
+ * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com>
+ * MIT License, see LICENSE file for more details.
+ * ----------------------------------------------------------------------------
+ */
+
+#ifndef YAGE_ENGINE_SPACE_H
+#define YAGE_ENGINE_SPACE_H
+
+#include <vector>
+
+#include "entitymanager.h"
+
+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.
+ */
+ unsigned createEntity();
+
+private:
+ /**
+ * The subspaces 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<System *> systems_;
+
+ /**
+ * Manages all the entities in the system, can create them for the current
+ * space.
+ */
+ EntityManager em_;
+};
+
+} // namespace yage
+
+#endif