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

Author Topic: Exception when trying to draw text from a function  (Read 2367 times)

0 Members and 1 Guest are viewing this topic.

supernintendo64

  • Newbie
  • *
  • Posts: 3
    • View Profile
Exception when trying to draw text from a function
« on: May 04, 2019, 08:07:18 pm »
Hi,

So I'm trying to get started on a new game. I figured that I could streamline the action of rendering text by putting all of the routines needed to do it (setting the font, the size, the color, and the text itself) into a function but when I try to execute this function, I get the error "Exception thrown at 0x0FD2485F (sfml-graphics-d-2.dll) in SFMLProject.exe: 0xC0000005: Access violation reading location 0x3F800004."

Here is my code:

#include <SFML/Graphics.hpp>

sf::Text drawText(sf::Font font, std::string inputText, int size, sf::Color color) //Function to draw text (TODO: Add the ability to specify coordinates)
{
        sf::Text outputText;
        outputText.setFont(font);
        outputText.setString(inputText);
        outputText.setCharacterSize(size);
        outputText.setFillColor(color);
        return outputText;
}

int main()
{
        sf::RenderWindow window(sf::VideoMode(200, 200), "SFML Project");
        sf::CircleShape shape(100.f);
        sf::Font testFont;
        testFont.loadFromFile("calibri.ttf");

        shape.setFillColor(sf::Color::Green);

        while (window.isOpen())
        {
                sf::Event event;
                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                                window.close();
                }

                window.clear();
                window.draw(shape);
                window.draw(drawText(testFont, "Development Build 0", 12, sf::Color::White));
                window.display();
        }

        return 0;
}

EDIT: I just want to add that this does not happen when I manually draw text in the main function.
« Last Edit: May 04, 2019, 09:39:02 pm by supernintendo64 »

fallahn

  • Hero Member
  • *****
  • Posts: 507
  • Buns.
    • View Profile
    • Trederia
Re: Exception when trying to draw text from a function
« Reply #1 on: May 04, 2019, 10:24:57 pm »
You're passing in a copy of the font (which will go out of scope when the function returns) instead of a reference. Resources such as fonts, textures and sound buffers need to make sure their lifetime exceeds that of anything which uses them. Usually this is done with some sort of resource manager, examples of which are on the wiki, or available in libraries like Thor.

supernintendo64

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Exception when trying to draw text from a function
« Reply #2 on: May 04, 2019, 11:44:33 pm »
You're passing in a copy of the font (which will go out of scope when the function returns) instead of a reference. Resources such as fonts, textures and sound buffers need to make sure their lifetime exceeds that of anything which uses them. Usually this is done with some sort of resource manager, examples of which are on the wiki, or available in libraries like Thor.

Thank you so much. I changed the function so that the variable font is being referenced instead of being copied (i.e. I changed sf::Font font to sf::Font &font) and it fixed the problem.