-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageList.cpp
More file actions
100 lines (80 loc) · 1.78 KB
/
Copy pathImageList.cpp
File metadata and controls
100 lines (80 loc) · 1.78 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
#include <iostream>
#include "ImageList.h"
ImageList::ImageList()
{
}
ImageList::~ImageList() {
}
// Finds image by name via findImageIndex, returns pointer or nullptr
Image* ImageList::getByName(MyString& name)
{
int i = findImageIndex(name);
if (i >= 0) {
return &images[i];
}
return nullptr;
}
// Find index of an image
int ImageList::findImageIndex(MyString& name)
{
for (size_t i = 0; i < images.size(); i++)
{
if (images[i].getName() == name)
{
return i; // found - return index
}
}
return -1; // not found
}
// Singleton
ImageList& ImageList::getInstance() {
static ImageList instance;
return instance;
}
Image* ImageList::add(MyString& path)
{
Image* image = new Image(path);
// If loading failed
if (!image->isLoaded()) {
std::cout << "Failed to add: " << image->getName() << "\n";
delete image;
return nullptr;
}
images.push_back(*image); // copy into vector
std::cout << "Added: " << images[images.size() - 1].getName() << "\n";
return &images[images.size() - 1];
}
// Removes image by name: finds index, erases from vector
void ImageList::remove(MyString& name)
{
int index = findImageIndex(name);
if (index < 0) {
std::cout << "Image doesnt exist!\n";
return;
}
images.erase(images.begin() + index);
std::cout << "Image removed!\n";
}
// Prints names of all loaded images
void ImageList::printAll()
{
for (size_t i = 0; i < images.size(); i++)
{
std::cout << images[i].getName() << "\n";
}
}
// Returns a vector of names of all loaded images
MyVector<MyString> ImageList::getImageList()
{
MyVector<MyString> imagesName;
for (size_t i = 0; i < images.size(); i++)
{
imagesName.push_back(images[i].getName());
}
return imagesName;
}
// Access operator by name
Image* ImageList::operator[](MyString& name)
{
return getByName(name);
}