I am working on a toy project. Basically, it's a set of classes and functions for making games that run in terminal. I want to use ANSI escape codes to change text color, style, background color and etc. I am trying to input shapes and colors from text files. I'm storing them in a class, which contains an array of strings (for the shapes) and a 2D array of strings (for the colors). When I hard code the colors it works fine, but when I input them from a file (using ifstream) it just outputs the codes and does nothing to the text style.
Is there a way to solve this problem without writing a separate function to process the inputted codes?
The load function:
void charbox::load(std::string path, std::string path2) {
std::ifstream txt;
std::string s;
txt.open(path);
if (txt.is_open() == 0) {
std::cout << "ERROR 404, " << path << " NOT FOUND!\n";
}
while (std::getline(txt,s)) {
frame.push_back(s);
}
txt.close();
txt.open(path2);
if (txt.is_open() == 0) {
std::cout << "ERROR 404, " << path2 << " NOT FOUND!\n";
}
std::vector <std::string> v(frame[0].size(), "");
color.resize(frame.size(), v);
for (int i = 0, height = frame.size(); i < height; i++) {
for (int j = 0, width = frame[i].size(); j < width; j++) {
txt >> color[i][j];
}
}
txt.close();
}
The class:
class charbox {
public:
std::vector <std::string> frame;
std::vector <std::vector <std::string>> color;
void load(std::string path, std::string path2);
charbox(int p_width, int p_height);
charbox(){};
private:
};
When I output:
std::cout << <some object>.color[<some index>][<some index>] << "test\n";
I want the color of the "test" to be changed based on the input, but it just outputs the code in form of plain text.
An example of files in path and path2:
path contains:
[][]
[][]
path2 contains:
\033[1;31;41m \033[1;31;41m \033[1;31;41m \033[1;31;41m
\033[1;31;41m \033[1;31;41m \033[1;31;41m \033[1;31;41m
Thanks in advance!
P.S. I am running Linux (windows-specific solutions won't work for me).