souleng
Game engine providing full Python scripting support
Loading...
Searching...
No Matches
include
ResourceManager.hpp
1
#pragma once
2
3
#include <SDL3/SDL.h>
4
#include <SDL3/SDL_log.h>
5
#include <SDL3_image/SDL_image.h>
6
#include <SDL3_ttf/SDL_ttf.h>
7
#include <cstddef>
8
#include <memory>
9
#include <string>
10
#include <unordered_map>
11
12
#include "Renderer.hpp"
13
15
struct
DeleteTextureFunctor
{
16
void
operator()(SDL_Texture *texture)
const
{ SDL_DestroyTexture(texture); }
17
};
18
20
struct
DeleteFontFunctor
{
21
void
operator()(TTF_Font *font)
const
{ TTF_CloseFont(font); }
22
};
23
26
inline
std::shared_ptr<SDL_Texture> make_shared_texture(SDL_Surface *pixels) {
27
auto
renderer =
Renderer::instance
();
28
SDL_Texture *texture = renderer->CreateTextureFromSurface(pixels);
29
if
(texture == NULL) {
30
SDL_Log(
"Could not load texture from surface!"
);
31
}
32
return
std::shared_ptr<SDL_Texture>(texture,
DeleteTextureFunctor
());
33
}
34
36
struct
ResourceManager
{
37
39
static
ResourceManager
&
instance
() {
40
if
(mInstance ==
nullptr
) {
41
mInstance =
new
ResourceManager
;
42
}
43
44
return
*mInstance;
45
}
46
50
std::shared_ptr<SDL_Texture>
LoadTexture
(std::string filepath) {
51
// c++20 -> has contains method. otherwise, use find, cmp with map.end()
52
if
(!mTextureResources.contains(filepath)) {
53
SDL_Surface *surface = IMG_Load(filepath.c_str());
54
if
(surface == NULL) {
55
SDL_LogError(
56
SDL_LOG_CATEGORY_APPLICATION,
57
"Failed to load texture at path: %s. Maybe the path is wrong?"
,
58
filepath.c_str());
59
return
NULL;
60
}
61
SDL_SetSurfaceColorKey(surface, SDL_TRUE,
62
SDL_MapRGB(surface->format, 255, 0, 255));
63
auto
texture = make_shared_texture(surface);
64
65
// c++20 idiom, does insertion in one step vs index assignment.
66
mTextureResources.insert({filepath, texture});
67
68
SDL_DestroySurface(surface);
69
SDL_Log(
"Created new resource: %s"
, filepath.c_str());
70
}
else
{
71
SDL_Log(
"Reused resource: %s"
, filepath.c_str());
72
}
73
return
mTextureResources[filepath];
74
}
75
80
std::shared_ptr<TTF_Font>
LoadFont
(std::string filepath,
int
ptsize) {
81
if
(!mFontResources.contains(filepath) ||
82
!mFontResources[filepath].contains(ptsize)) {
83
TTF_Font *font = TTF_OpenFont(filepath.c_str(), ptsize);
84
if
(font == NULL) {
85
SDL_LogError(
86
SDL_LOG_CATEGORY_APPLICATION,
87
"Failed to load font at path: %s. Maybe the path is wrong?"
,
88
filepath.c_str());
89
return
nullptr
;
90
}
else
{
91
SDL_Log(
"Successfully loaded font at path: %s."
, filepath.c_str());
92
mFontResources[filepath][ptsize] =
93
std::shared_ptr<TTF_Font>(font,
DeleteFontFunctor
());
94
}
95
}
96
97
return
mFontResources[filepath][ptsize];
98
}
99
100
private
:
101
ResourceManager
() {}
102
103
static
ResourceManager
*mInstance;
104
std::unordered_map<std::string, std::shared_ptr<SDL_Texture>>
105
mTextureResources;
106
std::unordered_map<std::string,
107
std::unordered_map<int, std::shared_ptr<TTF_Font>>>
108
mFontResources;
109
};
DeleteFontFunctor
Delayed deletion of an TTF_Font so a shared_ptr can hold onto this resource.
Definition
ResourceManager.hpp:20
DeleteTextureFunctor
Delayed deletion of an SDL_Texture so a shared_ptr can hold onto this resource.
Definition
ResourceManager.hpp:15
Renderer::instance
static Renderer * instance()
Singleton accessor function for the Renderer.
Definition
Renderer.cpp:17
ResourceManager
Singleton class which stores and manages resources like fonts and images.
Definition
ResourceManager.hpp:36
ResourceManager::instance
static ResourceManager & instance()
Singleton accessor.
Definition
ResourceManager.hpp:39
ResourceManager::LoadTexture
std::shared_ptr< SDL_Texture > LoadTexture(std::string filepath)
Definition
ResourceManager.hpp:50
ResourceManager::LoadFont
std::shared_ptr< TTF_Font > LoadFont(std::string filepath, int ptsize)
Definition
ResourceManager.hpp:80
Generated by
1.10.0