3 ms·
just want to point out, this unjustly uses std::shared_ptr in main.cpp auto flappy = std::make_shared<FlappyBird>(); flappy->run(); you can do this. F
by throwaway968321 4y ago
just want to point out, this unjustly uses std::shared_ptr
in main.cpp
auto flappy = std::make_shared<FlappyBird>();
flappy->run();
you can do this.
FlappyBird flappy;
flappy.run();
in flappy.hpp
std::shared_ptr<sf::RenderWindow> window;
std::shared_ptr<sf::Sprite> background, bird, pipeBottom, pipeTop;
just do this.
sf::RenderWindow window;
sf::Sprite background, bird, pipeBottom, pipeTop;
in flappy.cpp.
auto e = std::make_shared<sf::Event>();
while( window->pollEvent( *e ) ){
if( e->type == sf::Event::Closed){
window->close();
}
}
just do this
sf::Event event;
while (window.pollEvent(event)) {
if (e.type == sf::Event::Closed) {
window.close();
}
}
It's really that simple.
- throwaway968321 4y agoin https://github.com/terroo/flappybird/blob/main/flappy.cpp#L132 https://github.com/terroo/flappybird/blob/main/flappy.cpp#L1... for (std::size_t i {}; i < pipes.size(); ++i) { C++ has had range based loops for years now, can use that instead here. for (auto& pipe : pipes) I don't remember if the '&' is needed after the auto though. anyway you should just use Rust, Zig, or Go...
- eco 4y agoYeah, since C++11 (the 2011 standard). '&' isn't necessary to use auto but you would use it here to avoid making an unnecessary copy of the pipe.
- ncmncm 4y agoIn this case, where it is erasing one of the container elements inside the loop, the range-based loop would not be right. But the code is busted anyway. After it erases the pipe (which is always at index zero) it then skips the pipe that was at index 1, if there was one. Correct would be to do the check and erase before entering the loop. Then the range-based loop would better.