Advertisement
Averak_212

Alternate sprite animation

Apr 23rd, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.74 KB | None | 0 0
  1. #include <allegro5/allegro.h>
  2. #include <allegro5/allegro_native_dialog.h>
  3. #include <allegro5/allegro_image.h>
  4. #include <allegro5/allegro_audio.h>
  5. #include <allegro5/allegro_acodec.h>
  6. #include <sstream>
  7. #include <vector>
  8.  
  9. #define ScreenWidth 800
  10. #define ScreenHeight 600
  11.  
  12. int main()
  13. {
  14.     const int FPS = 8;
  15.     const int interval = 4;
  16.     enum direction { S = 0, A = 4, W = 8, D = 12 };
  17.  
  18.     if(!al_init()){
  19.         al_show_native_message_box(NULL, NULL, NULL, "Could not initialize allegro!", NULL, 0);
  20.         return -1;
  21.     }
  22.  
  23.     al_set_new_display_flags(ALLEGRO_WINDOWED);
  24.     ALLEGRO_DISPLAY *display = al_create_display(ScreenWidth, ScreenHeight);
  25.     al_set_window_position(display, 200, 100);
  26.     al_set_window_title(display, "Greverus");
  27.  
  28.     if(!display){
  29.         al_show_native_message_box(display, "Simple Title", "Display Settings", "Display window was not created successfully!", NULL, ALLEGRO_MESSAGEBOX_QUESTION);
  30.         return -1;
  31.     }
  32.  
  33.     bool done = false, draw = true, active = false;
  34.     float x = 10, y = 10, moveSpeed = 10;
  35.     int dir = S, prevDir, index = 0;
  36.  
  37.     int imageNumber = -1;
  38.     bool startAnimation = false;
  39.  
  40.     al_install_keyboard();
  41.     al_install_mouse();
  42.     al_init_image_addon();
  43.  
  44.     ALLEGRO_BITMAP *person = al_load_bitmap("Data/textures/human/Ganondorf/ganondorf_sprite_att.png");
  45.  
  46.     std::vector<int> source;
  47.     std::vector<int> width;
  48.  
  49.     ALLEGRO_COLOR pixel, lastPixel, color;
  50.  
  51.     source.push_back(0);
  52.     width.push_back(0);
  53.  
  54.     for(int i = 0; i < al_get_bitmap_width(person); i++){
  55.         color = al_map_rgba(255, 7, 49, 255);
  56.         pixel = al_get_pixel(person, i, 0);
  57.  
  58.         if(memcmp(&pixel, &lastPixel, sizeof(ALLEGRO_COLOR))){
  59.             if(!memcmp(&pixel, &color, sizeof(ALLEGRO_COLOR))){
  60.                 source.push_back(i);
  61.                 if(source.size() == 2)
  62.                     width.push_back(i);
  63.                 else
  64.                     width.push_back(i - width[width.size() - 1]);
  65.             }
  66.         }else if(i == al_get_bitmap_width(person) - 1){
  67.             width.push_back(i - width[width.size() - 1]);
  68.         }
  69.  
  70.         lastPixel = pixel;
  71.     }
  72.  
  73.     al_convert_mask_to_alpha(person, al_map_rgba(255, 7, 49, 255));
  74.  
  75.     ALLEGRO_KEYBOARD_STATE keyState;
  76.  
  77.     ALLEGRO_TIMER *timer = al_create_timer(1.0 / FPS);
  78.     ALLEGRO_EVENT_QUEUE *event_queue = al_create_event_queue();
  79.     al_register_event_source(event_queue, al_get_keyboard_event_source());
  80.     al_register_event_source(event_queue, al_get_timer_event_source(timer));
  81.     al_register_event_source(event_queue, al_get_display_event_source(display));
  82.     al_register_event_source(event_queue, al_get_mouse_event_source());
  83.  
  84.     al_start_timer(timer);
  85.  
  86.     while(!done){
  87.         ALLEGRO_EVENT events;
  88.         al_wait_for_event(event_queue, &events);
  89.         al_get_keyboard_state(&keyState);
  90.  
  91.         if(events.type == ALLEGRO_EVENT_KEY_UP){
  92.             switch(events.keyboard.keycode)
  93.             {
  94.             case ALLEGRO_KEY_ESCAPE:
  95.                 done = true;
  96.             }
  97.         }else if(events.type == ALLEGRO_EVENT_DISPLAY_CLOSE){
  98.             done = true;
  99.         }else if(events.type == ALLEGRO_EVENT_TIMER){
  100.             active = true;
  101.             prevDir = dir;
  102.             if(al_key_down(&keyState, ALLEGRO_KEY_S)){
  103.                 y += moveSpeed;
  104.                 dir = S;
  105.             }else if(al_key_down(&keyState, ALLEGRO_KEY_W)){
  106.                 y -= moveSpeed;
  107.                 dir = W;
  108.             }else if(al_key_down(&keyState, ALLEGRO_KEY_D)){
  109.                 x += moveSpeed;
  110.                 dir = D;
  111.             }else if(al_key_down(&keyState, ALLEGRO_KEY_A)){
  112.                 x -= moveSpeed;
  113.                 dir = A;
  114.             }else if(al_key_down(&keyState, ALLEGRO_KEY_SPACE) && !startAnimation){
  115.                 startAnimation = true;
  116.             }else{
  117.                 active = false;
  118.             }
  119.  
  120.             if(startAnimation){
  121.                 imageNumber++;
  122.                 if(imageNumber >= source.size()){
  123.                     imageNumber = -1;
  124.                     startAnimation = false;
  125.                 }
  126.             }
  127.             draw = true;
  128.         }
  129.  
  130.         if(draw){
  131.             if(!startAnimation){
  132.                 al_draw_bitmap(person, x, y, 0);
  133.             }else{
  134.                 al_draw_bitmap_region(person, source[imageNumber], 0, width[imageNumber], al_get_bitmap_height(person), x, y, 0);
  135.             }
  136.             al_flip_display();
  137.             al_clear_to_color(al_map_rgb(0, 0, 0));
  138.         }
  139.     }
  140.     al_destroy_display(display);
  141.     al_destroy_timer(timer);
  142.     //for(int i = 0; i < 16; i++){
  143.     //    al_destroy_bitmap(playerWalk[i]);
  144.     //}
  145.     al_destroy_event_queue(event_queue);
  146.     return 0;
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement