Skip to content

Commit

Permalink
ecs stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
StellaSmith committed Dec 9, 2023
1 parent b0d9a3b commit 18b6dd4
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 36 deletions.
18 changes: 9 additions & 9 deletions conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,20 @@ def layout(self):

def requirements(self):
self.requires("glm/cci.20230113")
self.requires("entt/[>=3.12.0 <3.13]")
self.requires("sdl_image/[>=2.0.0 <2.1]")
self.requires("fmt/10.1.1")
self.requires("spdlog/1.12.0")
self.requires("entt/[~3.12]")
self.requires("sdl_image/[~2.0]")
self.requires("fmt/[~10.1]")
self.requires("spdlog/[~1.12]")
self.requires("rapidjson/cci.20220822")
self.requires("imgui/[>=1.89.0 <1.90]")
self.requires("boost/[>=1.83.0 <1.84]")
self.requires("sdl/[>=2.28.0 <2.29]", override=True)
self.requires("imgui/[~1.90]")
self.requires("boost/[~1.83]")
self.requires("sdl/[~2]")

if self.options.with_opengl:
self.requires("glad/[>=0.1.0 <0.2.0]")
self.requires("glad/[~0.1]")

if self.options.with_vulkan:
sdk_version = "1.3.243.0"
sdk_version = "[~1.3]"
self.requires(f"vulkan-headers/{sdk_version}")
self.requires(f"volk/{sdk_version}")

Expand Down
59 changes: 59 additions & 0 deletions include/engine/ecs/system.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#pragma once

#include <entt/entt.hpp>
#include <span>

namespace engine::ecs {
struct System {
entt::hashed_string name;
void (*pfn_execute)(entt::registry &registry);
};

struct SystemDependencies {
entt::hashed_string name;
std::span<entt::hashed_string const> dependencies;
};

template <typename T>
System make_system(entt::hashed_string name, T func)
{
return System {
.name = name,
.pfn_execute = func,
};
}

template <typename With, typename Without>
class View;

template <typename... With, typename... Without>
class View<entt::get_t<With...>, entt::exclude_t<Without...>> {
template <typename Entity, typename Allocator>
static decltype(auto) get(entt::basic_registry<Entity, Allocator> &registry)
{
return registry.template view<With...>(entt::exclude<Without...>);
}
};

template <typename T>
class Res {
template <typename Entity, typename Allocator>
static decltype(auto) get(entt::basic_registry<Entity, Allocator> &registry)
{
return registry.ctx().template get<T>();
}
};

extern std::span<System const> const systems;
extern std::span<SystemDependencies const> const systems_dependencies;
} // namespace engine::ecs

#define CONCAT(a, b) a##b
#define ECS_SYSTEM(name) \
namespace { \
__attribute__((section("system_array"), used)) static auto CONCAT(name, _system) = engine::ecs::make_system(#name, &name); \
}
#define ECS_SYSTEM_DEPENDS(name, ...) \
namespace { \
__attribute__((section("system_dependencies_array"), used)) static auto const CONCAT(name, _system_dependencies) = engine::ecs::SystemDependencies { #name, { __VA_ARGS__ } }; \
}
9 changes: 0 additions & 9 deletions include/engine/ecs/systems/localplayer_camera.hpp

This file was deleted.

15 changes: 15 additions & 0 deletions src/engine/ecs/system.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <engine/ecs/system.hpp>

// these arrays are populated at link time thanks to the macros which use the section attribute
extern "C" engine::ecs::System const __start_system_array[];
extern "C" engine::ecs::System const __stop_system_array[];
extern "C" engine::ecs::SystemDependencies const __start_system_dependencies_array[];
extern "C" engine::ecs::SystemDependencies const __stop_system_dependencies_array[];

std::span<const engine::ecs::System> const engine::ecs::systems = std::span<engine::ecs::System const>(
&__start_system_array[0],
&__stop_system_array[0]);

std::span<engine::ecs::SystemDependencies const> const engine::ecs::systems_dependencies = std::span<engine::ecs::SystemDependencies const>(
&__start_system_dependencies_array[0],
&__stop_system_dependencies_array[0]);
28 changes: 19 additions & 9 deletions src/engine/ecs/systems/localplayer_camera.cpp
Original file line number Diff line number Diff line change
@@ -1,33 +1,43 @@
#include <engine/ecs/system.hpp>

#include <engine/ecs/components/Camera.hpp>
#include <engine/ecs/components/LocalPlayer.hpp>
#include <engine/ecs/systems/localplayer_camera.hpp>
#include <engine/rendering/IRenderer.hpp>

#include <SDL_keyboard.h>
#include <entt/entt.hpp>

constexpr float camera_speed = 25.0f;
#include <chrono>

struct Delta {
std::chrono::duration<double> delta;
};

void engine::systems::localplayer_camera(entt::registry &registry, std::chrono::duration<double> delta)
void localplayer_camera(entt::registry &registry)
{
auto const keyboard_state = SDL_GetKeyboardState(nullptr);
auto &renderer = registry.ctx().get<std::unique_ptr<engine::rendering::IRenderer>>();
auto const delta = static_cast<float>(registry.ctx().get<Delta>().delta.count());
(void)renderer;

auto view = registry.view<engine::components::LocalPlayer, engine::components::Camera>();
view.each([&](engine::components::Camera &camera) {
const glm::vec3 right = -glm::cross(camera.up, camera.forward);
if (keyboard_state[SDL_SCANCODE_W])
camera.position += glm::normalize(glm::vec3 { -camera.forward.x, 0.0f, camera.forward.z }) * camera_speed * static_cast<float>(delta.count());
camera.position += glm::normalize(glm::vec3 { -camera.forward.x, 0.0f, camera.forward.z }) * camera_speed * delta;
if (keyboard_state[SDL_SCANCODE_S])
camera.position -= glm::normalize(glm::vec3 { -camera.forward.x, 0.0f, camera.forward.z }) * camera_speed * static_cast<float>(delta.count());
camera.position -= glm::normalize(glm::vec3 { -camera.forward.x, 0.0f, camera.forward.z }) * camera_speed * delta;
if (keyboard_state[SDL_SCANCODE_D])
camera.position += glm::vec3 { -right.x, 0.0f, right.z } * camera_speed * static_cast<float>(delta.count());
camera.position += glm::vec3 { -right.x, 0.0f, right.z } * camera_speed * delta;
if (keyboard_state[SDL_SCANCODE_A])
camera.position -= glm::vec3 { -right.x, 0.0f, right.z } * camera_speed * static_cast<float>(delta.count());
camera.position -= glm::vec3 { -right.x, 0.0f, right.z } * camera_speed * delta;
if (keyboard_state[SDL_SCANCODE_SPACE])
camera.position.y += camera_speed * static_cast<float>(delta.count());
camera.position.y += camera_speed * delta;
if (keyboard_state[SDL_SCANCODE_LSHIFT])
camera.position.y -= camera_speed * static_cast<float>(delta.count());
camera.position.y -= camera_speed * delta;
});
}
}

ECS_SYSTEM(localplayer_camera)
ECS_SYSTEM_DEPENDS(localplayer_camera)
3 changes: 2 additions & 1 deletion src/game/shared.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <engine/Config.hpp>
#include <engine/Game.hpp>
#include <engine/ecs/components/Dirty.hpp>
#include <engine/rendering/opengl/Renderer.hpp>
#include <engine/rendering/vulkan/Renderer.hpp>
#include <math/bits.hpp>

Expand All @@ -16,7 +17,7 @@ engine::Camera g_camera;
void engine::Game::start()
{
constexpr int width = 640, height = 480;
m_renderer = std::make_unique<engine::rendering::vulkan::Renderer>(*this);
m_renderer = std::make_unique<engine::rendering::opengl::Renderer>(*this);
m_window = m_renderer->create_window(
"VGame",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
Expand Down
6 changes: 0 additions & 6 deletions src/game/update.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@
#include <SDL_scancode.h>
#include <imgui_impl_sdl2.h>

#include <algorithm>
#include <cstring>
#include <tuple>

#include <engine/ecs/systems/localplayer_camera.hpp>

extern engine::Camera g_camera;
static glm::vec3 previous_camera_position {};

Expand Down
37 changes: 35 additions & 2 deletions tools/build_unix.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,36 @@
#!/bin/sh
#!/bin/bash

exec conan build . -pr:h default -s:h compiler.cppstd=20 -b missing
set -e -o pipefail

case "$1" in
"debug")
declare -r build_type="Debug";
;;
"release")
declare -r build_type="Release";
;;
*)
echo "Unkown build_type $build_type" > /dev/stderr;
exit 1;
;;
esac;

declare -r build_directory="$PWD/build/$build_type";
declare -r generators_directory="$build_directory/generators";

if [[ ! -e "$generators_directory" ]]; then
conan install . -pr:h default -s:h compiler.cppstd=20 -b missing -s "build_type=$build_type" -u;
fi;


# shellcheck source=/dev/null
source "$generators_directory/conanbuild.sh";
if [[ ! -e "$build_directory/CMakeCache.txt" ]]; then
cmake --preset "conan-$1"
fi;

cmake --build --preset "conan-$1" --parallel;

# shellcheck source=/dev/null
source "$generators_directory/conanrun.sh";
cmake --install "$build_directory" --prefix "$PWD/install";

0 comments on commit 18b6dd4

Please sign in to comment.