- the problem is definitely in class. - the main(){...} really wont help you, its just creating object of Text_manager, than .set_text(0,24,10);and .add_text(...) twicely, and of course .draw() in main loop...
Something is wrong with class, the description of the problem is above.
Anyway the complete code is here:
#include <SFML/Graphics.hpp>
#include <windows.h>
#include <iostream>
sf::RenderWindow window(sf::VideoMode(800, 650), "screen");
class Text_manager{
public:
bool add_text(const std::string& text_worlds){
rectangle.setTexture(&texture);
rectangle.setPosition(0,0);
rectangle.setSize(sf::Vector2f(800, count * (text_size + space_size)));
sf::Font font;
if (!font.loadFromFile("arial.ttf"))
return false;
sf::Text text;
text.setFont(font);
text.setString(text_worlds);
text.setCharacterSize(text_size);
text.setColor(sf::Color::Red);
text.setPosition(0,count * (text_size + space_size) + space_size);
renderTexture.create(800, (count + 1) * (text_size + space_size));
renderTexture.clear();
renderTexture.draw(rectangle);
renderTexture.draw(text);
renderTexture.display();
texture = renderTexture.getTexture();
count ++;
return true;
};
void set_text(int p_count, int p_text_size, int p_space_size){
count = p_count;
text_size = p_text_size;
space_size = p_space_size;
};
void draw(){
sprite.setTexture(texture);
window.draw(sprite);
};
private:
int count;
int text_size;
int space_size;
sf::RenderTexture renderTexture;
sf::RectangleShape rectangle;
sf::Sprite sprite;
sf::Texture texture;
};
int main()
{
Text_manager try1;
try1.set_text(0,24,10);
try1.add_text("text");
try1.add_text("newText");
//---------------------------
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event)){
if (event.type == sf::Event::Closed)window.close();
}
//------------------------------------------------
window.clear(sf::Color::Black);
try1.draw();
//--------------------------
window.display();
}
return 0;
}