//Simple demo for raspberry Pi Stretch slow open GL check.
//Attempts to detetct whether hardware acceleration is available,
//then runs a very simple screen display loop, attempting to reach 60FPS
//displays HW acceleration status, frame count % 256 and FPS.
//IPL 24/4/2018

#include "allegro5/allegro.h"
//#include "allegro5/allegro_image.h"
//#include "allegro5/allegro_primitives.h"
#include "allegro5/allegro_font.h"
#include "allegro5/allegro_ttf.h"

ALLEGRO_DISPLAY *display;
ALLEGRO_TIMER *timer;
ALLEGRO_FONT *font;         //debug

ALLEGRO_BITMAP *background;

int fps, fps_accum;
double fps_time;

int main (int argc, char *argv[])
{
    unsigned char i=128;
    char result = 0, hwacc = true;

    ALLEGRO_EVENT_QUEUE *queue;
    ALLEGRO_EVENT event;

    result = al_init();
    //result = al_init_image_addon();
    //result = al_init_primitives_addon();
    result = al_init_font_addon();
    result = al_init_ttf_addon();
    result = al_install_keyboard();

    al_set_standard_file_interface();
    ALLEGRO_PATH *path = al_get_standard_path(ALLEGRO_RESOURCES_PATH);
    al_change_directory(al_path_cstr(path, '/'));

    al_set_new_display_option(ALLEGRO_RENDER_METHOD , 1 , ALLEGRO_REQUIRE);         //insist on hw acceleration
    display = al_create_display(500, 300);                                          //attempt to create display
    if (display == 0)                                                               //if it fails...
    {
        hwacc = false;                                                              //remember,...
        al_set_new_display_option(ALLEGRO_RENDER_METHOD , 1 , ALLEGRO_SUGGEST);     //ask politley for hw acc instead
        display = al_create_display(500, 300);                                      //and try again
    }

    //al_set_new_bitmap_flags(ALLEGRO_VIDEO_BITMAP);

    timer = al_create_timer(1.0 / 60);
	queue = al_create_event_queue();

    al_register_event_source(queue,al_get_keyboard_event_source());
	al_register_event_source(queue, al_get_display_event_source(display));
	al_register_event_source(queue, al_get_timer_event_source(timer));

    al_start_timer(timer);

    font = al_load_font("miriam.ttf", 20, 0);

    while (1)
    {
		al_wait_for_event(queue, &event);

        if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
        {
            break;
        }
        else if (event.type == ALLEGRO_EVENT_KEY_DOWN)
        {
            if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE)           //Quit
            {
                break;
            }
        }
        else if (event.type == ALLEGRO_EVENT_TIMER)
        {
            if (al_is_event_queue_empty(queue))                         //make sure the pipeline doesn't start backing up
            {
				al_flip_display();

				double t = al_get_time();                               //calculate fps
				fps_accum++;
				if (t - fps_time >= 1)
				{
					fps = fps_accum;
					fps_accum = 0;
					fps_time = t;
				}

                al_clear_to_color(al_map_rgb(i, i, i));
                al_draw_textf(font, al_map_rgb(i-128, i-128, i-128),50, 50,  ALLEGRO_ALIGN_LEFT, hwacc?"Hardware accelerated":"NOT hardware accelerated");
                al_draw_textf(font, al_map_rgb(i-128, i-128, i-128),50, 75,  ALLEGRO_ALIGN_LEFT, "Count:%d", i);
                al_draw_textf(font, al_map_rgb(i-128, i-128, i-128),50, 100,  ALLEGRO_ALIGN_LEFT, "FPS:%d", fps);
                i++;
                i&=0xff;
            }
        }
    }
    return 0;
}



