-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom_pixels.c
374 lines (327 loc) · 13.5 KB
/
random_pixels.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2004 Sam Hocevar <[email protected]>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
*/
#include <stdio.h> // Standard I/O functions
#include <unistd.h> // POSIX API for getopt
#include <stdlib.h> // Standard library functions
#include "raylib.h" // Raylib graphics library
// Default configuration values
#define DEFAULT_ROWS 16
#define DEFAULT_COLS 22
#define DEFAULT_BORDER_SIZE 2
#define DEFAULT_TICK_INTERVAL 0.5f
#define DEFAULT_INITIAL_RANDOM_SQUARES 10
#define DEFAULT_MAX_RANDOM_TICK 100
#define DEFAULT_GRID_COLOR BLUE
#define DEFAULT_DEACTIVE_COLOR BLACK
#define MAX_COLOR_VALUE 255 // Maximum value for RGB color components
#define DEFAULT_WINDOW_WIDTH 1280 // 720p width
#define DEFAULT_WINDOW_HEIGHT 720 // 720p height
// Global configuration variables
int ROWS = DEFAULT_ROWS;
int COLS = DEFAULT_COLS;
int BORDER_SIZE = DEFAULT_BORDER_SIZE;
float TICK_INTERVAL = DEFAULT_TICK_INTERVAL;
int INITIAL_RANDOM_SQUARES = DEFAULT_INITIAL_RANDOM_SQUARES;
int MAX_RANDOM_TICK = DEFAULT_MAX_RANDOM_TICK;
Color GRID_COLOR = DEFAULT_GRID_COLOR;
Color DEACTIVE_COLOR = DEFAULT_DEACTIVE_COLOR;
int WINDOW_WIDTH = DEFAULT_WINDOW_WIDTH;
int WINDOW_HEIGHT = DEFAULT_WINDOW_HEIGHT;
// Function to parse command line arguments
void ParseCommandLine(int argc, char *argv[]) {
int opt;
// Loop through all command line options using getopt
// The colon after each letter indicates the option requires an argument
while ((opt = getopt(argc, argv, "hr:c:b:t:i:m:g:d:w:H:")) != -1) {
switch (opt) {
case 'h': // Help option - display usage information and exit
printf("Art Grid - Interactive Visual Art Generator\n\n");
printf("Usage: %s [options]\n\n", argv[0]);
printf("Options:\n");
printf(" -h Show this help message\n");
printf(" -w WIDTH Window width (default: %d)\n", DEFAULT_WINDOW_WIDTH);
printf(" -H HEIGHT Window height (default: %d)\n", DEFAULT_WINDOW_HEIGHT);
printf(" -r ROWS Number of rows (default: %d)\n", DEFAULT_ROWS);
printf(" -c COLS Number of columns (default: %d)\n", DEFAULT_COLS);
printf(" -b BORDER Border size in pixels (default: %d)\n", DEFAULT_BORDER_SIZE);
printf(" -t INTERVAL Tick interval in seconds (default: %.1f)\n", DEFAULT_TICK_INTERVAL);
printf(" -i SQUARES Initial active squares (default: %d)\n", DEFAULT_INITIAL_RANDOM_SQUARES);
printf(" -m TICKS Maximum random ticks (default: %d)\n", DEFAULT_MAX_RANDOM_TICK);
printf(" -g R,G,B Grid color (default: %d,%d,%d)\n",
DEFAULT_GRID_COLOR.r, DEFAULT_GRID_COLOR.g, DEFAULT_GRID_COLOR.b);
printf(" -d R,G,B Deactive color (default: %d,%d,%d)\n\n",
DEFAULT_DEACTIVE_COLOR.r, DEFAULT_DEACTIVE_COLOR.g, DEFAULT_DEACTIVE_COLOR.b);
printf("Example:\n");
printf(" %s -r 20 -c 30 -w 1280 -H 720 -b 3 -t 5.0 -i 15 -m 200 -g 255,0,0 -d 0,0,0\n", argv[0]);
exit(EXIT_SUCCESS);
// Handle row count option
case 'r':
ROWS = atoi(optarg); // Convert string argument to integer
if (ROWS <= 0) {
fprintf(stderr, "Number of rows must be positive\n");
exit(EXIT_FAILURE);
}
break;
// Handle column count option
case 'c':
COLS = atoi(optarg);
if (COLS <= 0) {
fprintf(stderr, "Number of columns must be positive\n");
exit(EXIT_FAILURE);
}
break;
// Handle border size option
case 'b':
BORDER_SIZE = atoi(optarg);
if (BORDER_SIZE < 0) {
fprintf(stderr, "Border size cannot be negative\n");
exit(EXIT_FAILURE);
}
break;
// Handle tick interval option
case 't':
TICK_INTERVAL = atof(optarg); // Convert string argument to float
if (TICK_INTERVAL <= 0.0f) {
fprintf(stderr, "Tick interval must be positive\n");
exit(EXIT_FAILURE);
}
break;
// Handle initial active squares option
case 'i':
INITIAL_RANDOM_SQUARES = atoi(optarg);
if (INITIAL_RANDOM_SQUARES < 0) {
fprintf(stderr, "Initial squares cannot be negative\n");
exit(EXIT_FAILURE);
}
break;
// Handle maximum random ticks option
case 'm':
MAX_RANDOM_TICK = atoi(optarg);
if (MAX_RANDOM_TICK <= 0) {
fprintf(stderr, "Maximum ticks must be positive\n");
exit(EXIT_FAILURE);
}
break;
// Handle grid color option
case 'g': {
int r, g, b;
// Parse RGB values from comma-separated string
if (sscanf(optarg, "%d,%d,%d", &r, &g, &b) == 3) {
// Validate color values are within range
if (r < 0 || r > MAX_COLOR_VALUE ||
g < 0 || g > MAX_COLOR_VALUE ||
b < 0 || b > MAX_COLOR_VALUE) {
fprintf(stderr, "Color values must be between 0 and %d\n", MAX_COLOR_VALUE);
exit(EXIT_FAILURE);
}
GRID_COLOR = (Color){r, g, b, 255}; // Set color with full opacity
} else {
fprintf(stderr, "Invalid color format for -g. Use R,G,B (e.g., 255,0,0)\n");
exit(EXIT_FAILURE);
}
break;
}
// Handle deactive color option
case 'd': {
int r, g, b;
// Parse RGB values from comma-separated string
if (sscanf(optarg, "%d,%d,%d", &r, &g, &b) == 3) {
// Validate color values are within range
if (r < 0 || r > MAX_COLOR_VALUE ||
g < 0 || g > MAX_COLOR_VALUE ||
b < 0 || b > MAX_COLOR_VALUE) {
fprintf(stderr, "Color values must be between 0 and %d\n", MAX_COLOR_VALUE);
exit(EXIT_FAILURE);
}
DEACTIVE_COLOR = (Color){r, g, b, 255}; // Set color with full opacity
} else {
fprintf(stderr, "Invalid color format for -d. Use R,G,B (e.g., 0,0,0)\n");
exit(EXIT_FAILURE);
}
break;
}
// Handle window width option
case 'w':
WINDOW_WIDTH = atoi(optarg);
if (WINDOW_WIDTH <= 0) {
fprintf(stderr, "Window width must be positive\n");
exit(EXIT_FAILURE);
}
break;
// Handle window height option
case 'H':
WINDOW_HEIGHT = atoi(optarg);
if (WINDOW_HEIGHT <= 0) {
fprintf(stderr, "Window height must be positive\n");
exit(EXIT_FAILURE);
}
break;
// Handle unknown options
default:
fprintf(stderr, "Usage: %s [-r rows] [-c cols] [-b border_size] "
"[-t tick_interval] [-i initial_squares] [-m max_tick] "
"[-g grid_color] [-d deactive_color]\n", argv[0]);
exit(EXIT_FAILURE);
}
}
}
// Structure representing a single grid pixel
typedef struct {
Color color; // Current color of the pixel
int tick; // Timer controlling when the pixel changes
} Pixel;
// 2D array representing the pixel grid
Pixel **grid = NULL;
// Global timer for grid updates
float timer = 0.0f;
// Function to generate a random color
Color GetRandomColor() {
return (Color){
GetRandomValue(0, MAX_COLOR_VALUE), // Random red component
GetRandomValue(0, MAX_COLOR_VALUE), // Random green component
GetRandomValue(0, MAX_COLOR_VALUE), // Random blue component
MAX_COLOR_VALUE // Fully opaque
};
}
// Initialize the grid with default values
void InitGrid() {
// Allocate memory for rows
grid = (Pixel **)malloc(ROWS * sizeof(Pixel *));
if (!grid) {
fprintf(stderr, "Memory allocation failed\n");
exit(EXIT_FAILURE);
}
// Allocate memory for columns
for (int i = 0; i < ROWS; i++) {
grid[i] = (Pixel *)malloc(COLS * sizeof(Pixel));
if (!grid[i]) {
fprintf(stderr, "Memory allocation failed\n");
exit(EXIT_FAILURE);
}
}
// Set all pixels to the deactive color
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLS; col++) {
grid[row][col].color = DEACTIVE_COLOR;
grid[row][col].tick = 0;
}
}
// Activate random initial pixels
for (int i = 0; i < INITIAL_RANDOM_SQUARES; i++) {
int row = GetRandomValue(0, ROWS - 1);
int col = GetRandomValue(0, COLS - 1);
// Ensure we select an inactive pixel
while (grid[row][col].tick > 0) {
row = GetRandomValue(0, ROWS - 1);
col = GetRandomValue(0, COLS - 1);
}
grid[row][col].color = GetRandomColor();
grid[row][col].tick = GetRandomValue(1, MAX_RANDOM_TICK);
}
}
// Calculate the dimensions of each grid cell based on window size
void CalculateCellDimensions(int *cellWidth, int *cellHeight) {
*cellWidth = WINDOW_WIDTH / COLS;
*cellHeight = WINDOW_HEIGHT / ROWS;
}
// Draw the grid of rectangles
void DrawArtRectangles() {
int cellWidth, cellHeight;
CalculateCellDimensions(&cellWidth, &cellHeight);
// Draw each cell in the grid
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLS; col++) {
// Draw the cell border
DrawRectangle(col * cellWidth, row * cellHeight,
cellWidth, cellHeight, GRID_COLOR);
// Draw the inner colored rectangle
DrawRectangle(col * cellWidth + BORDER_SIZE,
row * cellHeight + BORDER_SIZE,
cellWidth - 2*BORDER_SIZE,
cellHeight - 2*BORDER_SIZE,
grid[row][col].color);
}
}
}
// Set color of a specific grid square
void SetSquareColor(int row, int col, Color color) {
// Check bounds before setting color
if (row >= 0 && row < DEFAULT_ROWS && col >= 0 && col < DEFAULT_COLS) {
grid[row][col].color = color;
}
}
// Set tick for a specific grid square
void SetSquareTick(int row, int col, int tick) {
// Check bounds before setting tick
if (row >= 0 && row < DEFAULT_ROWS && col >= 0 && col < DEFAULT_COLS) {
grid[row][col].tick = tick;
}
}
// Update grid state
void UpdateGrid() {
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLS; col++) {
// Process active pixels
if (grid[row][col].tick > 0) {
grid[row][col].tick--;
// When tick reaches 0, return to deactivated state
if (grid[row][col].tick == 0) {
grid[row][col].color = DEACTIVE_COLOR;
// Choose a new random square to activate
int newRow, newCol;
do {
newRow = GetRandomValue(0, ROWS - 1);
newCol = GetRandomValue(0, COLS - 1);
} while (grid[newRow][newCol].tick > 0);
// Activate the new pixel
grid[newRow][newCol].color = GetRandomColor();
grid[newRow][newCol].tick = GetRandomValue(1, MAX_RANDOM_TICK);
}
}
}
}
}
// Add cleanup function
void CleanupGrid() {
if (grid) {
for (int i = 0; i < ROWS; i++) {
free(grid[i]);
}
free(grid);
}
}
int main(int argc, char *argv[]) {
ParseCommandLine(argc, argv);
// Initialize the window and grid
InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Random Pixels");
InitGrid();
SetTargetFPS(60);
// Main program loop
while (!WindowShouldClose()) {
// Update the timer
timer += GetFrameTime();
// Update the grid at the specified interval
if (timer >= TICK_INTERVAL) {
UpdateGrid();
timer = 0.0f;
}
// Begin drawing
BeginDrawing();
ClearBackground(RAYWHITE);
DrawArtRectangles();
EndDrawing();
}
// Clean up and exit
CleanupGrid();
CloseWindow();
return 0;
}