diff options
| author | Brianna Rainey | 2023-02-17 16:49:03 -0500 |
|---|---|---|
| committer | Brianna Rainey | 2023-02-17 16:49:03 -0500 |
| commit | e137a2fe03df8717af51bb44111744283feaa776 (patch) | |
| tree | 598c2176fae58a86e2c6ea1ff66471fb646ff3d8 | |
| parent | 20090784f816e49a23e637f4c47f193c0210beca (diff) | |
fix #75 by computing evolution during frameRender
instead of pre-computing the entire evolution during preFrameRender, it creates 60 ticks' worth of grid data at a time during the rendering process, deleting old data as it goes. This works with the current render process and should still work if the render process changes, as long as render threads don't become extremely de-synchronized (which would probably be an issue anyway)
| -rw-r--r-- | src/components/life.py | 36 |
1 files changed, 12 insertions, 24 deletions
diff --git a/src/components/life.py b/src/components/life.py index a2a3a30..e19ed36 100644 --- a/src/components/life.py +++ b/src/components/life.py | |||
| @@ -121,30 +121,8 @@ class Component(Component): | |||
| 121 | return self.drawGrid(self.startingGrid) | 121 | return self.drawGrid(self.startingGrid) |
| 122 | 122 | ||
| 123 | def preFrameRender(self, *args, **kwargs): | 123 | def preFrameRender(self, *args, **kwargs): |
| 124 | ''' | ||
| 125 | Evolution must be computed before frameRender begins | ||
| 126 | because frameRender may be called non-sequentially. | ||
| 127 | ''' | ||
| 128 | super().preFrameRender(*args, **kwargs) | 124 | super().preFrameRender(*args, **kwargs) |
| 129 | self.progressBarSetText.emit("Computing evolution...") | ||
| 130 | self.tickGrids = {0: self.startingGrid} | 125 | self.tickGrids = {0: self.startingGrid} |
| 131 | tick = 0 | ||
| 132 | for frameNo in range( | ||
| 133 | self.tickRate, self.audioArrayLen, self.sampleSize | ||
| 134 | ): | ||
| 135 | if self.parent.canceled: | ||
| 136 | break | ||
| 137 | if frameNo % self.tickRate == 0: | ||
| 138 | tick += 1 | ||
| 139 | self.tickGrids[tick] = self.gridForTick(tick) | ||
| 140 | |||
| 141 | # update progress bar | ||
| 142 | progress = int(100*(frameNo/self.audioArrayLen)) | ||
| 143 | if progress >= 100: | ||
| 144 | progress = 100 | ||
| 145 | pStr = "Computing evolution: "+str(progress)+'%' | ||
| 146 | self.progressBarSetText.emit(pStr) | ||
| 147 | self.progressBarUpdate.emit(int(progress)) | ||
| 148 | 126 | ||
| 149 | def properties(self): | 127 | def properties(self): |
| 150 | if self.customImg and ( | 128 | if self.customImg and ( |
| @@ -158,7 +136,15 @@ class Component(Component): | |||
| 158 | 136 | ||
| 159 | def frameRender(self, frameNo): | 137 | def frameRender(self, frameNo): |
| 160 | tick = math.floor(frameNo / self.tickRate) | 138 | tick = math.floor(frameNo / self.tickRate) |
| 139 | |||
| 140 | # Compute grid evolution on this frame if it hasn't been computed yet | ||
| 141 | if tick not in self.tickGrids: | ||
| 142 | self.tickGrids[tick] = self.gridForTick(tick) | ||
| 161 | grid = self.tickGrids[tick] | 143 | grid = self.tickGrids[tick] |
| 144 | |||
| 145 | # Delete old evolution data which we shouldn't need anymore | ||
| 146 | if tick - 60 in self.tickGrids: | ||
| 147 | del self.tickGrids[tick - 60] | ||
| 162 | return self.drawGrid(grid) | 148 | return self.drawGrid(grid) |
| 163 | 149 | ||
| 164 | def drawGrid(self, grid): | 150 | def drawGrid(self, grid): |
| @@ -361,9 +347,11 @@ class Component(Component): | |||
| 361 | def gridForTick(self, tick): | 347 | def gridForTick(self, tick): |
| 362 | ''' | 348 | ''' |
| 363 | Given a tick number over 0, returns a new grid (a set of tuples). | 349 | Given a tick number over 0, returns a new grid (a set of tuples). |
| 364 | This depends on the previous tick's grid already being computed, | 350 | This must compute the previous ticks' grids if not already computed |
| 365 | so it must be called sequentially. | ||
| 366 | ''' | 351 | ''' |
| 352 | if tick - 1 not in self.tickGrids: | ||
| 353 | self.tickGrids[tick - 1] = self.gridForTick(tick - 1) | ||
| 354 | |||
| 367 | lastGrid = self.tickGrids[tick - 1] | 355 | lastGrid = self.tickGrids[tick - 1] |
| 368 | 356 | ||
| 369 | def neighbours(x, y): | 357 | def neighbours(x, y): |
