#include <stdio.h>
#include <allegro5/allegro.h>
#include <allegro5/allegro_ttf.h>
#include <allegro5/allegro_primitives.h>
#include <allegro5/allegro_native_dialog.h>

#define NUM_BLITS 100000
#define RADIUS 30.0

ALLEGRO_FONT *myfont;

char font_string[] = "DejaVuSans.ttf";

char msg[512];

bool key_array[ALLEGRO_KEY_MAX];

int main(void)
{
	ALLEGRO_DISPLAY *display;
	ALLEGRO_EVENT_QUEUE *queue;
	ALLEGRO_TIMER *timer;
	ALLEGRO_COLOR white;
	ALLEGRO_COLOR black;
	ALLEGRO_COLOR gray;

	ALLEGRO_EVENT event;
	int display_width = 640;
	int display_height = 480;

	int frames = 0;
	int redraw = 0;
	int paused = 1;
	double x_pos = 100.0;
	double y_pos = 100.0;
	double x_speed = 1.0;
	double y_speed = 1.0;

	double start;
	
	if (!al_init())
	{
		fprintf(stderr,"Could not init Allegro.\n");
		return 1;
	}

	al_install_keyboard();
	al_install_mouse();
	al_init_font_addon();
	al_init_ttf_addon();

	al_set_new_display_option(ALLEGRO_VSYNC, 2, ALLEGRO_SUGGEST);
	display = al_create_display(display_width, display_height);
	if (!display)
	{
		fprintf(stderr,"Could not create display.\n");
		return 1;
	}

	white = al_map_rgba(255,255,255,255);
	black = al_map_rgba(0,0,0,255);
	gray = al_map_rgba(50,50,50,128);

	myfont = al_load_font(font_string,-28,0);
	if(!myfont)
	{
		fprintf(stderr,"Couldn't load font\n");
		return 1;
	}

	timer = al_create_timer(1.0 / 60.0);
	
	al_set_window_position(display,320,240);
	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_mouse_event_source());
	al_register_event_source(queue, al_get_timer_event_source(timer));

	al_start_timer(timer);
	
	start = al_get_time();
	
	while (true)
	{
		al_wait_for_event(queue, &event);
		{
			switch (event.type)
			{
				case ALLEGRO_EVENT_DISPLAY_CLOSE:
					goto done;

				case ALLEGRO_EVENT_KEY_DOWN:
					key_array[event.keyboard.keycode] = true;
					break;

				case ALLEGRO_EVENT_KEY_UP:
					key_array[event.keyboard.keycode] = false;
					break;

				case ALLEGRO_EVENT_KEY_CHAR:
				{
					int c,k;
					c = event.keyboard.unichar;
					k = event.keyboard.keycode;

					if (k == ALLEGRO_KEY_ESCAPE)
					{
						goto done;
					}

					if(c == ' ')
						paused ^= 1;
				}

				case ALLEGRO_EVENT_TIMER:
				{
					redraw++;
					if(paused != 1)
					{
						x_pos += x_speed;
						y_pos += y_speed;
						if(x_pos < RADIUS)
						{
							x_pos = RADIUS;
							x_speed = 1.0;
						}
						else if(x_pos > (display_width - RADIUS))
						{
							x_pos = display_width - RADIUS;
							x_speed = -1.0;
						}
						if(y_pos < RADIUS)
						{
							y_pos = RADIUS;
							y_speed = 1.0;
						}
						else if(y_pos > (display_height - RADIUS))
						{
							y_pos = display_height - RADIUS;
							y_speed = -1.0;
						}
					}
				}
			}
			while( (redraw > 0) && al_is_event_queue_empty(queue))
			{
				int i;
				redraw--;

				al_clear_to_color(black);

				for(i=0;i<NUM_BLITS;i++)	//simulate slow video card
					al_draw_filled_circle(x_pos, y_pos, RADIUS, white);

				if(paused)
				{
					al_draw_filled_rectangle(0,0,display_width,display_height,gray);
					al_draw_text(myfont,white,10.0,10.0,0,"Press Space to unpause");
				}
				else
					al_draw_text(myfont,white,10.0,10.0,0,"Press Space to pause");

				al_flip_display();
				frames++;
			}
		}
	}

done:

	sprintf(msg,"%.1f FPS", frames / (al_get_time() - start));
	al_show_native_message_box(0,"FPS",msg,"",0,0);
	al_destroy_event_queue(queue);
	return 0;
}
