Skip to content

Commit

Permalink
Merge pull request #54 from sdslabs/add-imgui
Browse files Browse the repository at this point in the history
Add Editor
  • Loading branch information
sin3point14 authored Feb 25, 2020
2 parents 34cd9fc + 518bc38 commit e3cfed5
Show file tree
Hide file tree
Showing 56 changed files with 48,773 additions and 243 deletions.
12 changes: 7 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
cmake_minimum_required(VERSION 3.12)

# Project metadata
project(Rootex
project(
Rootex
LANGUAGES CXX
VERSION 1.0
DESCRIPTION "Rootex Engine"
)

option(BUILD_EDITOR "Build editor executable" OFF)

set_property(GLOBAL PROPERTY USE_FOLDERS ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
set_property(GLOBAL PROPERTY USE_FOLDERS ON)

set(ROOTEX_INCLUDES
${ROOTEX_INCLUDES}
Expand All @@ -20,6 +22,6 @@ CACHE INTERNAL "")
add_subdirectory(rootex)
add_subdirectory(game)

set_directory_properties(PROPERTIES
VS_STARTUP_PROJECT Game
)
if (BUILD_EDITOR)
add_subdirectory(editor)
endif(BUILD_EDITOR)
2 changes: 1 addition & 1 deletion GenerateCache.bat
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mkdir build
cd build
del /F CMakeCache.txt
cmake .. -G "Visual Studio 16 2019" -A x64
cmake .. -DBUILD_EDITOR=ON -G "Visual Studio 16 2019" -A x64
cd ..
17 changes: 17 additions & 0 deletions editor/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
file(GLOB_RECURSE EditorSource ./**.cpp)
file(GLOB_RECURSE EditorHeaders ./**.h)

add_definitions(-DROOTEX_EDITOR)
add_executable(Editor ${EditorSource} ${EditorHeaders})

target_include_directories(Editor PUBLIC ../)
target_link_libraries(Editor PUBLIC Rootex)

add_custom_command(TARGET Editor POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${ALUT_DLL_LIBRARY}
$<TARGET_FILE_DIR:Editor>)

set_directory_properties(PROPERTIES
VS_STARTUP_PROJECT Editor
)
10 changes: 10 additions & 0 deletions editor/config/config.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version = 1.0

project = "Rootex Editor"

window = {
x = 100,
y = 100,
width = 1920,
height = 1080
}
84 changes: 84 additions & 0 deletions editor/editor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#include "editor.h"

#include "core/renderer/rendering_device.h"

#include "framework/components/visual/visual_component_graph.h"
#include "framework/components/hierarchy_component.h"
#include "framework/systems/render_system.h"

void Editor::initialize(HWND hWnd)
{
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
ImGui_ImplWin32_Init(hWnd);
ImGui_ImplDX11_Init(RenderingDevice::GetSingleton()->getDevice(), RenderingDevice::GetSingleton()->getContext());
ImGui::StyleColorsDark();
}

void Editor::begin(VisualComponentGraph* visualGraph)
{
ImGui_ImplDX11_NewFrame();
ImGui_ImplWin32_NewFrame();
ImGui::NewFrame();
ImGui::ShowDemoWindow();
}

void explore(const HierarchyComponent* component)
{
bool x;
static bool y = false;
ImGui::Button(std::to_string(component->getOwner()->getID()).c_str());
if (ImGui::IsItemClicked())
{
y = true;
if (ImGui::BeginPopup("Info"))
{
ImGui::OpenPopup("Popup");
ImGui::Text("This has %d components", component->getOwner()->getAllComponents().size());
ImGui::EndPopup();
}
}

for (auto&& child : component->getChildren())
{
explore(child->getComponent<HierarchyComponent>());
}
}

void Editor::displayVisualGraph(VisualComponentGraph* graph)
{
if (ImGui::Begin("Visual Graph"))
{
ImGui::SetNextWindowBgAlpha(1.0f);
if (ImGui::Begin("Viewport"))
{
ImGui::Image(RenderingDevice::GetSingleton()->getRenderTextureShaderResourceView(), { 1280, 720 });
}
ImGui::End();
//explore(graph->getRoot());
}
ImGui::End();
}

void Editor::end(VisualComponentGraph* visualGraph)
{
RenderingDevice::GetSingleton()->setTextureRenderTarget();
RenderSystem::GetSingleton()->render(visualGraph);
RenderingDevice::GetSingleton()->setBackBufferRenderTarget();
ImGui::Render();
ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());
}

Editor::~Editor()
{
ImGui_ImplDX11_Shutdown();
ImGui_ImplWin32_Shutdown();
ImGui::DestroyContext();
}

Editor* Editor::GetSingleton()
{
static Editor singleton;
return &singleton;
}
24 changes: 24 additions & 0 deletions editor/editor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include <Windows.h>

#include "vendor/ImGUI/imgui.h"
#include "vendor/ImGUI/imgui_impl_dx11.h"
#include "vendor/ImGUI/imgui_impl_win32.h"

class VisualComponentGraph;

class Editor
{
Editor() = default;
Editor(Editor&) = delete;
~Editor();

public:
static Editor* GetSingleton();

void initialize(HWND hWnd);
void begin(VisualComponentGraph* visualGraph);
void displayVisualGraph(VisualComponentGraph* graph);
void end(VisualComponentGraph* visualGraph);
};
104 changes: 104 additions & 0 deletions editor/editor_main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#include "common/common.h"

#include "core/audio/audio_source.h"
#include "core/audio/audio_system.h"
#include "core/audio/static_audio_buffer.h"
#include "core/audio/streaming_audio_buffer.h"

#include "core/input/input_manager.h"

#include "core/renderer/buffer_format.h"
#include "core/renderer/index_buffer.h"
#include "core/renderer/material.h"
#include "core/renderer/renderer.h"
#include "core/renderer/shader_library.h"
#include "core/renderer/vertex_buffer.h"

#include "core/event_manager.h"
#include "core/resource_loader.h"

#include "framework/components/hierarchy_component.h"
#include "framework/components/test_component.h"
#include "framework/components/visual/diffuse_visual_component.h"
#include "framework/components/visual/visual_component.h"
#include "framework/components/visual/visual_component_graph.h"
#include "framework/entity_factory.h"
#include "framework/systems/debug_system.h"
#include "framework/systems/light_system.h"
#include "framework/systems/render_system.h"
#include "framework/systems/test_system.h"

#include "main/window.h"

#include "os/os.h"
#include "os/thread.h"
#include "os/timer.h"

#include "script/interpreter.h"

#include "editor/editor.h"

int main()
{
OS::Initialize();
OS::PrintLine("Rootex Editor is starting: Build(" + OS::GetBuildDate() + "|" + OS::GetBuildTime() + ")");
OS::PrintLine("Starting from path: " + OS::GetAbsolutePath(".").generic_string());

AudioSystem::GetSingleton()->initialize();

LuaTextResourceFile* configFile = ResourceLoader::CreateLuaTextResourceFile("editor/config/config.lua");
LuaInterpreter interpreter;
interpreter.loadExecuteScript(configFile);

String projectName = interpreter.getGlobal("project");
OS::PrintLine("Loading Project: " + projectName);

LuaVariable windowLua = interpreter.getGlobal("window");
Vector2 windowPosition = { windowLua["x"], windowLua["y"] };
Vector2 windowSize = { windowLua["width"], windowLua["height"] };

Ptr<Window> editorWindow(new Window(
windowPosition.x,
windowPosition.y,
windowSize.x,
windowSize.y,
projectName,
true));

InputManager::GetSingleton()->initialize(windowSize.x, windowSize.y);

ShaderLibrary::MakeShaders();

Ref<VisualComponentGraph> visualGraph(new VisualComponentGraph());
Ref<RenderSystem> renderSystem(new RenderSystem());
Ref<LightSystem> lightSystem(new LightSystem());

Ref<Entity> cube = EntityFactory::GetSingleton()->createEntity(ResourceLoader::CreateLuaTextResourceFile("game/assets/test/cube.lua"));
cube->getComponent<TransformComponent>()->addTransform(Matrix::CreateTranslation({ 0.0f, 0.0f, -5.0f }));
visualGraph->addChild(cube);

OS::PrintLine("Project loaded successfully: " + projectName);
Editor::GetSingleton()->initialize(editorWindow->getWindowHandle());

std::optional<int> ret;
while (true)
{
if (ret = editorWindow->processMessages())
{
break;
}

AudioSystem::GetSingleton()->update();
InputManager::GetSingleton()->update();
EventManager::GetSingleton()->dispatchDeferred();

Editor::GetSingleton()->begin(visualGraph.get());
Editor::GetSingleton()->displayVisualGraph(visualGraph.get());
Editor::GetSingleton()->end(visualGraph.get());

editorWindow->swapBuffers();
editorWindow->clear();
}

return ret.value();
}
4 changes: 4 additions & 0 deletions game/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ add_custom_command(TARGET Game POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${ALUT_DLL_LIBRARY}
$<TARGET_FILE_DIR:Game>)

set_directory_properties(PROPERTIES
VS_STARTUP_PROJECT Game
)
8 changes: 4 additions & 4 deletions game/assets/config/window.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
window = {
x = 100,
y = 200,
deltaX = 640,
deltaY = 480,
x = 0,
y = 0,
deltaX = 1920,
deltaY = 1080,
title = "Rootex"
}
6 changes: 3 additions & 3 deletions game/assets/test/cube.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ Entity = {
Components = {
TransformComponent = {
m_Position = {x = 0, y = 0, z = 0},
m_Rotation = {yaw = 0, pitch = 0, roll = 0},
m_Rotation = {yaw = 3.14, pitch = 0, roll = 0},
m_Scale = {x = 10, y = 10, z = 1}
},
DiffuseVisualComponent = {
VisualComponent = {
resFile = "game/assets/test/cube.obj",
texturePath = "game/assets/test/yellow.png"
color = {r = 0.2, g = 1.0, b = 0.2, a = 1.0}
}
}
}
2 changes: 1 addition & 1 deletion game/assets/test/spot_light.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Entity = {
attLin = 0.045,
attQuad = 0.0075,
attConst = 1.0,
range = 10.0,
range = 100.0,
diffuseIntensity = 1.0,
diffuseColor = {r = 1.0, g = 1.0, b = 1.0, a = 1.0},
ambientColor = {r = 0.05, g = 0.05, b = 0.05, a = 1.0},
Expand Down
Loading

0 comments on commit e3cfed5

Please sign in to comment.