-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResourceManager.cpp
More file actions
115 lines (91 loc) · 2.37 KB
/
ResourceManager.cpp
File metadata and controls
115 lines (91 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include "ResourceManager.h"
ResourceManager::ResourceManager(SDL_Renderer* renderer)
: renderer(renderer)
{
int imgFlags = IMG_INIT_PNG;
int success = IMG_Init(imgFlags);
// Load the test texture.
loadTexture("ship", "res/ship.png");
// Assemble animation...
}
ResourceManager::~ResourceManager()
{
// Iterate through all textures and deallocate them all
for (map<string, Texture*>::iterator i = idToTexture.begin();
i != idToTexture.end(); i++)
{
// For iterator, i->second = value
Texture* wrappedTexture = i->second;
delete wrappedTexture;
}
// Same goes for animations
for (map<string, Animation*>::iterator i = idToAnimation.begin();
i != idToAnimation.end(); i++)
{
std::cout << "here we are in the animation deallocation loop.\n";
Animation* animationResource = i->second;
delete animationResource;
}
// Stop SDL_Image
IMG_Quit();
}
void ResourceManager::loadTexture(string textureID, string filepath)
{
Texture* wrappedTexture = loadPNGTexture(filepath);
if (wrappedTexture == NULL)
{
cout << "Error loading PNG texture resource.\n";
}
else
{
idToTexture[textureID] = wrappedTexture;
}
}
void ResourceManager::addMultiTexAnimation(string* textureIDs,
float* delays,
int frameCount,
string animationID)
{
// Assemble list of textures
Texture** textures = new Texture*[frameCount];
for (int i = 0; i < frameCount; i++)
{
textures[i] = getTexture(textureIDs[i]);
}
Animation* newAnimation = new MultiTextureAnimation(textures, delays, frameCount);
idToAnimation[animationID] = newAnimation;
}
// Get texture from the id
Texture* ResourceManager::getTexture(string id)
{
return idToTexture[id];
}
// Get animation from the id
Animation* ResourceManager::getAnimation(string id)
{
return idToAnimation[id];
}
// Loading function which takes care of loading Texture
// using SDL_Image
Texture* ResourceManager::loadPNGTexture(string filepath)
{
SDL_Texture* newTexture = NULL;
SDL_Surface* loadedSurface = NULL;
loadedSurface = IMG_Load(filepath.c_str());
if (loadedSurface != NULL)
{
newTexture = SDL_CreateTextureFromSurface(renderer, loadedSurface);
Texture* wrappedTexture = new Texture(
newTexture,
loadedSurface->w,
loadedSurface->h,
renderer);
SDL_FreeSurface(loadedSurface);
return wrappedTexture;
}
else
{
std::cout << "Error loading image resource!\n";
return NULL;
}
}