summaryrefslogtreecommitdiff
path: root/sources/main.c
blob: 136feacdbf9f8ef21f1c6fe55f2dec29f8b500de (plain)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include "raylib.h"
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <stdio.h>

typedef struct {
    int width;
    int height;
    int **data;
} State;

void initializeState(State *state) {
    state->data = malloc(state->height * sizeof (int *));
    for (int i=0; i < state->height; i++) {
        int *row = malloc(state->width * sizeof (int));
        for (int j=0; j < state->width; j++) {
            row[j] = 0;
        }
        state->data[i] = row;
    }
}

void copyState(State *dest, State *src) {
    for(int i=0; i<dest->height; i++) {
        int *destRow = dest->data[i];
        int *srcRow = src->data[i];
        memcpy(destRow, srcRow, dest->width * sizeof(int));
    }
}

void freeStateData(State *state) {
    for (int i=0; i < state->height; i++) {
        free(state->data[i]);
    }
    free(state->data);
}

void printState(State *state) {
    for (int i=0; i<state->height; i++) {
        for (int j=0; j<state->width; j++) {
            printf("%d ", state->data[i][j]);
        }
        printf("\n");
    }
}

int main(void)
{
    const int pixSize = 5;
    const int padding = 2;
    const int height = 80;
    const int width = 100;
    const int space = pixSize + (2 * padding);
    const int winHeight = space * height;
    const int winWidth = space * width;

    State currState = {.height = height, .width = width};
    initializeState(&currState);
    State nextState = {.height = height, .width = width};
    initializeState(&nextState);

    srand(time(NULL));
    int prob = 30;
    for (int i=0; i<height; i++) {
        for (int j=0; j<width; j++) {
            if (rand() % 100 < prob) {
                currState.data[i][j] = true;
                nextState.data[i][j] = true;
            }
        }
    }

    InitWindow(winWidth, winHeight, "Keren");
    SetTargetFPS(10);

    bool isRunning = true;
    while (!WindowShouldClose())
    {
        BeginDrawing();

        if (IsKeyDown(KEY_A))
            isRunning = !isRunning;
        ClearBackground(BLACK);

        int nearestI = GetMouseY() / space;
        int nearestJ = GetMouseX() / space;

        if (IsMouseButtonDown(MOUSE_BUTTON_LEFT))
            currState.data[nearestI][nearestJ] = true;
        if (IsMouseButtonDown(MOUSE_BUTTON_RIGHT))
            currState.data[nearestI][nearestJ] = false;

        for (int i=0; i<height; i++) {
            for (int j=0; j<width; j++) {
                printf("drawing row %d col %d\n", i, j);
                Color pixColor = GRAY;
                if (currState.data[i][j]) pixColor = WHITE;
                DrawRectangle(j * space, i * space, pixSize, pixSize, pixColor);

                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.data[i - 1][j - 1];
                        n2 = currState.data[i - 1][j];
                        n4 = currState.data[i][j - 1];
                    }
                    if (i > 0 && j < width-1) {
                        n3 = currState.data[i - 1][j + 1];
                    }
                    if (i < height-1 && j < width-1) {
                        n5 = currState.data[i][j + 1];
                        n7 = currState.data[i + 1][j];
                    }
                    if (i < height-1 && j > 0) {
                        n6 = currState.data[i + 1][j - 1];
                    }
                    if (i < height-1 && j < width-1) {
                        n8 = currState.data[i + 1][j + 1];
                    }

                    int total = n1 + n2 + n3 + n4 + n5 + n6 + n7 + n8;
                    if (total < 2 && currState.data[i][j]) nextState.data[i][j] = false;
                    if (total > 3 && currState.data[i][j]) nextState.data[i][j] = false;
                    if ((total == 2 || total == 3) && currState.data[i][j]) nextState.data[i][j] = true;
                    if (total == 3 && !currState.data[i][j]) nextState.data[i][j] = true;
                }
            }
        }

        if (isRunning) {
            copyState(&currState, &nextState);
        }

        EndDrawing();
    }

    freeStateData(&currState);
    freeStateData(&nextState);

    CloseWindow();

    return 0;
}