Advertisement
Guest User

Allegro 5 Lighting Demo

a guest
Dec 3rd, 2017
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.91 KB | None | 0 0
  1.  
  2.  
  3.  
  4.  
  5. #include "allegro5/allegro.h"
  6. #include "allegro5/allegro_font.h"
  7. #include "allegro5/allegro_ttf.h"
  8. #include "allegro5/allegro_image.h"
  9. #include "allegro5/allegro_primitives.h"
  10.  
  11.  
  12.  
  13. void DrawLightB(int x , int y , int r , float fr , float fg , float fb);
  14. void DrawLightB(int x , int y , int r , float fr , float fg , float fb) {
  15.    al_draw_filled_circle(x,y,r,al_map_rgb_f(fr,fg,fb));
  16. }
  17. void DrawLightA(int x , int y , int r , float fr , float fg , float fb);
  18. void DrawLightA(int x , int y , int r , float fr , float fg , float fb) {
  19.    int rad = r;
  20.    float l = 1. / ((float) r);
  21.    while (rad > 0) {
  22.       al_draw_filled_circle(x , y , rad , al_map_rgb_f(fr*l,fg*l,fb*l));
  23.       rad -= 1;
  24.    }
  25. }
  26. void DrawLightC(int x , int y , int r , float fr , float fg , float fb);
  27. void DrawLightC(int x , int y , int r , float fr , float fg , float fb) {
  28.    int rad = r;
  29.    while (rad > 0) {
  30.       float l = (r-rad)/(float)(r*10.0);///1. / ((float) r);
  31.       al_draw_filled_circle(x , y , rad , al_map_rgb_f(fr*l,fg*l,fb*l));
  32.       rad -= 10;
  33.    }
  34. }
  35.  
  36. int main(int argc , char** argv) {
  37.  
  38.    (void)argc;
  39.    (void)argv;
  40.  
  41.  
  42.    if (!al_init()) {
  43.       return 1;
  44.    }
  45.    if (!al_init_image_addon()) {return 2;}
  46.    if (!al_init_font_addon()) {return 3;}
  47.    if (!al_init_ttf_addon()) {return 4;}
  48.    if (!al_init_primitives_addon()) {return 5;}
  49.  
  50.    al_install_keyboard();
  51.    al_install_mouse();
  52.  
  53. ///   al_set_new_display_flags(ALLEGRO_WINDOWED | ALLEGRO_OPENGL);
  54.    al_set_new_display_flags(ALLEGRO_FULLSCREEN_WINDOW | ALLEGRO_OPENGL);
  55.    ALLEGRO_DISPLAY* d = al_create_display(1920,1080);
  56.  
  57.    ALLEGRO_TIMER* t = al_create_timer(1.0/60.0);
  58.  
  59.    ALLEGRO_EVENT_QUEUE* q = al_create_event_queue();
  60.  
  61.    al_register_event_source(q , al_get_timer_event_source(t));
  62.    al_register_event_source(q , al_get_display_event_source(d));
  63.    al_register_event_source(q , al_get_keyboard_event_source());
  64.    al_register_event_source(q , al_get_mouse_event_source());
  65.  
  66.    ALLEGRO_BITMAP* lmap = al_create_bitmap(1920,1080);
  67.    ALLEGRO_BITMAP* bg = al_load_bitmap("BG.png");
  68.  
  69.    if (!bg || !lmap) {return 7;}
  70.  
  71.    int mx = 0;
  72.    int my = 0;
  73.  
  74.    
  75.    
  76.    ALLEGRO_BITMAP* lmap2 = al_create_bitmap(2048,2048);
  77.    
  78.    /// draw our light map
  79.    al_set_target_bitmap(lmap2);
  80.    al_clear_to_color(al_map_rgb(0,0,0));
  81.  
  82.    mx = 1024;
  83.    my = 1024;
  84.    
  85.    // Additive blending
  86.    al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_ONE);
  87. ///         DrawLight(0 , 0 , 200);
  88. ///         DrawLight(mx , my , 200 , 1.0 , 1.0 , 1.0);
  89.    DrawLightA(mx - 100 , my - 100 , 400 , 1.0 , 0.0 , 0.0);
  90.    DrawLightA(mx + 100 , my - 100 , 400 , 0.0 , 1.0 , 0.0);
  91.    DrawLightA(mx       , my + 73  , 400 , 0.0 , 0.0 , 1.0);
  92.  
  93.    al_start_timer(t);
  94.    
  95.    bool run = true;
  96.    bool draw = true;
  97.    while (run) {
  98.       if (draw) {
  99.          /// draw our light map
  100.          al_set_target_bitmap(lmap);
  101.          al_clear_to_color(al_map_rgb(0,0,0));
  102.  
  103.          // Additive blending
  104.          al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_ONE);
  105.          
  106.          al_draw_bitmap(lmap2 , mx - 1024 , my - 1024 , 0);
  107.          
  108. ///         DrawLight(0 , 0 , 200);
  109. ///         DrawLight(mx , my , 200 , 1.0 , 1.0 , 1.0);
  110. /**
  111.  
  112.          DrawLightA(mx - 100 , my - 100 , 400 , 1.0 , 0.0 , 0.0);
  113.          DrawLightA(mx + 100 , my - 100 , 400 , 0.0 , 1.0 , 0.0);
  114.          DrawLightA(mx       , my + 73  , 400 , 0.0 , 0.0 , 1.0);
  115.  
  116.          DrawLightB(mx - 100 , my - 100 , 300 , 1.0 , 0.0 , 0.0);
  117.          DrawLightB(mx + 100 , my - 100 , 300 , 0.0 , 1.0 , 0.0);
  118.          DrawLightB(mx       , my + 73  , 300 , 0.0 , 0.0 , 1.0);
  119.  
  120.          DrawLightC(mx - 100 , my - 100 , 400 , 1.0 , 0.0 , 0.0);
  121.          DrawLightC(mx + 100 , my - 100 , 400 , 0.0 , 1.0 , 0.0);
  122.          DrawLightC(mx       , my + 73  , 400 , 0.0 , 0.0 , 1.0);
  123. */
  124.  
  125.          al_set_target_backbuffer(d);
  126.          al_clear_to_color(al_map_rgb_f(1,1,1));
  127.  
  128.          // Default blending (premult alpha)
  129.          al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_INVERSE_ALPHA);
  130.          al_draw_bitmap(bg , (1920 - al_get_bitmap_width(bg))/2 , (1080 - al_get_bitmap_height(bg))/2 , 0);
  131.  
  132.          // Mult source & dest blending
  133.          al_set_blender(ALLEGRO_ADD, ALLEGRO_DEST_COLOR, ALLEGRO_ZERO);
  134.          al_draw_bitmap(lmap , 0 , 0 , 0);
  135.          al_flip_display();
  136.          draw = false;
  137.       }
  138.  
  139.       do {
  140.          ALLEGRO_EVENT ev;
  141.          al_wait_for_event(q , &ev);
  142.          if (ev.type == ALLEGRO_EVENT_MOUSE_AXES) {
  143.             mx = ev.mouse.x;
  144.             my = ev.mouse.y;
  145.             draw = true;
  146.          }
  147.          if (ev.type == ALLEGRO_EVENT_KEY_DOWN && ev.keyboard.keycode == ALLEGRO_KEY_ESCAPE) {
  148.             run = false;
  149.          }
  150.          if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
  151.             run = false;
  152.          }
  153.          if (ev.type == ALLEGRO_EVENT_TIMER) {
  154.             draw = true;
  155.          }
  156.       } while (!al_is_event_queue_empty(q));
  157.  
  158.    }
  159.    return 0;
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement