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.
assetmanager.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 <hjson.h>
20#include <spdlog/spdlog.h>
21
22#include <istream>
23#include <map>
24#include <memory>
25#include <optional>
26#include <queue>
27#include <string>
28#include <utility>
29#include <vector>
30
31#include "engine/asset/asset.h"
36#include "engine/engine.h"
37#include "engine/enginelogger.h"
41#include "engine/gui.h"
43
44namespace cqsp::asset {
45class AssetLoader;
47 public:
48 AssetManager() = default;
49
50 ShaderProgram_t MakeShader(const std::string& vert, const std::string& frag);
51 ShaderProgram_t MakeShader(const std::string& vert, const std::string& frag, const std::string& geom);
52
62 template <class T>
63 T* GetAsset(const std::string& key) {
64 static_assert(std::is_base_of<Asset, T>::value, "Class is not child of cqsp::asset::Asset");
65 std::size_t separation = key.find(":");
66 // Default name is core
67 std::string package_name = "core";
68 if (separation != std::string::npos) {
69 package_name = key.substr(0, separation);
70 }
71
72 // Check if package exists
73 if (packages.count(package_name) == 0) {
74 ENGINE_LOG_ERROR("Cannot find package {}", package_name);
75 return nullptr;
76 }
77 std::string pkg_key = key.substr(separation + 1, key.length());
78 auto& package = packages[package_name];
79 // Probably a better way to do this, to be honest
80 // Load default texture
81 if (!package->HasAsset(pkg_key)) {
82 ENGINE_LOG_ERROR("Cannot find asset {}", pkg_key);
83 if constexpr (std::is_same<T, asset::Texture>::value) {
84 return &empty_texture;
85 }
86 return nullptr;
87 }
88 // Check if asset exists
89 T* ptr = package->GetAsset<T>(pkg_key);
90 if (ptr == nullptr) {
91 SPDLOG_WARN("Asset {} is wrong type", key);
92 } else {
93 ptr->accessed++;
94 }
95 ptr->PostLoad(*this);
96 return ptr;
97 }
98
99 void LoadDefaultTexture();
100 void ClearAssets();
101
102 Package* GetPackage(const std::string& name) { return packages[name].get(); }
103
104 size_t GetPackageCount() { return packages.size(); }
105
106 auto begin() { return packages.begin(); }
107
108 auto end() { return packages.end(); }
109
110 void SaveModList();
111
112 std::map<std::string, PackagePrototype> m_package_prototype_list;
113
114 private:
115 std::map<std::string, std::unique_ptr<Package>> packages;
117 friend class AssetLoader;
118};
119} // namespace cqsp::asset
Definition: assetloader.h:44
Definition: assetmanager.h:46
std::map< std::string, PackagePrototype > m_package_prototype_list
Definition: assetmanager.h:112
ShaderProgram_t MakeShader(const std::string &vert, const std::string &frag)
Definition: assetmanager.cpp:49
std::map< std::string, std::unique_ptr< Package > > packages
Definition: assetmanager.h:115
size_t GetPackageCount()
Definition: assetmanager.h:104
T * GetAsset(const std::string &key)
Gets an asset.
Definition: assetmanager.h:63
void ClearAssets()
Definition: assetmanager.cpp:121
void LoadDefaultTexture()
Definition: assetmanager.cpp:110
Package * GetPackage(const std::string &name)
Definition: assetmanager.h:102
asset::Texture empty_texture
Definition: assetmanager.h:116
void SaveModList()
Definition: assetmanager.cpp:123
auto begin()
Definition: assetmanager.h:106
auto end()
Definition: assetmanager.h:108
Definition: package.h:30
Definition: texture.h:32
#define ENGINE_LOG_ERROR(...)
Definition: enginelogger.h:31
When adding assets, it is extremely crucial that you read cqsp::asset::AssetLoader::LoadResources to ...
Definition: asset.h:29
std::shared_ptr< ShaderProgram > ShaderProgram_t
The preferred way of using a shader program.
Definition: shader.h:113