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
|
#include <allegro5\allegro.h>
#include <allegro5\allegro_primitives.h>
#include <allegro5\allegro_font.h>
#include <allegro5\allegro_ttf.h>
#include <iostream>
#include <math.h>
const int WIDTH = 800;
const int HEIGHT = 600;
const float GRAVITY = 2;
struct Particle
{
float x;
float x2;
float y;
float y2;
int dirX;
int dirY;
int velX;
int velY;
int lifeTime;
bool life;
};
void initParticle(Particle particle[], int numParticles);
void startParticle(Particle particle[], int spawnX, int spawnY, int numParticles);
void updateParticles(Particle particle[], int numParticles);
void drawParticles(Particle particle[],int numParticles);
void randomizeDirection(Particle particle[], int numParticles);
double angleToTarget(double x1, double y1, double x2, double y2);
int main()
{
srand(time(NULL));
bool done = false;
bool render = false;
float gameTime = 0;
int frames = 0;
int gameFPS = 0;
int spawnX = 0;
int spawnY = 0;
const int numParticles = 200;
Particle particles[numParticles];
ALLEGRO_DISPLAY *display = NULL;
ALLEGRO_EVENT_QUEUE *event_queue = NULL;
ALLEGRO_TIMER *timer;
ALLEGRO_FONT *font = NULL;
if(!al_init())
return -1;
display = al_create_display(WIDTH, HEIGHT);
if(!display)
return -1;
al_init_font_addon();
al_init_ttf_addon();
al_init_primitives_addon();
al_install_mouse();
font = al_load_font("arial.ttf", 18, 0);
event_queue = al_create_event_queue();
timer = al_create_timer(1.0/60);
initParticle(particles, numParticles);
al_register_event_source(event_queue, al_get_timer_event_source(timer));
al_register_event_source(event_queue, al_get_mouse_event_source());
al_start_timer(timer);
gameTime = al_current_time();
while(!done)
{
ALLEGRO_EVENT ev;
al_wait_for_event(event_queue, &ev);
startParticle(particles, spawnX, spawnY, numParticles);
if(ev.type == ALLEGRO_EVENT_MOUSE_AXES)
{
spawnX = ev.mouse.x;
spawnY = ev.mouse.y;
}
else if (ev.type = ALLEGRO_EVENT_TIMER)
{
render = true;
frames++;
if(al_current_time() - gameTime >= 1)
{
gameTime = al_current_time();
gameFPS = frames;
frames = 0;
}
randomizeDirection(particles, numParticles);
updateParticles(particles, numParticles);
}
if(render && al_is_event_queue_empty(event_queue))
{
render = false;
drawParticles(particles, numParticles);
al_draw_textf(font, al_map_rgb(255,0,255), 5,5,0,"FPS : %i", gameFPS);
al_flip_display();
al_clear_to_color(al_map_rgb(0,0,0));
}
}
al_destroy_font(font);
al_destroy_event_queue(event_queue);
al_destroy_display(display);
return 0;
}
void initParticle(Particle particle[], int numParticles)
{
for(int i = 0; i < numParticles; i++)
{
particle[i].lifeTime = 0;
particle[i].life = false;
particle[i].dirY = (rand() % 4) + 1;
particle[i].dirX = (rand() % 3) + 1;
}
}
void startParticle(Particle particle[], int spawnX, int spawnY, int numParticles)
{
for(int i = 0; i < numParticles; i++)
{
if(rand() % 500 == 0)
{
if(!particle[i].life)
{
particle[i].life = true;
if(rand() % 2 == 1)
particle[i].x = spawnX + (rand() % 5) + 1;
else
particle[i].x = spawnX - (rand() % 5) + 1;
if(rand() % 2 == 1)
particle[i].y = spawnY + (rand() % 5) + 1;
else
particle[i].y = spawnY - (rand() % 5) + 1;
if(rand() % 2 == 1)
particle[i].x2 =((rand() % 10) + 2) + particle[i].x;
if(rand() % 2 == 1)
particle[i].y2 = ((rand() % 10) + 2) + particle[i].y;
}
}
}
}
void updateParticles(Particle particle[], int numParticles)
{
for(int i = 0; i < numParticles; i++)
{
if(particle[i].lifeTime != 100)
{
particle[i].x += particle[i].velX;
particle[i].y += particle[i].velY;
particle[i].lifeTime++;
if(particle[i].x <= 0 || particle[i].x2 >= WIDTH)
{
particle[i].velX *= -1;
}
if(particle[i].y <= 0 || particle[i].y2 >= HEIGHT)
{
particle[i].velY *= -1;
}
} else
{
particle[i].life = false;
}
}
}
void drawParticles(Particle particle[], int numParticles)
{
for(int i = 0; i < numParticles; i++)
{
if(particle[i].life != false)
{
if(particle[i].lifeTime != 100)
{
al_draw_filled_rectangle(particle[i].x, particle[i].y, particle[i].x2, particle[i].y2,al_map_rgb(250,250,250));
}
}
}
}
void randomizeDirection(Particle particle[], int numParticles)
{
for(int i = 0; i < numParticles ; i++)
{
float dvx = 0;
float dvy = 0;
for (int j = 0; j < numParticles; j++)
{
if(i != j)
{
float angle = angleToTarget(particle[i].x, particle[i].y, particle[j].x, particle[j].y);
dvx -= (GRAVITY * cos(angle)) / 1000;
dvy -= (GRAVITY * sin(angle)) / 1000;
}
}
}
}
double angleToTarget(double x1,double y1,double x2,double y2)
{
double deltaX = (x2-x1);
double deltaY = (y2-y1);
return atan2(deltaY,deltaX);
}
|