summaryrefslogtreecommitdiff
path: root/libraries/pake/source/dirtiable-image.cpp
blob: 9e5c979718e4b5622da6ff762a315f419e724408 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <pake/dirtiable-image.hpp>

namespace pake {

  struct dirty_region_builder {

    std::vector<region> regions_not_on_bottom;
    std::list<region> regions_on_bottom;

    void add_row(const std::vector<region> &row) {

      std::list<region> new_regions_on_bottom;

      for (auto i = row.begin(); i < row.end(); ++i) {
        bool expanded = false;
        for (auto j = regions_on_bottom.begin(); j != regions_on_bottom.end(); ++j)
          if (i->start_x == j->start_x && i->width == j->width) {
            j->height += i->height;
            new_regions_on_bottom.push_back(*j);
            regions_on_bottom.erase(j);
            expanded = true;
            break;
          }
        if (!expanded)
          new_regions_on_bottom.push_back(*i);
      }

      for (auto i = regions_on_bottom.begin(); i != regions_on_bottom.end(); ++i)
        regions_not_on_bottom.push_back(*i);

      regions_on_bottom = std::move(new_regions_on_bottom);

    }

  };

  std::vector<region> dirtiable_image::get_dirty_regions() {

    dirty_region_builder builder;

    std::vector<region> row;

    for (int y = 0; y < dirty.height; ++y) {

      int r = 0;
      for (int x = 0; x < dirty.width; ++x)
        if (!dirty.at(x, y)) {
          if (r != x)
            row.push_back({
              .start_x = r, .start_y = y,
              .width = x - r, .height = 1
            });
          r = x + 1;
        }
      if (r != dirty.width)
        row.push_back({
          .start_x = r, .start_y = y,
          .width = dirty.width - r, .height = 1
        });

      builder.add_row(row);
      row.clear();

    }

    builder.add_row(row);
    return builder.regions_not_on_bottom;

  }

  void dirtiable_image::clear_dirty() {
    dirty.fill(false);
  }

  dirtiable_image::dirtiable_image(int width, int height)
  : image(width, height), dirty(width, height) {
    dirty.fill(false);
  }

}