Guest User

Untitled

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