Advertisement
copyrat90

Butano `bn::generic_pool` basic usage

Mar 6th, 2023 (edited)
760
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.96 KB | None | 0 0
  1. // See also https://pastebin.com/kLcr1sik
  2.  
  3. #include <bn_core.h>
  4. #include <bn_generic_pool.h>
  5.  
  6. struct Entity {
  7.     int x, y;
  8.     virtual ~Entity() = default;
  9.     Entity(int x_, int y_) : x(x_), y(y_){};
  10. };
  11.  
  12. struct CircularBullet : public Entity {
  13.     CircularBullet(int x_, int y_) : Entity(x_, y_){};
  14. };
  15. struct SquareBullet : public Entity {
  16.     SquareBullet(int x_, int y_) : Entity(x_, y_){};
  17. };
  18.  
  19. int main() {
  20.     bn::core::init();
  21.     constexpr int MaxElementSize = 64; // max size of type in bytes
  22.     constexpr int MaxSize = 4;         // max elements count
  23.  
  24.     bn::generic_pool<MaxElementSize, MaxSize> pool;
  25.  
  26.     // Store childs as parent `Entity` type
  27.     Entity* bullet1 = &pool.create<CircularBullet>(11, 11);
  28.     Entity* bullet2 = &pool.create<SquareBullet>(22, 22);
  29.     // Destory them.  don't need to know child's concrete type.
  30.     pool.destroy(*bullet1);
  31.     pool.destroy(*bullet2);
  32.  
  33.     while (true)
  34.         bn::core::update();
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement