-
Notifications
You must be signed in to change notification settings - Fork 1
/
TextureCache.cpp
72 lines (61 loc) · 1.82 KB
/
TextureCache.cpp
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
/**
* Implementation of TextureCache as declared in TextureCache.h
*
* Author: Skylar Payne
* Date: 7/24/2013
* File: TextureCache.cpp
**/
#include "TextureCache.h"
#include "Logger.h"
/**
* @brief TextureCache::Add adds a texture into the cache
* @param file the texture to add into the cache
* @return true if texture was successfully loaded or already loaded, false otherwise
*/
bool TextureCache::Add(const char* file)
{
TextureMap::iterator it = _Resources.find(file);
if(it != _Resources.end())
{
g_Logger << __FILE__ << ": " << __LINE__ << "-Error: " << file << " already exists in TextureCache\n";
return true;
}
sf::Texture* newTexture = new sf::Texture();
if(!newTexture->loadFromFile(file))
{
delete newTexture;
newTexture = nullptr;
g_Logger << __FILE__ << ": " << __LINE__ << "-Error: " << file << " failed to load\n";
return false;
}
_Resources[file] = newTexture;
g_Logger << __FILE__ << ": " << __LINE__ << "-" << file << " was successfullly added to SoundCache\n";
return true;
}
/**
* @brief TextureCache::Remove removes a texture from the cache
* @param file the texture to remove
*/
void TextureCache::Remove(const char* file)
{
TextureMap::iterator it = _Resources.find(file);
if(it != _Resources.end())
{
_Resources.erase(it);
g_Logger << __FILE__ << ": " << __LINE__ << "-" << file << " was removed from TextureCache\n";
}
}
/**
* @brief TextureCache::Get gets a texture from the cache
* @param file the texture to get
* @return the texture if found, nullptr otherwise
*/
sf::Texture* TextureCache::Get(const char* file)
{
TextureMap::iterator it = _Resources.find(file);
if(it == _Resources.end())
{
return nullptr;
}
return _Resources[file];
}