-
Notifications
You must be signed in to change notification settings - Fork 219
/
main_opengl.cpp
466 lines (395 loc) · 12.8 KB
/
main_opengl.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/**
* OpenBoardView
*
* Copyright inflex 2016 (Paul Daniels)
* Copyright chloridite 2016
*
* https://github.com/OpenBoardView/OpenBoardView
*
*/
#include "platform.h" // Should be kept first
#include "utils.h"
#include "version.h"
#include "BoardView.h"
#include "history.h"
#include "FileFormats/FZFile.h"
#include "confparse.h"
#include "resource.h"
#include <SDL.h>
#include <chrono>
#include <deque>
#include <memory>
#include <cstdio>
#include <string>
#include <sys/stat.h>
#include <sys/types.h>
#include <thread>
#ifndef _MSC_VER
#include <unistd.h>
#endif
// Rendering stuff
#include "Renderers/Renderers.h"
#include "filesystem_impl.h"
struct globals {
char *input_file;
char *config_file;
bool slowCPU;
int width;
int height;
int dpi;
double font_size;
bool debug;
Renderers::Renderer renderer;
globals() {
this->input_file = NULL;
this->config_file = NULL;
this->slowCPU = false;
this->width = 0;
this->height = 0;
this->dpi = 0;
this->font_size = 0.0f;
this->debug = false;
this->renderer = Renderers::Renderer::DEFAULT;
}
};
static SDL_Window *window = nullptr;
char help[] =
" [-h] [-V] [-l] [-c <config file>] [-i <intput file>] [-x <width>] [-y <height>] [-z <fontsize>] [-p <dpi>] [-r <renderer>] [-d]\n\
-h : This help\n\
-V : Version information\n\
-l : slow CPU mode, disables AA and other items to try provide more FPS\n\
-c <config file> : alternative configuration file (default is ~/.config/" OBV_NAME
"/obv.conf)\n\
-i <input file> : board file to load\n\
-x <width> : Set window width\n\
-y <height> : Set window height\n\
-z <pixels> : Set font size\n\
-p <dpi> : Set the dpi\n\
-r <renderer> : Set the renderer [ OPENGL1 = 1; OPENGL3 = 2; OPENGLES2 = 3 ]\n\
-d : Debug mode\n\
";
int parse_parameters(int argc, char **argv, struct globals *g) {
int param;
/**
* Decode the input parameters.
* Yes, I know, I should do this using gnu params etc.
*/
for (param = 1; param < argc; param++) {
char *p = argv[param];
// macOS quirk: Ignore psn_* parameter passed by launchd
if (strncmp(p, "-psn_", 5) == 0) {
continue;
}
if (strcmp(p, "-h") == 0) {
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "%s %s", argv[0], help);
exit(0);
}
if (strcmp(p, "-V") == 0) {
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "OFBV-BUILD: %s %s\n", OBV_BUILD, __DATE__ " " __TIME__);
exit(0);
}
if (strcmp(p, "-c") == 0) {
param++;
if ((param < argc)&&(argv[param][0] != '-')) {
g->config_file = argv[param];
} else {
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Not enough paramters for -c <config>\n\n%s %s", argv[0], help );
exit(1);
}
} else if (strcmp(p, "-i") == 0) {
param++;
if ((param < argc)&&(argv[param][0] != '-')) {
g->input_file = argv[param];
} else {
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Not enough paramters for -i <input file>\n\n%s %s", argv[0], help );
exit(1);
}
} else if (strcmp(p, "-x") == 0) {
param++;
if ((param < argc)&&(argv[param][0] != '-')) {
g->width = strtol(argv[param], NULL, 10);
} else {
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Not enough paramters for -x <window width>\n\n%s %s", argv[0], help );
exit(1);
}
} else if (strcmp(p, "-y") == 0) {
param++;
if ((param < argc)&&(argv[param][0] != '-')) {
g->height = strtol(argv[param], NULL, 10);
} else {
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Not enough paramters for -y <window height>\n\n%s %s", argv[0], help );
exit(1);
}
} else if (strcmp(p, "-z") == 0) {
param++;
if ((param < argc)&&(argv[param][0] != '-')) {
g->font_size = strtof(argv[param], NULL);
} else {
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Not enough paramters for -z <font size>\n\n%s %s", argv[0], help );
exit(1);
}
} else if (strcmp(p, "-p") == 0) {
param++;
if ((param < argc)&&(argv[param][0] != '-')) {
g->dpi = strtof(argv[param], NULL);
} else {
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Not enough paramters for -p <dpi>\n\n%s %s", argv[0], help );
exit(1);
}
} else if (strcmp(p, "-r") == 0) {
param++;
if ((param < argc)&&(argv[param][0] != '-')) {
g->renderer = Renderers::get(atoi(argv[param]));
} else {
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Not enough paramters for -r <render engine>\n\n%s %s", argv[0], help );
exit(1);
}
} else if (strcmp(p, "-l") == 0) {
g->slowCPU = true;
} else if (strcmp(p, "-d") == 0) {
g->debug = true;
} else if (argc == 2) {
/*
* When we're using file-associations, the OS usually just
* passes the filename to be loaded as the single initial
* parameter, so in this special case situation we try to
* load it.
*/
g->input_file = argv[1];
return 0;
} else {
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Unknown parameter '%s'\n\n%s %s", p, argv[0], help);
exit(1);
}
}
return 0;
}
void cleanupAndExit(int c) {
if (window) SDL_DestroyWindow(window);
SDL_Quit();
exit(c);
}
int main(int argc, char **argv) {
uint8_t sleepout;
std::string configDir;
globals g; // because some things we have to store *before* we load the config file in BoardView app.obvconf
BoardView app{};
// Log all messages
SDL_LogSetAllPriority(SDL_LOG_PRIORITY_VERBOSE);
/*
* Parse the parameters first up, store the results in the global struct.
*
* This does mean a little more redundancy between the OS builds but not
* as bad as it was before.
*
*/
parse_parameters(argc, argv, &g);
app.debug = g.debug;
// Setup SDL
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0) {
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Error: %s\n", SDL_GetError());
return -1;
}
// Enable touch gestures on multi-touch touchpads
SDL_SetHint(SDL_HINT_MOUSE_TOUCH_EVENTS, "1");
// Load the configuration file
configDir = get_user_dir(UserDir::Config);
if (!configDir.empty()) app.obvconfig.Load(configDir + "obv.conf", true);
// Load file history
std::string dataDir = get_user_dir(UserDir::Data);
if (!dataDir.empty()) {
app.fhistory.Set_filename(dataDir + "obv.history");
app.fhistory.Load();
}
// If we've chosen to override the normally found config.
if (g.config_file) app.obvconfig.Load(g.config_file, true);
// Apply the slowCPU flag if required.
app.slowCPU = g.slowCPU;
if (g.width == 0) g.width = app.obvconfig.ParseInt("windowX", 1100);
if (g.height == 0) g.height = app.obvconfig.ParseInt("windowY", 700);
if (g.renderer == Renderers::Renderer::DEFAULT) {
g.renderer = Renderers::get(app.obvconfig.ParseInt("renderer", static_cast<int>(Renderers::Preferred)));
}
// Setup window
SDL_DisplayMode current;
SDL_GetCurrentDisplayMode(0, ¤t);
window = SDL_CreateWindow(
OBV_NAME, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, g.width, g.height, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
if (window == NULL) {
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Failed to create the sdlWindow: %s\n", SDL_GetError());
cleanupAndExit(1);
}
// Needs to be done before initializing the renderer or using any of ImGui stuff
ImGui::CreateContext();
// Setup renderer
bool initialized = Renderers::initBestRenderer(g.renderer, window);
if (!initialized) {
SDL_LogError(SDL_LOG_CATEGORY_RENDER, "%s", "No renderer not available. Exiting.");
cleanupAndExit(1);
}
SDL_EventState(SDL_DROPFILE, SDL_ENABLE);
// SDL disables screen saver by default which doesn't make sense for us.
SDL_EnableScreenSaver();
ImGuiIO &io = ImGui::GetIO();
io.IniFilename = NULL;
// io.Fonts->AddFontDefault();
// Main loop
bool done = false;
bool preload_required = false;
// Set the dpi, if we've not set any parameters it'll be 0 which
// will make the ConfigParse load and set the right dpi.
app.dpi = g.dpi;
// Now that the configuration file is loaded in to BoardView, parse its settings.
app.ConfigParse();
// Preset some workable sizes
app.m_board_surface.x = g.width;
app.m_board_surface.y = g.height;
if (app.showInfoPanel) app.m_board_surface.x -= app.m_info_surface.x;
if (g.font_size == 0.0f) g.font_size = app.obvconfig.ParseDouble("fontSize", 20.0f);
g.font_size = (g.font_size * app.dpi) / 100;
{
ImGuiStyle &style = ImGui::GetStyle();
style.ScrollbarSize *= app.dpi / 100;
}
// Font selection
std::deque<std::string> fontList(
{"Liberation Sans", "DejaVu Sans", "Arial", "Helvetica", ""}); // Empty string = use system default font
std::string customFont(app.obvconfig.ParseStr("fontName", ""));
if (!customFont.empty()) fontList.push_front(customFont);
for (const auto &name : fontList) {
app.obvconfig.WriteStr("fontName", name.c_str());
#ifdef _WIN32
ImFontConfig font_cfg{};
font_cfg.FontDataOwnedByAtlas = false;
const std::vector<char> ttf = load_font(name);
if (!ttf.empty()) {
io.Fonts->AddFontFromMemoryTTF(
const_cast<void *>(reinterpret_cast<const void *>(ttf.data())), ttf.size(), g.font_size, &font_cfg);
break;
}
#else
const std::string fontpath = get_font_path(name);
if (fontpath.empty()) continue; // Font not found
if (check_fileext(fontpath, ".ttf")) { // ImGui handles only TrueType fonts so exclude anything which has a different ext
io.Fonts->AddFontFromFileTTF(fontpath.c_str(), g.font_size);
break;
}
#endif
}
// ImVec4 clear_color = ImColor(20, 20, 30);
ImVec4 clear_color = ImColor(app.m_colors.backgroundColor);
/*
* If we've asked to load a file from the command line
* then this is where we stage it to be loaded directly
* in to OBV
*/
if (g.input_file) {
preload_required = true;
}
/*
* The sleepout var keeps track of how many iterations of the main loop
* are left, without an event having happened before OBV will start to sleep
* and continue without redrawing the page.
*
* The reason we don't just sleep immediately after a non-event is because
* sometimes there are internal things that still need to be done on the next
* render (such as responding to a mouse click
*
* For now we've got this set to 3 frames which seems to work okay with OBV.
* If you find some things aren't working properly without you having to move
* the mouse or 'waking up' OBV then increase to 5 or more.
*/
sleepout = 30;
float angleacc = 0.0;
while (!done) {
SDL_Event event;
while (SDL_PollEvent(&event)) {
sleepout = 30;
Renderers::current->processEvent(event);
if (event.type == SDL_DROPFILE) {
app.LoadFile(filesystem::u8path(event.drop.file));
} else if(event.type == SDL_MULTIGESTURE && event.mgesture.numFingers == 2 && !ImGui::IsMouseDown(ImGuiMouseButton_Left)) {
//Inhibit dragging board area
app.m_dragging_token = -1;
//Rotation detected, at least 1°
if (fabs(event.mgesture.dTheta) > 3.14 / 180.0) {
angleacc += event.mgesture.dTheta;
if (angleacc >= 3.14 / 2) {
// > 90°
app.Rotate(1);
angleacc = 0.0;
} else if (angleacc <= -3.14 / 2) {
// < 90°
app.Rotate(-1);
angleacc = 0.0;
}
}
//Pinch-to-zoom
else if (fabs(event.mgesture.dDist) > 0.002) {
int w, h;
SDL_GetWindowSize(window, &w, &h);
app.Zoom(event.mgesture.x * w, event.mgesture.y * h, event.mgesture.dDist * app.zoomFactor * 10);
}
}
if (event.type == SDL_QUIT) done = true;
}
// reset rotation angle accumulator
if (ImGui::IsMouseDown(ImGuiMouseButton_Left)) {
angleacc = 0.0;
}
if (app.reloadConfig) {
app.reloadConfig = false;
app.obvconfig.Load(configDir + "obv.conf");
app.ConfigParse();
clear_color = ImColor(app.m_colors.backgroundColor);
}
if (!(sleepout--)) {
#ifdef _WIN32
Sleep(50);
#else
usleep(50000);
#endif
sleepout = 0;
continue;
} // puts OBV to sleep if nothing is happening.
// Prepare frame
Renderers::current->initFrame();
ImGui::NewFrame();
// If we have a board to view being passed from command line, then "inject"
// it here.
if (preload_required) {
app.LoadFile(filesystem::u8path(g.input_file));
preload_required = false;
}
app.Update();
if (app.m_wantsQuit) {
SDL_Event sdlevent;
sdlevent.type = SDL_QUIT;
SDL_PushEvent(&sdlevent);
}
// Update the title of the SDL app if the board filename has changed. -
// PLD20160618
if (app.history_file_has_changed) {
char scratch[1024];
snprintf(scratch, sizeof(scratch), "%s - %s", OBV_NAME, app.fhistory.history[0]);
SDL_SetWindowTitle(window, scratch);
app.history_file_has_changed = 0;
}
// Render frame
ImGui::Render();
Renderers::current->renderFrame(clear_color);
// vsync disabled, manual FPS limiting
if (!SDL_GL_GetSwapInterval()) {
static const int FPS = 30;
static const std::chrono::duration<std::intmax_t, std::ratio<1, FPS>> frameDuration{1};
static auto nextFrame = std::chrono::steady_clock::now() + frameDuration;
std::this_thread::sleep_until(nextFrame);
nextFrame += frameDuration;
}
}
// Cleanup
Renderers::current->shutdown();
ImGui::DestroyContext();
cleanupAndExit(0);
return 0;
}