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

Author Topic: Drawing text with a class  (Read 1176 times)

0 Members and 1 Guest are viewing this topic.

Catalin_Cristian

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
Drawing text with a class
« 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.
« Last Edit: April 28, 2018, 07:50:46 pm by Catalin_Cristian »

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Drawing text with a class
« Reply #1 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.

 

anything