Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bn_core.h>
- #include <bn_forward_list.h>
- #include <bn_log.h>
- #include <bn_pool.h>
- struct Bullet {
- int x, y;
- bool is_dead;
- };
- int main() {
- bn::core::init();
- constexpr int POOL_SIZE = 2;
- bn::pool<Bullet, POOL_SIZE> bullet_pool;
- bn::forward_list<Bullet*, POOL_SIZE> bullet_list;
- // Create bullets in `bullet_pool`, store their pointers to `bullet_list`.
- Bullet& bullet1 = bullet_pool.create(11, 11, true);
- Bullet& bullet2 = bullet_pool.create(22, 22, true);
- bullet_list.push_front(&bullet1);
- bullet_list.push_front(&bullet2);
- BN_LOG("bullet1 created on {x=", bullet1.x, ", y=", bullet1.y, "}");
- BN_LOG("bullet2 created on {x=", bullet2.x, ", y=", bullet2.y, "}");
- // Destroy the dead bullets
- bullet_list.remove_if([&bullet_pool](Bullet* bullet) -> bool {
- if (bullet->is_dead) {
- BN_LOG("Destroy bullet on {x=", bullet->x, ", y=", bullet->y, "}");
- bullet_pool.destroy(*bullet);
- return true;
- }
- return false;
- });
- // Now that the `bullet_pool` is empty, you can push 2 bullets again.
- bullet_list.push_front(&bullet_pool.create(42, 42, true));
- bullet_list.push_front(&bullet_pool.create(99, 99, true));
- for (const Bullet* bullet : bullet_list)
- BN_LOG("bullet created on {x=", bullet->x, ", y=", bullet->y, "}");
- // Another way to conditionally destroy.
- for (auto before_iter = bullet_list.before_begin(),
- iter = bullet_list.begin();
- iter != bullet_list.end();) {
- Bullet* bullet = *iter;
- if (bullet->is_dead) {
- BN_LOG("Destroy bullet on {x=", bullet->x, ", y=", bullet->y, "}");
- bullet_pool.destroy(*bullet);
- iter = bullet_list.erase_after(before_iter);
- } else {
- before_iter = iter;
- ++iter;
- }
- }
- BN_LOG("bullet_list.size(): ", bullet_list.size());
- BN_LOG("bullet_pool.size(): ", bullet_pool.size());
- while (true)
- bn::core::update();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement