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
|
#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 "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 <iostream>
#include <memory>
#include <stdexcept>
#include <string>
#include <string_view>
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("--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);
ImGui::StyleColorsLight();
// Includes latin alphabet, although for some reason smaller than if rendered using 18 point NotoSans regular
ImGui::GetIO().Fonts->AddFontFromFileTTF("fonts/NotoSansSC-Regular.otf", 18, nullptr, ImGui::GetIO().Fonts->GetGlyphRangesChineseSimplifiedCommon());
ImWchar iconRanges[] = { ICON_MIN_FA, ICON_MAX_FA };
ImFontConfig config;
config.MergeMode = true;
ImGui::GetIO().Fonts->AddFontFromFileTTF("fonts/FontAwesome5-Solid.otf", 14, &config, iconRanges);
I18n::OnReload.Connect([]() { LocaleStrings::Instance = std::make_unique<LocaleStrings>(); });
// Do i18n intialization 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");
UIState::Init();
auto window = backend->GetWindow();
while (!glfwWindowShouldClose(window)) {
backend->BeginFrame();
UI::MainWindow();
backend->EndFrame();
}
return 0;
}
|