Files
ReCaveGame/Client/include/Client/AssetService.hpp
2025-04-01 18:57:01 -04:00

108 lines
3.2 KiB
C++

/// CaveGame - A procedural 2D platformer sandbox.
/// Created by Josh O'Leary @ Redacted Software, 2020-2025
/// Contact: josh@redacted.cc
/// Contributors: william@redacted.cc maxi@redacted.cc
/// This work is dedicated to the public domain.
/// @file AssetService.hpp
/// @desc Manages game asset data.
/// @edit 3/31/2025
/// @auth Josh O'Leary
/// The AssetService is a class / static library that manages on-file game assets.
/// This class service provides asynchronous loading of game content, with introspection for hooking to a loading menu.
#pragma once
#include <string>
#include <unordered_map>
#include <filesystem>
#include <iostream>
#include <JGL/types/Texture.h>
#include <JGL/types/Font.h>
#include <queue>
#include <Core/Singleton.hpp>
#include <Core/Macros.hpp>
namespace CaveGame::Client {
using namespace JGL;
enum AssetType { DATA, TEXT, AUDIO, MODEL, TEXTURE, FONT, SHADER };
enum AssetLifestyle { STATIC, STREAMED};
struct AssetRequest {
std::string name;
std::filesystem::path path;
AssetType type;
};
class AssetService {
private:
public:
static AssetService* Get();
JGL::Texture *player_sprite;
JGL::Texture *explosion_sprite;
JGL::Texture *title_img;
AssetService();
~AssetService() {}
void TempLoadSimple();
std::shared_ptr<Texture> GetTexture(const std::string &textureName) {
return textures[textureName];
}
std::shared_ptr<JGL::Font> GetFont(const std::string &fontName) {
//return fonts[fontName];
}
void EnqueueTexture(const std::string& name, const std::filesystem::path &path);
void EnqueueFont(const std::string& name, const std::filesystem::path& path);
void EnqueueSound(const std::string& name, const std::filesystem::path& path);
void LoadAllFromQueue();
/// Performs one "Load Cycle" on the current thread. This means we will attempt to load a number of queued items until we've spent at least maxTTL seconds.
bool LoadFromQueue(float maxTimeExpenditure = 1e-3f);
bool LoadAsset(const AssetRequest& request);
bool IsLoadComplete() const { return queue.empty(); }
void EnqueueDefaultAssetsFolder();
std::string LastAsset() const { return last_asset_processed; }
unsigned int TotalQueued() const { return total_queued; }
unsigned int TotalLoaded() const { return total_loaded; }
void ParseManifest();
void PreloadCertainAssets();
protected:
/// @returns only the filename of the given path.
static std::string FilenameFromPath(const std::filesystem::path& path);
/// @returns only the filename, without a file extension, of the given path.
static std::string FilenameFromPathWithoutExtension(const std::filesystem::path& path);
protected:
std::unordered_map<std::string, std::shared_ptr<Font>> fonts;
std::unordered_map<std::string, std::shared_ptr<Texture>> textures;
std::queue<AssetRequest> queue;
std::string last_asset_processed;
unsigned int total_queued = 0;
unsigned int total_loaded = 0;
};
}