Skip to content

Commit

Permalink
Fixed the build
Browse files Browse the repository at this point in the history
  • Loading branch information
cmaughan committed Feb 25, 2022
1 parent 376c10d commit 0f623e8
Show file tree
Hide file tree
Showing 16 changed files with 237 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "demo/zep"]
path = demo/zep
url = https://github.com/cmaughan/zep
26 changes: 25 additions & 1 deletion source/CMakeLists.txt → demo/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,28 @@ find_package(Vulkan REQUIRED)
find_package(Freetype REQUIRED)
find_package(sdl2 REQUIRED)

# Set this if we are sitting on SDL
add_definitions(-DZEP_USE_SDL)

# Set this to get the default file system
add_definitions(-DZEP_FEATURE_CPP_FILE_SYSTEM)

# Set this to use a lib instead of the single header build
#set (ZEP_LIB 1)

if (DEFINED ZEP_LIB)
add_subdirectory(zep/src)
else()
add_definitions(-DZEP_SINGLE_HEADER=1)
endif()

set(APP_ROOT ${CMAKE_CURRENT_LIST_DIR})
configure_file(${CMAKE_CURRENT_LIST_DIR}/cmake/config_app.h.cmake ${CMAKE_BINARY_DIR}/config_app.h)

set(SOURCES src/main.cpp)
set(SOURCES src/main.cpp
src/editor.cpp
include/editor.h)

if (WIN32)
set(SOURCES ${SOURCES}
src/app.manifest)
Expand All @@ -36,6 +54,12 @@ target_include_directories(main
PRIVATE
${CMAKE_CURRENT_LIST_DIR}
${CMAKE_BINARY_DIR}
zep/include
include
)

target_link_libraries(main PRIVATE imgui::imgui SDL2::SDL2-static SDL2::SDL2main freetype)

if (DEFINED ZEP_LIB)
target_link_libraries(main PRIVATE Zep::Zep)
endif()
3 changes: 3 additions & 0 deletions demo/build.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
cd build
cmake --build . --config Debug
cd ..
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#cmakedefine APPLICATION_NAME "${APPLICATION_NAME}"
#cmakedefine APPLICATION_VERSION "${APPLICATION_VERSION}"
#cmakedefine APP_ROOT "${APP_ROOT}"
#cmakedefine ZEP_SINGLE_HEADER "${ZEP_SINGLE_HEADER}"


File renamed without changes.
11 changes: 11 additions & 0 deletions demo/include/editor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#include <zep.h>

// Helpers to create zep editor
Zep::ZepEditor& zep_get_editor();
void zep_init(const Zep::NVec2f& pixelScale);
void zep_update();
void zep_show(const Zep::NVec2i& displaySize);
void zep_destroy();
void zep_load(const Zep::ZepPath& file);
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
150 changes: 150 additions & 0 deletions demo/src/editor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@

#if ZEP_SINGLE_HEADER == 1
#define ZEP_SINGLE_HEADER_BUILD
#endif

#include "editor.h"
#include <functional>
#include <filesystem>
#include "config_app.h"

namespace fs = std::filesystem;

using namespace Zep;

using cmdFunc = std::function<void(const std::vector<std::string>&)>;
class ZepCmd : public ZepExCommand
{
public:
ZepCmd(ZepEditor& editor, const std::string name, cmdFunc fn)
: ZepExCommand(editor)
, m_name(name)
, m_func(fn)
{
}

virtual void Run(const std::vector<std::string>& args) override
{
m_func(args);
}

virtual const char* ExCommandName() const override
{
return m_name.c_str();
}

private:
std::string m_name;
cmdFunc m_func;
};

struct ZepWrapper : public Zep::IZepComponent
{
ZepWrapper(const fs::path& root_path, const Zep::NVec2f& pixelScale, std::function<void(std::shared_ptr<Zep::ZepMessage>)> fnCommandCB)
: zepEditor(Zep::ZepPath(root_path.string()), pixelScale)
, Callback(fnCommandCB)
{
zepEditor.RegisterCallback(this);

}

virtual Zep::ZepEditor& GetEditor() const override
{
return (Zep::ZepEditor&)zepEditor;
}

virtual void Notify(std::shared_ptr<Zep::ZepMessage> message) override
{
Callback(message);

return;
}

virtual void HandleInput()
{
zepEditor.HandleInput();
}

Zep::ZepEditor_ImGui zepEditor;
std::function<void(std::shared_ptr<Zep::ZepMessage>)> Callback;
};

std::shared_ptr<ZepWrapper> spZep;

void zep_init(const Zep::NVec2f& pixelScale)
{
// Initialize the editor and watch for changes
spZep = std::make_shared<ZepWrapper>(APP_ROOT, Zep::NVec2f(pixelScale.x, pixelScale.y), [](std::shared_ptr<ZepMessage> spMessage) -> void {
});

auto& display = spZep->GetEditor().GetDisplay();
auto pImFont = ImGui::GetIO().Fonts[0].Fonts[0];
auto pixelHeight = pImFont->FontSize;
display.SetFont(ZepTextType::UI, std::make_shared<ZepFont_ImGui>(display, pImFont, int(pixelHeight)));
display.SetFont(ZepTextType::Text, std::make_shared<ZepFont_ImGui>(display, pImFont, int(pixelHeight)));
display.SetFont(ZepTextType::Heading1, std::make_shared<ZepFont_ImGui>(display, pImFont, int(pixelHeight * 1.5)));
display.SetFont(ZepTextType::Heading2, std::make_shared<ZepFont_ImGui>(display, pImFont, int(pixelHeight * 1.25)));
display.SetFont(ZepTextType::Heading3, std::make_shared<ZepFont_ImGui>(display, pImFont, int(pixelHeight * 1.125)));
}

void zep_update()
{
if (spZep)
{
spZep->GetEditor().RefreshRequired();
}
}

void zep_destroy()
{
spZep.reset();
}

ZepEditor& zep_get_editor()
{
return spZep->GetEditor();
}

void zep_load(const Zep::ZepPath& file)
{
auto pBuffer = zep_get_editor().InitWithFileOrDir(file);
}

void zep_show(const Zep::NVec2i& displaySize)
{
bool show = true;
ImGui::SetNextWindowSize(ImVec2(displaySize.x, displaySize.y), ImGuiCond_FirstUseEver);
if (!ImGui::Begin("Zep", &show, ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_MenuBar))
{
ImGui::End();
return;
}

auto min = ImGui::GetCursorScreenPos();
auto max = ImGui::GetContentRegionAvail();
if (max.x <= 0)
max.x = 1;
if (max.y <= 0)
max.y = 1;
ImGui::InvisibleButton("ZepContainer", max);

// Fill the window
max.x = min.x + max.x;
max.y = min.y + max.y;

spZep->zepEditor.SetDisplayRegion(Zep::NVec2f(min.x, min.y), Zep::NVec2f(max.x, max.y));
spZep->zepEditor.Display();
bool zep_focused = ImGui::IsWindowFocused();
if (zep_focused)
{
spZep->zepEditor.HandleInput();
}

// TODO: A Better solution for this; I think the audio graph is creating a new window and stealing focus
static int focus_count = 0;
if (focus_count++ < 2)
{
ImGui::SetWindowFocus();
}
ImGui::End();
}
25 changes: 23 additions & 2 deletions source/src/main.cpp → demo/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@
#include <vulkan/vulkan.h>

#include <filesystem>

#include "config_app.h"

namespace fs = std::filesystem;

#include "editor.h"

//#define IMGUI_UNLIMITED_FRAME_RATE
#ifdef _DEBUG
#define IMGUI_VULKAN_DEBUG_REPORT
Expand Down Expand Up @@ -356,7 +359,7 @@ int main(int, char**)

// Setup window
SDL_WindowFlags window_flags = (SDL_WindowFlags)(SDL_WINDOW_VULKAN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
SDL_Window* window = SDL_CreateWindow("Dear ImGui SDL2+Vulkan example", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, window_flags);
SDL_Window* window = SDL_CreateWindow("ImGui+SDL2+Vulkan+Zep example", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, window_flags);
if (!window)
{
printf("%s\n", SDL_GetError());
Expand Down Expand Up @@ -443,7 +446,7 @@ int main(int, char**)
//IM_ASSERT(font != NULL);

// Note: Adjust font size as appropriate!
auto fontPath = fs::path(APP_ROOT) / "run_tree" / "fonts" / "Roboto-Regular.ttf";
auto fontPath = fs::path(APP_ROOT) / "run_tree" / "fonts" / "Cousine-Regular.ttf";
io.Fonts->AddFontFromFileTTF(fontPath.string().c_str(), 26);

// Upload Fonts
Expand Down Expand Up @@ -479,6 +482,7 @@ int main(int, char**)
// Our state
bool show_demo_window = true;
bool show_another_window = false;
bool z_init = false;
ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);

// Main loop
Expand Down Expand Up @@ -519,6 +523,21 @@ int main(int, char**)
ImGui_ImplSDL2_NewFrame();
ImGui::NewFrame();

if (!z_init)
{
// Called once the fonts/device is guaranteed setup
zep_init(Zep::NVec2f(1.0f, 1.0f));
zep_load(Zep::ZepPath(APP_ROOT) / "src" / "main.cpp");
z_init = true;
}

// Required for CTRL+P and flashing cursor.
zep_update();

// Just show it
static Zep::NVec2i size = Zep::NVec2i(640, 480);
zep_show(size);

// 1. Show the big demo window (Most of the sample code is in ImGui::ShowDemoWindow()! You can browse its code to learn more about Dear ImGui!).
if (show_demo_window)
ImGui::ShowDemoWindow(&show_demo_window);
Expand Down Expand Up @@ -580,6 +599,8 @@ int main(int, char**)
}

// Cleanup
zep_destroy();

err = vkDeviceWaitIdle(g_Device);
check_vk_result(err);
ImGui_ImplVulkan_Shutdown();
Expand Down
1 change: 1 addition & 0 deletions demo/zep
Submodule zep added at 667a4f
20 changes: 20 additions & 0 deletions demo/zep.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[editor]

# Vertical Scroller: 0 = off, 1 = on if necessary, 2 = on always
show_scrollbar = 0

# Style: normal, or minimal for a live-coding style of editor
style = "normal"
show_line_numbers = true
show_indicator_region = true
autohide_command_region = false
cursor_line_solid = true
short_tab_names = false

line_margin_top = 1
line_margin_bottom = 1
widget_margin_top = 5
widget_margin_bottom = 5

background_fade_time = 20
background_fade_wait = 5

0 comments on commit 0f623e8

Please sign in to comment.