Rehti MMORPG  1.0.0
Rehti MMORPG is a free and open source MMORPG game.
rehtiLib/assets/loader/src/Utils.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <exception>
4 #include <filesystem>
5 #include <fstream>
6 #include <iostream>
7 #include <ranges>
8 #include <vector>
9 
10 #include "../lodepng/lodepng.h"
11 
19 std::vector<std::string> readDirectory(std::string path, std::string extension)
20 {
21  std::vector<std::string> files;
22 
23  for (const auto& entry : std::filesystem::directory_iterator(path))
24  {
25  if (entry.path().extension() == extension)
26  {
27  files.push_back(entry.path().filename().string());
28  }
29  }
30 
31  return files;
32 }
33 
42 void readPng(std::vector<unsigned char>& image, unsigned& width, unsigned& height, std::string filepath)
43 {
44  std::cout << "Reading " << filepath << std::endl;
45 
46  unsigned error = lodepng::decode(image, width, height, filepath);
47 
48  if (error)
49  {
50  throw std::runtime_error("PNG decoding error" + std::to_string(error) + ": " + lodepng_error_text(error));
51  }
52 }
53 
60 template <typename T>
61 void rotateMatrix(std::vector<std::vector<T>>& matrix, uint8_t rotation)
62 {
63  if (rotation == 0)
64  {
65  return;
66  }
67 
68  std::vector<std::vector<T>> rotatedMatrix;
69 
70  for (unsigned i = 0; i < matrix[0].size(); i++)
71  {
72  std::vector<T> row;
73  for (int j = matrix.size() - 1; j >= 0; j--)
74  {
75  row.push_back(matrix[j][i]);
76  }
77  rotatedMatrix.push_back(row);
78  }
79 
80  matrix = rotatedMatrix;
81 
82  rotateMatrix(matrix, rotation - 1);
83 }
84 
95 template <typename T>
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)
97 {
98  for (unsigned i = 0; i < areaMap.size(); i++)
99  {
100  for (unsigned j = 0; j < AREA_HEIGHT; j++)
101  {
102  std::vector<T> row(AREA_WIDTH * areaMap[i].size(), defaultValue);
103  matrix.push_back(row);
104  }
105  }
106 }
107 
115 template <typename T, typename K>
116 void populateMatrixFromReference(std::vector<std::vector<T>>& matrix, const std::vector<std::vector<K>>& referenceMatrix, T defaultValue)
117 {
118  for (unsigned i = 0; i < referenceMatrix.size(); i++)
119  {
120  std::vector<T> row(referenceMatrix[i].size(), defaultValue);
121  matrix.push_back(row);
122  }
123 }
124 
132 template <typename T>
133 void writeMatrixToFile(const std::vector<std::vector<T>>& matrix, const std::string& filepath)
134 {
135  // Create the directory structure if it doesn't exist
136  std::filesystem::create_directories(std::filesystem::path(filepath).parent_path());
137 
138  std::ofstream file(filepath, std::ios::out); // std::ios::out to create the file if it does not exist
139 
140  if (file.is_open())
141  {
142  for (const auto& row : matrix)
143  {
144  for (const auto& col : row)
145  {
146  file << col << ',';
147  }
148  file << '\n';
149  }
150 
151  file.close();
152  }
153  else
154  {
155  throw std::runtime_error("Unable to open the file for writing.");
156  }
157 }
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