I'm building a game in Sfml, and I want to print text to the display screen easier so I created a function that does it for me, but, whenever I run the function, the window opens then closes really fast, not allowing me to see the results. However, when I don't use the function to print text to the console, nothing happens, I was wondering what would help me resolve this problem
This really should be in the window section, but I can't move the question.
Here is my Code
Main.cpp
#include <SFML/Graphics.hpp>
#include <iostream>
#include "write.h"
#include <SFML/Graphics/Font.hpp>
int main()
{
sf::RenderWindow window(sf::VideoMode::getDesktopMode(), "SFML works!");
sf::Font fonts;
if(!fonts.loadFromFile("bit.ttf"))
{
std::cerr << "Error loading font" << std::endl;
return EXIT_SUCCESS;
}
sf::Text word;
word.setFont(fonts);
word.setString("Hello World");
word.setFillColor(sf::Color::Red);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
window.draw(print("aiden"));
window.display();
}
return 0;
}
write.h
#ifndef WRITE_H
#define WRITE_H
#include <SFML/Graphics.hpp>
#include <SFML/Graphics/Font.hpp>
sf::Text print(std::string text);
#endif
write.cpp
#include "write.h"
#include <iostream>
#include <SFML/Graphics.hpp>
#include <SFML/Graphics/Font.hpp>
sf::Text print(std::string text) // std::string font)
{
sf::Font fonts;
if(!fonts.loadFromFile("bit.ttf"))
{
std::cerr << "Error loading font" << std::endl;
}
sf::Text word;
word.setFont(fonts);
word.setString(text);
word.setFillColor(sf::Color::White);
return word;
}