Skip to content

Commit

Permalink
feat(tools): add toggleable debug camera
Browse files Browse the repository at this point in the history
  • Loading branch information
roby2014 committed Oct 14, 2023
1 parent 78b4a0c commit 4da61fd
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
49 changes: 45 additions & 4 deletions engine/src/cubos/engine/tools/debug_camera/plugin.cpp
Original file line number Diff line number Diff line change
@@ -1,22 +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;

static void inspector(Write<Settings> settings)
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");

printf("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.system(inspector).tagged("cubos.imgui");
cubos.addPlugin(imguiPlugin);

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

cubos.addResource<DebugCameraInfo>();
}
3 changes: 3 additions & 0 deletions tools/tesseratos/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#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>
#include <cubos/engine/tools/world_inspector/plugin.hpp>
#include <cubos/engine/transform/plugin.hpp>


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

Expand Down Expand Up @@ -35,6 +37,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 4da61fd

Please sign in to comment.