Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit 97e9dfa

Browse files
authored
Remove bytestream from Asset interface and derived assets (#15)
* Remove bytestream from Asset interface and derived assets * Fix formatting * Fix asset loading * update text asset * Update TextAsset.hpp * Update engine.cpp * Change return type of Load to bool * return true; * Split TextAsset::Load into a source file * Make paths static * formating * Asset loader fix
1 parent 3dbd1d2 commit 97e9dfa

File tree

4 files changed

+31
-52
lines changed

4 files changed

+31
-52
lines changed

lib/include/tkge/Assets/IAsset.hpp

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -135,40 +135,6 @@ namespace tkge::Assets
135135
{
136136
public:
137137
virtual ~IAsset() = default;
138-
139-
protected:
140-
[[nodiscard]] virtual ReadonlyByteStream& byteStream() noexcept = 0;
141-
[[nodiscard]] virtual const ReadonlyByteStream& byteStream() const noexcept = 0;
142-
};
143-
144-
template <typename T>
145-
class IMoveableAsset : public virtual IAsset
146-
{
147-
public:
148-
explicit IMoveableAsset(std::string filename) : _byteStream(std::move(filename)) {}
149-
// static_assert(!std::is_base_of_v<ICopyableAsset<T>, T>, "Cannot derive from both move and copy assets!");
150-
~IMoveableAsset() override = default;
151-
[[nodiscard]] ReadonlyByteStream& byteStream() noexcept override { return this->_byteStream; }
152-
[[nodiscard]] const ReadonlyByteStream& byteStream() const noexcept override { return this->_byteStream; }
153-
154-
private:
155-
ReadonlyByteStream _byteStream;
156-
157-
friend T;
158-
};
159-
template <typename T>
160-
class ICopyableAsset : public virtual IAsset
161-
{
162-
public:
163-
explicit ICopyableAsset(std::string filename) : _byteStream(std::make_shared<ReadonlyByteStream>(std::move(filename))) {}
164-
// static_assert(!std::is_base_of_v<IMoveableAsset<T>, T>, "Cannot derive from both move and copy assets!");
165-
~ICopyableAsset() override = default;
166-
[[nodiscard]] ReadonlyByteStream& byteStream() noexcept override { return *this->_byteStream; }
167-
[[nodiscard]] const ReadonlyByteStream& byteStream() const noexcept override { return *this->_byteStream; }
168-
169-
private:
170-
std::shared_ptr<ReadonlyByteStream> _byteStream;
171-
172-
friend T;
138+
virtual bool Load(ReadonlyByteStream) = 0;
173139
};
174140
} // namespace tkge::Assets

lib/include/tkge/Assets/TextAsset.hpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44

55
namespace tkge::Assets
66
{
7-
class TextAsset final : public ICopyableAsset<TextAsset>
7+
class TextAsset final : public IAsset
88
{
99
public:
10-
explicit TextAsset(std::string filename) : ICopyableAsset(std::move(filename)) {}
11-
[[nodiscard]] std::string ReadAllText() const;
12-
[[nodiscard]] std::string ReadAt(std::size_t position, std::size_t size) const;
10+
explicit TextAsset() = default;
11+
bool Load(ReadonlyByteStream byteStream) override;
12+
13+
[[nodiscard]] const std::string& Text() const noexcept { return this->_text; }
14+
15+
private:
16+
std::string _text;
1317
};
1418
} // namespace tkge::Assets

lib/include/tkge/assetLoader.hpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,33 @@ namespace tkge
1010
class AssetLoader
1111
{
1212
public:
13+
explicit AssetLoader() : _paths(this->GetSearchPaths()) {}
14+
AssetLoader(const AssetLoader&) = delete;
15+
AssetLoader(AssetLoader&&) = default;
16+
AssetLoader& operator=(const AssetLoader&) = delete;
17+
AssetLoader& operator=(AssetLoader&&) = default;
18+
~AssetLoader() = default;
19+
1320
template <typename T>
1421
requires std::is_base_of_v<Assets::IAsset, T>
1522
std::unique_ptr<T> LoadAsset(const std::string& fileName) const
1623
{
17-
const std::vector<std::filesystem::path> paths = this->GetSearchPaths();
18-
19-
for (const auto& path : paths)
24+
for (const auto& path : this->_paths)
2025
{
21-
if (std::filesystem::exists(path / fileName)) { return std::make_unique<T>((path / fileName).string()); }
26+
if (std::filesystem::exists(path / fileName))
27+
{
28+
auto asset = std::make_unique<T>();
29+
asset->Load(Assets::ReadonlyByteStream{(path / fileName).string()});
30+
return asset;
31+
}
2232
}
2333

2434
throw std::runtime_error("Asset not found: " + fileName);
2535
}
2636

2737
[[nodiscard]] std::vector<std::filesystem::path> GetSearchPaths() const;
38+
39+
private:
40+
std::vector<std::filesystem::path> _paths;
2841
};
2942
} // namespace tkge

lib/src/Assets/TextAsset.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
#include <tkge/Assets/TextAsset.hpp>
2-
std::string tkge::Assets::TextAsset::ReadAllText() const
3-
{
4-
const auto vData = this->byteStream().AsSpan<char>(0, this->byteStream().GetStreamSize());
5-
return std::string{vData.begin(), vData.end()};
6-
}
72

8-
std::string tkge::Assets::TextAsset::ReadAt(std::size_t position, std::size_t size) const
3+
bool tkge::Assets::TextAsset::Load(ReadonlyByteStream byteStream)
94
{
10-
const auto vData = this->byteStream().AsSpan<char>(position, size);
11-
return std::string{vData.begin(), vData.end()};
12-
}
5+
const auto vData = byteStream.AsSpan<char>(0, byteStream.GetStreamSize());
6+
this->_text = std::string{vData.begin(), vData.end()};
7+
return true;
8+
}

0 commit comments

Comments
 (0)