forked from jakogut/tinyflock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
231 lines (179 loc) · 6.44 KB
/
main.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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <SDL/SDL.h>
#include <SDL/SDL_thread.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include "flock.h"
#include "boid.h"
#include "render.h"
#include "configuration.h"
// Returns true if key pressed, false otherwise
int key_pressed(SDL_Event* event, int key)
{
return (event->type == SDL_KEYDOWN && event->key.keysym.sym == SDLK_ESCAPE);
}
// Handle input and events. Return 0 to quit the program, or 1 to keep running.
inline int handle_events(SDL_Event* event, vector* cursor_pos, int* cursor_interaction)
{
while(SDL_PollEvent(event))
{
int x, y;
switch(event->type)
{
case SDL_KEYDOWN:
if(key_pressed(event, SDLK_ESCAPE)) return 0;
break;
case SDL_MOUSEBUTTONDOWN:
if(event->button.button == 1) *cursor_interaction = 1;
else if(event->button.button == 3) *cursor_interaction = 2;
break;
case SDL_MOUSEBUTTONUP:
*cursor_interaction = 0;
break;
case SDL_MOUSEMOTION:
SDL_GetMouseState(&x, &y);
cursor_pos->x = x, cursor_pos->y = y;
break;
case SDL_QUIT:
return 0;
break;
}
}
return 1;
}
void init_gl(int width, int height)
{
glViewport(0, 0, width, height);
glClearColor(255.0f, 255.0f, 255.0f, 0.0f);
glClearDepth(1.0);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glEnable(GL_LINE_SMOOTH);
glLineWidth(0.2f);
glShadeModel(GL_SMOOTH);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0f, width, height, 0.0f, -1.0f, 1.0f);
glMatrixMode(GL_MODELVIEW);
}
int print_help()
{
printf("\n(C) 2011 by Joseph A. Kogut (joseph.kogut@gmail.com)\n"
"This software is distributed under the MIT license,\n"
"with no warranty, express or implied. Run this software\n"
"at your own risk.\n\n"
"-h | --help\t\tPrint this help message.\n\n"
"Video configuration\n"
"------------------------------------------------------------\n"
"--height [number]\n"
"\tSpecify screen height in pixels.\n\n"
"--width [number]\n"
"\tSpecify screen width in pixels.\n\n"
"--depth [number]\n"
"\tSpecify screen depth in bits.\n\n"
"--fps [number]\n"
"\tLimit the framerate to the number specified\n\n"
"--draw-anchor\n"
"\tDisplay a visual anchor to prevent motion sickness\n\n"
"Input Configuration\n"
"------------------------------------------------------------\n"
"-ir | --influence-radius [pixels]\n\tSpecify the maximum distance from the cursor that"
"\n\twill influence boids.\n\n"
"Flock configuration\n"
"------------------------------------------------------------\n"
"-fc | --flock-count\n\tSpecify the number of boids to create.\n\n"
"-fs | --flock-separation\n\tSpecify a minimum distance to keep from neighbors.\n\n"
"-fv | --flock-velocity\n\tSpecify a maximum velocity a boid can travel.\n\n"
"-fn | --flock-neighborhood\n\tSpecify the size of the neighborhood a boid can see.\n\n"
"Misc.\n"
"------------------------------------------------------------\n"
"-t | --num-threads\n\tSpecify the number of worker threads used to\n"
"\tcalculate boid movement.\n"
);
return 0;
}
int main(int argc, char** argv)
{
// Create a configuration object, and set the values to the defaults
configuration config;
config.num_threads = NUM_THREADS;
config.video.screen_width = SCREEN_WIDTH;
config.video.screen_height = SCREEN_HEIGHT;
config.video.screen_depth = SCREEN_DEPTH;
config.video.frames_per_second = FPS;
config.input.influence_radius = INFLUENCE_RADIUS;
config.flock.size = NUM_BOIDS;
config.flock.max_velocity = MAX_BOID_VELOCITY;
config.flock.min_separation = MIN_BOID_SEPARATION;
config.flock.max_steering_force = MAX_BOID_STEERING_FORCE;
config.flock.neighborhood_radius = NEIGHBORHOOD_RADIUS;
// Parse arguments
int i;
for(i = 1; i < argc; i++)
{
if(strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0)
return print_help();
else if(strcmp(argv[i], "--width") == 0)
config.video.screen_width = atoi(argv[++i]);
else if(strcmp(argv[i], "--height") == 0)
config.video.screen_height = atoi(argv[++i]);
else if(strcmp(argv[i], "--depth") == 0)
config.video.screen_depth = atoi(argv[++i]);
else if(strcmp(argv[i], "--fps") == 0)
config.video.frames_per_second = atoi(argv[++i]);
else if(strcmp(argv[i], "-ir") == 0 || strcmp(argv[i], "--influence-radius") == 0)
config.input.influence_radius = atoi(argv[++i]);
else if(strcmp(argv[i], "-fc") == 0 || strcmp(argv[i], "--flock-count") == 0)
config.flock.size = atoi(argv[++i]);
else if(strcmp(argv[i], "-fs") == 0 || strcmp(argv[i], "--flock-separation") == 0)
config.flock.min_separation = atoi(argv[++i]);
else if(strcmp(argv[i], "-fv") == 0 || strcmp(argv[i], "--flock-velocity") == 0)
config.flock.max_velocity = atoi(argv[++i]);
else if(strcmp(argv[i], "-fn") == 0 || strcmp(argv[i], "--flock-neighborhood") == 0)
config.flock.neighborhood_radius = atoi(argv[++i]);
else if(strcmp(argv[i], "-t") == 0 || strcmp(argv[i], "--num-threads") == 0)
config.num_threads = atoi(argv[++i]);
}
srand(time(NULL));
// Init SDL and create our screen
if(SDL_Init(SDL_INIT_VIDEO) < 0) printf("Unable to initialize SDL. %s\n", SDL_GetError());
SDL_Event event;
int flags = SDL_OPENGL;
// Attempt to set the video mode
SDL_Surface* screen = SDL_SetVideoMode(config.video.screen_width, config.video.screen_height,
config.video.screen_depth, flags);
if(!screen) printf("Unable to set video mode.\n");
// Set the window caption
SDL_WM_SetCaption("tinyflock", NULL);
// Create our flock
boid* flock = create_flock(&config);
init_gl(config.video.screen_width, config.video.screen_height);
vector cursor_pos;
vector_init_scalar(&cursor_pos, 0);
int cursor_interaction = 0;
int run = 1;
int update_count = 0;
int frame_count = 0;
flock_update_args update_args = {&run, flock, &config, &cursor_pos, &cursor_interaction, &update_count };
SDL_Thread* update = SDL_CreateThread(flock_update_thread, (void*)&update_args);
status_args stat_args = {&run, &frame_count, &update_count};
SDL_Thread* status = SDL_CreateThread(status_thread, (void*)&stat_args);
// If the frame limit is not greater than 0, don't delay between frames at all.
float delay = (1000 / config.video.frames_per_second);
while(run)
{
run = handle_events(&event, &cursor_pos, &cursor_interaction);
flock_render(flock, &config, screen);
SDL_Delay(delay);
frame_count++;
}
SDL_WaitThread(update, NULL);
SDL_WaitThread(status, NULL);
destroy_flock(flock);
SDL_Quit();
return 0;
}