1
#pragma once

#include <SFML/Graphics.hpp>
class RectangleWText
{
private:
    sf::RectangleShape shape;
    sf::Font font;
    sf::Text label(std::string&);

public:
    RectangleWText(const sf::Vector2f& size, const sf::Vector2f& position, const 
std::string& text = "", const std::string& fontPath = "fonts/verdana.ttf") {
    
    if (!font.openFromFile("verdana.ttf"))
    {
        std::cerr << "Error loading font" << std::endl;
    }
    sf::Font font(fontPath);
    sf::Text label(font);


    shape.setSize(size);
    shape.setPosition(position);

    label.setString(text);
    label.setPosition(position);
}

void draw(sf::RenderWindow& window) {
    window.draw(shape);
    window.draw(label); 
}
};

SFML 3 changed the way you initiate text and it is killing me since I can't draw it in my draw function now, I am new to SFML please someone help me.

1

1 Answer 1

1

Default constructor of sf::Text was removed in SFML3.0. Now one have to initialize text with sf::Font object.

You can create a font instance before creating your RectangleWText and pass a font's reference to constructor. In this case you'll be able to properly create label member this manner

RectangleWText(const sf::Vector2f& position, const sf::Font& _font, const std::string& text = "", /* other parameters */) :
  label{_font}
{
    ...
    
    label.setString(text);
    label.setPosition(position);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.