Hello everyone,
I've been looking for something similar to this error in the internet as well as in this forums and I haven't been able to found anything that solves my problem.
I'm going to simple down the error. I have a main() function where the first thing I create is a sf::RenderWindow. I have an fps counter (still in main()) which is a sf::Text and it displays correctly. However if I create a class (a button, for example) that has a sf::Texture as a member variable and I create a variable on Button, the program crashes in the declaration of sf::RenderWindow. Here is the simplest code I can get to reproduce the error:
// In one file
#ifndef __BUTTON_H__
#define __BUTTON_H__
#include <SFML/Graphics/RenderWindow.hpp>
#incldue <SFML/Graphics/Text.hpp>
class Button {
public:
Button();
~Button();
void draw(sf::RenderWindow* window);
private:
sf::Text m_text;
};
#endif // __BUTTON_H__
// ============================================================================
// In other file
#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Graphics/Text.hpp>
#include <SFML/Window/Keyboard.hpp>
#include <SFML/Window/Event.hpp>
int main() {
sf::RenderWindow window(sf::VideoMode(800, 600), "");
sf::Font font;
font.loadFromFile("resources/fonts/arial.ttf");
sf::Text text;
text.setFont(font);
text.setCharacterSize(40);
text.setPosition(400, 300);
text.setString("0");
while (window.isOpen()) {
// Input
sf::Event e;
if (window.pollEvent(e)) {
if (e.type == sf::Event::Closed) {
window.close();
break;
}
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Escape)) {
window.close();
break;
}
// Draw
window.clear();
window.draw(text);
window.display();
}
return 0;
}
That code would run correctly. But in the moment I include button.h lets say, and create a Button object the program crashes as I mentioned. Just ignore that the counter always displays 0 and that I don't make any check for if loading the font correctly (it loads correctly). I tried to debug and found that the program crashes in toAnsiString() when parsing the window's title, I think. Oddly enough, if I declare a std::string just above the declaration of the RenderWindow the program runs, but the graphics displayed are just garbage.
int main() {
std::string s("anything");
sf::RenderWindow window(sf::VideoMode(800, 600), "");
}
Any ideas? Thanks in advance.
Kind regards,
Olmo.