Nicolas Winkler 5 năm trước cách đây
mục cha
commit
ff98bd8b99
2 tập tin đã thay đổi với 37 bổ sung2 xóa
  1. 35 2
      MandelWidget.cpp
  2. 2 0
      MandelWidget.h

+ 35 - 2
MandelWidget.cpp

@@ -224,10 +224,38 @@ TexGrid& MandelV::getGrid(int level)
 }
 
 
+void MandelV::garbageCollect(int level)
+{
+    for(auto& [l, grid] : levels) {
+        int dist = ::abs(l - level);
+        if (dist > 20) {
+            grid.clearCells();
+        }
+        else if (dist > 10) {
+            if (grid.countAllocatedCells() > 20)
+                grid.clearCells();
+        }
+        else if (dist > 3) {
+            if (grid.countAllocatedCells() > 80)
+                grid.clearCells();
+        }
+        else if (dist > 0) {
+            if (grid.countAllocatedCells() > 150)
+                grid.clearCells();
+        }
+        else {
+            if (grid.countAllocatedCells() > 250)
+                grid.clearCells();
+        }
+    }
+}
+
+
 void MandelV::paint(const mnd::MandelViewport& mvp)
 {
     double dpp = mvp.width / width;
     int level = getLevel(dpp);
+    garbageCollect(level);
 
     auto& grid = getGrid(level);
     double gw = getDpp(level) * chunkSize;
@@ -267,7 +295,6 @@ void MandelV::cellReady(int level, int i, int j, Bitmap<RGBColor>* bmp)
 {
     this->getGrid(level).setCell(i, j, std::make_unique<Texture>(*bmp));
     delete bmp;
-    printf("cellReady: %d --> %d, %d\n", level, i, j);
     emit redrawRequested();
 }
 
@@ -550,6 +577,7 @@ void MandelWidget::requestRecalc()
 void MandelWidget::resizeGL(int width, int height)
 {
     //glViewport(0, 0, (GLint) width, (GLint) height);
+    this->update();
 }
 
 
@@ -570,6 +598,7 @@ void MandelWidget::resizeGL(int width, int height)
 
 void MandelWidget::resizeEvent(QResizeEvent* re)
 {
+    QOpenGLWidget::resizeEvent(re);
     double aspect = double(geometry().width()) / geometry().height();
 
     //if (viewport.width > viewport.height * aspect)
@@ -577,6 +606,11 @@ void MandelWidget::resizeEvent(QResizeEvent* re)
     //else
     //    viewport.width = (viewport.height * aspect);
 
+    if (v.get() != nullptr) {
+        v->width = this->width();
+        v->height = this->height();
+    }
+    printf("resized\n");
     requestRecalc();
     //redraw();
 }
@@ -623,7 +657,6 @@ void MandelWidget::viewUpdated(Bitmap<RGBColor>* bitmap)
     if (bitmap != nullptr) {
         tex = std::make_unique<Texture>(*bitmap);
         delete bitmap;
-        printf("viewUpdated\n");
         emit repaint();
     }
 }

+ 2 - 0
MandelWidget.h

@@ -87,6 +87,7 @@ public:
     Texture* getCell(int i, int j);
     void setCell(int i, int j, std::unique_ptr<Texture> tex);
 
+    inline size_t countAllocatedCells(void) const { return cells.size(); }
     void clearCells(void);
 };
 
@@ -152,6 +153,7 @@ public:
 
     TexGrid& getGrid(int level);
 
+    void garbageCollect(int level);
     void paint(const mnd::MandelViewport& mvp);
 public slots:
     void cellReady(int level, int i, int j, Bitmap<RGBColor>* bmp);