Skip to content

Commit

Permalink
refactor: Move engine into its own project
Browse files Browse the repository at this point in the history
- The engine is now located in the `engine` folder
- Header files are now in `engine/include` and source files are in `engine/src`
  • Loading branch information
BenjaminHalko committed Jun 9, 2024
1 parent 3cd6592 commit 01c15be
Show file tree
Hide file tree
Showing 29 changed files with 148 additions and 139 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ set(CMAKE_CXX_STANDARD 17)
# Add source files
file(GLOB_RECURSE SOURCES src/*)
add_executable(Textual_Game ${SOURCES})

# Link libraries
add_subdirectory(engine)
target_link_libraries(Textual_Game Engine)
11 changes: 11 additions & 0 deletions engine/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
cmake_minimum_required(VERSION 3.5)
project(Engine)

set(CMAKE_CXX_STANDARD 17)

# Add source files
file(GLOB_RECURSE ENGINE_SOURCES src/*)
add_library(Engine ${ENGINE_SOURCES})

# Set include directory
target_include_directories(Engine PUBLIC include)
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include "_component.h"
#include <engine/components/_component.h>

class GravityComponent : public Component {
private:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "_component.h"
#pragma once

#include <engine/components/_component.h>

class ScriptComponent : public Component {
private:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include "_component.h"
#include "../common.h"
#include <engine/components/_component.h>
#include <engine/common.h>
#include <vector>
#include <iterator>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include "_component.h"
#include <engine/components/_component.h>

/**
* @brief A component that represents the position, scale, and rotation of an entity.
Expand Down
25 changes: 14 additions & 11 deletions src/engine/ecs.h → engine/include/engine/ecs.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
#pragma once

#include "entities/_entity.h"
#include "systems/_system.h"
#include <engine/entity.h>
#include <engine/systems/_system.h>
#include <memory>
#include <set>

class ECS {
static std::multiset<std::unique_ptr<Entity>> _entities;
static std::set<std::unique_ptr<System>> _systems;
static bool _isRunning;

/**
* @brief Adds a system to the ECS
* @tparam SystemType
*/
template <typename SystemType>
static void AddSystem() {
_systems.insert(std::make_unique<SystemType>());
}
public:
/**
* @brief Adds an entity to the ECS
Expand All @@ -21,19 +30,13 @@ class ECS {
_entities.insert(std::make_unique<EntityType>());
}

/**
* @brief Adds a system to the ECS
* @tparam SystemType
*/
template <typename SystemType>
static void AddSystem() {
_systems.insert(std::make_unique<SystemType>());
}

static std::multiset<std::unique_ptr<Entity>>& GetEntities();

static void GameLoop();

/**
* @brief Stops the game loop
*/
static void StopGame();
};

Expand Down
10 changes: 5 additions & 5 deletions src/engine/entities/_entity.h → engine/include/engine/entity.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
#pragma once

#include "../components/_component.h"
#include <engine/components/_component.h>
#include <memory>
#include <typeindex>
#include <unordered_map>
#include <unordered_set>
#include <string>
#include <iostream>

#define ComponentMap std::unordered_map<std::type_index, std::unique_ptr<Component>>

/*
* @brief The base class for all entities
*/
Expand All @@ -20,7 +18,7 @@ class Entity {
* The value is a unique pointer to the component.
* This is a map so that we can easily access the component by its type.
*/
ComponentMap components;
std::unordered_map<std::type_index, std::unique_ptr<Component>> components;

// Tells systems the order in which to execute the entity
int executionPriority = 0;
Expand Down Expand Up @@ -59,7 +57,9 @@ class Entity {
return ((components.find(typeid(ComponentTypes)) != components.end()) && ...);
}

// Orders the entity inside of sets
bool operator<(const Entity &other) const;

bool gotDestroyed() const;
// Check if the entity is destroyed
[[nodiscard]] bool gotDestroyed() const;
};
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
#pragma once

#include "../entities/_entity.h"


/**
* @brief Base class for all systems
*/
Expand Down
36 changes: 36 additions & 0 deletions engine/include/engine/systems/input.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#pragma once

#include <engine/systems/_system.h>
#include <unordered_map>

enum Key {
LEFT = 0,
RIGHT,
QUIT
};

class Input : public System {
enum KeyState {UP, PRESSED, HELD};
static std::unordered_map<Key, KeyState> keys;
public:
/**
* @brief Loops over all the keys and tracks their state
* @note This function should be called once per frame
* to update the key states for the current frame
*/
void Update() override;

/**
* @brief Check if a key is held down
* @param key The key to check
* @return bool True if the key is held
*/
static bool GetKeyDown(Key key);

/**
* @brief Check if a key is pressed
* @param key The key to check
* @return bool True if the key is pressed
*/
static bool GetKeyPressed(Key key);
};
49 changes: 49 additions & 0 deletions engine/include/engine/systems/renderSystem.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#pragma once

#include <engine/systems/_system.h>
#include <engine/components/spriteComponent.h>
#include <string>
#include <windows.h>
#include <algorithm>
#include <iostream>

class RenderSystem : public System {
// Handle to the console
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO consoleInfo{};

// Buffer to store the console
std::vector<std::pair<char, Color>> consoleBuffer;
int charCount = 0;

// Queue screen clear
bool clearScreen = false;

/**
* @brief Determines the alpha and color of a point
* @details The alpha is calculated using the barycentric coordinates method
* @param SpriteComponent The points of the triangle strip
* @param x The x-coordinate of the point
* @param y The y-coordinate of the point
* @return A character representing the luminance of the point
*/
static std::pair<char, Color> AlphaColorOfPoint(SpriteComponent &sprite, int x, int y);

/**
* @brief Sets a character at a position in the console
* @param x The x-coordinate of the character
* @param y The y-coordinate of the character
* @param character The character to set
*/
void SetConsoleCharacter(int x, int y, std::pair<char, Color> character);

/**
* @brief Draws a triangle to the console
* @param triangleList The points of the triangle strip
* @param index The index of the first point in the triangle strip
*/
void DrawTriangle(SpriteComponent &sprite, int index);
public:
RenderSystem();
void Update() override;
};
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include "_system.h"
#include "../components/spriteComponent.h"
#include <engine/systems/_system.h>
#include <engine/components/spriteComponent.h>

class SpriteSystem : public System {
public:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include "_system.h"
#include <engine/systems/_system.h>

#include <chrono>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "_system.h"
#include "../common.h"
#include "../components/transformComponent.h"
#include "../components/spriteComponent.h"
#include <engine/systems/_system.h>
#include <engine/common.h>
#include <engine/components/transformComponent.h>
#include <engine/components/spriteComponent.h>

class TransformSystem : public System {
public:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "gravityComponent.h"
#include <engine/components/gravityComponent.h>

GravityComponent& GravityComponent::operator=(float gravity) {
this->_gravity = gravity;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "scriptComponent.h"
#include <engine/components/scriptComponent.h>

void ScriptComponent::operator()() {
_script();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "spriteComponent.h"
#include <engine/components/spriteComponent.h>

SpriteComponent::SpriteComponent(std::initializer_list<ColoredPoint> sprite) {
std::copy(sprite.begin(), sprite.end(), std::back_inserter(_sprite));
Expand Down
11 changes: 2 additions & 9 deletions src/engine/ecs.cpp → engine/src/engine/ecs.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "ecs.h"
#include "systems/timeSystem.h"
#include <engine/ecs.h>
#include <engine/systems/timeSystem.h>

// We need to define the static variables here
std::multiset<std::unique_ptr<Entity>> ECS::_entities;
Expand All @@ -13,10 +13,6 @@ std::multiset<std::unique_ptr<Entity>>& ECS::GetEntities() {
return _entities;
}

/**
* @brief Runs the every frame
* @details It loops over all the systems and calls their UpdateEntity function
*/
void ECS::GameLoop() {
while(_isRunning) {
TimeSystem::FrameStart();
Expand All @@ -27,9 +23,6 @@ void ECS::GameLoop() {
}
}

/**
* @brief Stops the game loop
*/
void ECS::StopGame() {
_isRunning = false;
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
#include "_entity.h"
#include <engine/entity.h>

// Orders the entity inside of sets
bool Entity::operator<(const Entity &other) const {
return executionPriority < other.executionPriority;
}

// Check if the entity is destroyed
bool Entity::gotDestroyed() const {
return destroyed;
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
#include "input.h"
#include <engine/systems/input.h>
#include <windows.h>

// Define the static variable
std::unordered_map<Key, Input::KeyState> Input::keys;

/**
* @brief Loops over all the keys and tracks their state
* @note This function should be called once per frame
* to update the key states for the current frame
*/
void Input::Update() {
for(int i = Key::LEFT; i <= Key::QUIT; i++) {
bool held;
Expand Down Expand Up @@ -39,20 +34,10 @@ void Input::Update() {
}
}

/**
* @brief Check if a key is held down
* @param key The key to check
* @return bool True if the key is held
*/
bool Input::GetKeyDown(Key key) {
return keys[key] != KeyState::UP;
}

/**
* @brief Check if a key is pressed
* @param key The key to check
* @return bool True if the key is pressed
*/
bool Input::GetKeyPressed(Key key) {
return keys[key] == KeyState::PRESSED;
}
Loading

0 comments on commit 01c15be

Please sign in to comment.