1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
#include <SFML/Graphics.hpp>
#include <vector>
void draw_partial_box(sf::RenderTarget& t, const std::vector<sf::Vector2f>& box,
float time_passed, float target_time)
{
const float quarter_time = target_time * .25f;
std::vector<sf::Vertex> v(1, { box[0], sf::Color::Black });
for (std::size_t i = 0; i < box.size()-1; ++i)
{
if (time_passed > quarter_time * i)
{
float ratio = 1.0f;
if (time_passed < quarter_time * (i + 1))
ratio = 1.0f - (quarter_time * (i + 1) - time_passed) / quarter_time;
v.push_back({ {(box[i + 1].x - box[i].x)*ratio + box[i].x,
(box[i + 1].y - box[i].y)*ratio + box[i].y}, sf::Color::Black });
}
}
t.draw(v.data(), v.size(), sf::LinesStrip);
}
int main()
{
sf::VideoMode vm(800, 600);
sf::RenderWindow win(vm, "Boxy");
std::vector<sf::Vector2f> points =
{
{ vm.width * .25f, vm.height * .25f },
{ vm.width * .75f, vm.height * .25f },
{ vm.width * .75f, vm.height * .75f },
{ vm.width * .25f, vm.height * .75f },
{ vm.width * .25f, vm.height * .25f }
};
sf::Clock clock;
while (win.isOpen())
{
sf::Event event;
while (win.pollEvent(event))
{
switch (event.type)
{
case sf::Event::Closed:
win.close();
break;
}
}
win.clear(sf::Color::White);
draw_partial_box(win, points, clock.getElapsedTime().asSeconds(), 5.0f);
win.display();
}
}
|