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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
|
#include <random>
#include <deque>
#include <SFML/Graphics.hpp>
sf::Color lighten( sf::Color color )
{
return sf::Color(color.r+15, color.g+15, color.b+15) ;
}
class CascadingRectangle : public sf::Drawable
{
public:
CascadingRectangle() : _rect(sf::Vector2f(10,10))
{
_rect.setPosition(sf::Vector2f(0,0)) ;
_rect.setFillColor(sf::Color::Black) ;
}
CascadingRectangle( sf::Vector2f position, sf::Vector2f size, sf::Color color ) : _rect(size)
{
_rect.setPosition(position) ;
_rect.setFillColor(color) ;
}
void draw( sf::RenderTarget& tgt, sf::RenderStates states) const
{
sf::RectangleShape cascade[2] =
{
_rect,
_rect
};
sf::Vector2f offset(5.0, 5.0) ;
cascade[0].move(offset) ;
cascade[1].move(offset*2.0f) ;
cascade[0].setFillColor(lighten(_rect.getFillColor())) ;
cascade[1].setFillColor(lighten(cascade[0].getFillColor())) ;
tgt.draw(_rect, states) ;
tgt.draw(cascade[0], states) ;
tgt.draw(cascade[1], states) ;
}
private:
sf::RectangleShape _rect ;
};
int main()
{
std::default_random_engine rng((std::random_device()())) ;
sf::RenderWindow window(sf::VideoMode(800, 600), "EVENT Handling") ;
std::deque<CascadingRectangle> boxes ;
while ( window.isOpen() )
{
sf::Event event ;
while ( window.pollEvent(event) )
{
if ( event.type == sf::Event::Closed )
window.close() ;
else if ( event.type == sf::Event::MouseButtonReleased )
{
sf::Vector2f pos(event.mouseButton.x, event.mouseButton.y) ;
sf::Vector2f size( rng()%80 + 30, rng()%80 + 30) ;
sf::Color color(rng()%128 + 64, rng()%128 + 64, rng()%128+64) ;
boxes.push_back(CascadingRectangle(pos, size, color)) ;
if ( boxes.size() > 5 )
boxes.pop_front() ;
}
}
window.clear(sf::Color::White) ;
for ( auto & box : boxes )
window.draw(box) ;
window.display() ;
}
}
|