fissh

termios terminal aquarium. demo at ssh://fish@kloet.net
git clone git://kloet.net/fissh
Download | Log | Files | Refs

Aquarium.h (1916B)


      1 #pragma once
      2 #include "Entity.h"
      3 #include <memory>
      4 #include <vector>
      5 
      6 extern int g_maxCells;
      7 
      8 class Aquarium {
      9 private:
     10   int width;
     11   int height;
     12 
     13   struct Cell {
     14     char ch = ' ';
     15     char colorChar = 'k';
     16     bool bold = false;
     17     bool operator==(const Cell &other) const {
     18       return ch == other.ch && colorChar == other.colorChar &&
     19              bold == other.bold;
     20     }
     21     bool operator!=(const Cell &other) const { return !(*this == other); }
     22   };
     23 
     24   std::vector<std::vector<Cell>> currentFrame;
     25   std::vector<std::vector<Cell>> previousFrame;
     26   std::vector<std::unique_ptr<Entity>> entities;
     27   size_t big_entity_index = 0;
     28   void ensureBigEntityExists();
     29   bool entities_need_sorting = true;
     30   static inline const char *colorLookup[256] = {nullptr};
     31   static inline bool colorLookupInitialized = false;
     32 
     33 public:
     34   Aquarium();
     35   ~Aquarium();
     36 
     37   static Aquarium &getInstance() {
     38     static Aquarium instance;
     39     return instance;
     40   }
     41 
     42   [[nodiscard]] int getWidth() const { return width; }
     43   [[nodiscard]] int getHeight() const { return height; }
     44 
     45   void addFish();
     46   void addBubble(float x, float y);
     47   void addSeaweed();
     48   void addWaterline();
     49   void addCastle();
     50   void addShip();
     51   void addSeaMonster();
     52   void addWhale();
     53   void redraw();
     54   void initColors();
     55   void resize();
     56   void drawToFrame(int y, int x, const std::string &line,
     57                    const std::string &colorLine);
     58 
     59   // New termios-specific methods
     60   int checkInput();   // Returns character code or -1 if no input
     61   bool checkResize(); // Returns true if terminal was resized
     62 
     63 private:
     64   void clearCurrentFrame();
     65   void renderToScreen();
     66   void ensureEntitiesSorted();
     67   void getTerminalSize();
     68   static void initColorLookup();
     69 
     70   template <typename T, typename... Args> void addEntityImpl(Args &&...args) {
     71     entities.emplace_back(std::make_unique<T>(std::forward<Args>(args)...));
     72     entities_need_sorting = true;
     73   }
     74 };