-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
152 lines (129 loc) · 3.61 KB
/
main.cpp
File metadata and controls
152 lines (129 loc) · 3.61 KB
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
#include <cstdarg>
// Vendor
#include "action.hpp"
#include "hjson.h"
#include "imhtml.hpp"
#include "io.hpp"
#include "log.hpp"
#include "raylib.h"
#include "rlImGui.h"
// Subsystems
#include "audio.hpp"
#include "elements.hpp"
#include "renderer.hpp"
#include "settings.hpp"
#include "state.hpp"
#include "texture_cache.hpp"
#include "ui/style.hpp"
//
// Request high performance mode to run on GPU if available
// instead of integrated graphics.
//
#ifdef _WIN32
typedef unsigned long DWORD;
extern "C" {
__declspec(dllexport) int AmdPowerXpressRequestHighPerformance = 1;
__declspec(dllexport) DWORD NvOptimusEnablement = 0x00000001;
}
#endif
int main() {
//
// Initialize IO subsystem to read from stdin.
//
IO::Init();
//
// Route raylib log messages through the protocol.
//
SetTraceLogCallback([](int logLevel, const char *text, va_list args) {
char buffer[2048];
vsnprintf(buffer, sizeof(buffer), text, args);
Log::Print("Raylib", "%s", buffer);
});
#ifdef GIT_TAG
Log::Print("Main", "Starting stdui version %s", strlen(GIT_TAG) == 0 ? "unknown" : GIT_TAG);
#else
Log::Print("Main", "Starting stdui version: unknown");
#endif
//
// Wait for settings to be loaded from stdin before initializing anything else.
//
auto settingsAction = Action::ExpectData(IO::MustReadValue(), "settings");
if (!settingsAction) {
Log::Print("Main", "Failed to load settings");
IO::Shutdown();
return 1;
}
Settings::Load(settingsAction.value());
auto settings = Settings::Get();
//
// Init raylib.
//
if (settings->resizable) {
SetConfigFlags(FLAG_WINDOW_RESIZABLE);
}
InitWindow(settings->windowWidth, settings->windowHeight, settings->title.c_str());
if (settings->windowMinWidth > 0 && settings->windowMinHeight > 0) {
SetWindowMinSize(settings->windowMinWidth, settings->windowMinHeight);
}
if (settings->windowMaxWidth > 0 && settings->windowMaxHeight > 0) {
SetWindowMaxSize(settings->windowMaxWidth, settings->windowMaxHeight);
}
InitAudioDevice();
Log::Print("Main", "Raylib initialized");
//
// Configure raylib.
//
SetExitKey(KEY_NULL);
SetTargetFPS(settings->targetFps);
//
// Register all ui-* custom elements.
//
Elements::RegisterAll();
//
// Setup ImGui styling.
//
rlImGuiBeginInitImGui();
UI::Style::SetupStyle();
rlImGuiEndInitImGui();
//
// Setup ImHTML image callbacks.
//
auto conf = ImHTML::GetConfig();
conf->GetImageMeta = [](const char *url, const char *) {
auto tex = TextureCache::GetPtr(url);
return ImHTML::ImageMeta{tex->width, tex->height};
};
conf->GetImageTexture = [](const char *url, const char *) { return (ImTextureID)TextureCache::GetPtr(url); };
conf->FontRegular = UI::Style::GetFont(UI::Style::FontStyle::Regular);
conf->FontBold = UI::Style::GetFont(UI::Style::FontStyle::Bold);
conf->FontItalic = UI::Style::GetFont(UI::Style::FontStyle::Italic);
conf->FontBoldItalic = UI::Style::GetFont(UI::Style::FontStyle::BoldItalic);
//
// Notify the controlling process that stdui is fully initialized and
// ready to accept content.
//
Hjson::Value readyMsg;
readyMsg["action"] = "ready";
IO::WriteValue(readyMsg);
//
// Main game loop.
//
Log::Print("Main", "Mainloop starting...");
while (!WindowShouldClose() && !CLOSE_REQUESTED) {
Renderer::MainLoopIteration();
}
//
// Cleanup.
//
Audio::Cleanup();
rlImGuiShutdown();
CloseWindow();
//
// Notify the controlling process that the window has been closed.
//
Hjson::Value windowClosed;
windowClosed["action"] = "window-closed";
IO::WriteValue(windowClosed);
IO::Shutdown();
return 0;
}