Skip to content

Commit 832e425

Browse files
committed
New SEngineSettings struct. Works similarly to the
SLevelSettings struct, where it just holds info about how the engine should be setup, and info about the current state of the game (like CLevel's SLevelSettings data being owned by it. CLevel does not actually "own" that data, it just has a raw pointer to it).
1 parent b359024 commit 832e425

13 files changed

Lines changed: 138 additions & 84 deletions

include/Tails/AssetCache.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define TAILS_ASSETCACHE_HPP
33

44
#include <Tails/Config.hpp>
5+
#include <Tails/Debug.hpp>
56

67
#include <unordered_map>
78
#include <string>
@@ -32,7 +33,10 @@ namespace tails
3233
if (!get().m_data.contains(name)) return;
3334

3435
if (get().m_data[name].expired())
36+
{
3537
get().m_data.erase(name);
38+
CDebug::print("Erased asset \"", name, "\"");
39+
}
3640
};
3741

3842
std::shared_ptr<T> data {new T, std::bind(deleter, id)};

include/Tails/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@ target_sources(
2323
EngineRegistry.hpp
2424
Debug.hpp
2525
Vector2.hpp
26+
EngineSettings.hpp
2627
)

include/Tails/Engine.hpp

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
namespace tails
1818
{
1919
class CClassRegistry;
20+
struct SEngineSettings;
2021

2122
struct TAILS_API SWindowProperties final
2223
{
@@ -36,12 +37,8 @@ namespace tails
3637
class TAILS_API CEngine final : public CObject, public ITickable, public sf::Drawable
3738
{
3839
public:
39-
/**
40-
* Sets up engine with "engine.json" as engine setup file
41-
*/
4240
CEngine();
43-
explicit CEngine(const std::string& engineSetupFile,
44-
std::vector<std::unique_ptr<CClassRegistry>>&& registries);
41+
explicit CEngine(std::unique_ptr<SEngineSettings> engineSettings);
4542
~CEngine() override;
4643

4744
template<typename T, typename... Args>
@@ -64,26 +61,7 @@ namespace tails
6461
[[nodiscard]] const sf::View& getRenderView() const {return m_renderView;}
6562
[[nodiscard]] CWorld& getWorld() {return m_world;}
6663
[[nodiscard]] const CWorld& getWorld() const {return m_world;}
67-
68-
template<typename T>
69-
[[nodiscard]] bool registryExists() const
70-
{
71-
return getRegistry<T>();
72-
}
73-
74-
template<typename T>
75-
[[nodiscard]] T* getRegistry() const
76-
{
77-
if (m_registries.empty()) return nullptr;
78-
79-
for (auto& registry : m_registries)
80-
{
81-
if (auto castedRegistry = dynamic_cast<T*>(registry.get()))
82-
return castedRegistry;
83-
}
84-
85-
return nullptr;
86-
}
64+
[[nodiscard]] SEngineSettings& getSettings() const {return *m_settings;}
8765

8866
protected:
8967
void preTick() override;
@@ -92,7 +70,9 @@ namespace tails
9270
void postTick() override;
9371

9472
private:
73+
void initMembers();
9574
void initInternalRender();
75+
void initWorldLevel(std::string path);
9676
void calculateInternalAspectRatio(sf::Vector2u windowSize);
9777

9878
/**
@@ -126,14 +106,9 @@ namespace tails
126106

127107
SRenderProperties m_renderProperties;
128108
SWindowProperties m_windowProperties;
109+
std::unique_ptr<SEngineSettings> m_settings;
129110

130111
float m_lifeTime {0.f};
131-
132-
/**
133-
* The registries that handle all the serialisation stuff for engine and custom
134-
* classes. They are set in the engine's constructor, and cannot be added after.
135-
*/
136-
std::vector<std::unique_ptr<CClassRegistry>> m_registries;
137112
};
138113
}
139114

include/Tails/EngineSettings.hpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#ifndef TAILS_ENGINESETTINGS_HPP
2+
#define TAILS_ENGINESETTINGS_HPP
3+
4+
#include <Tails/Config.hpp>
5+
6+
#include <string>
7+
#include <vector>
8+
#include <memory>
9+
10+
namespace tails
11+
{
12+
class CClassRegistry;
13+
struct SLevelSettings;
14+
class CEngine;
15+
16+
/**
17+
* Holds data for general engine setup and game-specific classes
18+
*/
19+
struct TAILS_API SEngineSettings
20+
{
21+
friend CEngine;
22+
23+
SEngineSettings();
24+
25+
[[nodiscard]] const std::string& getSetupFilePath() const {return m_setupFilePath;}
26+
27+
template<typename T>
28+
[[nodiscard]] T* getRegistry() const
29+
{
30+
if (m_registries.empty()) return nullptr;
31+
32+
for (auto& registry : m_registries)
33+
{
34+
if (auto castedRegistry = dynamic_cast<T*>(registry.get()))
35+
return castedRegistry;
36+
}
37+
38+
return nullptr;
39+
}
40+
41+
template<typename T>
42+
[[nodiscard]] bool registryExists() const
43+
{
44+
return getRegistry<T>();
45+
}
46+
47+
[[nodiscard]] SLevelSettings& getLevelSettings() const {return *m_levelSettings;}
48+
49+
private:
50+
std::string m_setupFilePath;
51+
std::vector<std::unique_ptr<CClassRegistry>> m_registries;
52+
std::unique_ptr<SLevelSettings> m_levelSettings;
53+
};
54+
}
55+
56+
#endif // TAILS_ENGINESETTINGS_HPP

include/Tails/Level.hpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ namespace tails
6363
CLevel(std::string path);
6464

6565
void open();
66-
void setSettings(std::unique_ptr<SLevelSettings> settings);
66+
void setSettings(SLevelSettings* settings);
6767

6868
void preTick() override;
6969
void tick(float deltaTime) override;
@@ -80,7 +80,12 @@ namespace tails
8080
CEntity* spawnEntityImpl(std::unique_ptr<CEntity> entity, const sf::Vector2f& position, float rotation, const sf::Vector2f& scale);
8181

8282
std::string m_path;
83-
std::unique_ptr<SLevelSettings> m_settings;
83+
84+
/**
85+
* Settings for the level (could be anything, like gravity scale, day length, etc.).
86+
* Non-owning raw pointer because SEngineSettings owns the data for this
87+
*/
88+
SLevelSettings* m_settings {nullptr};
8489

8590
std::vector<std::unique_ptr<CEntity>> m_entities;
8691

include/Tails/World.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace tails
2222
friend CEngine;
2323

2424
public:
25-
CLevel* openLevel(std::string path, std::unique_ptr<SLevelSettings> settings = nullptr);
25+
CLevel* openLevel(std::string path, SLevelSettings* settings = nullptr);
2626
bool closeLevel(CLevel* level);
2727

2828
[[nodiscard]] CEngine& getEngine() const;

src/Tails/AssetCache.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,19 @@ namespace tails
3333
const std::string& id, const std::string& path, const std::shared_ptr<IAssetData>& data)
3434
{
3535
// TODO - debug print on fail load
36-
data->load(CDirectories::getDirectory(data->getType()) + path);
36+
if (!data->load(CDirectories::getDirectory(data->getType()) + path))
37+
{
38+
// SFML has a print for this already, we're ok to not print this I think
39+
//CDebug::print("Failed to load path ", path);
40+
return nullptr;
41+
}
3742

3843
if (get().m_data.contains(id))
3944
get().m_data[id] = data;
4045
else
4146
get().m_data.try_emplace(id, data);
47+
48+
CDebug::print("Loaded \"", id, "\" {type: ", data->getType(), ", path: \"", path, "\"}");
4249

4350
return data;
4451
}

src/Tails/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ add_library(
1818
ClassRegistry.cpp
1919
EngineRegistry.cpp
2020
Debug.cpp
21+
EngineSettings.cpp
2122
)

src/Tails/Engine.cpp

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <Tails/Debug.hpp>
66
#include <Tails/Vector2.hpp>
77
#include <Tails/LevelSettings.hpp>
8+
#include <Tails/EngineSettings.hpp>
89

910
#include <SFML/Graphics/Sprite.hpp>
1011
#include <SFML/Graphics/RenderWindow.hpp>
@@ -18,73 +19,57 @@ namespace tails
1819
{
1920
std::string SWindowProperties::toString() const
2021
{
21-
return "Window properties:\nTitle - " + title + "\nResolution - " + SVector2u::toString(resolution);
22+
return "Window properties:\nTitle - \"" + title + "\"\nResolution - " + SVector2u::toString(resolution);
2223
}
2324

2425
std::string SRenderProperties::toString() const
2526
{
2627
return "Render properties:\nResolution - " + SVector2u::toString(resolution);
2728
}
2829

29-
// removed make_unique for engine registry here because it was causing compile errors
30-
// to do with unique_ptr's deleted copy-constructor (I think). Couldn't fix it.
31-
CEngine::CEngine() : CEngine("engine.json", {})
30+
CEngine::CEngine() : CEngine(nullptr)
3231
{
3332
}
3433

35-
CEngine::CEngine(const std::string& engineSetupFile,
36-
std::vector<std::unique_ptr<CClassRegistry>>&& registries)
37-
: m_registries(std::move(registries))
34+
CEngine::CEngine(std::unique_ptr<SEngineSettings> engineSettings)
35+
: m_settings(std::move(engineSettings))
3836
{
39-
// TODO - replace the above paramters with some SEngineSettings struct:
40-
/*
41-
* struct SEngineSettings
42-
* {
43-
* string setupFile;
44-
* registries;
45-
* levelSettings;??
46-
* }
47-
*
48-
* And maybe a templated constructor for this? or just take in a unique_ptr?
49-
* so clients can derive the struct to add their own stuff. how would they use
50-
* that stuff? no idea lol. maybe not. Would be nice to have a custom levelsettings
51-
* struct though!
52-
*/
53-
if (!getRegistry<CEngineRegistry>())
54-
m_registries.emplace_back(std::make_unique<CEngineRegistry>());
37+
// If input engine settings is null, use default
38+
if (!engineSettings)
39+
m_settings = std::make_unique<SEngineSettings>();
5540

5641
// Setup world
5742
m_world.outer = this;
5843

59-
CDebug::print("Loading ", engineSetupFile);
44+
CDebug::print("Loading ", m_settings->getSetupFilePath());
6045

61-
std::ifstream setupFile {engineSetupFile};
46+
std::ifstream setupFile {m_settings->getSetupFilePath()};
6247
if (!setupFile.is_open())
6348
{
64-
CDebug::print("Failed to find ", engineSetupFile);
65-
initInternalRender();
66-
m_world.openLevel("");
49+
CDebug::print("Failed to find ", m_settings->getSetupFilePath());
50+
initMembers();
6751
return;
6852
}
6953

7054
nlohmann::json setupJson = nlohmann::json::parse(setupFile);
7155

7256
if (setupJson.is_null())
7357
{
74-
CDebug::print("Failed to load ", engineSetupFile);
75-
initInternalRender();
76-
m_world.openLevel("");
58+
CDebug::print("Failed to load ", m_settings->getSetupFilePath());
59+
initMembers();
7760
return;
7861
}
7962

8063
CDebug::print(std::string("JSON is a valid "), setupJson.type_name());
64+
CDebug::print();
8165

8266
/* DIRECTORIES */
8367

8468
if (const auto& dirJson = setupJson["dirs"]; !dirJson.is_null())
8569
CDirectories::loadDirectories(dirJson);
8670
else
8771
CDebug::print("Failed to load directories, using default");
72+
CDebug::print();
8873

8974
/* RENDER SETTINGS */
9075

@@ -111,15 +96,15 @@ namespace tails
11196
/* WORLD AND LEVEL */
11297

11398
if (const auto& defaultLevelJson = setupJson["default_level"]; !defaultLevelJson.is_null())
114-
m_world.openLevel(defaultLevelJson.get<std::string>());
99+
initWorldLevel(defaultLevelJson.get<std::string>());
115100
else
116101
{
117102
CDebug::print("Failed to open default level, opening blank level");
118-
m_world.openLevel("");
103+
initWorldLevel("");
119104
}
120105

121106
if (const auto level = m_world.getLevel(0))
122-
CDebug::print("Opened level - ", level->getSettings().name);
107+
CDebug::print("Opened level - \"", level->getSettings().name, "\"");
123108
else
124109
CDebug::error("Created level is invalid!");
125110
CDebug::print();
@@ -148,7 +133,7 @@ namespace tails
148133
CDebug::print(m_windowProperties.toString());
149134
CDebug::print();
150135

151-
CDebug::print(engineSetupFile, " loaded");
136+
CDebug::print(m_settings->getSetupFilePath(), " loaded");
152137
CDebug::print();
153138
}
154139

@@ -160,6 +145,7 @@ namespace tails
160145

161146
void CEngine::run()
162147
{
148+
CDebug::print();
163149
CDebug::print("Initialising final render target");
164150
// Set default render target as window if it has not already been set
165151
if (!m_renderTarget)
@@ -170,6 +156,7 @@ namespace tails
170156
),
171157
m_windowProperties.title
172158
);
159+
CDebug::print();
173160

174161
sf::Clock clock;
175162
const auto window = dynamic_cast<sf::RenderWindow*>(m_renderTarget.get());
@@ -258,6 +245,12 @@ namespace tails
258245
m_world.postTick();
259246
}
260247

248+
void CEngine::initMembers()
249+
{
250+
initInternalRender();
251+
initWorldLevel("");
252+
}
253+
261254
void CEngine::initInternalRender()
262255
{
263256
CDebug::print("Initialising internal render target");
@@ -272,6 +265,11 @@ namespace tails
272265
m_renderView.setCenter(m_renderView.getSize() * 0.5f);
273266
}
274267

268+
void CEngine::initWorldLevel(std::string path)
269+
{
270+
m_world.openLevel(std::move(path), &m_settings->getLevelSettings());
271+
}
272+
275273
void CEngine::calculateInternalAspectRatio(sf::Vector2u windowSize)
276274
{
277275
const sf::Vector2f ratio {

src/Tails/EngineSettings.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <Tails/EngineSettings.hpp>
2+
#include <Tails/EngineRegistry.hpp>
3+
#include <Tails/LevelSettings.hpp>
4+
5+
namespace tails
6+
{
7+
SEngineSettings::SEngineSettings()
8+
: m_setupFilePath("engine.json"), m_levelSettings(std::make_unique<SLevelSettings>())
9+
{
10+
m_registries.emplace_back(std::make_unique<CEngineRegistry>());
11+
}
12+
}

0 commit comments

Comments
 (0)