Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void Window::draw(const TermBuffer& term_buffer) {
- auto& damage_buffer = term_buffer.get_damage_buffer();
- if (damage_buffer.empty()) {
- SDL_Delay(16);
- return;
- }
- for (auto i : damage_buffer) {
- std::cout << i << " ";
- }
- std::cout << std::endl;
- auto font_size = get_font_size();
- cursor_pos_.x = 10;
- cursor_pos_.y = get_font_size().second / 2;
- if (SDL_SetRenderTarget(renderer_, backbuffer_) < 0) {
- throw std::runtime_error("Hah");
- }
- decltype(auto) buffer = term_buffer.get_buffer();
- auto render_limit = get_window_size().second / font_size.second; // How many lines in the viewport
- for (auto i = scroll_offset_; i < (scroll_offset_ + render_limit > buffer.size() ? buffer.size() : scroll_offset_ + render_limit); ++i) {
- if (!damage_buffer.contains(i)) {
- cursor_pos_.x = 10;
- cursor_pos_.y += font_size.second;
- continue;
- }
- SDL_Rect row_dim{0, cursor_pos_.y, width_, font_size.second};
- SDL_SetRenderDrawColor(renderer_, 0, 0, 0, 255);
- SDL_RenderFillRect(renderer_, &row_dim); // Clearing damaged row
- for (auto cell : buffer[i]) {
- if (cell.codepoint == 0) cell.codepoint = ' ';
- auto* atlas = glyph_cache_->atlas();
- SDL_Rect src = glyph_cache_->get_or_create_glyph_pos(renderer_, font_, cell.codepoint);
- SDL_Rect glyph_rect{cursor_pos_.x, cursor_pos_.y, src.w, src.h};
- SDL_SetTextureColorMod(atlas, cell.fg_color.r, cell.fg_color.g, cell.fg_color.b);
- if (cell.bg_color != SDL_Color{0, 0, 0, 255}) { // Default background
- SDL_SetRenderDrawColor(renderer_, cell.bg_color.r, cell.bg_color.g, cell.bg_color.b, cell.bg_color.a);
- SDL_Rect glyph_rect{ cursor_pos_.x, cursor_pos_.y, src.w, src.h};
- SDL_RenderFillRect(renderer_, &glyph_rect);
- SDL_SetRenderDrawColor(renderer_, 0, 0, 0, 255);
- }
- if (cell.is_underline()) {
- SDL_SetRenderDrawColor(renderer_, cell.fg_color.r, cell.fg_color.g, cell.fg_color.b, cell.fg_color.a);
- 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);
- }
- if (cell.is_bold()) {
- SDL_SetTextureColorMod(atlas, 255, 255, 255);
- }
- if (cell.is_strikethrough()) {
- SDL_SetRenderDrawColor(renderer_, 255, 255, 255, 255);
- SDL_RenderDrawLine(renderer_, cursor_pos_.x, cursor_pos_.y + src.h / 2, cursor_pos_.x + src.w, cursor_pos_.y + src.h / 2);
- }
- SDL_RenderCopy(renderer_, atlas, &src, &glyph_rect);
- cursor_pos_.x += src.w;
- }
- cursor_pos_.x = 10;
- cursor_pos_.y += font_size.second;
- }
- // auto [cursor_x, cursor_y] = term_buffer.get_cursor_pos();
- // const auto& row = buffer[cursor_y];
- // 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};
- // SDL_SetRenderDrawColor(renderer_, 255, 255, 255, 255);
- // SDL_RenderFillRect(renderer_, &cursor_rect);
- SDL_SetRenderTarget(renderer_, nullptr);
- for (auto i : damage_buffer) {
- SDL_Rect row_dim {0, static_cast<int>((i - scroll_offset_) * font_size.second), width_, font_size.second};
- SDL_SetRenderDrawColor(renderer_, 0, 0, 0, 255);
- SDL_RenderCopy(renderer_, backbuffer_, &row_dim, &row_dim);
- }
- SDL_RenderPresent(renderer_);
- should_render_ = false;
- damage_buffer.clear();
- }
Advertisement
Add Comment
Please, Sign In to add comment