forked from jakogut/tinyflock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
148 lines (111 loc) · 3.58 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
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <SDL.h>
#include <SDL_image.h>
#include "flockconfig.h"
#include "boid.h"
float rand_range(float min, float max)
{
float range = max - min;
float num = (rand() / (float)RAND_MAX) * range;
num += min;
return num;
}
// Returns true if key pressed, false otherwise
int keyPressed(SDL_Event* event, int key)
{
return (event->type == SDL_KEYDOWN && event->key.keysym.sym == SDLK_ESCAPE);
}
// Update the positions of all the boids based on their velocity
void update(boid** flock)
{
vector* separation;
vector* alignment;
vector* cohesion;
vector* random;
int i;
for(i = 0; i < NUM_BOIDS; i++)
{
// Get our influence vectors
separation = flock_separate(flock, flock[i]);
alignment = flock_align(flock, flock[i]);
cohesion = flock_cohere(flock, flock[i]);
random = create_randomized_vector(0, 1.3);
// Add our influence vectors to the acceleration vector of the boid
vector_add(flock[i]->acceleration, separation);
vector_add(flock[i]->acceleration, alignment);
vector_add(flock[i]->acceleration, cohesion);
// Add a little random weighting to the acceleration, to make movements more organic
vector_mul(flock[i]->acceleration, random);
vector_add(flock[i]->velocity, flock[i]->acceleration);
vector_add(flock[i]->location, flock[i]->velocity);
// Reset the acceleration vectors for the flock
vector_init(flock[i]->acceleration, 0);
// Clean up our temporary influence vectors
destroy_vector(separation);
destroy_vector(alignment);
destroy_vector(cohesion);
}
// Limit boid velocity to MAX_BOID_VELOCITY as defined in config.h
flock_limit_velocity(flock, NUM_BOIDS, MAX_BOID_VELOCITY);
}
// Render the boids
void render(boid** flock, SDL_Surface* screen)
{
SDL_FillRect(screen, NULL, 0xFFFFFF);
int i;
for(i = 0; i < NUM_BOIDS; i++)
{
// If the boid goes off the screen, wrap the location around to the other side
if(flock[i]->location->x >= SCREEN_WIDTH) flock[i]->location->x = 0;
else if(flock[i]->location->x <= 0) flock[i]->location->x = SCREEN_WIDTH;
if(flock[i]->location->y >= SCREEN_HEIGHT) flock[i]->location->y = 0;
else if(flock[i]->location->y <= 0) flock[i]->location->x = SCREEN_HEIGHT;
SDL_Rect offset;
offset.x = flock[i]->location->x;
offset.y = flock[i]->location->y;
SDL_BlitSurface(flock[i]->sprite, NULL, screen, &offset);
}
SDL_Flip(screen);
}
int main(int argc, char** argv)
{
SDL_Init(SDL_INIT_VIDEO);
SDL_Event event;
SDL_Surface* screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_DEPTH, SDL_HWSURFACE | SDL_DOUBLEBUF);
// Load and format our boid image
SDL_Surface* temp = IMG_Load("boid.png");
SDL_Surface* boid_image = SDL_DisplayFormatAlpha(temp);
SDL_FreeSurface(temp);
// Create an array of boids
boid** flock = malloc(sizeof(boid*) * NUM_BOIDS);
srand(time(NULL));
int i;
for(i = 0; i < NUM_BOIDS; i++)
{
flock[i] = create_boid(boid_image, rand_range(0, SCREEN_WIDTH), rand_range(0, SCREEN_HEIGHT),
rand_range((0 - MAX_BOID_VELOCITY), MAX_BOID_VELOCITY),
rand_range((0 - MAX_BOID_VELOCITY), MAX_BOID_VELOCITY));
}
// Run while true
int run = 1;
while(run)
{
update(flock);
render(flock, screen);
// Handle keyboard input
while(SDL_PollEvent(&event))
if(event.type == SDL_QUIT || keyPressed(&event, SDLK_ESCAPE)) run = 0;
// If the framerate is not a positive number greater than zero, don't limit it
#if (FPS > 0)
SDL_Delay(1000 / FPS);
#endif
}
SDL_FreeSurface(boid_image);
for(i = 0; i < NUM_BOIDS; i++) destroy_boid(flock[i]);
SDL_Quit();
return 0;
}