SFML community forums

Help => General => Topic started by: Catalin_Cristian on April 28, 2018, 06:33:32 pm

Title: Drawing text with a class
Post by: Catalin_Cristian on April 28, 2018, 06:33:32 pm
Hi guys, I tried to render a text and I got this error:  sf::RenderTarget::draw': no overloaded function takes 0 arguments.

This is the code:

the class:

class menu
{
public:
   void draw_text();
   void move_up();
   void move_down();


private:

   sf::Text text;
   sf::Font font;
};

menu cpp:

void menu::draw_text()
{
   try {

      if (!font.loadFromFile("champ.ttf"))
         throw font.loadFromFile("champ.ttf");
   }
   catch (...) {
      cout << "Error! Missing font!";
   }

   text.setFont(font);
   text.setFillColor(sf::Color::Red);
   text.setString("Hello there");
        text.setCharacterSize(30);
       
}


the main cpp:

int main()
{
   menu menuObj;

   sf::RenderWindow window(sf::VideoMode(1920, 1080), "SpaceShooter");

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

      window.clear();
      window.draw();
                menuObj.draw_text();
      window.display();
   }

   return 0;
}


I didn't finish the text code but I tried it to see if it works, to see if at least the text appears on screen.I don't know if you will understand the code but I hope someone can help me.
Title: Re: Drawing text with a class
Post by: G. on April 28, 2018, 06:46:24 pm
Pretty explicit, window.draw() doesn't exist without argument.

If you want to draw the variable "text", you will definitely want to write "window.draw(text);" at some point. Either you pass window as an argument to your draw_text function so that you can use "window" inside it, or you return "text" from a function so that you can use it in your main.

And by the way, loading the font doesn't need be done every frame. Once is enough.