forked from Return-To-The-Roots/s25edit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCGame.cpp
More file actions
256 lines (223 loc) · 6.34 KB
/
CGame.cpp
File metadata and controls
256 lines (223 loc) · 6.34 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
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
// Copyright (C) 2009 - 2021 Marc Vester (XaserLE)
// Copyright (C) 2009 - 2021 Settlers Freaks <sf-team at siedler25.org>
//
// SPDX-License-Identifier: GPL-3.0-or-later
#include "CGame.h"
#include "CIO/CMenu.h"
#include "CIO/CWindow.h"
#include "CMap.h"
#include "RttrConfig.h"
#include "files.h"
#include "globals.h"
#include <boost/filesystem.hpp>
#include <boost/nowide/cstdio.hpp>
#include <iostream>
#include <limits>
namespace bfs = boost::filesystem;
//#include <vld.h>
CGame::CGame()
: GameResolution(1024, 768), fullscreen(false), Running(true), showLoadScreen(true),
lastFps("", 0, 0, FontSize::Medium)
{
global::bmpArray.resize(MAXBOBBMP);
global::shadowArray.resize(MAXBOBSHADOW);
global::s2 = this;
}
CGame::~CGame()
{
global::s2 = nullptr;
}
int CGame::Execute()
{
if(!Init())
return -1;
SDL_Event Event;
lastFps.setText("");
lastFpsTick = SDL_GetTicks();
lastFrameTime = SDL_GetTicks();
framesPassedSinceLastFps = 0;
while(Running)
{
while(SDL_PollEvent(&Event))
EventHandling(&Event);
GameLoop();
Render();
}
return 0;
}
void CGame::RenderPresent() const
{
SDL_UpdateTexture(displayTexture_.get(), nullptr, Surf_Display->pixels, Surf_Display->w * sizeof(Uint32));
SDL_RenderClear(renderer_.get());
SDL_RenderCopy(renderer_.get(), displayTexture_.get(), nullptr, nullptr);
SDL_RenderPresent(renderer_.get());
}
CMenu* CGame::RegisterMenu(std::unique_ptr<CMenu> Menu)
{
for(auto& i : Menus)
i->setInactive();
Menu->setActive();
Menus.emplace_back(std::move(Menu));
return Menus.back().get();
}
bool CGame::UnregisterMenu(CMenu* Menu)
{
auto it = std::find_if(Menus.begin(), Menus.end(), [Menu](const auto& cur) { return cur.get() == Menu; });
if(it == Menus.end())
return false;
if(it != Menus.begin())
it[-1]->setActive();
Menus.erase(it);
return true;
}
CWindow* CGame::RegisterWindow(std::unique_ptr<CWindow> Window)
{
// first find the highest priority
const auto itHighestPriority =
std::max_element(Windows.cbegin(), Windows.cend(),
[](const auto& lhs, const auto& rhs) { return lhs->getPriority() < rhs->getPriority(); });
const int highestPriority = itHighestPriority == Windows.cend() ? 0 : (*itHighestPriority)->getPriority();
for(auto& i : Windows)
i->setInactive();
Window->setActive();
Window->setPriority(highestPriority + 1);
Windows.emplace_back(std::move(Window));
return Windows.back().get();
}
bool CGame::UnregisterWindow(CWindow* Window)
{
auto it = std::find_if(Windows.begin(), Windows.end(), [Window](const auto& cur) { return cur.get() == Window; });
if(it == Windows.end())
return false;
if(it != Windows.begin())
it[-1]->setActive();
Windows.erase(it);
return true;
}
void CGame::RegisterCallback(void (*callback)(int))
{
assert(callback);
Callbacks.push_back(callback);
}
bool CGame::UnregisterCallback(void (*callback)(int))
{
auto it = std::find(Callbacks.begin(), Callbacks.end(), callback);
if(it == Callbacks.end())
return false;
Callbacks.erase(it);
return true;
}
void CGame::setMapObj(std::unique_ptr<CMap> MapObj)
{
this->MapObj = std::move(MapObj);
}
CMap* CGame::getMapObj()
{
return MapObj.get();
}
void CGame::delMapObj()
{
MapObj.reset();
}
void CGame::GameLoop()
{
for(auto&& callback : Callbacks)
callback(CALL_FROM_GAMELOOP);
const auto isWaste = [](const auto& p) { return p->isWaste(); };
auto itMenu = std::find_if(Menus.begin(), Menus.end(), isWaste);
while(itMenu != Menus.end())
{
UnregisterMenu(itMenu->get());
itMenu = std::find_if(Menus.begin(), Menus.end(), isWaste);
}
auto itWnd = std::find_if(Windows.begin(), Windows.end(), isWaste);
while(itWnd != Windows.end())
{
UnregisterWindow(itWnd->get());
itWnd = std::find_if(Windows.begin(), Windows.end(), isWaste);
}
}
namespace {
void WaitForEnter()
{
#ifndef NDEBUG
static bool waited = false;
if(waited)
return;
waited = true;
std::cout << "\n\nPress ENTER *twice* to close this window . . ." << std::endl;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cin.get();
#endif // !NDEBUG
}
bool checkWriteable(const bfs::path& folder)
{
if(!bfs::exists(folder))
{
boost::system::error_code ec;
bfs::create_directories(folder, ec);
if(ec)
return false;
}
bfs::path testFileName = folder / bfs::unique_path();
FILE* fp = boost::nowide::fopen(testFileName.string().c_str(), "wb");
if(!fp)
return false;
fclose(fp);
bfs::remove(testFileName);
return true;
}
} // namespace
#undef main
int main(int /*argc*/, char* /*argv*/[])
{
if(!RTTRCONFIG.Init())
{
std::cerr << "Failed to init program!" << std::endl;
WaitForEnter();
return 1;
}
global::gameDataFilePath = RTTRCONFIG.ExpandPath("<RTTR_GAME>");
// Prefer application folder over user folder
global::userMapsPath = RTTRCONFIG.ExpandPath("WORLDS");
if(!checkWriteable(global::userMapsPath))
{
global::userMapsPath = RTTRCONFIG.ExpandPath(s25::folders::mapsOwn);
if(!checkWriteable(global::userMapsPath))
{
std::cerr << "Could not find a writable folder for maps\nCheck " << global::userMapsPath << std::endl;
return 1;
}
}
std::cout << "Expecting S2 game files in " << global::gameDataFilePath << std::endl;
std::cout << "Maps folder set to " << global::userMapsPath << std::endl;
boost::system::error_code ec;
boost::filesystem::create_directories(global::userMapsPath, ec);
if(ec)
{
std::cerr << "Could not create " << global::userMapsPath << ": " << ec.message() << std::endl;
WaitForEnter();
return 1;
}
std::cout << "Initializing SDL...";
if(SDL_Init(SDL_INIT_EVERYTHING) < 0)
{
std::cout << "failure";
return 1;
}
std::cout << "done\n";
int result = 0;
try
{
auto s2 = std::make_unique<CGame>();
result = s2->Execute();
} catch(...)
{
std::cerr << "Unhandled Exception" << std::endl;
result = 1;
}
SDL_Quit();
WaitForEnter();
return result;
}