fissh

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

Entity.cpp (1723B)


      1 #include "Entity.h"
      2 #include "../core/Aquarium.h"
      3 
      4 void Entity::draw() const {
      5   auto &aquarium = Aquarium::getInstance();
      6 
      7   const auto &image = getImage();
      8   const auto &mask = getMask();
      9   const char default_color = getDefaultColor();
     10 
     11   const int base_x = static_cast<int>(x);
     12   const int base_y = static_cast<int>(y);
     13 
     14   for (size_t i = 0; i < image.size(); ++i) {
     15     const std::string &row = image[i];
     16     const std::string &mask_row = (i < mask.size()) ? mask[i] : "";
     17 
     18     // Build complete line at once instead of segments
     19     std::string line;
     20     std::string colors;
     21     line.reserve(row.size());
     22     colors.reserve(row.size());
     23 
     24     int start_x = base_x;
     25 
     26     for (size_t j = 0; j < row.size(); ++j) {
     27       const char ch = row[j];
     28 
     29       if (ch == '?') {
     30         // Flush current line if not empty
     31         if (!line.empty()) {
     32           aquarium.drawToFrame(base_y + static_cast<int>(i), start_x, line,
     33                                colors);
     34           start_x += static_cast<int>(line.size()) + 1; // +1 for the '?' skip
     35           line.clear();
     36           colors.clear();
     37         } else {
     38           ++start_x; // Just skip the '?' position
     39         }
     40         continue;
     41       }
     42 
     43       line.push_back(ch);
     44 
     45       char color = default_color;
     46       if (j < mask_row.size() && mask_row[j] != ' ') {
     47         color = mask_row[j];
     48       }
     49       colors.push_back(color);
     50     }
     51 
     52     // Flush remaining line
     53     if (!line.empty()) {
     54       aquarium.drawToFrame(base_y + static_cast<int>(i), start_x, line, colors);
     55     }
     56   }
     57 }
     58 
     59 bool Entity::shouldBeRemoved() const noexcept {
     60   const auto &aquarium = Aquarium::getInstance();
     61   // unsigned nonsense
     62   return x < -static_cast<int>(getWidth()) || x > aquarium.getWidth();
     63 }