Optimization: grid processing array

This commit is contained in:
Dejvino
2026-03-01 12:09:07 +01:00
parent 82bab0698b
commit 1047d846f9
4 changed files with 372 additions and 292 deletions
+49 -42
View File
@@ -768,27 +768,24 @@ void drawGridCell(SDL_Renderer* renderer, int x, int y, int size, SynthEngine::G
void randomizeGrid() {
printf("Randomizing grid...\n");
Uint32 startTime = SDL_GetTicks();
SynthLockGuard<SynthMutex> lock(engine.gridMutex);
// Number of types to choose from (excluding SINK)
const int numTypes = (int)SynthEngine::GridCell::SINK;
// 1. Clear existing buffers first (resets the pool)
// engine.clearGrid(); // Avoid deadlock by clearing manually
for (int x = 0; x < SynthEngine::GRID_W; ++x) {
for (int y = 0; y < SynthEngine::GRID_H; ++y) {
SynthEngine::GridCell& c = engine.grid[x][y];
if (c.type == SynthEngine::GridCell::SINK) continue;
c.type = SynthEngine::GridCell::EMPTY;
c.param = 0.5f;
c.rotation = 0;
c.value = 0.0f;
c.phase = 0.0f;
}
}
int attempts = 0;
bool validGrid = false;
{
SynthLockGuard<SynthMutex> lock(engine.gridMutex);
// Number of types to choose from (excluding SINK)
const int numTypes = (int)SynthEngine::GridCell::SINK;
// 1. Clear existing buffers first (resets the pool)
for (int x = 0; x < SynthEngine::GRID_W; ++x) {
for (int y = 0; y < SynthEngine::GRID_H; ++y) {
SynthEngine::GridCell& c = engine.grid[x][y];
if (c.type == SynthEngine::GridCell::SINK) continue;
c.type = SynthEngine::GridCell::EMPTY;
}
}
bool visited[SynthEngine::GRID_W][SynthEngine::GRID_H];
while (!validGrid && attempts < 1000) {
@@ -944,7 +941,9 @@ void randomizeGrid() {
}
}
}
}
engine.rebuildProcessingOrder();
printf("Randomized in %d attempts (%d ms). Valid: %s\n", attempts, SDL_GetTicks() - startTime, validGrid ? "YES" : "NO");
}
@@ -1076,36 +1075,44 @@ int main(int argc, char* argv[]) {
if (e.type == SDL_MOUSEBUTTONDOWN) {
int mx = e.button.x;
int my = e.button.y;
if (mx < GRID_PANEL_WIDTH) {
int gx = mx / CELL_SIZE;
int gy = my / CELL_SIZE;
if (gx >= 0 && gx < SynthEngine::GRID_W && gy >= 0 && gy < SynthEngine::GRID_H) {
SynthLockGuard<SynthMutex> lock(engine.gridMutex);
SynthEngine::GridCell& c = engine.grid[gx][gy];
if (c.type != SynthEngine::GridCell::SINK) {
SynthEngine::GridCell::Type oldType = c.type;
SynthEngine::GridCell::Type newType = oldType;
if (e.button.button == SDL_BUTTON_LEFT) {
for (int i = 0; i < numCellTypes; ++i) {
if (cellTypes[i] == oldType) {
newType = cellTypes[(i + 1) % numCellTypes];
break;
if (mx < GRID_PANEL_WIDTH) {
int gx = mx / CELL_SIZE;
int gy = my / CELL_SIZE;
if (gx >= 0 && gx < SynthEngine::GRID_W && gy >= 0 && gy < SynthEngine::GRID_H) {
bool grid_modified = false;
{
SynthLockGuard<SynthMutex> lock(engine.gridMutex);
SynthEngine::GridCell& c = engine.grid[gx][gy];
if (c.type != SynthEngine::GridCell::SINK) {
grid_modified = true;
SynthEngine::GridCell::Type oldType = c.type;
SynthEngine::GridCell::Type newType = oldType;
if (e.button.button == SDL_BUTTON_LEFT) {
for (int i = 0; i < numCellTypes; ++i) {
if (cellTypes[i] == oldType) {
newType = cellTypes[(i + 1) % numCellTypes];
break;
}
}
} else if (e.button.button == SDL_BUTTON_RIGHT) {
c.rotation = (c.rotation + 1) % 4;
} else if (e.button.button == SDL_BUTTON_MIDDLE) {
newType = SynthEngine::GridCell::EMPTY;
c.param = 0.5f;
c.rotation = 0;
}
if (newType != oldType) {
c.type = newType;
}
}
} else if (e.button.button == SDL_BUTTON_RIGHT) {
c.rotation = (c.rotation + 1) % 4;
} else if (e.button.button == SDL_BUTTON_MIDDLE) {
newType = SynthEngine::GridCell::EMPTY;
c.param = 0.5f;
c.rotation = 0;
}
if (newType != oldType) {
c.type = newType;
if (grid_modified) {
engine.rebuildProcessingOrder();
}
}
}
} else {
// Synth Panel Click
int synthX = mx - GRID_PANEL_WIDTH;