diff --git a/CMakeLists.txt b/CMakeLists.txt index 4facd23..f9eed40 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,6 +25,7 @@ set(SRC_FILES "src/scene.cpp" "src/scene_manager.cpp" "src/systems/collisions.cpp" + "src/systems/custom_behaviour.cpp" "src/systems/render.cpp" "src/systems/transform.cpp" "src/util/files.cpp" @@ -43,6 +44,7 @@ set(SRC_FILES set(INCLUDE_FILES "include/application.h" "include/components/collider.h" + "include/components/custom.h" "include/components/renderable.h" "include/components/transform.h" "include/ecs_system.h" @@ -64,6 +66,7 @@ set(INCLUDE_FILES "include/scene.h" "include/scene_manager.h" "include/systems/collisions.h" + "include/systems/custom_behaviour.h" "include/systems/render.h" "include/systems/transform.h" "include/util.h" diff --git a/include/components/custom.h b/include/components/custom.h new file mode 100644 index 0000000..221e9f1 --- /dev/null +++ b/include/components/custom.h @@ -0,0 +1,14 @@ +#ifndef ENGINE_INCLUDE_COMPONENTS_CUSTOM_H_ +#define ENGINE_INCLUDE_COMPONENTS_CUSTOM_H_ + +#include + +namespace engine { + +struct CustomComponent { + std::function onUpdate; // void onUpdate(float ts); +}; + +} // namespace engine + +#endif \ No newline at end of file diff --git a/include/systems/custom_behaviour.h b/include/systems/custom_behaviour.h new file mode 100644 index 0000000..aa6f917 --- /dev/null +++ b/include/systems/custom_behaviour.h @@ -0,0 +1,24 @@ +#ifndef ENGINE_INCLUDE_CUSTOM_BEHAVIOUR_H_ +#define ENGINE_INCLUDE_CUSTOM_BEHAVIOUR_H_ + +#include "ecs_system.h" + +/* This system allows for one-off custom components that execute arbitrary code + * It is similar to Unity's 'MonoBehavior' system */ + +namespace engine { + + class CustomBehaviourSystem : public System { + public: + CustomBehaviourSystem(Scene* scene); + ~CustomBehaviourSystem(); + + void OnUpdate(float ts) override; + + private: + + }; + +} // namespace engine + +#endif \ No newline at end of file diff --git a/src/gfx_device_vulkan.cpp b/src/gfx_device_vulkan.cpp index f58ffa6..087dffe 100644 --- a/src/gfx_device_vulkan.cpp +++ b/src/gfx_device_vulkan.cpp @@ -1602,7 +1602,7 @@ namespace engine { LOG_INFO("GPU Memory Statistics:"); for (uint32_t i = 0; i < memProps.memoryProperties.memoryHeapCount; i++) { - const VmaStatistics& statistics = pStats.memoryType[i].statistics; + const VmaStatistics& statistics = pStats.memoryHeap[i].statistics; VkMemoryHeap heap = memProps.memoryProperties.memoryHeaps[i]; LOG_INFO("Memory heap {}", i); if (heap.flags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT) { diff --git a/src/resources/font.cpp b/src/resources/font.cpp index 137d8b6..4429ce4 100644 --- a/src/resources/font.cpp +++ b/src/resources/font.cpp @@ -82,7 +82,7 @@ std::unique_ptr> Font::GetTextBitmap( bitmap->at(i * 4 + 0) = 0x00; bitmap->at(i * 4 + 1) = 0x00; bitmap->at(i * 4 + 2) = 0x00; - bitmap->at(i * 4 + 3) = 0xFF; + bitmap->at(i * 4 + 3) = 0x00; } int top_left_x = 0; diff --git a/src/scene.cpp b/src/scene.cpp index ecdc5c3..203228b 100644 --- a/src/scene.cpp +++ b/src/scene.cpp @@ -3,69 +3,65 @@ #include "components/transform.h" #include "components/renderable.h" #include "components/collider.h" +#include "components/custom.h" #include "systems/transform.h" #include "systems/render.h" #include "systems/collisions.h" +#include "systems/custom_behaviour.h" namespace engine { - Scene::Scene(Application* app) - : app_(app) - { - // event system - event_system_ = std::make_unique(); +Scene::Scene(Application* app) : app_(app) { + // event system + event_system_ = std::make_unique(); - // ecs configuration: + // ecs configuration: - RegisterComponent(); - RegisterComponent(); - RegisterComponent(); - - // Order here matters: - RegisterSystem(); - RegisterSystem(); - RegisterSystem(); - } - - Scene::~Scene() - { - } - - uint32_t Scene::CreateEntity(const std::string& tag, uint32_t parent) - { - uint32_t id = next_entity_id_++; - - signatures_.emplace(id, std::bitset{}); - - auto t = AddComponent(id); - - t->position = {0.0f, 0.0f, 0.0f}; - t->rotation = {}; - t->scale = {1.0f, 1.0f, 1.0f}; - - t->tag = tag; - t->parent = parent; - - return id; - } - - uint32_t Scene::getEntity(const std::string& tag, uint32_t parent) - { - return GetSystem()->GetChildEntity(parent, tag); - } - - size_t Scene::GetComponentSignaturePosition(size_t hash) - { - return component_signature_positions_.at(hash); - } - - void Scene::Update(float ts) - { - for (auto& [name, system] : systems_) { - system->OnUpdate(ts); - } - - event_system_->DespatchEvents(); // clears event queue - } + RegisterComponent(); + RegisterComponent(); + RegisterComponent(); + RegisterComponent(); + // Order here matters: + RegisterSystem(); + RegisterSystem(); + RegisterSystem(); + RegisterSystem(); } + +Scene::~Scene() {} + +uint32_t Scene::CreateEntity(const std::string& tag, uint32_t parent) { + uint32_t id = next_entity_id_++; + + signatures_.emplace(id, std::bitset{}); + + auto t = AddComponent(id); + + t->position = {0.0f, 0.0f, 0.0f}; + t->rotation = {}; + t->scale = {1.0f, 1.0f, 1.0f}; + + t->tag = tag; + t->parent = parent; + + return id; +} + +uint32_t Scene::getEntity(const std::string& tag, uint32_t parent) { + return GetSystem()->GetChildEntity(parent, tag); +} + +size_t Scene::GetComponentSignaturePosition(size_t hash) { + return component_signature_positions_.at(hash); +} + +void Scene::Update(float ts) { + for (auto& [name, system] : systems_) { + system->OnUpdate(ts); + } + + event_system_->DespatchEvents(); // clears event queue +} + +} // namespace engine diff --git a/src/systems/custom_behaviour.cpp b/src/systems/custom_behaviour.cpp new file mode 100644 index 0000000..9322bd7 --- /dev/null +++ b/src/systems/custom_behaviour.cpp @@ -0,0 +1,27 @@ +#include "systems/custom_behaviour.h" + +#include + +#include "components/custom.h" +#include "components/transform.h" +#include "scene.h" + +namespace engine { + +CustomBehaviourSystem::CustomBehaviourSystem(Scene* scene) + : System(scene, {typeid(TransformComponent).hash_code(), + typeid(CustomComponent).hash_code()}) { + // constructor here +} + +CustomBehaviourSystem::~CustomBehaviourSystem() {} + +void CustomBehaviourSystem::OnUpdate(float ts) { + for (uint32_t entity : entities_) { + auto c = scene_->GetComponent(entity); + assert(c != nullptr); + c->onUpdate(ts); + } +} + +} // namespace engine \ No newline at end of file diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index d97adbe..b71d514 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -17,6 +17,10 @@ set(GAME_SOURCES ) if (WIN32) + option(ENGINETEST_BUILD_WIN32_APP "Build as a standalone win32 app (no console window)" ON) +endif() + +if (WIN32 AND ENGINETEST_BUILD_WIN32_APP) add_executable(${PROJECT_NAME} WIN32 ${GAME_SOURCES} "game.rc") else() add_executable(${PROJECT_NAME} ${GAME_SOURCES}) diff --git a/test/src/game.cpp b/test/src/game.cpp index 082607f..f4cd098 100644 --- a/test/src/game.cpp +++ b/test/src/game.cpp @@ -3,6 +3,7 @@ #include "application.h" #include "camera_controller.hpp" #include "components/collider.h" +#include "components/custom.h" #include "components/renderable.h" #include "components/transform.h" #include "input_manager.h" @@ -129,8 +130,8 @@ void PlayGame(GameSettings settings) { floor_collider->aabb = {{0.0f, 0.0f, 0.0f}, {10000.0f, 1.0f, 10000.0f}}; } - // engine::util::LoadMeshFromFile( - // my_scene, app.GetResourcePath("models/astronaut/astronaut.dae")); + //engine::util::LoadMeshFromFile( + // my_scene, app.GetResourcePath("models/astronaut/astronaut.dae")); /* skybox */ { @@ -150,7 +151,8 @@ void PlayGame(GameSettings settings) { { int width, height; auto bitmap = app.GetResource("builtin.mono") - ->GetTextBitmap("The", 768.0f, width, height); + ->GetTextBitmap("ABCDEFGHIJKLMNOPQRSTUVWXYZ12345", 768.0f, + width, height); uint32_t textbox = my_scene->CreateEntity("textbox"); auto textbox_renderable = @@ -161,8 +163,15 @@ void PlayGame(GameSettings settings) { textbox_renderable->material->texture_ = std::make_unique( &app.render_data_, bitmap->data(), width, height, - engine::resources::Texture::Filtering::kOff); - textbox_renderable->mesh = GenSphereMesh(app.gfxdev(), 1.0f, 8); + engine::resources::Texture::Filtering::kBilinear); + textbox_renderable->mesh = GenSphereMesh(app.gfxdev(), 1.0f, 5); + my_scene->GetComponent(textbox)->scale.y = + (float)height / (float)width; + + my_scene->AddComponent(textbox)->onUpdate = + [](float ts) { + /* LOG_INFO("Time step: {}", ts); */ + }; } app.GameLoop();