-
Notifications
You must be signed in to change notification settings - Fork 0
/
game_windows.cpp
413 lines (337 loc) · 10.2 KB
/
game_windows.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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/*
* This file is part of EasyRPG Player.
*
* EasyRPG Player is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EasyRPG Player is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EasyRPG Player. If not, see <http://www.gnu.org/licenses/>.
*/
#include "game_windows.h"
#include "game_message.h"
#include "main_data.h"
#include "compiler.h"
#include "text.h"
#include "cache.h"
#include "pending_message.h"
#include "filefinder.h"
#include "output.h"
#include "player.h"
Game_Windows::Window_User::Window_User(lcf::rpg::SaveEasyRpgWindow save)
: data(std::move(save))
{
}
void Game_Windows::SetSaveData(std::vector<lcf::rpg::SaveEasyRpgWindow> save) {
windows.clear();
int num_windows = static_cast<int>(save.size());
windows.reserve(num_windows);
for (int i = 0; i < num_windows; ++i) {
windows.emplace_back(std::move(save[i]));
auto& win = windows.back();
int id = win.data.ID;
if (id > 0) {
win.Refresh();
auto& pic = Main_Data::game_pictures->GetPicture(id);
pic.AttachWindow(*win.window);
}
}
}
std::vector<lcf::rpg::SaveEasyRpgWindow> Game_Windows::GetSaveData() const {
std::vector<lcf::rpg::SaveEasyRpgWindow> save;
auto data_size = static_cast<int>(windows.size());
save.reserve(data_size);
for (auto& win: windows) {
save.push_back(win.data);
}
// RPG_RT Save game data always has a constant number of pictures
// depending on the engine version. We replicate this, unless we have even
// more pictures than that.
while (data_size > static_cast<int>(save.size())) {
lcf::rpg::SaveEasyRpgWindow data;
data.ID = static_cast<int>(save.size()) + 1;
save.push_back(std::move(data));
}
return save;
}
Game_Windows::Window_User& Game_Windows::GetWindow(int id) {
if (EP_UNLIKELY(id > static_cast<int>(windows.size()))) {
windows.reserve(id);
while (static_cast<int>(windows.size()) < id) {
windows.emplace_back(windows.size() + 1);
}
}
return windows[id - 1];
}
Game_Windows::Window_User* Game_Windows::GetWindowPtr(int id) {
return id <= static_cast<int>(windows.size())
? &windows[id - 1] : nullptr;
}
bool Game_Windows::Window_User::Create(const WindowParams& params) {
Erase();
data.width = params.width;
data.height = params.height;
data.system_name = lcf::DBString(params.system_name);
data.message_stretch = params.message_stretch;
data.flags.draw_frame = params.draw_frame;
data.flags.border_margin = params.border_margin;
for (const auto& text: params.texts) {
lcf::rpg::SaveEasyRpgText data_text;
data_text.text = lcf::DBString(text.text);
data_text.position_x = text.position_x;
data_text.position_y = text.position_y;
data_text.font_name = lcf::DBString(text.font_name);
data_text.font_size = text.font_size;
data_text.letter_spacing = text.letter_spacing;
data_text.line_spacing = text.line_spacing;
data_text.flags.draw_gradient = text.draw_gradient;
data_text.flags.draw_shadow = text.draw_shadow;
data_text.flags.bold = text.bold;
data_text.flags.italic = text.italic;
data.texts.push_back(data_text);
}
Refresh();
return true;
}
bool Game_Windows::Create(int id, const WindowParams& params) {
auto& window = GetWindow(id);
if (window.Create(params)) {
if (Main_Data::game_pictures->Show(id, params)) {
auto& pic = Main_Data::game_pictures->GetPicture(id);
pic.drawFrame = params.draw_frame;
pic.AttachWindow(*window.window);
return true;
} else {
window.Erase();
return false;
}
}
return false;
}
void Game_Windows::Window_User::Erase() {
data = {};
}
void Game_Windows::Window_User::Refresh() {
std::vector<FontRef> fonts;
std::vector<PendingMessage> messages;
for (const auto& text: data.texts) {
// TODO: Async requests
FontRef font;
Filesystem_Stream::InputStream font_file;
std::string font_name = ToString(text.font_name);
if (!font_name.empty()) {
// Try to find best fitting font
if (text.flags.bold && text.flags.italic) {
font_file = FileFinder::OpenFont(font_name + "-BoldItalic");
}
if (!font_file && text.flags.bold) {
font_file = FileFinder::OpenFont(font_name + "-Bold");
}
if (!font_file && text.flags.italic) {
font_file = FileFinder::OpenFont(font_name + "-Italic");
}
if (!font_file) {
font_file = FileFinder::OpenFont(font_name+ "-Regular");
}
if (!font_file) {
font_file = FileFinder::OpenFont(font_name);
}
if (!font_file) {
Output::Warning("Font not found: {}", text.font_name);
font = Font::Default();
} else {
font = Font::CreateFtFont(std::move(font_file), text.font_size, text.flags.bold, text.flags.italic);
if (!font) {
Output::Warning("Error loading font: {}", text.font_name);
font = Font::Default();
}
}
} else {
font = Font::Default();
}
fonts.emplace_back(font);
std::stringstream ss(ToString(text.text));
std::string out;
PendingMessage pm;
while (Utils::ReadLine(ss, out)) {
pm.PushLine(out);
}
messages.emplace_back(pm);
}
if (data.width == 0 || data.height == 0) {
// Automatic window size
int x_max = 0;
int y_max = 0;
for (size_t i = 0; i < data.texts.size(); ++i) {
// Lots of duplication with the rendering code below but cannot be easily reduced more
const auto& font = fonts[i];
const auto& pm = messages[i];
const auto& text = data.texts[i];
auto style = font->GetCurrentStyle();
style.size = text.font_size;
auto sg = font->ApplyStyle(style);
int x = text.position_x;
int y = text.position_y;
for (const auto& line: pm.GetLines()) {
std::u32string line32;
auto* text_index = line.data();
const auto* end = line.data() + line.size();
while (text_index != end) {
auto tret = Utils::TextNext(text_index, end, Player::escape_char);
text_index = tret.next;
if (EP_UNLIKELY(!tret)) {
continue;
}
const char ch = tret.ch;
if (Utils::IsControlCharacter(ch)) {
// control characters not handled
continue;
}
if (tret.is_exfont) {
// exfont processed later
line32 += '$';
}
if (tret.is_escape && ch != Player::escape_char) {
if (!line32.empty()) {
x += Text::GetSize(*font, Utils::EncodeUTF(line32)).width;
line32.clear();
}
// Special message codes
switch (ch) {
case 'c':
case 'C':
{
// Color
Game_Message::ParseColor(text_index, end, Player::escape_char, true);
auto* ti = text_index;
while (ti != end) {
auto ret = Utils::TextNext(ti, end, Player::escape_char);
ti = ret.next;
const char c = ret.ch;
x -= Text::GetSize(*font, c, false).width;
if (c == ']') {
break;
}
}
}
break;
}
continue;
}
line32 += static_cast<char32_t>(ch);
}
if (!line32.empty()) {
x += Text::GetSize(*font, Utils::EncodeUTF(line32)).width;
}
x_max = std::max(x, x_max);
x = 0;
y += text.font_size + text.line_spacing;
}
y -= text.line_spacing;
y_max = std::max(y, y_max);
}
// Border size
x_max += 16;
if (data.flags.border_margin)
y_max += 20; // 16 looks better but this matches better with Maniac Patch
if (data.width == 0)
data.width = x_max;
if (data.height == 0)
data.height = y_max;
}
window = std::make_unique<Window_Selectable>(0, 0, data.width, data.height);
window->CreateContents();
window->SetVisible(false);
if (data.message_stretch == lcf::rpg::System::Stretch_easyrpg_none) {
window->SetWindowskin(nullptr);
} else {
BitmapRef system;
if (!data.system_name.empty()) {
// TODO: Async request
system = Cache::System(data.system_name);
} else {
system = Cache::SystemOrBlack();
}
window->SetWindowskin(system);
window->SetStretch(data.message_stretch == lcf::rpg::System::Stretch_stretch);
}
if (!data.flags.draw_frame) {
window->SetFrameOpacity(0);
}
if (!data.flags.border_margin) {
window->SetBorderX(0);
window->SetBorderY(-3);
}
for (size_t i = 0; i < data.texts.size(); ++i) {
// Lots of duplication with the rendering code below but cannot be easily reduced more
const auto& font = fonts[i];
const auto& pm = messages[i];
const auto& text = data.texts[i];
auto style = font->GetCurrentStyle();
style.size = text.font_size;
style.draw_shadow = text.flags.draw_shadow;
auto sg = font->ApplyStyle(style);
int x = text.position_x;
int y = text.position_y;
int text_color = 0;
for (const auto& line: pm.GetLines()) {
std::u32string line32;
auto* text_index = line.data();
const auto* end = line.data() + line.size();
while (text_index != end) {
auto tret = Utils::TextNext(text_index, end, Player::escape_char);
text_index = tret.next;
if (EP_UNLIKELY(!tret)) {
continue;
}
const auto ch = tret.ch;
if (Utils::IsControlCharacter(ch)) {
// control characters not handled
continue;
}
if (tret.is_exfont) {
// exfont processed later
line32 += '$';
}
if (tret.is_escape && ch != Player::escape_char) {
if (!line32.empty()) {
x += Text::Draw(*window->GetContents(), x, y, *font, *Cache::System(), text_color, Utils::EncodeUTF(line32)).x;
line32.clear();
}
// Special message codes
switch (ch) {
case 'c':
case 'C':
{
// Color
auto pres = Game_Message::ParseColor(text_index, end, Player::escape_char, true);
auto value = pres.value;
text_index = pres.next;
text_color = value > 19 ? 0 : value;
}
break;
}
continue;
}
line32 += static_cast<char32_t>(ch);
}
if (!line32.empty()) {
Text::Draw(*window->GetContents(), x, y, *font, *Cache::System(), text_color, Utils::EncodeUTF(line32));
}
x = 0;
y += text.font_size + text.line_spacing;
}
}
}
void Game_Windows::Erase(int id) {
auto* window = GetWindowPtr(id);
if (EP_LIKELY(window)) {
window->Erase();
}
}