Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
preloader.hpp
Go to the documentation of this file.
1
3#pragma once
4
6
7#include <atomic>
8#include <cstddef>
9#include <exception>
10#include <mutex>
11#include <string>
12#include <string_view>
13#include <thread>
14#include <utility>
15#include <vector>
16
17namespace aegis2d::core {
18
22 std::string name{};
25 std::string reason{};
26};
27
41template <typename Resource>
42class Preloader {
43public:
47 Preloader(ResourceCache<Resource>& cache, std::vector<std::string> names)
48 : names_{std::move(names)} {
49 worker_ = std::jthread{[this, &cache] {
50 for (const std::string& name : names_) {
51 try {
52 auto handle = cache.load(name);
53 const std::scoped_lock lock{mutex_};
54 handles_.emplace_back(name, std::move(handle));
55 } catch (const std::exception& error) {
56 const std::scoped_lock lock{mutex_};
57 failures_.push_back({.name = name, .reason = error.what()});
58 } catch (...) {
59 // Not an std::exception: recorded too, rather than ending the program.
60 const std::scoped_lock lock{mutex_};
61 failures_.push_back({.name = name, .reason = "unknown error"});
62 }
63 processed_.fetch_add(1, std::memory_order_release);
64 }
65 }};
66 }
67
68 Preloader(const Preloader&) = delete;
69 Preloader& operator=(const Preloader&) = delete;
70 Preloader(Preloader&&) = delete;
73 ~Preloader() = default;
74
77 [[nodiscard]] std::size_t total() const noexcept { return names_.size(); }
78
81 [[nodiscard]] std::size_t processed() const noexcept {
82 return processed_.load(std::memory_order_acquire);
83 }
84
87 [[nodiscard]] float progress() const noexcept {
88 return names_.empty() ? 1.0F
89 : static_cast<float>(processed()) / static_cast<float>(total());
90 }
91
94 [[nodiscard]] bool done() const noexcept { return processed() == total(); }
95
97 void wait() {
98 if (worker_.joinable()) {
99 worker_.join();
100 }
101 }
102
105 [[nodiscard]] std::vector<PreloadFailure> failures() const {
106 const std::scoped_lock lock{mutex_};
107 return failures_;
108 }
109
113 [[nodiscard]] ResourceCache<Resource>::Handle handle(std::string_view name) const {
114 const std::scoped_lock lock{mutex_};
115 for (const auto& [loaded, kept] : handles_) {
116 if (loaded == name) {
117 return kept;
118 }
119 }
120 return nullptr;
121 }
122
125 void release() {
126 wait();
127 const std::scoped_lock lock{mutex_};
128 handles_.clear();
129 }
130
131private:
132 std::vector<std::string> names_;
133 mutable std::mutex mutex_;
134 std::vector<std::pair<std::string, typename ResourceCache<Resource>::Handle>> handles_;
135 std::vector<PreloadFailure> failures_;
136 std::atomic<std::size_t> processed_{0};
137 // Last member: destroyed (joined) first, while the rest still lives.
138 std::jthread worker_;
139};
140
141} // namespace aegis2d::core
Loads a list of resources in the background, telling how far it is.
Definition preloader.hpp:42
Preloader(Preloader &&)=delete
Preloader(const Preloader &)=delete
void wait()
Waits until every name is dealt with.
Definition preloader.hpp:97
Preloader & operator=(Preloader &&)=delete
void release()
Lets go of the kept handles, once the game holds its own.
Definition preloader.hpp:125
std::size_t processed() const noexcept
Names dealt with so far, loaded or failed.
Definition preloader.hpp:81
ResourceCache< Resource >::Handle handle(std::string_view name) const
The kept handle of a loaded name.
Definition preloader.hpp:113
Preloader & operator=(const Preloader &)=delete
bool done() const noexcept
Tells whether every name was dealt with.
Definition preloader.hpp:94
std::size_t total() const noexcept
Names to load in all.
Definition preloader.hpp:77
float progress() const noexcept
How far the loading is.
Definition preloader.hpp:87
Preloader(ResourceCache< Resource > &cache, std::vector< std::string > names)
Starts loading at once, in its own thread.
Definition preloader.hpp:47
std::vector< PreloadFailure > failures() const
The resources that could not be loaded.
Definition preloader.hpp:105
~Preloader()=default
Waits for the loading thread.
Shares resources by name: each is loaded once, counted, and freed when unused.
Definition resource_cache.hpp:38
Handle load(std::string_view name)
Loads a resource, or shares the one already loaded or being loaded.
Definition resource_cache.hpp:60
std::shared_ptr< Resource > Handle
Handle of a loaded resource; the resource lives while handles do.
Definition resource_cache.hpp:41
Logging, time, main loop, events and configuration shared by client and server.
Definition clock.hpp:11
std::string_view name() noexcept
Name of the module, used to check that it is linked.
Definition endpoint.hpp:85
Resources shared by name, loaded once and freed when nothing uses them.
A resource a Preloader could not load, and why.
Definition preloader.hpp:20
std::string name
Name given to the cache.
Definition preloader.hpp:22
std::string reason
what() of the loader's exception, or "unknown error" for one that is not an std::exception.
Definition preloader.hpp:25