Skip to content

Commit 27375e2

Browse files
committed
Application: Convert to inheritable class
Adding things to application became more and more annoying. To avoid need of patching every example and two backends every time something change application interface was converted to inheritable class. As for now, there is one main app for all examples.
1 parent 50a93f8 commit 27375e2

33 files changed

+5056
-3107
lines changed

docs/README.md

+56-32
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ Please report issues or questions if something isn't clear.
5353

5454
* Vanilla ImGui 1.72+
5555
* C++14
56-
56+
5757
### Dependencies for examples:
5858
* https://github.com/thedmd/imgui/tree/feature/layout (used in blueprints sample only)
59-
59+
6060
### Optional extension you can pull into your local copy of ImGui node editor can take advantage of:
6161
* https://github.com/thedmd/imgui/tree/feature/draw-list-fringe-scale (for sharp rendering, while zooming)
6262
* https://github.com/thedmd/imgui/tree/feature/extra-keys (for extra shortcuts)
@@ -89,44 +89,68 @@ Main node editor header is located in [imgui_node_editor.h](../imgui_node_editor
8989
Minimal example of one node can be found in [simple-example.cpp](../examples/simple-example/simple-example.cpp).
9090
Press 'F' in editor to focus on editor content if you see only grid.
9191
```cpp
92-
# include <application.h>
92+
# include <imgui.h>
9393
# include <imgui_node_editor.h>
94+
# include <application.h>
9495

9596
namespace ed = ax::NodeEditor;
9697

97-
static ed::EditorContext* g_Context = nullptr;
98-
99-
void Application_Initialize()
100-
{
101-
g_Context = ed::CreateEditor();
102-
}
103-
104-
void Application_Finalize()
98+
struct Example:
99+
public Application
105100
{
106-
ed::DestroyEditor(g_Context);
107-
}
108-
109-
void Application_Frame()
101+
using Application::Application;
102+
103+
void OnStart() override
104+
{
105+
ed::Config config;
106+
config.SettingsFile = "Simple.json";
107+
m_Context = ed::CreateEditor(&config);
108+
}
109+
110+
void OnStop() override
111+
{
112+
ed::DestroyEditor(m_Context);
113+
}
114+
115+
void OnFrame(float deltaTime) override
116+
{
117+
auto& io = ImGui::GetIO();
118+
119+
ImGui::Text("FPS: %.2f (%.2gms)", io.Framerate, io.Framerate ? 1000.0f / io.Framerate : 0.0f);
120+
121+
ImGui::Separator();
122+
123+
ed::SetCurrentEditor(m_Context);
124+
ed::Begin("My Editor", ImVec2(0.0, 0.0f));
125+
int uniqueId = 1;
126+
// Start drawing nodes.
127+
ed::BeginNode(uniqueId++);
128+
ImGui::Text("Node A");
129+
ed::BeginPin(uniqueId++, ed::PinKind::Input);
130+
ImGui::Text("-> In");
131+
ed::EndPin();
132+
ImGui::SameLine();
133+
ed::BeginPin(uniqueId++, ed::PinKind::Output);
134+
ImGui::Text("Out ->");
135+
ed::EndPin();
136+
ed::EndNode();
137+
ed::End();
138+
ed::SetCurrentEditor(nullptr);
139+
140+
//ImGui::ShowMetricsWindow();
141+
}
142+
143+
ed::EditorContext* m_Context = nullptr;
144+
};
145+
146+
int Main(int argc, char** argv)
110147
{
111-
ed::SetCurrentEditor(g_Context);
112-
113-
ed::Begin("My Editor");
114-
115-
int uniqueId = 1;
148+
Example exampe("Simple", argc, argv);
116149

117-
// Start drawing nodes.
118-
ed::BeginNode(uniqueId++);
119-
ImGui::Text("Node A");
120-
ed::BeginPin(uniqueId++, ed::PinKind::Input);
121-
ImGui::Text("-> In");
122-
ed::EndPin();
123-
ImGui::SameLine();
124-
ed::BeginPin(uniqueId++, ed::PinKind::Output);
125-
ImGui::Text("Out ->");
126-
ed::EndPin();
127-
ed::EndNode();
150+
if (exampe.Create())
151+
return exampe.Run();
128152

129-
ed::End();
153+
return 0;
130154
}
131155
```
132156

examples/application/CMakeLists.txt

+62-29
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,20 @@ project(application)
22

33
set(_Application_Sources
44
include/application.h
5+
source/application.cpp
6+
source/entry_point.cpp
7+
source/imgui_extra_keys.h
8+
source/config.h.in
9+
source/setup.h
10+
source/platform.h
11+
source/platform_win32.cpp
12+
source/platform_glfw.cpp
13+
source/renderer.h
14+
source/renderer_dx11.cpp
15+
source/renderer_ogl3.cpp
516
)
617

7-
if (WIN32)
8-
list(APPEND _Application_Sources
9-
source/dx11/entry.cpp
10-
source/dx11/imgui_impl_dx11.cpp
11-
source/dx11/imgui_impl_dx11.h
12-
source/dx11/imgui_impl_win32.cpp
13-
source/dx11/imgui_impl_win32.h
14-
)
15-
else()
16-
find_package(glfw3 3 REQUIRED)
17-
find_package(OpenGL REQUIRED)
18-
19-
list(APPEND _Application_Sources
20-
source/glfw/entry.cpp
21-
source/glfw/imgui_impl_glfw_gl3.cpp
22-
source/glfw/imgui_impl_glfw_gl3.h
23-
)
24-
endif()
25-
26-
27-
source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}" FILES ${_Application_Sources})
28-
29-
add_library(application STATIC ${_Application_Sources})
18+
add_library(application STATIC)
3019

3120
target_include_directories(application PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
3221

@@ -37,6 +26,13 @@ target_link_libraries(application PUBLIC imgui)
3726
target_link_libraries(application PRIVATE stb_image ScopeGuard)
3827

3928
if (WIN32)
29+
list(APPEND _Application_Sources
30+
source/imgui_impl_dx11.cpp
31+
source/imgui_impl_dx11.h
32+
source/imgui_impl_win32.cpp
33+
source/imgui_impl_win32.h
34+
)
35+
4036
set(_DXSDK_Dir ${IMGUI_NODE_EDITOR_ROOT_DIR}/external/DXSDK)
4137
set(_DXSDK_Arch x86)
4238
if (${CMAKE_SIZEOF_VOID_P} EQUAL 8)
@@ -57,20 +53,57 @@ if (WIN32)
5753

5854
target_link_libraries(application PRIVATE d3d11.lib d3dcompiler.lib d3dx11)
5955
else()
60-
target_include_directories(application PRIVATE ${OPENGL_INCLUDE_DIR})
61-
62-
find_package(gl3w REQUIRED)
63-
target_link_libraries(application PRIVATE ${OPENGL_gl_LIBRARY} gl3w glfw)
56+
find_package(OpenGL REQUIRED)
57+
find_package(glfw3 3 REQUIRED)
6458

6559
if (APPLE)
6660
target_link_libraries(application PRIVATE
6761
"-framework CoreFoundation"
6862
"-framework Cocoa"
69-
"-framework OpenGL"
7063
"-framework IOKit"
7164
"-framework CoreVideo"
7265
)
7366
endif()
7467
endif()
7568

76-
set_property(TARGET application PROPERTY FOLDER "examples")
69+
if (OpenGL_FOUND)
70+
set(HAVE_OPENGL YES)
71+
72+
find_package(gl3w REQUIRED)
73+
target_include_directories(application PRIVATE ${OPENGL_INCLUDE_DIR})
74+
target_link_libraries(application PRIVATE ${OPENGL_gl_LIBRARY} gl3w)
75+
list(APPEND _Application_Sources
76+
source/imgui_impl_opengl3.cpp
77+
source/imgui_impl_opengl3.h
78+
)
79+
endif()
80+
81+
if (glfw3_FOUND)
82+
set(HAVE_GLFW3 YES)
83+
84+
list(APPEND _Application_Sources
85+
source/imgui_impl_glfw.cpp
86+
source/imgui_impl_glfw.h
87+
)
88+
target_link_libraries(application PRIVATE
89+
glfw
90+
)
91+
endif()
92+
93+
configure_file(
94+
source/config.h.in
95+
${CMAKE_CURRENT_BINARY_DIR}/source/config.h
96+
)
97+
98+
target_compile_definitions(application PRIVATE
99+
#BACKEND_CONFIG=IMGUI_GLFW
100+
#RENDERER_CONFIG=IMGUI_OGL3
101+
)
102+
103+
target_include_directories(application PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/source)
104+
105+
source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}" FILES ${_Application_Sources})
106+
107+
target_sources(application PRIVATE ${_Application_Sources})
108+
109+
set_property(TARGET application PROPERTY FOLDER "examples")
+52-17
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,57 @@
1-
#pragma once
2-
#include <imgui.h>
1+
# pragma once
2+
# include <imgui.h>
3+
# include <string>
4+
# include <memory>
35

4-
ImFont* Application_DefaultFont();
5-
ImFont* Application_HeaderFont();
6+
struct Platform;
7+
struct Renderer;
68

7-
ImTextureID Application_LoadTexture(const char* path);
8-
ImTextureID Application_CreateTexture(const void* data, int width, int height);
9-
void Application_DestroyTexture(ImTextureID texture);
10-
int Application_GetTextureWidth(ImTextureID texture);
11-
int Application_GetTextureHeight(ImTextureID texture);
9+
struct Application
10+
{
11+
Application(const char* name);
12+
Application(const char* name, int argc, char** argv);
13+
~Application();
1214

13-
extern ImGuiWindowFlags g_ApplicationWindowFlags;
15+
bool Create(int width = -1, int height = -1);
1416

15-
void Application_SetTitle(const char* title);
16-
void Application_Quit();
17+
int Run();
1718

18-
const char* Application_GetName();
19-
void Application_Initialize();
20-
void Application_Finalize();
21-
void Application_Frame();
22-
bool Application_Close();
19+
void SetTitle(const char* title);
20+
21+
bool Close();
22+
void Quit();
23+
24+
const std::string& GetName() const;
25+
26+
ImFont* DefaultFont() const;
27+
ImFont* HeaderFont() const;
28+
29+
ImTextureID LoadTexture(const char* path);
30+
ImTextureID CreateTexture(const void* data, int width, int height);
31+
void DestroyTexture(ImTextureID texture);
32+
int GetTextureWidth(ImTextureID texture);
33+
int GetTextureHeight(ImTextureID texture);
34+
35+
virtual void OnStart() {}
36+
virtual void OnStop() {}
37+
virtual void OnFrame(float deltaTime) {}
38+
39+
virtual ImGuiWindowFlags GetWindowFlags() const;
40+
41+
virtual bool CanClose() { return true; }
42+
43+
private:
44+
void RecreateFontAtlas();
45+
46+
void Frame();
47+
48+
std::string m_Name;
49+
std::string m_IniFilename;
50+
std::unique_ptr<Platform> m_Platform;
51+
std::unique_ptr<Renderer> m_Renderer;
52+
ImGuiContext* m_Context = nullptr;
53+
ImFont* m_DefaultFont = nullptr;
54+
ImFont* m_HeaderFont = nullptr;
55+
};
56+
57+
int Main(int argc, char** argv);

0 commit comments

Comments
 (0)