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