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
|
#include "Entrypoint/Common.hpp"
#include "Entrypoint/DirectX11.hpp"
#include "Entrypoint/DirectX12.hpp"
#include "Entrypoint/Metal.hpp"
#include "Entrypoint/OpenGL2.hpp"
#include "Entrypoint/OpenGL3.hpp"
#include "Entrypoint/Vulkan.hpp"
#include "Model/GlobalStates.hpp"
#include "UI/Localization.hpp"
#include "UI/States.hpp"
#include "UI/UI.hpp"
#include "Utils/I18n.hpp"
#include "Utils/Sigslot.hpp"
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <IconsFontAwesome.h>
#include <imgui.h>
#include <argparse/argparse.hpp>
#include <filesystem>
#include <iostream>
#include <memory>
#include <stdexcept>
#include <string>
#include <string_view>
namespace fs = std::filesystem;
using namespace std::literals::string_literals;
using namespace std::literals::string_view_literals;
static std::unique_ptr<RenderingBackend> CreateDefaultBackend() {
#if PLATFORM_WIN32
# if BUILD_CORE_WITH_DX12_BACKEND
try {
auto backend = std::make_unique<DirectX12Backend>();
return backend;
} catch (const std::exception&) {
}
# elif BUILD_CORE_WITH_DX11_BACKEND
try {
auto backend = std::make_unique<DirectX11Backend>();
return backend;
} catch (const std::exception&) {
}
# elif BUILD_CORE_WITH_VULKAN_BACKEND
try {
auto backend = std::make_unique<VulkanBackend>();
return backend;
} catch (const std::exception&) {
}
# elif BUILD_CORE_WITH_OPENGL3_BACKEND
try {
auto backend = std::make_unique<OpenGL3Backend>();
return backend;
} catch (const std::exception&) {
}
# elif BUILD_CORE_WITH_OPENGL2_BACKEND
try {
auto backend = std::make_unique<OpenGL2Backend>();
return backend;
} catch (const std::exception&) {
}
# endif
#elif PLATFORM_MACOS
// We currently only support using metal on macos
backend = std::make_unique<MetalBackend>();
#elif PLATFORM_LINUX
# if BUILD_CORE_WITH_VULKAN_BACKEND
try {
auto backend = std::make_unique<VulkanBackend>();
return backend;
} catch (const std::exception&) {
}
# elif BUILD_CORE_WITH_OPENGL3_BACKEND
try {
auto backend = std::make_unique<OpenGL3Backend>();
return backend;
} catch (const std::exception&) {
}
# elif BUILD_CORE_WITH_OPENGL2_BACKEND
try {
auto backend = std::make_unique<OpenGL2Backend>();
return backend;
} catch (const std::exception&) {
}
# endif
#endif
return nullptr;
}
static std::unique_ptr<RenderingBackend> CreateBackend(std::string_view option) {
if (option == "default") {
return CreateDefaultBackend();
} else if (option == "opengl2") {
return std::make_unique<OpenGL2Backend>();
} else if (option == "opengl3") {
return std::make_unique<OpenGL3Backend>();
} else if (option == "vulkan") {
return std::make_unique<VulkanBackend>();
} else if (option == "dx11") {
return std::make_unique<DirectX11Backend>();
} else if (option == "dx12") {
return std::make_unique<DirectX12Backend>();
} else if (option == "metal") {
return std::make_unique<MetalBackend>();
} else {
std::string message;
message += "Unknown backend '";
message += option;
message += "'.\n";
throw std::runtime_error(message);
return nullptr;
}
}
int main(int argc, char* argv[]) {
argparse::ArgumentParser parser;
parser.add_argument("--global-data-directory")
.help("Directory in which global data (such as recently used projects) are saved to. Use 'default' to use the default directory on each platform.")
.default_value("default"s);
parser.add_argument("--rendering-backend")
.help("Which rendering backend to use. If equals 'default', the preferred API for each platform will be used")
.default_value("default"s);
try {
parser.parse_args(argc, argv);
} catch (const std::runtime_error& error) {
std::cout << error.what() << '\n';
std::cout << parser;
return -1;
}
auto backendOption = parser.get<std::string>("--rendering-backend");
auto backend = CreateBackend(backendOption);
auto& io = ImGui::GetIO();
// Disable saving window positions
io.IniFilename = nullptr;
// Disable log (dump widget tree) file, we don't trigger it but just to be safe
io.LogFilename = nullptr;
// Light mode because all major OS's default theme is white
// TODO follow system theme
ImGui::StyleColorsLight();
// Configure default fonts
{
// Includes latin alphabet, although for some reason smaller than if rendered using 18 point NotoSans regular
io.Fonts->AddFontFromFileTTF("fonts/NotoSansSC-Regular.otf", 18, nullptr, io.Fonts->GetGlyphRangesChineseSimplifiedCommon());
ImWchar iconRanges[] = { ICON_MIN_FA, ICON_MAX_FA };
ImFontConfig config;
config.MergeMode = true;
io.Fonts->AddFontFromFileTTF("fonts/FontAwesome5-Solid.otf", 14, &config, iconRanges);
}
// Initialize localization utilities
{
I18n::OnLanguageChange.Connect([]() { LocaleStrings::Instance = std::make_unique<LocaleStrings>(); });
// Do i18n initialization after linking reload signals, so that when SetLanguage() is called, the locale strings will be initialized (without us writing the code another time outside the slot)
I18n::Init();
I18n::SetLanguage("zh_CN");
// All of our usage are cached in XxxTranslation objects, no need to keep key -> entry mappings anymore
I18n::Unload();
}
auto dataDirOption = parser.get<std::string>("--global-data-directory");
if (dataDirOption == "default") {
GlobalStates::Init();
} else {
fs::path path(dataDirOption);
if (fs::exists(path)) {
GlobalStates::Init(std::move(path));
} else {
GlobalStates::Init();
}
}
UIState::Init();
auto window = backend->GetWindow();
while (!glfwWindowShouldClose(window)) {
backend->BeginFrame();
UI::MainWindow();
backend->EndFrame();
}
UIState::Shutdown();
GlobalStates::Shutdown();
return 0;
}
|