summaryrefslogtreecommitdiff
path: root/src/SSVOpenHexagon/Global/AssetStorage.cpp
blob: 22fb2c74d98679d42086cbe28ac6357e76fb13b2 (plain)
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
// Copyright (c) 2013-2020 Vittorio Romeo
// License: Academic Free License ("AFL") v. 3.0
// AFL License page: https://opensource.org/licenses/AFL-3.0

#include "SSVOpenHexagon/Global/AssetStorage.hpp"

#include "SSVOpenHexagon/Global/Assert.hpp"
#include "SSVOpenHexagon/Utils/Concat.hpp"

#include <SFML/Graphics/Font.hpp>
#include <SFML/Graphics/Image.hpp>
#include <SFML/Graphics/Texture.hpp>

#include <SFML/Audio/SoundBuffer.hpp>

#include <string>
#include <memory>
#include <unordered_map>

namespace hg {

template <typename Map, typename Key, typename F>
[[nodiscard]] static decltype(auto) tryEmplaceAndThen(
    Map& map, const Key& key, F&& f)
{
    auto [it, inserted] =
        map.emplace(std::piecewise_construct, std::tuple{key}, std::tuple{});

    return inserted && f(it->second);
}

template <typename Map, typename Key>
[[nodiscard]] static decltype(auto) tryEmplaceAndThenLoadFromFile(
    Map& map, const Key& key, const std::string& path)
{
    return tryEmplaceAndThen(
        map, key, [&path](auto& value) { return value.loadFromFile(path); });
}

template <typename Map, typename Key>
[[nodiscard]] static auto* getAsPtr(Map& map, const Key& key) noexcept
{
    auto it = map.find(key);
    return it == map.end() ? nullptr : &it->second;
}

class AssetStorage::AssetStorageImpl
{
private:
    std::unordered_map<std::string, sf::Texture> _textures;
    std::unordered_map<std::string, sf::Font> _fonts;
    std::unordered_map<std::string, sf::SoundBuffer> _soundBuffers;

public:
    [[nodiscard]] bool loadTexture(
        const std::string& id, const std::string& path)
    {
        return tryEmplaceAndThenLoadFromFile(_textures, id, path);
    }

    [[nodiscard]] bool loadFont(const std::string& id, const std::string& path)
    {
        return tryEmplaceAndThenLoadFromFile(_fonts, id, path);
    }

    [[nodiscard]] bool loadSoundBuffer(
        const std::string& id, const std::string& path)
    {
        return tryEmplaceAndThenLoadFromFile(_soundBuffers, id, path);
    }

    [[nodiscard]] sf::Texture* getTexture(const std::string& id) noexcept
    {
        return getAsPtr(_textures, id);
    }

    [[nodiscard]] sf::Font* getFont(const std::string& id) noexcept
    {
        return getAsPtr(_fonts, id);
    }

    [[nodiscard]] sf::SoundBuffer* getSoundBuffer(
        const std::string& id) noexcept
    {
        return getAsPtr(_soundBuffers, id);
    }

    [[nodiscard]] bool hasTexture(const std::string& id) noexcept
    {
        return _textures.find(id) != _textures.end();
    }

    [[nodiscard]] bool hasFont(const std::string& id) noexcept
    {
        return _fonts.find(id) != _fonts.end();
    }

    [[nodiscard]] bool hasSoundBuffer(const std::string& id) noexcept
    {
        return _soundBuffers.find(id) != _soundBuffers.end();
    }
};

[[nodiscard]] const AssetStorage::AssetStorageImpl&
AssetStorage::impl() const noexcept
{
    SSVOH_ASSERT(_impl != nullptr);
    return *_impl;
}

[[nodiscard]] AssetStorage::AssetStorageImpl& AssetStorage::impl() noexcept
{
    SSVOH_ASSERT(_impl != nullptr);
    return *_impl;
}

AssetStorage::AssetStorage() : _impl{std::make_unique<AssetStorageImpl>()}
{}

AssetStorage::~AssetStorage() = default;

[[nodiscard]] bool AssetStorage::loadTexture(
    const std::string& id, const std::string& path)
{
    return impl().loadTexture(id, path);
}

[[nodiscard]] bool AssetStorage::loadFont(
    const std::string& id, const std::string& path)
{
    return impl().loadFont(id, path);
}

[[nodiscard]] bool AssetStorage::loadSoundBuffer(
    const std::string& id, const std::string& path)
{
    return impl().loadSoundBuffer(id, path);
}

[[nodiscard]] sf::Texture* AssetStorage::getTexture(
    const std::string& id) noexcept
{
    return impl().getTexture(id);
}

[[nodiscard]] sf::Font* AssetStorage::getFont(const std::string& id) noexcept
{
    return impl().getFont(id);
}

[[nodiscard]] sf::SoundBuffer* AssetStorage::getSoundBuffer(
    const std::string& id) noexcept
{
    return impl().getSoundBuffer(id);
}

[[nodiscard]] bool AssetStorage::hasTexture(const std::string& id) noexcept
{
    return impl().hasTexture(id);
}

[[nodiscard]] bool AssetStorage::hasFont(const std::string& id) noexcept
{
    return impl().hasFont(id);
}

[[nodiscard]] bool AssetStorage::hasSoundBuffer(const std::string& id) noexcept
{
    return impl().hasSoundBuffer(id);
}

} // namespace hg