Conquer Space 0.0.0
A space themed grand strategy game set in the near future, with realistic orbital mechanics, and an emphasis on economics and politics.
assetloader.h
Go to the documentation of this file.
1/* Conquer Space
2 * Copyright (C) 2021-2025 Conquer Space
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17#pragma once
18
19#include <atomic>
20#include <functional>
21#include <map>
22#include <memory>
23#include <optional>
24#include <vector>
25
34
35namespace cqsp::asset {
37 public:
38 QueueHolder() { prototype = nullptr; }
39 explicit QueueHolder(AssetPrototype* type) : prototype(type) {}
40
42};
43
45 public:
47
53 void LoadMods();
54
85 std::unique_ptr<Package> LoadPackage(const std::string& path);
86
90 void BuildNextAsset();
91
96 bool QueueHasItems() { return m_asset_queue.size() != 0; }
97
102 std::vector<std::string>& GetMissingAssets() { return missing_assets; }
103
108 static std::string GetModFilePath();
109
110 std::atomic_int& getMaxLoading() { return max_loading; }
111 std::atomic_int& getCurrentLoading() { return currentloading; }
112
113 friend class AssetManager;
114
116
117 typedef std::function<std::unique_ptr<Asset>(cqsp::asset::VirtualMounter* mount, const std::string& path,
118 const std::string& key, const Hjson::Value& hints)>
120
121 private:
122 std::optional<PackagePrototype> LoadModPrototype(const std::string&);
123
129 std::unique_ptr<cqsp::asset::Asset> LoadText(cqsp::asset::VirtualMounter* mount, const std::string& path,
130 const std::string& key, const Hjson::Value& hints);
135 std::unique_ptr<cqsp::asset::Asset> LoadTextDirectory(cqsp::asset::VirtualMounter* mount, const std::string& path,
136 const std::string& key, const Hjson::Value& hints);
137
143 std::unique_ptr<cqsp::asset::Asset> LoadTexture(cqsp::asset::VirtualMounter* mount, const std::string& path,
144 const std::string& key, const Hjson::Value& hints);
145
149 std::unique_ptr<cqsp::asset::Asset> LoadBinaryAsset(cqsp::asset::VirtualMounter* mount, const std::string& path,
150 const std::string& key, const Hjson::Value& hints);
151
163 std::unique_ptr<cqsp::asset::Asset> LoadHjson(cqsp::asset::VirtualMounter* mount, const std::string& path,
164 const std::string& key, const Hjson::Value& hints);
165
171 std::unique_ptr<cqsp::asset::Asset> LoadShader(cqsp::asset::VirtualMounter* mount, const std::string& path,
172 const std::string& key, const Hjson::Value& hints);
181 std::unique_ptr<cqsp::asset::Asset> LoadFont(cqsp::asset::VirtualMounter* mount, const std::string& path,
182 const std::string& key, const Hjson::Value& hints);
189 std::unique_ptr<cqsp::asset::Asset> LoadAudio(cqsp::asset::VirtualMounter* mount, const std::string& path,
190 const std::string& key, const Hjson::Value& hints);
191
215 std::unique_ptr<cqsp::asset::Asset> LoadCubemap(cqsp::asset::VirtualMounter* mount, const std::string& path,
216 const std::string& key, const Hjson::Value& hints);
217
218 std::unique_ptr<cqsp::asset::Asset> LoadModel(cqsp::asset::VirtualMounter* mount, const std::string& path,
219 const std::string& key, const Hjson::Value& hints);
220
243 std::unique_ptr<ShaderDefinition> LoadShaderDefinition(cqsp::asset::VirtualMounter* mount, const std::string& path,
244 const std::string& key, const Hjson::Value& hints);
245
252 std::unique_ptr<TextDirectoryAsset> LoadScriptDirectory(VirtualMounter* mount, const std::string& path,
253 const Hjson::Value& hints);
254
263 std::unique_ptr<cqsp::asset::Asset> LoadAsset(const AssetType& type, const std::string& path,
264 const std::string& key, const Hjson::Value& hints);
265
271 void PlaceAsset(Package& package, const AssetType& type, const std::string& path, const std::string& key,
272 const Hjson::Value& hints);
273
282 void LoadDirectory(const std::string& path, const std::function<void(std::string)>& file);
283
302 void LoadResources(Package& package, const std::string& path);
303
312 void LoadResourceHjsonFile(Package& package, const std::string& package_mount_path,
313 const std::string& resource_file_path, const Hjson::Value& asset_value);
321 bool HjsonPrototypeDirectory(Package& package, const std::string& path, const std::string& name);
322
328 IVirtualFileSystem* GetVfs(const std::string& path);
329
333 std::vector<std::string> missing_assets;
338
339 // All the assets that are to be loaded
340 std::atomic_int max_loading;
341
345 std::atomic_int currentloading;
350 std::map<AssetType, LoaderFunction> loading_functions;
351 std::map<AssetType, uint64_t> loading_times;
353
355};
356} // namespace cqsp::asset
Definition: assetloader.h:44
std::map< AssetType, uint64_t > loading_times
Definition: assetloader.h:351
std::unique_ptr< cqsp::asset::Asset > LoadModel(cqsp::asset::VirtualMounter *mount, const std::string &path, const std::string &key, const Hjson::Value &hints)
Definition: assetloader.cpp:586
std::unique_ptr< cqsp::asset::Asset > LoadTextDirectory(cqsp::asset::VirtualMounter *mount, const std::string &path, const std::string &key, const Hjson::Value &hints)
Loads a directory of text files into a map of strings keyed by their relative path to the resource....
Definition: assetloader.cpp:357
std::map< AssetType, LoaderFunction > loading_functions
The list of functions that are loading.
Definition: assetloader.h:350
AssetLoader(AssetOptions options=AssetOptions())
Definition: assetloader.cpp:53
std::unique_ptr< cqsp::asset::Asset > LoadText(cqsp::asset::VirtualMounter *mount, const std::string &path, const std::string &key, const Hjson::Value &hints)
Pretty straightforward, loads a text file into a string.
Definition: assetloader.cpp:344
void LoadDirectory(const std::string &path, const std::function< void(std::string)> &file)
Loads any type of directory, and executes the function for every single file. This is a helper func...
Definition: assetloader.cpp:732
std::unique_ptr< cqsp::asset::Asset > LoadBinaryAsset(cqsp::asset::VirtualMounter *mount, const std::string &path, const std::string &key, const Hjson::Value &hints)
Loads binary data straight from the file.
Definition: assetloader.cpp:412
std::unique_ptr< cqsp::asset::Asset > LoadAsset(const AssetType &type, const std::string &path, const std::string &key, const Hjson::Value &hints)
Loads the asset specified in path
Definition: assetloader.cpp:253
VirtualMounter mounter
Definition: assetloader.h:352
std::function< std::unique_ptr< Asset >(cqsp::asset::VirtualMounter *mount, const std::string &path, const std::string &key, const Hjson::Value &hints)> LoaderFunction
Definition: assetloader.h:119
std::optional< PackagePrototype > LoadModPrototype(const std::string &)
Definition: assetloader.cpp:155
static std::string GetModFilePath()
Get where the mod.hjson file is.
Definition: assetloader.cpp:151
std::unique_ptr< cqsp::asset::Asset > LoadCubemap(cqsp::asset::VirtualMounter *mount, const std::string &path, const std::string &key, const Hjson::Value &hints)
Cubemaps are a special type of texture that make up the skybox. Although they are specified different...
Definition: assetloader.cpp:539
std::unique_ptr< cqsp::asset::Asset > LoadAudio(cqsp::asset::VirtualMounter *mount, const std::string &path, const std::string &key, const Hjson::Value &hints)
Only ogg files are supported for now.
Definition: assetloader.cpp:526
std::unique_ptr< cqsp::asset::Asset > LoadFont(cqsp::asset::VirtualMounter *mount, const std::string &path, const std::string &key, const Hjson::Value &hints)
Just specify a .ttf file, and it will load it into an opengl texture font object.
Definition: assetloader.cpp:503
std::unique_ptr< TextDirectoryAsset > LoadScriptDirectory(VirtualMounter *mount, const std::string &path, const Hjson::Value &hints)
Loads a script directory.
Definition: assetloader.cpp:682
std::unique_ptr< cqsp::asset::Asset > LoadHjson(cqsp::asset::VirtualMounter *mount, const std::string &path, const std::string &key, const Hjson::Value &hints)
Hjson is rather flexible, it can load a single file or a directory, with just the same option....
Definition: assetloader.cpp:421
std::unique_ptr< cqsp::asset::Asset > LoadTexture(cqsp::asset::VirtualMounter *mount, const std::string &path, const std::string &key, const Hjson::Value &hints)
Textures have one hint, the magfilter hint. If it is present, and set to true, it will enable closest...
Definition: assetloader.cpp:376
void LoadResourceHjsonFile(Package &package, const std::string &package_mount_path, const std::string &resource_file_path, const Hjson::Value &asset_value)
Loads all the resources defined in the hjson asset_value in the hjson resource loading format.
Definition: assetloader.cpp:780
bool HjsonPrototypeDirectory(Package &package, const std::string &path, const std::string &name)
Defines a directory that contains hjson asset data.
Definition: assetloader.cpp:821
void PlaceAsset(Package &package, const AssetType &type, const std::string &path, const std::string &key, const Hjson::Value &hints)
Conducts checks to determine if the asset was loaded correctly. Wraps Load asset, and contains the sa...
Definition: assetloader.cpp:271
std::vector< std::string > missing_assets
List of all assets that are missing
Definition: assetloader.h:333
void LoadResources(Package &package, const std::string &path)
Loads all the resource.hjson files in the specified directory. It will look recursively and find all ...
Definition: assetloader.cpp:742
void BuildNextAsset()
The assets that need to be on the main thread. Takes one asset from the queue and processes it
Definition: assetloader.cpp:284
void LoadMods()
Checks for all the loaded mods, and loads the assets that the mod loads.
Definition: assetloader.cpp:69
IVirtualFileSystem * GetVfs(const std::string &path)
Creates a virtual file system starting in path
Definition: assetloader.cpp:830
std::unique_ptr< cqsp::asset::Asset > LoadShader(cqsp::asset::VirtualMounter *mount, const std::string &path, const std::string &key, const Hjson::Value &hints)
Shaders have one option, the type hint, to specify what type of shader it is. We have two so far,...
Definition: assetloader.cpp:463
std::atomic_int currentloading
All the assets that have already been loaded
Definition: assetloader.h:345
std::atomic_int & getCurrentLoading()
Definition: assetloader.h:111
std::vector< std::string > & GetMissingAssets()
Gets the list of assets that were listed in the resource.hjson files, but were not found.
Definition: assetloader.h:102
std::atomic_int max_loading
Definition: assetloader.h:340
AssetManager * manager
Definition: assetloader.h:115
std::unique_ptr< Package > LoadPackage(const std::string &path)
Loads a package. A package contains a few things that it loads by default. First it loads the info....
Definition: assetloader.cpp:187
bool QueueHasItems()
Checks if the queue has any remaining items to load on the main thread or not.
Definition: assetloader.h:96
std::unique_ptr< ShaderDefinition > LoadShaderDefinition(cqsp::asset::VirtualMounter *mount, const std::string &path, const std::string &key, const Hjson::Value &hints)
A shader defintion is a way to simplify the creation of shader objects. A shader definiton file is ...
Definition: assetloader.cpp:636
AssetOptions asset_options
Definition: assetloader.h:354
engine::ThreadsafeQueue< QueueHolder > m_asset_queue
Queue for AssetPrototype when loading in frame.
Definition: assetloader.h:337
std::atomic_int & getMaxLoading()
Definition: assetloader.h:110
Definition: assetmanager.h:46
Definition: assetoptions.h:20
Definition: assetprototype.h:37
The main functionality for this is to read files, so writing to files will not really be supported.
Definition: vfs.h:41
Definition: package.h:30
Definition: assetloader.h:36
QueueHolder()
Definition: assetloader.h:38
QueueHolder(AssetPrototype *type)
Definition: assetloader.h:39
AssetPrototype * prototype
Definition: assetloader.h:41
Definition: vfs.h:154
Definition: threadsafequeue.h:27
lib tracy file(GLOB_RECURSE CPP_FILES *.cpp) list(FILTER CPP_FILES EXCLUDE REGEX ".*main.cpp$") file(GLOB_RECURSE H_FILES *.h) set(SOURCE_FILES $
Definition: CMakeLists.txt:22
When adding assets, it is extremely crucial that you read cqsp::asset::AssetLoader::LoadResources to ...
Definition: asset.h:29
AssetType
Definition: asset.h:32