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 - ConnorJW

Pages: [1]
1
Graphics / Re: Managing multiple animations
« on: December 04, 2015, 07:07:56 pm »
Yes, the code provided is just for one sprite, sorry about that.

The sprites are loaded through an init function in the DrawCups constructor:

void DrawCups::initSprites()
{
        loadImage("./images/InsertionSort/red_cup_1.png", _cup1, _cup1Sprite);
        _cup1Sprite.setPosition(200, 230);
        _cup1.setSmooth(true);

        loadImage("./images/InsertionSort/red_cup_9.png", _cup9, _cup9Sprite);
        _cup9Sprite.setPosition(sf::Vector2f(300, 230));

        loadImage("./images/InsertionSort/red_cup_3.png", _cup3, _cup3Sprite);
        _cup3Sprite.setPosition(sf::Vector2f(450, 230));

        loadImage("./images/InsertionSort/red_cup_5.png", _cup5, _cup5Sprite);
        _cup5Sprite.setPosition(sf::Vector2f(600, 230));

        loadImage("./images/InsertionSort/red_cup_2.png", _cup2, _cup2Sprite);
        _cup2Sprite.setPosition(sf::Vector2f(750, 230));

        loadImage("./images/InsertionSort/red_cup_8.png", _cup8, _cup8Sprite);
        _cup8Sprite.setPosition(sf::Vector2f(900, 230));
}

Each sprite is being drawn via the 'drawCups' function:

void DrawCups::drawCups()
{
        _window.draw(_cup1Sprite);
        _window.draw(_cup9Sprite);
        _window.draw(_cup9Sprite);
        _window.draw(_cup3Sprite);
        _window.draw(_cup5Sprite);
        _window.draw(_cup2Sprite);
        _window.draw(_cup8Sprite);
}

I removed the code for the moving of the other cups because it was the exact same function as the .update(), just updateCup3() etc... I've decided to go in the direction of just having one update function that will call the name of the sprite needing to be updated.

2
Graphics / Re: Managing multiple animations
« on: December 04, 2015, 06:20:34 pm »
Yes, sorry, all the sprites are doing is moving via the sprite.move() every frame until it reaches the desired location on the x,y axis, then stays there. This is done via an update function:

void DrawCups::update(float perFrameX, float perFrameY, float desiredX, float desiredY)
{
        _cup1Sprite.move(perFrameX, perFrameY);
        if (_cup1Sprite.getPosition().x >= desiredX)
        {
                _cup1Sprite.setPosition(desiredX, desiredY);
                _cup1Sprite.setColor(sf::Color(0, 255, 0));
        }
}

which is called in the window main loop:

while (window.isOpen())
        {
                sf::Event event;
                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                                window.close();
                }

                window.clear(sf::Color(250, 240, 230));
                drawToWindow.drawBench();
                drawToWindow.update(1,0, 400,230);
                drawToWindow.drawCups();
                //drawToWindow.update(1,1,500,330);
                window.display();
        }

The program isn't running on the back of a sorting algorithm, it serves the purpose to explain problem solving process in a visual way; i.e. Insertion Sort uses cups with numerical values which then move in location to demonstrate the updating elements within the data structure as the sort continues through.

3
Graphics / Managing multiple animations
« on: December 03, 2015, 01:49:14 am »
Hey all,

I'm working on a project at the moment which visualises sort algorithms to demonstrate how they differ in handling the sort problem. This requires a lot of on-screen animation, but vitally, takes in no user input. I'm pretty new to SFML, so I was wondering what's the best approach to handling the animation of each element on the screen?

The elements (each are sprites), will be moved one after another to show the process of sorting. Then once sorted, the sprite will be highlighted green; this has already been implemented. The problem is with all the animations playing at the same time, apposed to first animation, pause, second animation, pause etc...

Originally I though of using threads (coming from a currency programming background), but the SFML tutorials suggest to not use threads to handle graphics. So would you suggest using state machines to achieve this?

Any document/references will be most helpful - or your opinion on how you would go about implementation.

4
Graphics / Re: Update function for re-drawing sprite
« on: December 02, 2015, 07:15:58 pm »
Where would you suggest for me to initially set the location for the sprite on start-up?
The constructor of your class would seem to be the obvious place...
That's what constructors are for after all - setting initial values..

Yeah, sorry haha. Feel pretty stupid at the fact it took me so long to fix, what now seems to be a simple mistake. Thanks all! It works perfectly now - I'll update the title to solved.

5
Graphics / Re: Update function for re-drawing sprite
« on: December 02, 2015, 07:08:08 pm »
Thanks for the replies all :)

Where would you suggest for me to initially set the location for the sprite on start-up? Also thanks for the hint for loading the sprite every frame - I'm literally brand new to SFML/graphics libraries.

6
Graphics / Re: Update function for re-drawing sprite
« on: December 02, 2015, 05:59:34 pm »
void DrawCups::drawCup1()
{
        // load our image
        loadImage("./images/InsertionSort/red_cup_1.png", _cup1, _cup1Sprite);
        _cup1Sprite.setPosition(sf::Vector2f(150, 230));
        _cup1.setSmooth(true);
        _window.draw(_cup1Sprite);
}

is the draw function

and loading the sprite:

void DrawCups::loadImage(const char* pathname, sf::Texture& texture, sf::Sprite& sprite)
{
        texture.loadFromFile(pathname);     // load it from the file
        sprite.setTexture(texture);         // put that texture in our sprite
        sprite.setTextureRect(sf::IntRect(0, 0, 139, 186));    // the rectangle of the texture to use for this sprite
}

7
Graphics / [Solved] Update function for re-drawing sprite
« on: December 02, 2015, 03:43:28 am »
The current update function should be moving the sprite to the left 10 pixels every time it's called, but the sprite (cup1Sprite) is static and not moving. I tried using sprite.rotate(); and the sprite rotated every frame (which was expected), so something is wrong with the way I've written the update function for updating the sprite position. Can anyone suggest what's going wrong?

Code:
DrawCups.h

    class DrawCups
    {
    public:
        DrawCups(sf::RenderWindow& window);
        ~DrawCups();
   
        void update();
   
        void drawCup1();
   
    private:
        sf::RenderWindow& _window;
        //Cup1
        sf::Texture _cup1;        // the texture which will contain our pixel data
        sf::Sprite _cup1Sprite;         // the sprite which will actually draw it
    };

Update function in DrawCups.cpp

 
   void DrawCups::update()
    {
        sf::Vector2f pos = this->_cup1Sprite.getPosition();
        pos.x+=10;
        this->_cup1Sprite.setPosition(pos);
    }

main.cpp

   
 int main()
    {
        sf::RenderWindow window(sf::VideoMode(1366, 768), "Sorting Algorithm Visualisation: SFML");
        window.setFramerateLimit(60);
        DrawCups drawToWindow(window);
   
        while (window.isOpen())
        {
                sf::Event event;
                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                                window.close();
                }
   
                drawToWindow.update();
   
                window.clear(sf::Color::White);
                drawToWindow.drawBench();
                drawToWindow.drawCup1();
                window.display();
        }
   
        return 0;
    }

Pages: [1]