-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain-1d.c
223 lines (191 loc) · 5.42 KB
/
main-1d.c
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include <stdio.h>
#include <SDL2/SDL.h>
#include <stdlib.h>
#define FAILED_STATUS 1
#define WINDOW_W 1300
#define WINDOW_H 800
#define CELL_W 5
#define CELL_H 5
// has to divide by two
#define CELLS_IN_ROW WINDOW_W / CELL_W
#define CELLS_IN_COLUMN WINDOW_H / CELL_H
struct Context
{
SDL_Window *window;
SDL_Renderer *window_renderer;
};
void clear_context(struct Context *context)
{
if (context->window_renderer != NULL)
{
SDL_RenderClear(context->window_renderer);
}
if (context->window != NULL)
{
SDL_DestroyWindow(context->window);
}
SDL_Quit();
}
void exit_with_failure(char *error_message, struct Context *context)
{
printf("%s\n", error_message);
printf("Last SDL error %s \n", SDL_GetError());
clear_context(context);
exit(FAILED_STATUS);
}
#define NEIGHBORS_COUNT 3
#define RULE_SIZE 8
int choosen_rule = 90;
void decode_rule(int *rule, int rule_id)
{
for (int i = 0; i < RULE_SIZE; i++)
{
rule[i] = rule_id % 2;
rule_id = rule_id >> 1;
}
}
int state_transition(int *neighbors)
{
int rule_index = 0;
int bit = 1;
for (int i = 0; i < NEIGHBORS_COUNT; i++)
{
rule_index += neighbors[i] * bit;
bit = bit << 1;
}
int rule[RULE_SIZE];
decode_rule(rule, choosen_rule);
return rule[rule_index];
}
// selecting 3 neighbors
// don't modify edges
void next_state(int *cells_current_state)
{
int new_state[CELLS_IN_ROW];
for (int i = 1; i < CELLS_IN_ROW - 1; i++)
{
int neighbors[] = {cells_current_state[i - 1], cells_current_state[i], cells_current_state[i + 1]};
new_state[i] = state_transition(neighbors);
}
// copy values
for (int i = 0; i < CELLS_IN_ROW; i++)
{
cells_current_state[i] = new_state[i];
}
}
void draw_cell(struct Context *context, int row, int column, int cell_state)
{
struct SDL_Rect rect;
rect.w = CELL_W;
rect.h = CELL_H;
rect.x = column * CELL_W;
rect.y = row * CELL_H;
if (cell_state == 1)
{
// black
if (SDL_SetRenderDrawColor(context->window_renderer, 255, 255, 255, 255) < 0)
{
exit_with_failure("ERROR: failed to StRenderDrawColor", context);
};
}
else if (cell_state == 0)
{
// white
if (SDL_SetRenderDrawColor(context->window_renderer, 0, 0, 0, 255) < 0)
{
exit_with_failure("ERROR: failed to StRenderDrawColor", context);
};
}
else
{
exit_with_failure("cell state should be 0 or 1", context);
}
if (SDL_RenderFillRect(context->window_renderer, &rect) < 0)
{
exit_with_failure("ERROR: failed to RenderFillRect", context);
};
}
void draw_cells(struct Context *context, int row, int *cells)
{
for (int i = 0; i < CELLS_IN_ROW; i++)
{
draw_cell(context, row, i, cells[i]);
}
}
int main()
{
struct Context context;
context.window = NULL;
context.window_renderer = NULL;
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
exit_with_failure("ERROR: Failed to initialize the SDL2 library: ", &context);
}
context.window = SDL_CreateWindow("Cell", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WINDOW_W, WINDOW_H, 0);
if (context.window == NULL)
{
exit_with_failure("ERROR: Failed to create window: ", &context);
}
context.window_renderer = SDL_CreateRenderer(context.window, -1, SDL_RENDERER_ACCELERATED);
if (context.window_renderer == NULL)
{
exit_with_failure("ERROR: Failed to get renderer: ", &context);
}
if (SDL_RenderClear(context.window_renderer) < 0)
{
exit_with_failure("ERROR: Failed to clear renderer: ", &context);
}
// init cells - 1D
int cells[CELLS_IN_ROW] = {0};
cells[CELLS_IN_ROW / 2] = 1;
char window_open = 1;
while (window_open)
{
SDL_Event e;
while (SDL_PollEvent(&e) > 0)
{
switch (e.type)
{
case SDL_QUIT:
window_open = 0;
break;
case SDL_KEYDOWN:
{
const Uint8 *keys = SDL_GetKeyboardState(NULL);
if (keys[SDL_SCANCODE_UP] == 1)
{
choosen_rule = (choosen_rule + 1) % 256;
for (int i = 0; i < CELLS_IN_ROW; i++)
{
cells[i] = 0;
}
cells[CELLS_IN_ROW / 2] = 1;
printf("choosen rule: %d\n", choosen_rule);
}
else if (keys[SDL_SCANCODE_DOWN] == 1)
{
choosen_rule = (choosen_rule - 1) % 256;
for (int i = 0; i < CELLS_IN_ROW; i++)
{
cells[i] = 0;
}
cells[CELLS_IN_ROW / 2] = 1;
printf("choosen rule: %d\n", choosen_rule);
}
else if (keys[SDL_SCANCODE_RIGHT] == 1)
{
SDL_RenderClear(context.window_renderer);
for (int row = 0; row < CELLS_IN_COLUMN; row++)
{
draw_cells(&context, row, cells);
next_state(cells);
}
SDL_RenderPresent(context.window_renderer);
}
break;
}
}
}
}
clear_context(&context);
}