Repository navigation
Expand file tree
/
Copy pathimagecontainer.cpp
More file actions
89 lines (72 loc) · 2.45 KB
/
Copy pathimagecontainer.cpp
File metadata and controls
89 lines (72 loc) · 2.45 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
#include <iostream>
#include <algorithm>
#include "imagecontainer.h"
#include "common.h"
bool ImageContainer::load(const std::vector<std::string>& filenames, int textureType, int mipmapFilter) {
bool mipmapped = (textureType & FLAG_MIPMAPPED);
if ((filenames.size() > 1) && !mipmapped) {
std::cerr << "[ERROR] Only one input file may be specified if no mipmap flag is set.\n";
return false;
}
for (const auto& filename : filenames) {
Image img;
if (!img.loadFromFile(filename)) {
std::cerr << "[ERROR] Failed to load image: " << filename << "\n";
return false;
}
if (!isValidSize(img.width(), img.height(), textureType)) {
std::cerr << "[ERROR] Image " << filename
<< " has invalid texture size "
<< img.width() << "x" << img.height() << "\n";
return false;
}
if (mipmapped && img.width() != img.height()) {
std::cerr << "[ERROR] Image " << filename
<< " is not square. Mipmapped textures require square images.\n";
return false;
}
textureWidth = std::max(textureWidth, img.width());
textureHeight = std::max(textureHeight, img.height());
images[img.width()] = img;
std::cout << "[INFO] Loaded image " << filename << "\n";
}
if (mipmapped) {
if (mipmapFilter == 0) {
std::cout << "[INFO] Using nearest-neighbor filtering for mipmaps\n";
} else {
std::cout << "[INFO] Using bilinear filtering for mipmaps\n";
}
for (int size = TEXTURE_SIZE_MAX/2; size >= 1; size /= 2) {
if (images.count(size*2) && !images.count(size)) {
Image mipmap = images[size*2].scaled(size, size,
mipmapFilter == 0);
images[size] = mipmap;
std::cout << "[INFO] Generated " << size << "x" << size << " mipmap\n";
}
}
}
if (textureWidth < TEXTURE_SIZE_MIN || textureHeight < TEXTURE_SIZE_MIN) {
std::cerr << "[ERROR] At least one input image must be 8x8 or larger.\n";
return false;
}
keys.clear();
for (auto& kv : images) keys.push_back(kv.first);
std::sort(keys.begin(), keys.end());
return true;
}
void ImageContainer::unloadAll() {
textureWidth = 0;
textureHeight = 0;
images.clear();
keys.clear();
}
const Image& ImageContainer::getByIndex(int index, bool ascending) const {
if (index >= (int)keys.size()) {
static Image dummy;
return dummy;
} else {
int realIdx = ascending ? index : ((int)keys.size() - index - 1);
int size = keys[realIdx];
return images.at(size);
}
}