Repository navigation
Expand file tree
/
Copy pathmain.c
More file actions
274 lines (246 loc) · 7.95 KB
/
Copy pathmain.c
File metadata and controls
274 lines (246 loc) · 7.95 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <SDL2/SDL.h>
#define FPS 30
#define WIDTH 1280
#define HEIGHT 720
enum
{
RANDOM_COLOR,
RANDOM_RGB,
RANDOM_PIXEL,
};
typedef struct
{
int x;
int y;
Uint32 color;
} LittleGuy;
typedef struct
{
int x;
int y;
} Vector2i;
Vector2i get_next_direction()
{
switch (rand() % 4)
{
default:
case 0:
return (Vector2i){0, -1};
case 1:
return (Vector2i){0, 1};
case 2:
return (Vector2i){1, 0};
case 3:
return (Vector2i){-1, 0};
}
}
Uint32 get_random_rgb()
{
switch (rand() % 3)
{
default:
case 0:
return 0xFF0000;
case 1:
return 0x00FF00;
case 2:
return 0x0000FF;
}
}
void print_instructions()
{
printf("[USAGE]:\n");
printf("r: clear screen\n");
printf("f: center pixels\n");
printf("p: spread pixels\n");
printf("c: change pixel color\n");
printf("d: change color mode\n");
printf("w: toggle movement\n");
printf("↑: increase spacing\n");
printf("↓: decrease spacing\n");
printf("→: increase path length\n");
printf("←: decrease path length\n");
printf("mouse wheel: change pixel size\n");
printf("increase number of guys by passing argument... `./random-walk 1000` (default 100)\n\n");
printf("Escape to quit.\n");
}
int main(const int argc, char *argv[])
{
srand(time(NULL));
int color_mode = RANDOM_COLOR;
int pixel_size = 1;
int spacing = 1;
int path_length = 1;
int number_of_guys = 100;
if (argc == 2)
{
number_of_guys = atoi(argv[1]);
if (number_of_guys <= 0)
{
number_of_guys = 1;
}
if (number_of_guys > 50000)
{
number_of_guys = 50000;
}
}
SDL_Window *window = SDL_CreateWindow("Hoppy's Pixel Simulator", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WIDTH, HEIGHT, SDL_WINDOW_SHOWN);
SDL_Surface *surface = SDL_GetWindowSurface(window);
LittleGuy *little_guys = calloc(number_of_guys, sizeof(LittleGuy));
for (int i = 0; i < number_of_guys; i++)
{
little_guys[i] = (LittleGuy){WIDTH / 2 - pixel_size / 2, HEIGHT / 2 - pixel_size / 2, SDL_MapRGB(surface->format, rand() % 256, rand() % 256, rand() % 256)};
}
const int desiredDelta = 1000 / FPS;
int app_running = 1;
int guys_moving = 1;
SDL_Event event;
print_instructions();
while (app_running)
{
const Uint32 startTick = SDL_GetTicks();
if (guys_moving)
{
SDL_Rect pixel = {WIDTH / 2 - pixel_size / 2, HEIGHT / 2 - pixel_size / 2, pixel_size, pixel_size};
for (int i = 0; i < number_of_guys; i++)
{
const Vector2i direction = get_next_direction();
const int step_size = pixel_size * spacing;
for (int j = 0; j < path_length; j++)
{
little_guys[i].x += direction.x * step_size;
little_guys[i].y += direction.y * step_size;
if (little_guys[i].x > WIDTH - pixel_size / 2)
little_guys[i].x -= step_size * path_length;
if (little_guys[i].y > HEIGHT - pixel_size / 2)
little_guys[i].y -= step_size * path_length;
if (little_guys[i].x < 0 + pixel_size / 2)
little_guys[i].x += step_size * path_length;
if (little_guys[i].y < 0 + pixel_size / 2)
little_guys[i].y += step_size * path_length;
pixel.x = little_guys[i].x;
pixel.y = little_guys[i].y;
switch (color_mode)
{
default:
case RANDOM_COLOR:
SDL_FillRect(surface, &pixel, little_guys[i].color);
break;
case RANDOM_RGB:
SDL_FillRect(surface, &pixel, get_random_rgb());
break;
case RANDOM_PIXEL:
SDL_FillRect(surface, &pixel, SDL_MapRGB(surface->format, rand() % 256, rand() % 256, rand() % 256));
break;
}
}
}
}
SDL_UpdateWindowSurface(window);
while (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
{
app_running = 0;
}
if (event.type == SDL_KEYDOWN)
{
switch (event.key.keysym.sym)
{
default:
break;
case SDLK_ESCAPE:
app_running = 0;
break;
case SDLK_c:
for (int i = 0; i < number_of_guys; i++)
{
little_guys[i].color = SDL_MapRGB(surface->format, rand() % 256, rand() % 256, rand() % 256);
}
break;
case SDLK_r:
SDL_FillRect(surface, NULL, 0);
break;
case SDLK_p:
for (int i = 0; i < number_of_guys; i++)
{
little_guys[i].x = rand() % WIDTH + 1;
little_guys[i].y = rand() % HEIGHT + 1;
}
break;
case SDLK_f:
for (int i = 0; i < number_of_guys; i++)
{
little_guys[i].x = WIDTH / 2 - pixel_size / 2;
little_guys[i].y = HEIGHT / 2 - pixel_size / 2;
}
break;
case SDLK_d:
switch (color_mode)
{
default:
case RANDOM_COLOR:
color_mode = RANDOM_RGB;
break;
case RANDOM_RGB:
color_mode = RANDOM_PIXEL;
break;
case RANDOM_PIXEL:
color_mode = RANDOM_COLOR;
break;
}
break;
case SDLK_UP:
spacing++;
break;
case SDLK_DOWN:
spacing--;
if (spacing <= 0)
{
spacing = 1;
}
break;
case SDLK_RIGHT:
path_length++;
break;
case SDLK_LEFT:
path_length--;
if (path_length <= 0)
{
path_length = 1;
}
break;
case SDLK_w:
guys_moving = !guys_moving;
break;
}
}
if (event.type == SDL_MOUSEWHEEL)
{
if (event.wheel.y == 1)
{
pixel_size++;
} else
{
pixel_size--;
}
if (pixel_size <= 0)
{
pixel_size = 1;
}
}
}
const Uint32 delta = SDL_GetTicks() - startTick;
if (delta < desiredDelta)
{
SDL_Delay(desiredDelta - delta);
}
}
free(little_guys);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}