Guest User

Untitled

a guest
May 1st, 2025
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.74 KB | None | 0 0
  1. void Window::draw(const TermBuffer& term_buffer) {
  2.     auto& damage_buffer = term_buffer.get_damage_buffer();
  3.  
  4.     if (damage_buffer.empty()) {
  5.         SDL_Delay(16);
  6.         return;
  7.     }
  8.  
  9.     for (auto i : damage_buffer) {
  10.         std::cout << i << " ";
  11.     }
  12.     std::cout << std::endl;
  13.  
  14.     auto font_size = get_font_size();
  15.  
  16.     cursor_pos_.x = 10;
  17.     cursor_pos_.y = get_font_size().second / 2;
  18.  
  19.     if (SDL_SetRenderTarget(renderer_, backbuffer_) < 0) {
  20.         throw std::runtime_error("Hah");
  21.     }
  22.  
  23.     decltype(auto) buffer = term_buffer.get_buffer();
  24.     auto render_limit = get_window_size().second / font_size.second; // How many lines in the viewport
  25.     for (auto i = scroll_offset_; i < (scroll_offset_ + render_limit > buffer.size() ? buffer.size() : scroll_offset_ + render_limit); ++i) {
  26.         if (!damage_buffer.contains(i)) {
  27.             cursor_pos_.x = 10;
  28.             cursor_pos_.y += font_size.second;
  29.             continue;
  30.         }
  31.  
  32.         SDL_Rect row_dim{0, cursor_pos_.y, width_, font_size.second};
  33.         SDL_SetRenderDrawColor(renderer_, 0, 0, 0, 255);
  34.         SDL_RenderFillRect(renderer_, &row_dim); // Clearing damaged row
  35.  
  36.         for (auto cell : buffer[i]) {
  37.             if (cell.codepoint == 0) cell.codepoint = ' ';
  38.             auto* atlas = glyph_cache_->atlas();
  39.            
  40.             SDL_Rect src = glyph_cache_->get_or_create_glyph_pos(renderer_, font_, cell.codepoint);
  41.             SDL_Rect glyph_rect{cursor_pos_.x, cursor_pos_.y, src.w, src.h};
  42.  
  43.             SDL_SetTextureColorMod(atlas, cell.fg_color.r, cell.fg_color.g, cell.fg_color.b);
  44.  
  45.             if (cell.bg_color != SDL_Color{0, 0, 0, 255}) { // Default background
  46.                 SDL_SetRenderDrawColor(renderer_, cell.bg_color.r, cell.bg_color.g, cell.bg_color.b, cell.bg_color.a);
  47.                 SDL_Rect glyph_rect{ cursor_pos_.x, cursor_pos_.y, src.w, src.h};
  48.                 SDL_RenderFillRect(renderer_, &glyph_rect);
  49.                 SDL_SetRenderDrawColor(renderer_, 0, 0, 0, 255);
  50.             }
  51.  
  52.             if (cell.is_underline()) {
  53.                 SDL_SetRenderDrawColor(renderer_, cell.fg_color.r, cell.fg_color.g, cell.fg_color.b, cell.fg_color.a);
  54.                 SDL_RenderDrawLine(renderer_, cursor_pos_.x, cursor_pos_.y + src.h - src.h / 5, cursor_pos_.x + src.w, cursor_pos_.y + src.h - src.h / 5);
  55.             }
  56.  
  57.             if (cell.is_bold()) {
  58.                 SDL_SetTextureColorMod(atlas, 255, 255, 255);
  59.             }
  60.  
  61.             if (cell.is_strikethrough()) {
  62.                 SDL_SetRenderDrawColor(renderer_, 255, 255, 255, 255);
  63.                 SDL_RenderDrawLine(renderer_, cursor_pos_.x, cursor_pos_.y + src.h / 2, cursor_pos_.x + src.w, cursor_pos_.y + src.h / 2);
  64.             }
  65.  
  66.             SDL_RenderCopy(renderer_, atlas, &src, &glyph_rect);
  67.  
  68.             cursor_pos_.x += src.w;
  69.         }
  70.         cursor_pos_.x = 10;
  71.         cursor_pos_.y += font_size.second;
  72.     }
  73.  
  74.  
  75.     // auto [cursor_x, cursor_y] = term_buffer.get_cursor_pos();
  76.    
  77.     // const auto& row = buffer[cursor_y];
  78.     // SDL_Rect cursor_rect{cursor_x * font_size.first + font_size.first, (cursor_y - (int)scroll_offset_) * font_size.second + font_size.second / 2, 10, font_size.second};
  79.     // SDL_SetRenderDrawColor(renderer_, 255, 255, 255, 255);
  80.     // SDL_RenderFillRect(renderer_, &cursor_rect);    
  81.  
  82.     SDL_SetRenderTarget(renderer_, nullptr);
  83.     for (auto i : damage_buffer) {
  84.         SDL_Rect row_dim {0, static_cast<int>((i - scroll_offset_) * font_size.second), width_, font_size.second};
  85.         SDL_SetRenderDrawColor(renderer_, 0, 0, 0, 255);
  86.         SDL_RenderCopy(renderer_, backbuffer_, &row_dim, &row_dim);
  87.     }
  88.  
  89.     SDL_RenderPresent(renderer_);
  90.     should_render_ = false;
  91.  
  92.     damage_buffer.clear();
  93. }
Advertisement
Add Comment
Please, Sign In to add comment