fissh

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

Aquarium.h (1952B)


      1 #pragma once
      2 #include "../entities/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   void drawDebugInfo();
     33 
     34 public:
     35   Aquarium();
     36   ~Aquarium();
     37 
     38   static Aquarium &getInstance() {
     39     static Aquarium instance;
     40     return instance;
     41   }
     42 
     43   [[nodiscard]] int getWidth() const { return width; }
     44   [[nodiscard]] int getHeight() const { return height; }
     45 
     46   void addFish();
     47   void addBubble(float x, float y);
     48   void addSeaweed();
     49   void addWaterline();
     50   void addCastle();
     51   void addShip();
     52   void addSeaMonster();
     53   void addWhale();
     54   void redraw();
     55   void initColors();
     56   void resize();
     57   void drawToFrame(int y, int x, const std::string &line,
     58                    const std::string &colorLine);
     59 
     60   // New termios-specific methods
     61   int checkInput();   // Returns character code or -1 if no input
     62   bool checkResize(); // Returns true if terminal was resized
     63 
     64 private:
     65   void clearCurrentFrame();
     66   void renderToScreen();
     67   void ensureEntitiesSorted();
     68   void getTerminalSize();
     69   static void initColorLookup();
     70 
     71   template <typename T, typename... Args> void addEntityImpl(Args &&...args) {
     72     entities.emplace_back(std::make_unique<T>(std::forward<Args>(args)...));
     73     entities_need_sorting = true;
     74   }
     75 };