forked from danilw/nanogui-GLES-wasm
-
Notifications
You must be signed in to change notification settings - Fork 2
/
common.cpp
344 lines (300 loc) · 9.76 KB
/
common.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
nanogui/nanogui.cpp -- Basic initialization and utility routines
NanoGUI was developed by Wenzel Jakob <[email protected]>.
The widget drawing code is based on the NanoVG demo application
by Mikko Mononen.
All rights reserved. Use of this source code is governed by a
BSD-style license that can be found in the LICENSE.txt file.
*/
#include <nanogui/screen.h>
#if defined(_WIN32)
# include <windows.h>
#endif
#include <nanogui/opengl.h>
#include <map>
//#include <thread>
#include <chrono>
#include <iostream>
#if !defined(_WIN32)
# include <locale.h>
# include <signal.h>
# include <sys/dir.h>
#endif
NAMESPACE_BEGIN(nanogui)
extern std::map<GLFWwindow *, Screen *> __nanogui_screens;
#if defined(__APPLE__)
extern void disable_saved_application_state_osx();
#endif
void init() {
#if !defined(_WIN32)
/* Avoid locale-related number parsing issues */
setlocale(LC_NUMERIC, "C");
#endif
#if defined(__APPLE__)
disable_saved_application_state_osx();
#endif
glfwSetErrorCallback(
[](int error, const char *descr) {
if (error == GLFW_NOT_INITIALIZED)
return; /* Ignore */
std::cerr << "GLFW error " << error << ": " << descr << std::endl;
}
);
if (!glfwInit())
throw std::runtime_error("Could not initialize GLFW!");
glfwSetTime(0);
}
static bool mainloop_active = false;
void mainloop(int refresh) {
/* if (mainloop_active)
throw std::runtime_error("Main loop is already running!");
*/
mainloop_active = true;
//std::thread refresh_thread;
if (refresh > 0) {
/* If there are no mouse/keyboard events, try to refresh the
view roughly every 50 ms (default); this is to support animations
such as progress bars while keeping the system load
reasonably low */
/*refresh_thread = std::thread(
[refresh]() {
std::chrono::milliseconds time(refresh);
//while (mainloop_active) {
std::this_thread::sleep_for(time);
glfwPostEmptyEvent();
//}
}
);*/
}
try
{
//while (mainloop_active)
{
int numScreens = 0;
for (auto kv : __nanogui_screens) {
Screen *screen = kv.second;
if (!screen->visible()) {
continue;
} else if (glfwWindowShouldClose(screen->glfwWindow())) {
screen->setVisible(false);
continue;
}
screen->drawAll();
numScreens++;
}
if (numScreens == 0) {
/* Give up if there was nothing to draw */
mainloop_active = false;
return;
}
/* Wait for mouse/keyboard or empty refresh events */
glfwWaitEvents();
}
/* Process events once more */
glfwPollEvents();
} catch (const std::exception &e) {
std::cerr << "Caught exception in main loop: " << e.what() << std::endl;
leave();
}
/*if (refresh > 0)
refresh_thread.join();*/
}
void leave() {
mainloop_active = false;
}
bool active() {
return mainloop_active;
}
void shutdown() {
glfwTerminate();
}
std::array<char, 8> utf8(int c) {
std::array<char, 8> seq;
int n = 0;
if (c < 0x80) n = 1;
else if (c < 0x800) n = 2;
else if (c < 0x10000) n = 3;
else if (c < 0x200000) n = 4;
else if (c < 0x4000000) n = 5;
else if (c <= 0x7fffffff) n = 6;
seq[n] = '\0';
switch (n) {
case 6: seq[5] = 0x80 | (c & 0x3f); c = c >> 6; c |= 0x4000000;
case 5: seq[4] = 0x80 | (c & 0x3f); c = c >> 6; c |= 0x200000;
case 4: seq[3] = 0x80 | (c & 0x3f); c = c >> 6; c |= 0x10000;
case 3: seq[2] = 0x80 | (c & 0x3f); c = c >> 6; c |= 0x800;
case 2: seq[1] = 0x80 | (c & 0x3f); c = c >> 6; c |= 0xc0;
case 1: seq[0] = c;
}
return seq;
}
int __nanogui_get_image(NVGcontext *ctx, const std::string &name, uint8_t *data, uint32_t size) {
static std::map<std::string, int> iconCache;
auto it = iconCache.find(name);
if (it != iconCache.end())
return it->second;
int iconID = nvgCreateImageMem(ctx, 0, data, size);
if (iconID == 0)
throw std::runtime_error("Unable to load resource data.");
iconCache[name] = iconID;
return iconID;
}
std::vector<std::pair<int, std::string>>
loadImageDirectory(NVGcontext *ctx, const std::string &path) {
std::vector<std::pair<int, std::string> > result;
#if !defined(_WIN32)
DIR *dp = opendir(path.c_str());
if (!dp)
throw std::runtime_error("Could not open image directory!");
struct dirent *ep;
while ((ep = readdir(dp))) {
const char *fname = ep->d_name;
#else
WIN32_FIND_DATA ffd;
std::string searchPath = path + "/*.*";
HANDLE handle = FindFirstFileA(searchPath.c_str(), &ffd);
if (handle == INVALID_HANDLE_VALUE)
throw std::runtime_error("Could not open image directory!");
do {
const char *fname = ffd.cFileName;
#endif
if (strstr(fname, "png") == nullptr)
continue;
std::string fullName = path + "/" + std::string(fname);
int img = nvgCreateImage(ctx, fullName.c_str(), 0);
if (img == 0)
throw std::runtime_error("Could not open image data!");
result.push_back(
std::make_pair(img, fullName.substr(0, fullName.length() - 4)));
#if !defined(_WIN32)
}
closedir(dp);
#else
} while (FindNextFileA(handle, &ffd) != 0);
FindClose(handle);
#endif
return result;
}
std::string file_dialog(const std::vector<std::pair<std::string, std::string>> &filetypes, bool save) {
auto result = file_dialog(filetypes, save, false);
return result.empty() ? "" : result.front();
}
#if !defined(__APPLE__)
std::vector<std::string> file_dialog(const std::vector<std::pair<std::string, std::string>> &filetypes, bool save, bool multiple) {
static const int FILE_DIALOG_MAX_BUFFER = 16384;
if (save && multiple) {
throw std::invalid_argument("save and multiple must not both be true.");
}
#if defined(_WIN32)
OPENFILENAME ofn;
ZeroMemory(&ofn, sizeof(OPENFILENAME));
ofn.lStructSize = sizeof(OPENFILENAME);
char tmp[FILE_DIALOG_MAX_BUFFER];
ofn.lpstrFile = tmp;
ZeroMemory(tmp, FILE_DIALOG_MAX_BUFFER);
ofn.nMaxFile = FILE_DIALOG_MAX_BUFFER;
ofn.nFilterIndex = 1;
std::string filter;
if (!save && filetypes.size() > 1) {
filter.append("Supported file types (");
for (size_t i = 0; i < filetypes.size(); ++i) {
filter.append("*.");
filter.append(filetypes[i].first);
if (i + 1 < filetypes.size())
filter.append(";");
}
filter.append(")");
filter.push_back('\0');
for (size_t i = 0; i < filetypes.size(); ++i) {
filter.append("*.");
filter.append(filetypes[i].first);
if (i + 1 < filetypes.size())
filter.append(";");
}
filter.push_back('\0');
}
for (auto pair : filetypes) {
filter.append(pair.second);
filter.append(" (*.");
filter.append(pair.first);
filter.append(")");
filter.push_back('\0');
filter.append("*.");
filter.append(pair.first);
filter.push_back('\0');
}
filter.push_back('\0');
ofn.lpstrFilter = filter.data();
if (save) {
ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT;
if (GetSaveFileNameA(&ofn) == FALSE)
return {};
} else {
ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
if (multiple)
ofn.Flags |= OFN_ALLOWMULTISELECT;
if (GetOpenFileNameA(&ofn) == FALSE)
return {};
}
size_t i = 0;
std::vector<std::string> result;
while (tmp[i] != '\0') {
result.emplace_back(&tmp[i]);
i += result.back().size() + 1;
}
if (result.size() > 1) {
for (i = 1; i < result.size(); ++i) {
result[i] = result[0] + "\\" + result[i];
}
result.erase(begin(result));
}
return result;
#else
char buffer[FILE_DIALOG_MAX_BUFFER];
buffer[0] = '\0';
std::string cmd = "zenity --file-selection ";
// The safest separator for multiple selected paths is /, since / can never occur
// in file names. Only where two paths are concatenated will there be two / following
// each other.
if (multiple)
cmd += "--multiple --separator=\"/\" ";
if (save)
cmd += "--save ";
cmd += "--file-filter=\"";
for (auto pair : filetypes)
cmd += "\"*." + pair.first + "\" ";
cmd += "\"";
FILE *output = popen(cmd.c_str(), "r");
if (output == nullptr)
throw std::runtime_error("popen() failed -- could not launch zenity!");
while (fgets(buffer, FILE_DIALOG_MAX_BUFFER, output) != NULL)
;
pclose(output);
std::string paths(buffer);
paths.erase(std::remove(paths.begin(), paths.end(), '\n'), paths.end());
std::vector<std::string> result;
while (!paths.empty()) {
size_t end = paths.find("//");
if (end == std::string::npos) {
result.emplace_back(paths);
paths = "";
} else {
result.emplace_back(paths.substr(0, end));
paths = paths.substr(end + 1);
}
}
return result;
#endif
}
#endif
void Object::decRef(bool dealloc) const noexcept {
--m_refCount;
if (m_refCount == 0 && dealloc) {
delete this;
} else if (m_refCount < 0) {
fprintf(stderr, "Internal error: Object reference count < 0!\n");
abort();
}
}
Object::~Object() { }
NAMESPACE_END(nanogui)