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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - pistacchio

Pages: [1]
1
General / Re: Strange drawing problem
« 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.

2
General / [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;
}

 

Pages: [1]
anything