Hello, everyone!
I'm the begginer in the using of SFML and there is an error, which i can't fix:
#include <SFML/Graphics.hpp>
#include <iostream>
#include <memory>
#include <vector>
std::vector<std::shared_ptr<sf::Drawable>> createDrawableVector() {
std::vector<std::shared_ptr<sf::Drawable>> vec;
std::shared_ptr<sf::Font> tahoma = std::make_shared<sf::Font>();
if (!tahoma->loadFromFile("corbel.ttf")) {
return vec;
}
auto text = std::make_shared<sf::Text>("Clicks", *tahoma, 20);
text->setPosition(100.f, 100.f);
vec.push_back(std::move(text));
auto rect = std::make_shared<sf::RectangleShape>(sf::Vector2f(100.f, 100.f));
rect->setPosition(200.f, 200.f);
rect->setFillColor(sf::Color::Red);
vec.push_back(std::move(rect));
return vec;
}
int main() {
sf::RenderWindow window(sf::VideoMode(550, 700), "SFML Drawable Vector", sf::Style::Close);
std::vector<std::shared_ptr<sf::Drawable>> drawableVector = createDrawableVector();
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
for (auto &element : drawableVector) {
window.draw(*element);
}
window.display();
}
return 0;
}
When i'm running it i'm taking the
-1073741819 error.
But when i'm
replacing it into main() the error is going awayint main() {
sf::RenderWindow window(sf::VideoMode(550, 700), "SFML Drawable Vector", sf::Style::Close);
std::vector<std::shared_ptr<sf::Drawable>> vec;
std::shared_ptr<sf::Font> tahoma = std::make_shared<sf::Font>();
if (!tahoma->loadFromFile("corbel.ttf")) {
return -1;
}
auto text = std::make_shared<sf::Text>("Clicks", *tahoma, 20);
text->setPosition(100.f, 100.f);
vec.push_back(std::move(text));
auto rect = std::make_shared<sf::RectangleShape>(sf::Vector2f(100.f, 100.f));
rect->setPosition(200.f, 200.f);
rect->setFillColor(sf::Color::Red);
vec.push_back(std::move(rect));
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
for (auto &element : vec) {
window.draw(*element);
}
window.display();
}
return 0;
}
The same code with sf::RectangleShape, not sf::Text is working great
I think, that some text data became null after createDrawableVector() was finished, therefore text can't be rendered.
Cuz this code is not working too#include <SFML/Graphics.hpp>
#include <iostream>
#include <memory>
#include <vector>
std::vector<sf::Text> createDrawableVector() {
std::vector<sf::Text> vec;
sf::Font corbel;
if (!corbel.loadFromFile("corbel.ttf")) {
return vec;
}
sf::Text text("Clicks", corbel, 20);
text.setPosition(100.f, 100.f);
vec.push_back(text);
return vec;
}
int main() {
sf::RenderWindow window(sf::VideoMode(550, 700), "SFML Drawable Vector", sf::Style::Close);
std::vector<sf::Text> drawableVector = createDrawableVector();
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
for (auto &element : drawableVector) {
window.draw(element);
}
window.display();
}
return 0;
}
Please help me with it)