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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
|
/*
client/savegamemenu.cc
This file is part of the Osirion project and is distributed under
the terms of the GNU General Public License version 2
*/
#include "client/savegamemenu.h"
#include "client/closeiconbutton.h"
#include "client/video.h"
#include "ui/ui.h"
#include "model/material.h"
#include "render/screenshot.h"
#include "render/camera.h"
#include "render/draw.h"
#include "render/textures.h"
#include "render/gl.h"
#include "filesystem/filesystem.h"
#include "core/application.h"
#include "core/gameinterface.h"
#include "core/gameserver.h"
#include "sys/sys.h"
#include <iomanip>
#include <cstdio>
namespace client
{
SaveGameMenu::SaveGameMenu(ui::Widget *parent, const char *label, const Mode mode) :
ui::Window(parent)
{
set_label(label);
set_border(true);
set_background(true);
set_font(ui::root()->font_small());
savegamemenu_mode = mode;
// window title
savegamemenu_titlelabel = new ui::Label(this);
savegamemenu_titlelabel->set_label("title");
savegamemenu_titlelabel->set_background(false);
savegamemenu_titlelabel->set_border(false);
savegamemenu_titlelabel->set_font(ui::root()->font_large());
savegamemenu_titlelabel->set_alignment(ui::AlignCenter);
if (savegamemenu_mode == Save) {
savegamemenu_titlelabel->set_text("SAVE GAME");
} else {
savegamemenu_titlelabel->set_text("LOAD GAME");
}
// close button
savegamemenu_closebutton = new CloseIconButton(savegamemenu_titlelabel);
// file listview
savegamemenu_filelistview = new ui::ListView(this);
savegamemenu_filelistview->set_label("files");
// savegame screenshot
savegamemenu_screenshot = new ui::Bitmap(this);
savegamemenu_screenshot->set_background(true);
savegamemenu_screenshot->set_border(true);
// savegame description title
savegamemenu_descrtitle = new ui::Label(this);
savegamemenu_descrtitle->set_label("subtitle");
savegamemenu_descrtitle->set_background(false);
savegamemenu_descrtitle->set_border(false);
savegamemenu_descrtitle->set_font(ui::root()->font_large());
savegamemenu_descrtitle->set_alignment(ui::AlignCenter);
// savegame description label
savegamemenu_descrlabel = new ui::Label(this);
savegamemenu_descrlabel->set_label("info");
savegamemenu_descrlabel->set_background(true);
savegamemenu_descrlabel->set_border(true);
// save button
savegamemenu_savebutton = new ui::Button(this, "Save");
savegamemenu_savebutton->set_label("save");
// delete button
savegamemenu_deletebutton = new ui::IconButton(this, "bitmaps/icons/button_delete", "Delete");
savegamemenu_deletebutton->set_label("delete");
// cancel save/load/overwrite button
savegamemenu_cancelbutton = new ui::Button(this, "Cancel");
savegamemenu_cancelbutton->set_label("cancel");
// confirm save/load/overwrite button
savegamemenu_confirmsavebutton = new ui::Button(this, "Confirm");
savegamemenu_confirmsavebutton->set_label("confirmsave");
// confirm save/load/overwrite button
savegamemenu_confirmdeletebutton = new ui::Button(this, "Delete");
savegamemenu_confirmdeletebutton->set_label("confirmdelete");
// description input box
savegamemenu_descrinput = new ui::InputBox(this);
savegamemenu_descrinput->set_label("description");
savegamemenu_descrinput->hide();
}
SaveGameMenu::~SaveGameMenu()
{
}
void SaveGameMenu::resize()
{
const float padding = ui::UI::padding;
const float icon_size = 24.0f; // small icons
// resize title label
savegamemenu_titlelabel->set_size(width() - padding * 2.0f, savegamemenu_titlelabel->font()->height());
savegamemenu_titlelabel->set_location(padding, padding);
// resize close button
savegamemenu_closebutton->set_size(savegamemenu_titlelabel->font()->height(), savegamemenu_titlelabel->font()->height());
savegamemenu_closebutton->set_location(savegamemenu_titlelabel->width() - savegamemenu_closebutton->width(), 0);
// resize files listview
savegamemenu_filelistview->set_size(
ui::UI::elementsize.width(),
height() - icon_size - padding * 3.0f - savegamemenu_titlelabel->bottom()
);
savegamemenu_filelistview->set_location(
padding,
savegamemenu_titlelabel->bottom() + padding
);
// resize delete button
savegamemenu_deletebutton->set_size(icon_size, icon_size);
savegamemenu_deletebutton->set_location(
savegamemenu_filelistview->left(),
savegamemenu_filelistview->bottom() + padding
);
// resize description title
savegamemenu_descrtitle->set_size(
width() - savegamemenu_filelistview->right() - 2.0f * padding,
savegamemenu_descrtitle->font()->height()
);
savegamemenu_descrtitle->set_location(
savegamemenu_filelistview->right() + padding,
savegamemenu_filelistview->top() + ui::UI::elementsize.width() + padding
);
// resize description label
savegamemenu_descrlabel->set_size(
savegamemenu_descrtitle->width(),
savegamemenu_filelistview->bottom() - savegamemenu_descrtitle->bottom() - padding
);
savegamemenu_descrlabel->set_location(
savegamemenu_filelistview->right() + padding,
savegamemenu_descrtitle->bottom() + padding
);
// resize screenshot
const model::Material *screenshotmaterial = model::Material::find("ui/screenshot");
if (screenshotmaterial) {
float sx = savegamemenu_descrtitle->width() / screenshotmaterial->size().width();
float sy = ui::UI::elementsize.width() / screenshotmaterial->size().height();
float s = math::min (sx, sy);
savegamemenu_screenshot->set_size(screenshotmaterial->size().width() * s, screenshotmaterial->size().height() * s);
} else {
savegamemenu_screenshot->set_size(
ui::UI::elementsize.width(),
ui::UI::elementsize.width()
);
}
savegamemenu_screenshot->set_location(
savegamemenu_descrtitle->left() + (savegamemenu_descrtitle->width() - savegamemenu_screenshot->width()) * 0.5f,
savegamemenu_filelistview->top() + (ui::UI::elementsize.width() - savegamemenu_screenshot->height()) * 0.5f
);
// resize save button
savegamemenu_savebutton->set_size(ui::UI::elementsize);
savegamemenu_savebutton->set_location(
savegamemenu_descrlabel->left() + (savegamemenu_descrlabel->width() - ui::UI::elementsize.width()) * 0.5f,
savegamemenu_descrlabel->bottom() + padding);
// resize save/load/overwrite confirm button
savegamemenu_confirmsavebutton->set_size(ui::UI::elementsize);
savegamemenu_confirmsavebutton->set_location(
savegamemenu_descrlabel->left() + (savegamemenu_descrlabel->width() - 2.0f * ui::UI::elementsize.width() - padding) * 0.5f,
savegamemenu_descrlabel->bottom() + padding
);
// resize cancel button
savegamemenu_cancelbutton->set_size(ui::UI::elementsize);
savegamemenu_cancelbutton->set_location(
savegamemenu_confirmsavebutton->right() + padding,
savegamemenu_descrlabel->bottom() + padding
);
// resize confirm delete button
savegamemenu_confirmdeletebutton->set_size(ui::UI::elementsize);
savegamemenu_confirmdeletebutton->set_location(savegamemenu_confirmsavebutton->location());
}
void SaveGameMenu::refresh()
{
ui::ListItem *listitem = 0;
std::string savegamepath(filesystem::writedir());
savegamepath.append("savegames");
filesystem::Directory directory(savegamepath);
savegamemenu_filelistview->clear();
savegamemenu_deletebutton->disable();
savegamemenu_savebutton->hide();
savegamemenu_savebutton->enable();
savegamemenu_confirmsavebutton->hide();
savegamemenu_confirmdeletebutton->hide();
savegamemenu_cancelbutton->hide();
if (savegamemenu_mode == Save) {
listitem = new ui::ListItem(savegamemenu_filelistview, "NEW SAVEGAME");
listitem->set_label("new");
listitem->set_sortkey("[new]");
listitem->set_font(ui::root()->font_tiny());
listitem->set_height(listitem->font()->height() * 3.0f);
savegamemenu_savebutton->set_text("Save");
if (!core::localcontrol()) {
savegamemenu_savebutton->disable();
}
} else {
savegamemenu_savebutton->set_text("Load");
}
for (filesystem::Directory::FileNames::const_iterator it = directory.filenames().begin();
it != directory.filenames().end(); ++it)
{
const std::string filename((*it).c_str());
if (filename.size() < 5) {
continue;
}
std::string ext = filename.substr(filename.size() - 4);
aux::lowercase(ext);
if (ext.compare(".ini")) {
continue;
}
std::string savename = filename.substr(0, filename.size() - 4);
std::string descr;
std::string timestamp;
std::string info;
get_savegame_info(savename, descr, info, timestamp);
if (descr.size()) {
std::ostringstream descrstr;
descrstr << descr << '\n';
descrstr << timestamp;
listitem = new ui::ListItem(savegamemenu_filelistview, descrstr.str().c_str());
listitem->set_value(savename);
listitem->set_sortkey(timestamp);
listitem->set_font(ui::root()->font_tiny());
listitem->set_height(listitem->font()->height() * 3.0f);
}
}
savegamemenu_filelistview->sort(false);
savegamemenu_filelistview->event_resize();
show_file_info();
}
void SaveGameMenu::hide()
{
Window::hide();
model::Material *screenshotmaterial = model::Material::find("ui/screenshot");
if (screenshotmaterial) {
for (model::Material::Layers::const_iterator lit = screenshotmaterial->layers().begin(); lit != screenshotmaterial->layers().end(); ++lit) {
if ((*lit)->texture().size()) {
render::Textures::unload((*lit)->texture());
(*lit)->set_texture("");
(*lit)->set_texture_id(0);
}
}
}
savegamemenu_screenshot->set_texture("");
}
void SaveGameMenu::show()
{
Window::show();
refresh();
}
void SaveGameMenu::show_file_info()
{
savegamemenu_descrtitle->clear();
savegamemenu_descrlabel->clear();
savegamemenu_deletebutton->disable();
savegamemenu_confirmsavebutton->hide();
savegamemenu_confirmdeletebutton->hide();
savegamemenu_cancelbutton->hide();
savegamemenu_descrlabel->hide();
savegamemenu_screenshot->hide();
if (savegamemenu_filelistview->selected()) {
savegamemenu_savebutton->show();
savegamemenu_descrlabel->show();
std::string savename(savegamemenu_filelistview->selected()->value().c_str());
if (savename.size()) {
std::string descr;
std::string timestamp;
std::string info;
get_savegame_info(savename, descr, info, timestamp);
std::ostringstream descrstr;
//descrstr << descr << '\n';
descrstr << info; // info has a trailing newline
descrstr << timestamp << '\n';
savegamemenu_deletebutton->enable();
savegamemenu_descrtitle->set_text(savename);
savegamemenu_descrlabel->set_text(descrstr.str().c_str());
model::Material *screenshotmaterial = model::Material::find("ui/screenshot");
if (!screenshotmaterial) {
screenshotmaterial = model::Material::add("ui/screenshot");
}
model::Layer *screenshotlayer = 0;
if (screenshotmaterial->layers().size() > 0) {
screenshotlayer = (*screenshotmaterial->layers().begin());
} else {
screenshotlayer = screenshotmaterial->add_layer();
screenshotlayer->set_fullbright(true);
}
if (screenshotlayer->texture().size()) {
render::Textures::unload(screenshotlayer->texture());
screenshotlayer->set_texture("");
}
const std::string screenshotfilename("savegames/" + savename);
screenshotlayer->set_texture(screenshotfilename);
render::Textures::image_loader(screenshotlayer);
screenshotmaterial->set_size(screenshotlayer->size());
savegamemenu_screenshot->set_texture(screenshotmaterial->name());
savegamemenu_screenshot->show();
resize();
} else {
// new savegame, immediatly show the save game dialog
show_save_dialog();
return;
}
}
}
void SaveGameMenu::show_save_dialog()
{
if (!savegamemenu_filelistview->selected()) {
return;
}
std::string savename(savegamemenu_filelistview->selected()->value().c_str());
if (!savename.size()) {
savegamemenu_descrtitle->set_text("New savegame");
savegamemenu_confirmsavebutton->set_text("Save");
} else {
savegamemenu_descrtitle->set_text("Overwrite game " + savename + "?");
savegamemenu_confirmsavebutton->set_text("Overwrite");
}
savegamemenu_savebutton->hide();
savegamemenu_confirmsavebutton->show();
savegamemenu_cancelbutton->show();
}
void SaveGameMenu::show_load_dialog()
{
if (!savegamemenu_filelistview->selected()) {
return;
}
std::string savename(savegamemenu_filelistview->selected()->value().c_str());
if (!savename.size()) {
return;
}
savegamemenu_descrtitle->set_text("Load game " + savename + "?");
savegamemenu_confirmsavebutton->set_text("Load");
savegamemenu_savebutton->hide();
savegamemenu_confirmsavebutton->show();
savegamemenu_cancelbutton->show();
}
void SaveGameMenu::show_delete_dialog()
{
if (!savegamemenu_filelistview->selected()) {
return;
}
std::string savename(savegamemenu_filelistview->selected()->value().c_str());
if (!savename.size()) {
return;
}
savegamemenu_descrtitle->set_text(savename);
savegamemenu_descrtitle->set_text("Delete game " + savename + "?");
savegamemenu_savebutton->hide();
savegamemenu_confirmdeletebutton->show();
savegamemenu_cancelbutton->show();
}
bool SaveGameMenu::on_keypress(const int key, const unsigned int modifier)
{
if (key == SDLK_ESCAPE) {
if (visible()) {
if (savegamemenu_cancelbutton->visible()) {
show_file_info();
return true;
} else {
// hide the parent mainwindow
parent()->hide();
return true;
}
}
}
return false;
}
bool SaveGameMenu::on_emit(ui::Widget *sender, const ui::Widget::Event event, void *data)
{
if (sender == savegamemenu_closebutton) {
if (event == ui::Widget::EventButtonClicked) {
// hide the parent mainwindow
parent()->hide();
return true;
}
} else if (sender == savegamemenu_cancelbutton) {
if (event == ui::Widget::EventButtonClicked) {
show_file_info();
}
} else if (sender == savegamemenu_confirmsavebutton) {
if (event == ui::Widget::EventButtonClicked) {
if (savegamemenu_mode == Save) {
if (savegamemenu_filelistview->selected()) {
std::string descr;
savegame(savegamemenu_filelistview->selected()->value(), descr);
parent()->hide();
}
} else {
if (savegamemenu_filelistview->selected()) {
loadgame(savegamemenu_filelistview->selected()->value());
parent()->hide();
}
}
return true;
}
} else if (sender == savegamemenu_savebutton) {
if (event == ui::Widget::EventButtonClicked) {
if (savegamemenu_mode == Save) {
show_save_dialog();
} else {
show_load_dialog();
}
return true;
}
} else if (sender == savegamemenu_confirmdeletebutton) {
if (event == ui::Widget::EventButtonClicked) {
if (savegamemenu_filelistview->selected()) {
deletegame(savegamemenu_filelistview->selected()->value());
refresh();
}
return true;
}
} else if (sender == savegamemenu_deletebutton) {
if (event == ui::Widget::EventButtonClicked) {
if (savegamemenu_filelistview->selected()) {
show_delete_dialog();
}
return true;
}
} else if (sender == savegamemenu_filelistview) {
if (event == ui::Widget::EventListViewChanged) {
show_file_info();
return true;
}
}
return Window::on_emit(sender, event, data);
}
void SaveGameMenu::get_savegame_info(const std::string & savename,
std::string &game_descr,
std::string &game_info,
std::string &game_timestamp)
{
filesystem::IniFile inifile("savegames/" + savename);
std::string infostr;
game_descr.clear();
game_info.clear();
game_timestamp.clear();
if (!inifile.is_open()) {
return;
}
while(inifile.getline()) {
if (inifile.got_key()) {
if (inifile.in_section("savegame")) {
if (inifile.got_key_string("description", game_descr)) {
continue;
} else if (inifile.got_key_string("timestamp", game_timestamp)) {
continue;
} else if (inifile.got_key_string("info", infostr)) {
game_info.append(infostr);
game_info += '\n';
continue;
}
}
}
}
inifile.close();
}
void SaveGameMenu::deletegame(std::string savename)
{
std::string filename(filesystem::writedir() + "savegames/" + savename + ".ini");
con_debug << "Deleteing " << filename << std::endl;
::remove(filename.c_str());
}
void SaveGameMenu::savescreenshot(std::string savename)
{
std::string filename(filesystem::writedir() + "savegames/" + savename + ".jpg");
// redraw the world without ui
/*
hack: win32 seems to have triple buffering or reads pixels from the wrong buffer
the workaround is to render the world twice
*/
#ifdef __WIN32
for (size_t i = 0; i < 2; ++i) {
#endif
// Clear the color and depth buffers.
gl::clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// set camera transformation
render::camera().frame(0.0f);
render::camera().draw();
// draw the world
render::draw(0.0f);
gl::disable(GL_TEXTURE_2D);
gl::disable(GL_BLEND);
video::swap_buffers();
#ifdef __WIN32
}
#endif
render::Screenshot::savegameshot(filename);
}
void SaveGameMenu::savegame(std::string savename, const std::string & description)
{
if (!core::server() || !core::server()->module()) {
return;
}
if (!core::localcontrol()) {
return;
}
// find an approriate savegame name if none was specified
if (!savename.size()) {
std::string filename;
unsigned long n = 0;
do {
n++;
std::ostringstream numberstr;
numberstr << std::setfill('0') << std::setw(4) << n;
savename.assign("save");
savename.append(numberstr.str());
filename.assign(filesystem::writedir() + "savegames/" + savename + ".ini");
} while (sys::file_exists(filename));
}
// create savegame diretory if required
std::string savegamepath(filesystem::writedir());
savegamepath.append("savegames");
if (!sys::directory_exists(savegamepath)) {
sys::mkdir(savegamepath);
}
filesystem::OFileStream ofs("savegames/" + savename + ".ini");
core::application()->notify_message(core::Message::Info, "Saving " + savename);
con_debug << "Saving " << ofs.filename() << std::endl;
ofs << "; Project::OSiRiON" << std::endl;
ofs << "; savegame data" << std::endl;
ofs << std::endl;
ofs << "[savegame]" << std::endl;
ofs << "description=";
if (description.size()) {
ofs << description << std::endl;
} else {
if (core::localcontrol()->state() == core::Entity::Docked) {
// FIXME implement entity->dock()
ofs << core::localplayer()->view()->name() << std::endl;
} else {
ofs << core::localplayer()->zone()->name() << std::endl;
}
}
ofs << "info=";
if (core::localcontrol()->state() == core::Entity::Docked) {
// FIXME implement entity->dock()
ofs << core::localplayer()->view()->name() << ", ";
}
ofs << core::localplayer()->zone()->name() << std::endl;
if (core::localcontrol()->info()) {
ofs << "info=" << core::localcontrol()->info()->name() << std::endl;
}
ofs << "info=" << core::localplayer()->credits() << " credits" << std::endl;
int year = 0; int month = 0; int day = 0; int hours = 0; int minutes = 0; int seconds = 0; int msec = 0;
sys::get_localtime(year, month, day, hours, minutes, seconds, msec);
std::ostringstream timestampstr;
timestampstr << std::setfill('0') << std::setw(4) << year << "-" << std::setw(2) << month << "-" << std::setw(2) << day << " ";
timestampstr << std::setw(2) << hours << ":" << std::setw(2) << minutes << ":" << std::setw(2) << seconds;
ofs << "timestamp=" << timestampstr.str() << std::endl;
ofs << std::endl;
core::server()->module()->game_save(core::localplayer(), ofs);
ofs.close();
// save screenshot
savescreenshot(savename);
}
void SaveGameMenu::loadgame(std::string savename) {
filesystem::IniFile inifile("savegames/" + savename);
if (!inifile.is_open()) {
con_warn << "Could not open savegames/" + savename << ".ini" << std::endl;
return;
}
if (core::application()->connected())
core::application()->disconnect();
core::application()->connect("");
con_debug << "Loading " << inifile.filename() << std::endl;
core::application()->notify_message(core::Message::Info, "Loading " + savename);
if (core::application()->connected() && core::server() && core::server()->module()) {
core::server()->module()->game_load(core::localplayer(), inifile);
}
inifile.close();
}
} // namespace client
|