-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathimapp.cpp
224 lines (180 loc) · 4.66 KB
/
imapp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// imapp: standalone application starter kit
#include "imgui.h"
#include "imapp.h"
#include "imapp_internal.h"
#ifdef IMAPP_SYSTEM_EMSCRIPTEN
#include <emscripten.h>
#endif
#ifdef IMAPP_STB_NAMESPACE
namespace IMAPP_STB_NAMESPACE
{
#endif
#define STB_IMAGE_IMPLEMENTATION
#ifdef IMAPP_STB_IMAGE_FILENAME
#include IMAPP_STB_IMAGE_FILENAME
#else
#include "imapp_stb_image.h"
#endif
#ifdef IMAPP_STB_NAMESPACE
} // namespace ImStb
using namespace IMAPP_STB_NAMESPACE;
#endif
namespace
{
ImGuiContext *root_context = NULL;
ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
bool done = false;
void (*main_loop)(void*) = NULL;
bool SetupImGui(ImGuiConfigFlags flags)
{
// Setup Dear ImGui context
IMGUI_CHECKVERSION();
root_context = ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
io.ConfigFlags |= flags;
#ifdef IMAPP_SYSTEM_EMSCRIPTEN
// For an Emscripten build we are disabling file-system access, so let's not attempt to do a fopen() of the imgui.ini file.
// You may manually call LoadIniSettingsFromMemory() to load settings from your own storage.
io.IniFilename = NULL;
#endif
return root_context != NULL;
}
void CleanupImGui()
{
ImGui::DestroyContext();
root_context = NULL;
}
void MainLoop(void* arg)
{
ImApp::BeginFrame();
if (main_loop) main_loop(arg);
ImApp::EndFrame();
}
} // namespace
namespace ImApp
{
bool BeginApplication(const char* name, const ImVec2& size, ImGuiConfigFlags flags)
{
bool succeeded = false;
if (!SetupPlatform(name, size))
{
fprintf(stderr, "Failed to Setup Platform Bindings!\n");
}
else if (!SetupRenderer())
{
fprintf(stderr, "Failed to Setup Renderer Bindings!\n");
}
else if (!SetupImGui(flags))
{
fprintf(stderr, "Failed to Setup Dear ImGui!\n");
}
else if (!InitPlatform())
{
fprintf(stderr, "Failed to Init Platform Bindings!\n");
}
else if (!InitRenderer())
{
fprintf(stderr, "Failed to Init Renderer Bindings!\n");
}
else
{
succeeded = true;
}
return succeeded;
}
void EndApplication()
{
// Cleanup
CleanupRenderer();
CleanupPlatform();
CleanupImGui();
ShutdownRenderer();
ShutdownPlatform();
}
bool BeginFrame()
{
ProcessEventPlatform();
// Start the Dear ImGui frame
BeginFrameRenderer();
BeginFramePlatform();
ImGui::NewFrame();
return !IsRequestedQuit();
}
void EndFrame()
{
// Rendering
ImGui::Render();
EndFrameRenderer(clear_color);
EndFramePlatform();
}
void StartMainLoop(void (*func)(void*), void* user_data)
{
if (func == NULL) return;
main_loop = func;
#ifdef IMAPP_SYSTEM_EMSCRIPTEN
// This function call won't return, and will engage in an infinite loop, processing events from the browser, and dispatching them.
emscripten_set_main_loop_arg(MainLoop, user_data, 0, true);
#else
while (!IsRequestedQuit())
{
MainLoop(user_data);
}
#endif
}
void RequestQuit()
{
done = true;
}
void CancelQuit()
{
done = false;
}
bool IsRequestedQuit()
{
return done;
}
void SetClearColor(const ImVec4& col)
{
clear_color = col;
}
void StyleViewport()
{
#ifdef IMGUI_HAS_VIEWPORT
// When viewports are enabled we tweak WindowRounding/WindowBg so platform windows can look identical to regular ones.
ImGuiStyle& style = ImGui::GetStyle();
if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
{
style.WindowRounding = 0.0f;
style.Colors[ImGuiCol_WindowBg].w = 1.0f;
}
#endif
}
bool LoadTextureFromFile(const char* filename, ImTextureID* out_texture_id, int* out_width, int* out_height)
{
// Load from file
int image_width = 0;
int image_height = 0;
unsigned char* image_data = stbi_load(filename, &image_width, &image_height, NULL, 4);
if (image_data == NULL)
return false;
bool ret = CreateTexture(image_data, image_width, image_height, out_texture_id);
stbi_image_free(image_data);
if (out_width) *out_width = image_width;
if (out_height) *out_height = image_height;
return ret;
}
bool LoadTextureFromMemory(const unsigned char* data, int size, ImTextureID* out_texture_id, int* out_width, int* out_height)
{
// Load from memory
int image_width = 0;
int image_height = 0;
unsigned char* image_data = stbi_load_from_memory(data, size, &image_width, &image_height, NULL, 4);
if (image_data == NULL)
return false;
bool ret = CreateTexture(image_data, image_width, image_height, out_texture_id);
stbi_image_free(image_data);
if (out_width) *out_width = image_width;
if (out_height) *out_height = image_height;
return ret;
}
}