#include <stdio.h>
#include <allegro5/allegro.h>
#include <allegro5/allegro_audio.h>
#include <allegro5/allegro_acodec.h>

const float FPS = 60;
const int SCREEN_W = 1024;
const int SCREEN_H = 768;
const int BOUNCER_SIZE = 32;


int main(int argc, char **argv) {
    ALLEGRO_DISPLAY *display = NULL;
    ALLEGRO_EVENT_QUEUE *event_queue = NULL;
    ALLEGRO_TIMER *timer = NULL;
    ALLEGRO_BITMAP *bouncer = NULL;
    float bouncer_x = SCREEN_W / 2.0 - BOUNCER_SIZE / 2.0;
    float bouncer_y = SCREEN_H / 2.0 - BOUNCER_SIZE / 2.0;
    float bouncer_dx = -4.0, bouncer_dy = 4.0;
    bool redraw = true;

    if(!al_init()) {
   	 printf("failed to initialize allegro!\n");
   	 return -1;
    }

    if (!al_install_audio()){
        fprintf(stderr, "failed to initialize audio!\n");
        return -1;
    }

    if (!al_init_acodec_addon()){
        fprintf(stderr, "failed to initialize audio codecs!\n");
        return -1;
    }

    if (!al_reserve_samples(1)){
        fprintf(stderr, "failed to reserve samples!\n");
        return -1;
    }

    al_install_audio();
    al_init_acodec_addon();

    ALLEGRO_SAMPLE *sample = al_load_sample("bounce.wav");   // sample always NULL

    al_reserve_samples(1);

    if (!sample){
        printf("Audio clip sample not loaded!\n");
        return -1;
    }

    timer = al_create_timer(1.0 / FPS);
    if(!timer) {
   	 printf("failed to create timer!\n");
   	 return -1;
    }

    al_set_new_display_flags(ALLEGRO_WINDOWED);

    display = al_create_display(1024, 768);
    if(!display) {
   	 printf("failed to create display!\n");
   	 al_destroy_timer(timer);
   	 return -1;
    }
   	 
    bouncer = al_create_bitmap(BOUNCER_SIZE, BOUNCER_SIZE);
    if(!bouncer) {
   	 printf("failed to create bouncer bitmap!\n");
   	 al_destroy_display(display);
   	 al_destroy_timer(timer);
   	 return -1;
    }

    al_set_target_bitmap(bouncer);

    al_clear_to_color(al_map_rgb(255, 0, 255));

    al_set_target_bitmap(al_get_backbuffer(display));

    event_queue = al_create_event_queue();
    if(!event_queue) {
   	 printf("failed to create event_queue!\n");
   	 al_destroy_bitmap(bouncer);
   	 al_destroy_display(display);
   	 al_destroy_timer(timer);
   	 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_clear_to_color(al_map_rgb(0,0,0));

    al_flip_display();

    al_start_timer(timer);

    while(al_get_time() < 20.0) {
   	 ALLEGRO_EVENT ev;
   	 al_wait_for_event(event_queue, &ev);

   	 if(ev.type == ALLEGRO_EVENT_TIMER) {
   		 if(bouncer_x < 0 || bouncer_x > SCREEN_W - BOUNCER_SIZE) {
   			 bouncer_dx = -bouncer_dx;
			 al_play_sample(sample, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, 0);
			//system("aplay bounce.wav");
   		 }
   		 if(bouncer_y < 0 || bouncer_y > SCREEN_H - BOUNCER_SIZE) {
   			 bouncer_dy = -bouncer_dy;
			 al_play_sample(sample, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, 0);
			//system("aplay bounce.wav");
   		 }

   		 bouncer_x += bouncer_dx;
   		 bouncer_y += bouncer_dy;

   		 redraw = true;
   	 }
   	 else {
   		 if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
   			 break;
   		 }
   	 }

   	 if(redraw && al_is_event_queue_empty(event_queue)) {
   		 redraw = false;

   		 al_clear_to_color(al_map_rgb(0,0,0));

   		 al_draw_bitmap(bouncer, bouncer_x, bouncer_y, 0);

   		 al_flip_display();
   	 }
    }

    al_destroy_bitmap(bouncer);
    al_destroy_timer(timer);
    al_destroy_display(display);
    al_destroy_sample(sample);
    al_destroy_event_queue(event_queue);

    //printf("hello world!\n");
    return 0;
}

