BHXSpecter

Allegro Primitives 2D Particle Stream

Jul 5th, 2013
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.31 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cstdlib>
  4. #include <ctime>
  5. #include <allegro5/allegro.h>
  6. #include <allegro5/allegro_primitives.h>
  7.  
  8. using namespace std;
  9.  
  10. struct PARTICLE{
  11.     float lifetime;
  12.     float decay;
  13.     float r, g, b;
  14.     float xpos, ypos;
  15.     float xspeed, yspeed;
  16.     bool active;
  17. };
  18.  
  19. const int MAX_PARTICLES = 1000;
  20. PARTICLE* particle = new PARTICLE[MAX_PARTICLES];
  21.  
  22. void CreateParticle(int i, ALLEGRO_DISPLAY* disp);
  23. void EvolveParticle();
  24. void DrawObjects(ALLEGRO_DISPLAY* disp);
  25.  
  26. const int SCREEN_W = 640;
  27. const int SCREEN_H = 480;
  28. const float FPS = 60.0;
  29.  
  30. int main()
  31. {
  32.     srand(time(NULL));
  33.     ALLEGRO_DISPLAY *display = NULL;
  34.     ALLEGRO_TIMER *timer = NULL;
  35.     ALLEGRO_EVENT_QUEUE *event_queue = NULL;
  36.  
  37.     bool redraw = true;
  38.     bool doexit = false;
  39.  
  40.     if(!al_init())
  41.     {
  42.         cout << "Failed to init allegro.\n";
  43.         return -1;
  44.     }
  45.  
  46.     if(!al_install_keyboard())
  47.     {
  48.         cout << "Failed to install keyboard.\n";
  49.         return -1;
  50.     }
  51.  
  52.     if(!al_init_primitives_addon())
  53.     {
  54.         cout << "Failed to init primitives.\n";
  55.         return -1;
  56.     }
  57.  
  58.     timer = al_create_timer(1.0 / FPS);
  59.     if(!timer)
  60.     {
  61.         cout << "failed to init timer.\n";
  62.         return -1;
  63.     }
  64.  
  65.     al_set_new_display_flags(ALLEGRO_FULLSCREEN_WINDOW);
  66.     display = al_create_display(SCREEN_W, SCREEN_H);
  67.     if(!display)
  68.     {
  69.         cout << "Failed to init display.\n";
  70.         return -1;
  71.     }
  72.  
  73.     event_queue = al_create_event_queue();
  74.     if(!event_queue)
  75.     {
  76.         cout << "Failed to make queue.\n";
  77.         return -1;
  78.     }
  79.  
  80.     al_register_event_source(event_queue, al_get_display_event_source(display));
  81.     al_register_event_source(event_queue, al_get_timer_event_source(timer));
  82.     al_register_event_source(event_queue, al_get_keyboard_event_source());
  83.  
  84.     al_clear_to_color(al_map_rgb(0, 0, 0));
  85.     al_flip_display();
  86.     al_start_timer(timer);
  87.  
  88.     while(!doexit)
  89.     {
  90.         ALLEGRO_EVENT ev;
  91.         al_wait_for_event(event_queue, &ev);
  92.  
  93.         if(ev.type == ALLEGRO_EVENT_TIMER)
  94.         {
  95.             // logic goes here
  96.  
  97.             redraw = true;
  98.         }
  99.         else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
  100.         {
  101.             break;
  102.         }
  103.         else if(ev.type == ALLEGRO_EVENT_KEY_DOWN)
  104.         {
  105.             switch(ev.keyboard.keycode)
  106.             {
  107.  
  108.             }
  109.         }
  110.         else if(ev.type == ALLEGRO_EVENT_KEY_UP)
  111.         {
  112.             switch(ev.keyboard.keycode)
  113.             {
  114.                 case ALLEGRO_KEY_ESCAPE:
  115.                     doexit = true;
  116.                     break;
  117.             }
  118.         }
  119.  
  120.         if(redraw && al_is_event_queue_empty(event_queue))
  121.         {
  122.             // draw code goes here
  123.             redraw = false;
  124.             al_clear_to_color(al_map_rgb(0, 0, 0));
  125.             DrawObjects(display);
  126.             al_flip_display();
  127.  
  128.         }
  129.     }
  130.     delete [] particle;
  131.     return 0;
  132. }
  133.  
  134.  
  135. void CreateParticle(int i, ALLEGRO_DISPLAY* disp)
  136. {
  137.     particle[i].lifetime = (float)(rand() % (500000) / 500000.0);
  138.     particle[i].decay = 0.0001; // 0.001 originally
  139.     particle[i].r = 0;
  140.     particle[i].g = 191;
  141.     particle[i].b = 255;
  142.     particle[i].xpos = al_get_display_width(disp) / 2;
  143.     particle[i].ypos = al_get_display_height(disp) - 20;
  144.     particle[i].xspeed = 0.0005 - (float)((rand() % (100))/100000.0);
  145.     particle[i].yspeed = 0.01 - (float)((rand() % (100))/100000.0);
  146.     particle[i].active = true;
  147. }
  148.  
  149. void EvolveParticle()
  150. {
  151.     for(int i = 0; i <= MAX_PARTICLES; i++)
  152.     {
  153.         particle[i].lifetime -= particle[i].decay;
  154.         particle[i].xpos += particle[i].xspeed;
  155.         particle[i].ypos += particle[i].yspeed;
  156.         particle[i].yspeed -= 0.00007;
  157.     }
  158. }
  159.  
  160. void DrawObjects(ALLEGRO_DISPLAY* disp)
  161. {
  162.     for(int i = 0; i <= MAX_PARTICLES; i++)
  163.     {
  164.         if(particle[i].ypos < 0.0)
  165.             particle[i].lifetime = 0.0;
  166.         if((particle[i].active == true) && (particle[i].lifetime > 0.0))
  167.         {
  168.             // draw particles
  169.             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));
  170.         }
  171.         else
  172.         {
  173.            CreateParticle(i, disp);
  174.         }
  175.     }
  176.     EvolveParticle();
  177. }
Add Comment
Please, Sign In to add comment