Skip to content

Commit 8b6604b

Browse files
committed
starting work to load an save games;
1 parent bd0ce4f commit 8b6604b

File tree

6 files changed

+71
-9
lines changed

6 files changed

+71
-9
lines changed

src/Item.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ struct ItemCallbacks
9999
struct ItemInstanceInfo
100100
{
101101
std::string objid; // the id of the object this is an instance of
102+
std::optional<std::uint32_t> index; // index of the object in an "instances" array
102103

103104
// a null x,y means that the coordinate was not specified,
104105
// and a value of -1 means it should be picked randomly
@@ -112,6 +113,14 @@ struct ItemInstanceInfo
112113

113114
ItemCallbacks callbacks;
114115

116+
ItemInstanceInfo() = default;
117+
ItemInstanceInfo(std::uint32_t idx)
118+
: index{idx}
119+
{
120+
// nothing to do
121+
}
122+
123+
115124
// will apply the default values if they are set in the `defaults` object
116125
// and *not* set in this object.
117126
// NOTE: We cannot use a constructor since these objects are constructed

src/Scenes/Scene.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#include <boost/range/adaptor/indexed.hpp>
2+
13
#include <lua.hpp>
24

35
#include <fmt/core.h>
@@ -711,13 +713,15 @@ void Scene::createItems()
711713
// default info for the item
712714
ItemInstanceInfo groupinfo = data.get<ItemInstanceInfo>();
713715
groupinfo.objid = itemid; // we don't want to require the objid to be set in json
714-
715-
for (const auto& instance : data["instances"])
716+
717+
for (const auto& instance : (data["instances"] | boost::adaptors::indexed()))
716718
{
717-
auto instanceinfo = instance.get<ItemInstanceInfo>();
719+
auto instanceinfo = instance.value().get<ItemInstanceInfo>();
720+
instanceinfo.index = instance.index();
721+
718722
instanceinfo.applyDefaults(groupinfo);
719723

720-
auto groupcallbacks = instance.get<ItemCallbacks>();
724+
auto groupcallbacks = instance.value().get<ItemCallbacks>();
721725
auto item = _itemFactory.createItem(instanceinfo);
722726
if (item) placeItem(item);
723727
}

src/Screens/IntroScreen.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -245,11 +245,6 @@ PollResult IntroScreen::poll(const sf::Event& e)
245245
break;
246246

247247
case 0: // new game
248-
{
249-
return {true, { ScreenActionType::CHANGE_SCREEN, SCREEN_LOADING }};
250-
}
251-
252-
case 1: // load game
253248
{
254249
_gui = std::make_unique<tgui::Gui>(_window);
255250

@@ -297,6 +292,11 @@ PollResult IntroScreen::poll(const sf::Event& e)
297292
break;
298293
}
299294

295+
case 1: // load game
296+
{
297+
return {true, { ScreenActionType::CHANGE_SCREEN, SCREEN_LOADING }};
298+
}
299+
300300
case 2: // settings
301301
{
302302
return {true, { ScreenActionType::CHANGE_SCREEN, SCREEN_SETTINGS }};

src/TTUtils.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# include <windows.h>
33
# include <shellapi.h>
44
# include <Shlobj.h>
5+
# include <codecvt>
56
#else
67
# include <unistd.h>
78
# include <sys/types.h>
@@ -156,4 +157,18 @@ retval = pw->pw_dir;
156157
return retval;
157158
}
158159

160+
std::string getDataFolder()
161+
{
162+
std::string retval;
163+
164+
#ifdef _WINDOWS
165+
retval = getWindowsFolder(CSIDL_COMMON_APPDATA);
166+
#else
167+
struct passwd *pw = getpwuid(getuid());
168+
retval = pw->pw_dir;
169+
#endif
170+
171+
return retval;
172+
}
173+
159174
} // namespace tt

src/TTUtils.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#include <iterator>
77
#include <cmath>
88

9+
#include <fmt/core.h>
10+
911
#include <boost/spirit/home/x3.hpp>
1012

1113
#include <nlohmann/json.hpp>
@@ -14,6 +16,16 @@
1416

1517
namespace nl = nlohmann;
1618

19+
// There's some weirdness going on in Ubuntu where using the / operator
20+
// on Ubuntu was throwing an error in some instances. Instead I set out
21+
// to use boost::filesystem::path::seperator but that turned out to be
22+
// a pain since it is multibyte on Windows! So I did this manually.
23+
#ifdef _WINDOWS
24+
# define PATH_SEPERATOR '\\'
25+
#else
26+
# define PATH_SEPERATOR '/'
27+
#endif
28+
1729
namespace sf
1830
{
1931

@@ -165,5 +177,7 @@ inline float distance(const sf::Vector2f& v1, const sf::Vector2f& v2)
165177
void openBrowser(const std::string& url_str);
166178
std::string getOsString();
167179
std::string getUserFolder();
180+
std::string getDataFolder();
181+
168182

169183
} // namespace tt

src/main.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ amb::SettingsPtr registerSettings()
6363
retval->registerBool("logs.sfx.enabled", false);
6464
retval->registerBool("video.fullscreen", true);
6565

66+
// default gamedb folder
67+
const std::string dbfolder = fmt::format("{}{}{}",
68+
tt::getDataFolder(), PATH_SEPERATOR, "TommyTuscon");
69+
70+
retval->registerString("database.gamefolder", dbfolder,
71+
std::make_shared<amb::NotEmptyValidator>());
72+
6673
return retval;
6774
}
6875

@@ -103,6 +110,15 @@ void initLogging(std::string_view logfile)
103110
}
104111
}
105112

113+
void initGameFolder(const std::string& gamefolder)
114+
{
115+
fs::path folder = gamefolder;
116+
if (!fs::exists(folder))
117+
{
118+
fs::create_directories(folder);
119+
}
120+
}
121+
106122
int main(int argc, char *argv[])
107123
{
108124
po::options_description desc("Allowed options");
@@ -184,6 +200,9 @@ int main(int argc, char *argv[])
184200
return 1;
185201
}
186202

203+
// make sure the game folder exists before we start up
204+
initGameFolder(settings->value("database.gamefolder", ""s));
205+
187206
std::size_t width = window_width;
188207
std::size_t height = window_height;
189208
if (vm.count("window-size") > 0)
@@ -264,6 +283,7 @@ int main(int argc, char *argv[])
264283
win->display();
265284
}
266285

286+
settings->save();
267287
logger->info("game shut down");
268288
return 0;
269289
}

0 commit comments

Comments
 (0)