diff options
| author | Daniel Eklöf <daniel@ekloef.se> | 2019-06-21 12:29:15 +0000 |
|---|---|---|
| committer | Daniel Eklöf <daniel@ekloef.se> | 2019-06-21 12:29:15 +0000 |
| commit | a50be28b9d2a4dd0e9b06ccb396a9a77247d60a5 (patch) | |
| tree | b5cadd9ff9077462d2910167f95f638688a1ca64 /grid.c | |
| parent | render: break up grid_render() (diff) | |
grid: implement DAMAGE_SCROLL
Diffstat (limited to 'grid.c')
| -rw-r--r-- | grid.c | 89 |
1 files changed, 79 insertions, 10 deletions
@@ -85,6 +85,72 @@ grid_damage_erase(struct grid *grid, int start, int length) tll_push_back(grid->damage, dmg); } +static void +damage_adjust_after_scroll(struct grid *grid, int top_margin, + int bottom_margin, int lines) +{ + const int adjustment = lines * grid->cols; + tll_foreach(grid->damage, it) { + if (it->item.type == DAMAGE_SCROLL) + continue; + + assert(top_margin == 0 && "must check if item is in the non-scrolling region"); + + it->item.range.start -= adjustment; + + if (it->item.range.start < 0) { + int new_length = it->item.range.length + it->item.range.start; + assert(new_length < it->item.range.length); + + if (new_length <= 0) + tll_remove(grid->damage, it); + else { + it->item.range.length = new_length; + it->item.range.start = 0; + } + } + } +} + +void +grid_damage_scroll(struct grid *grid, int top_margin, int bottom_margin, + int lines) +{ + if (tll_length(grid->damage) > 0 && + tll_front(grid->damage).type == DAMAGE_SCROLL) + { + /* Merge with existing scroll damage */ + + struct damage *dmg = &tll_front(grid->damage); + + assert(dmg->scroll.top_margin == top_margin); + assert(dmg->scroll.bottom_margin == bottom_margin); + + dmg->scroll.lines += lines; + + /* If we've scrolled away the entire screen, replace with an erase */ + if (dmg->scroll.lines >= (grid->rows - (dmg->scroll.top_margin + + dmg->scroll.bottom_margin))) { + dmg->type = DAMAGE_ERASE; + dmg->range.start = dmg->scroll.top_margin * grid->cols; + dmg->range.length = dmg->scroll.lines * grid->cols; + } + } else { + /* TODO: yield a DAMAGE_ERASE if lines > screen */ + struct damage dmg = { + .type = DAMAGE_SCROLL, + .scroll = { + .top_margin = top_margin, + .bottom_margin = bottom_margin, + .lines = lines + }, + }; + tll_push_front(grid->damage, dmg); + } + + damage_adjust_after_scroll(grid, top_margin, bottom_margin, lines); +} + void grid_erase(struct grid *grid, int start, int end) { @@ -159,24 +225,27 @@ grid_cursor_down(struct grid *grid, int count) void grid_scroll(struct grid *grid, int rows) { - if (rows >= grid->rows) { + const int top_margin = 0; + const int bottom_margin = 0; + const int grid_rows = grid->rows - top_margin - bottom_margin; + + if (rows >= grid_rows) { + assert(false && "need to take margins into account"); grid_erase(grid, 0, grid->rows * grid->cols); return; } - int first_row = rows; - int cell_start = first_row * grid->cols; - int cell_end = grid->rows * grid->cols; - int count = cell_end - cell_start; - LOG_DBG("moving %d cells from %d", count, cell_start); + int cell_dst = top_margin * grid->cols; + int cell_src = (top_margin + rows) * grid->cols; + int cell_end = (grid->rows - bottom_margin) * grid->cols; + int count = cell_end - cell_src; + LOG_DBG("moving %d cells from %d", count, cell_src); const size_t bytes = count * sizeof(grid->cells[0]); memmove( - &grid->cells[0], &grid->cells[cell_start], + &grid->cells[cell_dst], &grid->cells[cell_src], bytes); - tll_free(grid->damage); - - grid_damage_update(grid, 0, count); + grid_damage_scroll(grid, top_margin, bottom_margin, rows); grid_erase(grid, cell_end - rows * grid->cols, grid->rows * grid->cols); } |
