Hi,
I'm experiencing a strange problem. I have a simple entity (MyShape in the code below) that defines a shape and the ability to draw it in a window.
The code is very simple. If you run it, it should draw the shape as a white rectangle and not like it has been defined in the code (a circle with a "tail")!.
Creating a new "dummy" object uncommenting line 65 after having created the shapes in a vector, solves the issue.
Any Idea?
#include <algorithm>
#include <iostream>
#include <memory>
#include <utility>
#include <SFML/Graphics.hpp>
#include <SFML/System/Clock.hpp>
#include <random>
#include <math.h>
class MyShape {
public:
MyShape () {
sf::CircleShape circle{5};
circle.setFillColor({255, 255, 255});
circle.setPosition(0, 0);
sf::ConvexShape triangle {3};
triangle.setFillColor({255, 255, 255});
triangle.setPoint(0, {0, 5});
triangle.setPoint(1, {5, 17});
triangle.setPoint(2, {10, 5});
sf::RenderTexture canvas;
canvas.create(10, 17);
canvas.clear(sf::Color::Transparent);
canvas.draw(circle);
canvas.draw(triangle);
canvas.display();
shapeTexture = canvas.getTexture();
shape.setTexture(shapeTexture);
position.x = 200;
position.y = 200;
}
void draw(sf::RenderWindow& canvas) {
shape.setPosition(position.x, position.y);
canvas.draw(shape);
}
private:
sf::Vector2<float> position;
sf::Sprite shape;
sf::Texture shapeTexture;
};
int main()
{
// Create the main window
sf::RenderWindow App(sf::VideoMode(800, 600, 32), "Test");
App.setFramerateLimit(120);
App.setVerticalSyncEnabled(true);
// Test crittes
std::vector<MyShape> myShapes;
myShapes.resize(2);
std::generate(begin(myShapes), end(myShapes), []() {
return MyShape{};
});
// !!!! HACK !!!!
// Hack to make the other critters draw correctly. Bug in SFML?
// MyShape shp;
// Start the main loop
while (App.isOpen())
{
// Process events
sf::Event Event;
while (App.pollEvent(Event))
{
// Close window : exit
if (Event.type == sf::Event::Closed || (Event.type == sf::Event::KeyPressed && Event.key.code == sf::Keyboard::Escape)) {
App.close();
}
}
// Clear screen, and fill it with blue
App.clear({0, 0, 0});
std::for_each(begin(myShapes), end(myShapes), [&](MyShape& myShape) {
myShape.draw(App);
});
// Display the content of the window on screen
App.display();
}
return 0;
}