Hi All,
I have been trying to print text from an array, but every time it runs no text appears, unless I create an endless loop and then only the final text from each row of the array is displayed. I have read up on pointers and references and have tried both but neither work and I cannot for the life of me figure out what I am doing wrong.
Here is my code, it is currently set to a reference, after I tried pointers and it didn't work. Thanks in advance for any help offered!
in SetStats.h
// Set up vectors
std::vector<std::vector<int>> setsArray;
std::vector<sf::Text> displayArray;
sf::Font FONT;
const sf::Font& font = FONT;
-----------------------------------------------------
In SetStats.cpp:
SetStats::SetStats()
{
// Load font
if (!FONT.loadFromFile("assets/fonts/arial.ttf"))
{
// If there is an error loading the font
std::cerr << "Error reading font from file\n";
exit(-1);
}
else { std::cout << "Font loaded sucessfully"; }
setsArray.resize(33, std::vector<int>(11, 0));
setsArray = {
{5, 31, 2022, 6, 13, 23, 27, 35, 44, 50, 37},
{6, 3, 2022, 2, 16, 17, 21, 38, 43, 45, 42},
{6, 7, 2022, 6, 13, 17, 20, 28, 36, 44, 31}
};
populateDisplayVec();
...
void SetStats::populateDisplayVec()
{
int posX = 100;
int posY = 100;
sf::Text t;
//for (auto& text : setsArray)
for (int i = 0; i < setsArray.size(); i++)
{
for (int j = 0; j < 11; j++)
{
;
// Set text params
t.setFont(font);
t.setCharacterSize(24);
t.setFillColor(sf::Color::Black);
int num = setsArray[i][j];
std::string st = (std::to_string(num));
std::string s = " " + st + ", ";
t.setString(s);
// Bump to next set
if (j == 0)
{
posY += 60;
posX = 100;
}
std::cout << std::to_string(posX) << ", " << std::to_string(posY) << " ";
t.setPosition(sf::Vector2f(posX, posY));
posX += 60;
};
displayArray.push_back(t);
}
}
...
void SetStats::update()
{
pollEvents();
}
void SetStats::render()
{
window->clear(backColor);
// Draw the user input to the window
if (allowInput)
{
window->draw(userMsg);
}
else if (refresh)
{
for (int i = 0; i < displayArray.size(); i++)
{
window->draw(displayArray[i]);
}
refresh = false; // if I comment this out then the last 3 numbers of each row show but nothing else
}
window->display();
}