-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.cpp
220 lines (172 loc) · 6.58 KB
/
main.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
/*
* Copyright (c) 2006 - 2008
* Wandering Monster Studios Limited
*
* Any use of this program is governed by the terms of Wandering Monster
* Studios Limited's Licence Agreement included with this program, a copy
* of which can be obtained by contacting Wandering Monster Studios
* Limited at info@wanderingmonster.co.nz.
*
*/
#include <clipp.h>
#include "OpenGL.h"
#include "ElementGame.h"
#include <Rocket/Core.h>
#include <Rocket/Controls.h>
#include <Rocket/Debugger.h>
#include "EventManager.h"
#include "EventInstancer.h"
#include "EventHandlerNewGame.h"
#include <Input.h>
#include <Shell.h>
#include <locale>
#if defined ROCKET_PLATFORM_WIN32
#undef __GNUC__
#include <io.h>
#include <fcntl.h>
#endif
//#define SHOW_CONSOLE 1
#include <spdlog/spdlog.h>
#include <spdlog/sinks/basic_file_sink.h>
#include <spdlog/sinks/stdout_color_sinks.h>
#include <memory>
Rocket::Core::Context* context = NULL;
std::shared_ptr<Configuration> config;
void GameLoop() {
dynamic_cast<ElementGame*>(context->GetDocument("game_window")->GetElementById("game"))->gameLoop();
}
#if defined ROCKET_PLATFORM_WIN32
void DoAllocConsole()
{
static const WORD MAX_CONSOLE_LINES = 500;
int hConHandle;
intptr_t lStdHandle;
CONSOLE_SCREEN_BUFFER_INFO coninfo;
FILE *fp;
// allocate a console for this app
AllocConsole();
// set the screen buffer to be big enough to let us scroll text
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &coninfo);
coninfo.dwSize.Y = MAX_CONSOLE_LINES;
SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), coninfo.dwSize);
// redirect unbuffered STDOUT to the console
lStdHandle = reinterpret_cast<intptr_t>(GetStdHandle(STD_OUTPUT_HANDLE));
hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
fp = _fdopen(hConHandle, "w");
*stdout = *fp;
setvbuf(stdout, NULL, _IONBF, 0);
// redirect unbuffered STDIN to the console
lStdHandle = reinterpret_cast<intptr_t>(GetStdHandle(STD_INPUT_HANDLE));
hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
fp = _fdopen(hConHandle, "r");
*stdin = *fp;
setvbuf(stdin, NULL, _IONBF, 0);
// redirect unbuffered STDERR to the console
lStdHandle = reinterpret_cast<intptr_t>(GetStdHandle(STD_ERROR_HANDLE));
hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
fp = _fdopen(hConHandle, "w");
*stderr = *fp;
setvbuf(stderr, NULL, _IONBF, 0);
ShowWindow(GetConsoleWindow(), SW_HIDE);
}
int APIENTRY WinMain(HINSTANCE ROCKET_UNUSED_PARAMETER(instance_handle), HINSTANCE ROCKET_UNUSED_PARAMETER(previous_instance_handle), char* ROCKET_UNUSED_PARAMETER(command_line), int ROCKET_UNUSED_PARAMETER(command_show))
#else
int main(int argc, char** argv)
#endif
{
using namespace clipp;
std::string logLevel("warning");
std::string configurationFile("./data/config.json");
auto cli = (
option("-v", "--verbosity") & word("level", logLevel),
option("-c", "--config") & value("file", configurationFile)
);
#ifdef ROCKET_PLATFORM_WIN32
ROCKET_UNUSED(instance_handle);
ROCKET_UNUSED(previous_instance_handle);
ROCKET_UNUSED(command_line);
ROCKET_UNUSED(command_show);
parse(__argc, __argv, cli);
#else
parse(argc, argv, cli);
#endif
const Rocket::Core::String APP_PATH(".");
const char * WINDOW_NAME = "Goban";
#ifdef ROCKET_PLATFORM_WIN32
DoAllocConsole();
#endif
auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
console_sink->set_level(spdlog::level::from_str(logLevel));
auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>("last_run.log", true);
file_sink->set_level(spdlog::level::from_str(logLevel));
spdlog::sinks_init_list sink_list = { file_sink, console_sink };
auto logger = std::make_shared<spdlog::logger>("multi_sink", sink_list.begin(), sink_list.end());
spdlog::set_default_logger(logger);
unsigned window_width = 1024;
unsigned window_height = 768;
std::shared_ptr<ShellRenderInterfaceOpenGL> popengl_renderer(new ShellRenderInterfaceOpenGL());
ShellRenderInterfaceOpenGL *shell_renderer = popengl_renderer.get();
// Generic OS initialisation, creates a window and attaches OpenGL.
if (!Shell::Initialise(APP_PATH) ||
!Shell::OpenWindow(WINDOW_NAME, shell_renderer, window_width, window_height, true))
{
spdlog::critical("cannot Shell::OpenWindow.");
Shell::Shutdown();
return -1;
}
// Rocket initialisation.
Rocket::Core::SetRenderInterface(popengl_renderer.get());
//(&opengl_renderer)->SetViewport(window_width, window_height);
ShellSystemInterface system_interface;
Rocket::Core::SetSystemInterface(&system_interface);
//std::locale::global(std::locale("C"));
Rocket::Core::Initialise();
Rocket::Controls::Initialise();
config.reset(new Configuration(configurationFile));
// Create the main Rocket context and set it on the shell's input layer.
context = Rocket::Core::CreateContext("main",
Rocket::Core::Vector2i(window_width, window_height));
if (context == NULL)
{
Rocket::Core::Shutdown();
Shell::Shutdown();
return -1;
}
Rocket::Debugger::Initialise(context);
//Rocket::Debugger::SetVisible(true);
Input::SetContext(context);
shell_renderer->SetContext(context);
using nlohmann::json;
auto fonts = config->data
.value("fonts", json({}))
.value("gui", json::array());
Shell::LoadFonts(fonts);
Rocket::Core::ElementInstancer* element_instancer = new Rocket::Core::ElementInstancerGeneric< ElementGame >();
Rocket::Core::Factory::RegisterElementInstancer("game", element_instancer);
element_instancer->RemoveReference();
EventInstancer* event_instancer = new EventInstancer();
Rocket::Core::Factory::RegisterEventListenerInstancer(event_instancer);
event_instancer->RemoveReference();
EventManager::SetPrefix(config->data.value("gui","./data/gui").c_str());
EventManager::RegisterEventHandler("goban", new EventHandlerNewGame());
//Shell::ToggleFullscreen();
auto window = EventManager::LoadWindow("goban");
if(window) {
Shell::EventLoop(GameLoop);
}
else {
spdlog::critical("cannot create window, exiting immediately");
return 13;
}
EventManager::Shutdown();
spdlog::debug("Before context destroy");;
context->RemoveReference();
context = 0;
spdlog::debug("Before Rocket shutdown");
Rocket::Core::ReleaseTextures();
Rocket::Core::Shutdown();
spdlog::debug("Before Window Close");
Shell::CloseWindow();
Shell::Shutdown();
return 0;
}