-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageLoader.cpp
93 lines (67 loc) · 2.22 KB
/
ImageLoader.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include "ImageLoader.h"
#include "Console.h"
#include "Config.h"
extern Console console;
extern ConfigFile Preferences;
std::vector< std::pair<const char *, Image *> > ImageLoader::_images;
std::deque<int> ImageLoader::_freeIndices;
IMAGEHANDLE ImageLoader::Load(const char * filename, bool fromMemory) {
int found = -1;
if (fromMemory) {
Image * newImage = new Image();
if (newImage->FromMemory(filename)) {
}
return -1;
}
for (unsigned int i = 0; i < _images.size(); i++)
if (strcmp(_images.at(i).first, filename) == 0) { found = i; break; }
if (found < 0) {
Image * newimage;
if (Preferences.GetValue<bool>("USE_SDLBLITTER"))
newimage = new Image();
else
newimage = new Texture();
char * name = new char[strlen(filename)];
strcpy(name, filename);
if (newimage->Load(filename) >= 0) {
int spot = 0;
if (!_freeIndices.size()) {
_images.push_back(std::pair<const char *, Image *>(name, newimage));
spot = _images.size() - 1;
} else {
_images[_freeIndices.front()] = std::pair<const char *, Image *>(name, newimage);
spot = _freeIndices.front();
_freeIndices.pop_front();
}
console.AddLine(std::string("Loaded image <") + filename + ">");
return spot;
} else
return -1;
} else {
_images.at(found).second->_refs++;
return found;
}
}
const Image * ImageLoader::GetImage(IMAGEHANDLE hwnd) {
if ((hwnd < 0) || (hwnd >= _images.size())) {
return NULL;
}
return _images.at(hwnd).second;
}
void ImageLoader::ReleaseAll() {
for (unsigned int i = 0; i < _images.size(); i++) {
_images.at(i).second->Release();
delete _images.at(i).second;
}
}
void ImageLoader::TryRelease(IMAGEHANDLE & hwnd) {
if (hwnd < 0 || hwnd >= _images.size()) return;
_images[hwnd].second->_refs--;
if (_images.at(hwnd).second->_refs < 1) {
console.AddLine(std::string("Image <") + _images.at(hwnd).first + "> released.");
_images[hwnd].second->Release();
_images[hwnd].first = "";
_freeIndices.push_back(hwnd);
hwnd = -1;
}
}