Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [SOLVED] Cannot create a version of font that isn't destroyed for Text  (Read 557 times)

0 Members and 1 Guest are viewing this topic.

ZadiusC

  • Newbie
  • *
  • Posts: 2
    • View Profile
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();
}
« Last Edit: July 10, 2022, 09:33:39 am by eXpl0it3r »

ZadiusC

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Cannot create a version of font that isn't destroyed for Text
« Reply #1 on: July 09, 2022, 10:09:12 pm »
For anyone looking I had to create the object t as a reference to an index of display array prior to assigning any characteristics to the obj.

Here is the updated code:

void SetStats::populateDisplayVec()
{
        int posX = 100;
        int posY = 100;
        int track = 0;
        //for (auto& text : setsArray)

        int temp = 3 * 11 + 50;
        displayArray.resize(temp);

        for (int i = 0; i < setsArray.size(); i++)
        {
                for (int j = 0; j < 11; j++)
                {
                       
                        sf::Text &t = displayArray[track];
                        track++;
                        //      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;
                        //  Removed -> displayArray.push_back(t);
                };
        }
}
« Last Edit: July 10, 2022, 09:34:00 am by eXpl0it3r »