Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [SOLVED] Strange drawing problem  (Read 1281 times)

0 Members and 2 Guests are viewing this topic.

pistacchio

  • Newbie
  • *
  • Posts: 2
    • View Profile
[SOLVED] Strange drawing problem
« on: April 15, 2014, 06:20:12 pm »
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;
}

 
« Last Edit: April 15, 2014, 08:08:01 pm by pistacchio »

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Strange drawing problem
« Reply #1 on: April 15, 2014, 06:34:46 pm »

pistacchio

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Strange drawing problem
« Reply #2 on: April 15, 2014, 08:07:47 pm »
Thank you very much, that helped. I made the texture a static member of the class and all the shape objects refer to that single instance.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: [SOLVED] Strange drawing problem
« Reply #3 on: April 15, 2014, 08:14:46 pm »
Avoid static/global SFML objects, they will also lead to problems sooner or later. In general, it's a good idea to find a design that works with as few static/global variables as possible. You could store your texture elsewhere and pass a pointer or reference to the shape class.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything