summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRosyid Haryadi <rosyid_haryadi@protonmail.com>2024-10-11 11:22:24 +0700
committerRosyid Haryadi <rosyid_haryadi@protonmail.com>2024-10-11 11:22:24 +0700
commitdf220db85af00dcb69f5958f31c4606ddcac91ce (patch)
treebe626dc470dd1153ee1af79e51517d9dcb961dc9
parentb2a911d0ca1bf5da967be4ee6921471dedcefd53 (diff)
toggle run/pause
-rw-r--r--sources/main.c80
1 files changed, 43 insertions, 37 deletions
diff --git a/sources/main.c b/sources/main.c
index a84e253..8e76b33 100644
--- a/sources/main.c
+++ b/sources/main.c
@@ -30,59 +30,65 @@ int main(void)
}
InitWindow(winSize, winSize, "Kontol");
- SetTargetFPS(1);
+ SetTargetFPS(30);
+ bool isRunning = false;
while (!WindowShouldClose())
{
BeginDrawing();
+ if (IsKeyDown(KEY_SPACE))
+ isRunning = !isRunning;
ClearBackground(BLACK);
-
for (int i=0; i<dim; i++) {
for (int j=0; j<dim; j++) {
- Color pixColor = BLACK;
+ Color pixColor = GRAY;
if (currState[i][j]) pixColor = WHITE;
DrawRectangle(i * space, j * space, pixSize, pixSize, pixColor);
- int n1 = 0;
- int n2 = 0;
- int n3 = 0;
- int n4 = 0;
- int n5 = 0;
- int n6 = 0;
- int n7 = 0;
- int n8 = 0;
- if (i > 0 && j > 0) {
- n1 = currState[i - 1][j - 1];
- n2 = currState[i - 1][j];
- n4 = currState[i][j - 1];
- }
- if (i > 0 && j < dim-1) {
- n3 = currState[i - 1][j + 1];
- }
- if (i < dim-1 && j < dim-1) {
- n5 = currState[i][j + 1];
- n7 = currState[i + 1][j];
- }
- if (i < dim-1 && j > 0) {
- n6 = currState[i + 1][j - 1];
- }
- if (i < dim-1 && j < dim-1) {
- n8 = currState[i + 1][j + 1];
- }
+ if (isRunning) {
+ int n1 = 0;
+ int n2 = 0;
+ int n3 = 0;
+ int n4 = 0;
+ int n5 = 0;
+ int n6 = 0;
+ int n7 = 0;
+ int n8 = 0;
+ if (i > 0 && j > 0) {
+ n1 = currState[i - 1][j - 1];
+ n2 = currState[i - 1][j];
+ n4 = currState[i][j - 1];
+ }
+ if (i > 0 && j < dim-1) {
+ n3 = currState[i - 1][j + 1];
+ }
+ if (i < dim-1 && j < dim-1) {
+ n5 = currState[i][j + 1];
+ n7 = currState[i + 1][j];
+ }
+ if (i < dim-1 && j > 0) {
+ n6 = currState[i + 1][j - 1];
+ }
+ if (i < dim-1 && j < dim-1) {
+ n8 = currState[i + 1][j + 1];
+ }
- int total = n1 + n2 + n3 + n4 + n5 + n6 + n7 + n8;
- if (total < 2) nextState[i][j] = 0;
- if (total > 3) nextState[i][j] = 0;
- if ((total == 2 || total == 3) && currState[i][j]) nextState[i][j] = false;
- if (total == 3 && !currState[i][j]) nextState[i][j] = true;
+ int total = n1 + n2 + n3 + n4 + n5 + n6 + n7 + n8;
+ if (total < 2) nextState[i][j] = 0;
+ if (total > 3) nextState[i][j] = 0;
+ if ((total == 2 || total == 3) && currState[i][j]) nextState[i][j] = false;
+ if (total == 3 && !currState[i][j]) nextState[i][j] = true;
+ }
}
}
- bool (*temp)[dim] = currState;
- currState = nextState;
- nextState = temp;
+ if (isRunning) {
+ bool (*temp)[dim] = currState;
+ currState = nextState;
+ nextState = temp;
+ }
EndDrawing();
}