-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileHandler.cpp
More file actions
73 lines (66 loc) · 2.5 KB
/
Copy pathFileHandler.cpp
File metadata and controls
73 lines (66 loc) · 2.5 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
//
// Created by luniminex on 10/7/23.
//
#include "FileHandler.h"
std::shared_ptr<Handle> FileHandler::handle_;
void FileHandler::Initialize() {
handle_ = Handle::GetInstance();
}
std::vector<std::shared_ptr<TextureWrap>> FileHandler::LoadTextures(const std::string &folderPath) {
std::vector<std::shared_ptr<TextureWrap>> textures;
std::vector<std::string> filenames;
//Iterates through directory and saves stem names of tiles
for(const auto& entry : std::filesystem::directory_iterator(folderPath)) {
if(entry.path().extension() == ".png") {
filenames.push_back(entry.path().filename().stem().string());
}
}
//sorts filenames in ascending order
std::sort(filenames.begin(), filenames.end(), [](const std::string& a, const std::string& b) {
return std::stoi(a) < std::stoi(b);
});
//reconstructs folderPath and loads textures
for(const auto& filename : filenames) {
std::string filepath = folderPath + "/" + filename + ".png";
textures.push_back(std::make_shared<TextureWrap>(filepath, handle_->GetRenderer()));
std::cout << "Loaded texture" << filepath << std::endl;
}
return textures;
}
std::map<int, std::map<int, std::vector<int>>> FileHandler::LoadRules(const std::string &folderPath) {
std::fstream file(folderPath + "/rules");
if(!file.is_open()) {
std::cout << "ERROR::WFC::LOADRULES()::File not found" << std::endl;
}
std::map<int, std::map<int, std::vector<int>>> rules;
std::string line;
std::string num;
int idx = 0;
while(getline(file, line)){
std::stringstream stream(line);
std::vector<int> rulesHolder;
while(stream >> num) { //saves rules into vector
rulesHolder.push_back(std::stoi(num));
}
int rulesPerSide = rulesHolder.size() / 4;
//rotates rules
for(int j = 0; j < 4; j++){
//saves rules
for(const auto& rule : rulesHolder){
rules[idx][j].push_back(rule);
}
//offsets rules by one
for(int l = 0; l < rulesPerSide; l++) {
int temp = rulesHolder.back();
for (int k = static_cast<int>(rulesHolder.size()) - 2; k >= 0; k--) {
rulesHolder.at(k + 1) = rulesHolder.at(k);
}
rulesHolder.at(0) = temp;
}
}
idx++;
}
file.close();
std::cout << "Loaded rules for " << idx-- << " tiles " << std::endl;
return rules;
}