Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <cstdlib>
- #include <ctime>
- #include <allegro5/allegro.h>
- #include <allegro5/allegro_primitives.h>
- using namespace std;
- struct PARTICLE{
- float lifetime;
- float decay;
- float r, g, b;
- float xpos, ypos;
- float xspeed, yspeed;
- bool active;
- };
- const int MAX_PARTICLES = 1000;
- PARTICLE* particle = new PARTICLE[MAX_PARTICLES];
- void CreateParticle(int i, ALLEGRO_DISPLAY* disp);
- void EvolveParticle();
- void DrawObjects(ALLEGRO_DISPLAY* disp);
- const int SCREEN_W = 640;
- const int SCREEN_H = 480;
- const float FPS = 60.0;
- int main()
- {
- srand(time(NULL));
- ALLEGRO_DISPLAY *display = NULL;
- ALLEGRO_TIMER *timer = NULL;
- ALLEGRO_EVENT_QUEUE *event_queue = NULL;
- bool redraw = true;
- bool doexit = false;
- if(!al_init())
- {
- cout << "Failed to init allegro.\n";
- return -1;
- }
- if(!al_install_keyboard())
- {
- cout << "Failed to install keyboard.\n";
- return -1;
- }
- if(!al_init_primitives_addon())
- {
- cout << "Failed to init primitives.\n";
- return -1;
- }
- timer = al_create_timer(1.0 / FPS);
- if(!timer)
- {
- cout << "failed to init timer.\n";
- return -1;
- }
- al_set_new_display_flags(ALLEGRO_FULLSCREEN_WINDOW);
- display = al_create_display(SCREEN_W, SCREEN_H);
- if(!display)
- {
- cout << "Failed to init display.\n";
- return -1;
- }
- event_queue = al_create_event_queue();
- if(!event_queue)
- {
- cout << "Failed to make queue.\n";
- return -1;
- }
- al_register_event_source(event_queue, al_get_display_event_source(display));
- al_register_event_source(event_queue, al_get_timer_event_source(timer));
- al_register_event_source(event_queue, al_get_keyboard_event_source());
- al_clear_to_color(al_map_rgb(0, 0, 0));
- al_flip_display();
- al_start_timer(timer);
- while(!doexit)
- {
- ALLEGRO_EVENT ev;
- al_wait_for_event(event_queue, &ev);
- if(ev.type == ALLEGRO_EVENT_TIMER)
- {
- // logic goes here
- redraw = true;
- }
- else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
- {
- break;
- }
- else if(ev.type == ALLEGRO_EVENT_KEY_DOWN)
- {
- switch(ev.keyboard.keycode)
- {
- }
- }
- else if(ev.type == ALLEGRO_EVENT_KEY_UP)
- {
- switch(ev.keyboard.keycode)
- {
- case ALLEGRO_KEY_ESCAPE:
- doexit = true;
- break;
- }
- }
- if(redraw && al_is_event_queue_empty(event_queue))
- {
- // draw code goes here
- redraw = false;
- al_clear_to_color(al_map_rgb(0, 0, 0));
- DrawObjects(display);
- al_flip_display();
- }
- }
- delete [] particle;
- return 0;
- }
- void CreateParticle(int i, ALLEGRO_DISPLAY* disp)
- {
- particle[i].lifetime = (float)(rand() % (500000) / 500000.0);
- particle[i].decay = 0.0001; // 0.001 originally
- particle[i].r = 0;
- particle[i].g = 191;
- particle[i].b = 255;
- particle[i].xpos = al_get_display_width(disp) / 2;
- particle[i].ypos = al_get_display_height(disp) - 20;
- particle[i].xspeed = 0.0005 - (float)((rand() % (100))/100000.0);
- particle[i].yspeed = 0.01 - (float)((rand() % (100))/100000.0);
- particle[i].active = true;
- }
- void EvolveParticle()
- {
- for(int i = 0; i <= MAX_PARTICLES; i++)
- {
- particle[i].lifetime -= particle[i].decay;
- particle[i].xpos += particle[i].xspeed;
- particle[i].ypos += particle[i].yspeed;
- particle[i].yspeed -= 0.00007;
- }
- }
- void DrawObjects(ALLEGRO_DISPLAY* disp)
- {
- for(int i = 0; i <= MAX_PARTICLES; i++)
- {
- if(particle[i].ypos < 0.0)
- particle[i].lifetime = 0.0;
- if((particle[i].active == true) && (particle[i].lifetime > 0.0))
- {
- // draw particles
- al_draw_pixel(particle[i].xpos + 0.02, particle[i].ypos + 0.02, al_map_rgb(particle[i].r, particle[i].g, particle[i].b));
- }
- else
- {
- CreateParticle(i, disp);
- }
- }
- EvolveParticle();
- }
Add Comment
Please, Sign In to add comment