Skip to content

Commit

Permalink
feat(tools): add toggleable debug camera plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
roby2014 committed Oct 14, 2023
1 parent 85616ec commit b51f60b
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 0 deletions.
1 change: 1 addition & 0 deletions engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ set(CUBOS_ENGINE_SOURCE
"src/cubos/engine/tools/world_inspector/plugin.cpp"
"src/cubos/engine/tools/entity_inspector/plugin.cpp"
"src/cubos/engine/tools/scene_editor/plugin.cpp"
"src/cubos/engine/tools/debug_camera/plugin.cpp"

"src/cubos/engine/transform/plugin.cpp"
"src/cubos/engine/transform/local_to_world.cpp"
Expand Down
18 changes: 18 additions & 0 deletions engine/include/cubos/engine/tools/debug_camera/plugin.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/// @dir
/// @brief @ref debug-camera-plugin plugin directory.

/// @file
/// @brief Plugin entry point.
/// @ingroup debug-camera-plugin

#pragma once

#include <cubos/engine/cubos.hpp>

namespace cubos::engine::tools
{
/// @brief Plugin entry function.
/// @param cubos @b CUBOS. main class
/// @ingroup debug-camera-plugin
void debugCameraPlugin(Cubos& cubos);
} // namespace cubos::engine::tools
63 changes: 63 additions & 0 deletions engine/src/cubos/engine/tools/debug_camera/plugin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include <imgui.h>

#include <cubos/engine/imgui/plugin.hpp>
#include <cubos/engine/renderer/environment.hpp>
#include <cubos/engine/renderer/plugin.hpp>
#include <cubos/engine/renderer/point_light.hpp>
#include <cubos/engine/tools/debug_camera/plugin.hpp>
#include <cubos/engine/transform/plugin.hpp>

using cubos::core::ecs::Commands;
using cubos::core::ecs::Entity;
using cubos::core::ecs::Read;
using cubos::core::ecs::Write;

using namespace cubos::engine;

struct DebugCameraInfo
{
Entity copy[4]; // copy old camera entities to re-apply them in the next camera change
Entity ent; // debug camera entity
};

static void createDebugCameraSystem(Commands commands, Write<DebugCameraInfo> debugCamera)
{
debugCamera->ent = commands.create().add(Camera{}).add(Position{{}}).entity();
}

static void changeToDebugCameraSystem(Write<ActiveCameras> camera, Write<DebugCameraInfo> debugCamera)
{
ImGui::Begin("Debug Camera");

ImGui::Text("Current camera: %s", debugCamera->ent == camera->entities[0] ? "Debug" : "Game");
ImGui::Text("Key to change: F12");

if (ImGui::Button("Change camera") || ImGui::IsKeyPressed(ImGuiKey_F12))
{
CUBOS_DEBUG("Changed camera ...");
if (debugCamera->ent == camera->entities[0])
{
std::copy(std::begin(debugCamera->copy), std::end(debugCamera->copy), camera->entities);
}
else
{
std::copy(std::begin(camera->entities), std::end(camera->entities), debugCamera->copy);
camera->entities[0] = debugCamera->ent;
camera->entities[1] = Entity();
camera->entities[2] = Entity();
camera->entities[3] = Entity();
}
}

ImGui::End();
}

void cubos::engine::tools::debugCameraPlugin(Cubos& cubos)
{
cubos.addPlugin(imguiPlugin);

cubos.startupSystem(createDebugCameraSystem);
cubos.system(changeToDebugCameraSystem).tagged("cubos.imgui");

cubos.addResource<DebugCameraInfo>();
}
2 changes: 2 additions & 0 deletions tools/tesseratos/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <cubos/engine/renderer/plugin.hpp>
#include <cubos/engine/settings/settings.hpp>
#include <cubos/engine/tools/asset_explorer/plugin.hpp>
#include <cubos/engine/tools/debug_camera/plugin.hpp>
#include <cubos/engine/tools/entity_inspector/plugin.hpp>
#include <cubos/engine/tools/scene_editor/plugin.hpp>
#include <cubos/engine/tools/settings_inspector/plugin.hpp>
Expand Down Expand Up @@ -35,6 +36,7 @@ int main(int argc, char** argv)
cubos.addPlugin(tools::entityInspectorPlugin);
cubos.addPlugin(tools::worldInspectorPlugin);
cubos.addPlugin(tools::assetExplorerPlugin);
cubos.addPlugin(tools::debugCameraPlugin);

cubos.startupSystem(mockCamera).tagged("setup");
cubos.startupSystem(mockSettings).tagged("setup");
Expand Down

0 comments on commit b51f60b

Please sign in to comment.