10 #include "../lodepng/lodepng.h"
19 std::vector<std::string>
readDirectory(std::string path, std::string extension)
21 std::vector<std::string> files;
23 for (
const auto& entry : std::filesystem::directory_iterator(path))
25 if (entry.path().extension() == extension)
27 files.push_back(entry.path().filename().string());
42 void readPng(std::vector<unsigned char>& image,
unsigned& width,
unsigned& height, std::string filepath)
44 std::cout <<
"Reading " << filepath << std::endl;
46 unsigned error = lodepng::decode(image, width, height, filepath);
50 throw std::runtime_error(
"PNG decoding error" + std::to_string(error) +
": " + lodepng_error_text(error));
61 void rotateMatrix(std::vector<std::vector<T>>& matrix, uint8_t rotation)
68 std::vector<std::vector<T>> rotatedMatrix;
70 for (
unsigned i = 0; i < matrix[0].size(); i++)
73 for (
int j = matrix.size() - 1; j >= 0; j--)
75 row.push_back(matrix[j][i]);
77 rotatedMatrix.push_back(row);
80 matrix = rotatedMatrix;
96 void populateMatrix(std::vector<std::vector<T>>& matrix,
const std::vector<std::vector<std::string>>& areaMap, T defaultValue,
unsigned AREA_WIDTH,
unsigned AREA_HEIGHT)
98 for (
unsigned i = 0; i < areaMap.size(); i++)
100 for (
unsigned j = 0; j < AREA_HEIGHT; j++)
102 std::vector<T> row(AREA_WIDTH * areaMap[i].size(), defaultValue);
103 matrix.push_back(row);
115 template <
typename T,
typename K>
118 for (
unsigned i = 0; i < referenceMatrix.size(); i++)
120 std::vector<T> row(referenceMatrix[i].size(), defaultValue);
121 matrix.push_back(row);
132 template <
typename T>
136 std::filesystem::create_directories(std::filesystem::path(filepath).parent_path());
138 std::ofstream file(filepath, std::ios::out);
142 for (
const auto& row : matrix)
144 for (
const auto& col : row)
155 throw std::runtime_error(
"Unable to open the file for writing.");
void rotateMatrix(std::vector< std::vector< T >> &matrix, uint8_t rotation)
Rotates the matrix 90 degrees counter-clockwise. Rotation is calculated by rotation-param * 90 degree...
Definition: rehtiLib/assets/loader/src/Utils.hpp:61
void readPng(std::vector< unsigned char > &image, unsigned &width, unsigned &height, std::string filepath)
Reads a PNG file and returns the image data, width and height.
Definition: rehtiLib/assets/loader/src/Utils.hpp:42
std::vector< std::string > readDirectory(std::string path, std::string extension)
Reads all the files in the directory and returns a vector of the filenames. Only returns files with t...
Definition: rehtiLib/assets/loader/src/Utils.hpp:19
void populateMatrixFromReference(std::vector< std::vector< T >> &matrix, const std::vector< std::vector< K >> &referenceMatrix, T defaultValue)
Populates a matrix from a reference matrix. Uses a default value for empty cells. Reference matrix is...
Definition: rehtiLib/assets/loader/src/Utils.hpp:116
void populateMatrix(std::vector< std::vector< T >> &matrix, const std::vector< std::vector< std::string >> &areaMap, T defaultValue, unsigned AREA_WIDTH, unsigned AREA_HEIGHT)
Populates a matrix with a default value. The matrix is all the areas combined and expanded e....
Definition: rehtiLib/assets/loader/src/Utils.hpp:96
void writeMatrixToFile(const std::vector< std::vector< T >> &matrix, const std::string &filepath)
Takes in a matrix<T> and writes that to a text file. Values are separated by commas and rows by newli...
Definition: rehtiLib/assets/loader/src/Utils.hpp:133