#include <stdio.h>
#include <math.h>
#include <allegro5/allegro.h>
#include <allegro5/allegro_image.h>
#include <allegro5/allegro_native_dialog.h>

#define SCRW 640
#define SCRH 480

#define ROTATIONSPEED 0.01
#define MOVESPEED 1.0

bool key_array[ALLEGRO_KEY_MAX];
char buff[512];

int main(int argc, char **argv)
{
	ALLEGRO_DISPLAY *display;
	ALLEGRO_EVENT_QUEUE *queue;
	ALLEGRO_TIMER *timer;
	ALLEGRO_EVENT event;
	ALLEGRO_BITMAP *img;
	ALLEGRO_PATH *path;
	int display_width = SCRW;
	int display_height = SCRH;
	int redraw = 0;
	int frames = 0;
	int bmp_halfwidth;
	int bmp_halfheight;
	double start;
	double ship_x = SCRW/2;
	double ship_x_speed = 0.0;
	double ship_y = SCRH/2;
	double ship_y_speed = 0.0;
	double ship_angle = ALLEGRO_PI;


	if (!al_init())
	{
		al_show_native_message_box(0,"Error","Could not init Allegro.","",NULL,0);
		return 1;
	}

	path = al_get_standard_path(ALLEGRO_RESOURCES_PATH);
	al_change_directory(al_path_cstr(path,ALLEGRO_NATIVE_PATH_SEP));

	al_install_keyboard();
	al_init_image_addon();

	display_width = SCRW;
	display_height = SCRH;
	display = al_create_display(display_width, display_height);
	if (!display)
	{
		al_show_native_message_box(0,"Error","Unable to create display","",NULL,0);
		return 1;
	}

	img = al_load_bitmap("statek0.tga");
	if(!img)
	{
		al_show_native_message_box(0,"Error","Unable to load bitmap","",NULL,0);
		return 1;
	}

	bmp_halfwidth = al_get_bitmap_width(img) / 2;
	bmp_halfheight = al_get_bitmap_height(img) / 2;

	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_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_CHAR:
				{
					int c= event.keyboard.keycode;

					if (c == ALLEGRO_KEY_ESCAPE)
					{
						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_TIMER:
				{
					redraw++;
				}
			}

			while( (redraw > 0) && al_is_event_queue_empty(queue))
			{
				if(key_array[ALLEGRO_KEY_LEFT])
				{
					ship_angle -= ROTATIONSPEED;
					if(ship_angle < 0.0)	//keep sane values
					{
						ship_angle += ALLEGRO_PI;
					}
				}

				if(key_array[ALLEGRO_KEY_RIGHT])
				{
					ship_angle += ROTATIONSPEED;
					if(ship_angle > ALLEGRO_PI*2.0)	//keep sane values
					{
						ship_angle -= ALLEGRO_PI*2.0;
					}
				}

				if(key_array[ALLEGRO_KEY_UP])
				{
					ship_x_speed = sin(ship_angle) * -MOVESPEED;
					ship_y_speed = cos(ship_angle) * MOVESPEED;
				}
				else if(key_array[ALLEGRO_KEY_DOWN])
				{
					ship_x_speed = sin(ship_angle) * MOVESPEED;
					ship_y_speed = cos(ship_angle) * -MOVESPEED;
				}
				else
				{
					ship_x_speed = 0.0;
					ship_y_speed = 0.0;
				}

				ship_x += ship_x_speed;

				//keep in in the window
				if(ship_x < 50.0)
				{
					ship_x = 50.0;
				}
				else if(ship_x > SCRW-50.0)
				{
					ship_x = SCRW-50.0;
				}

				ship_y += ship_y_speed;

				if(ship_y < 50.0)
				{
					ship_y = 50.0;
				}
				else if(ship_y > SCRH-50.0)
				{
					ship_y = SCRH-50.0;
				}

				al_clear_to_color(al_map_rgb_f(0.5, 0.0, 0.0));	//dark red background

				al_draw_rotated_bitmap(img, bmp_halfwidth, bmp_halfheight, ship_x, ship_y, ship_angle, 0);

				al_flip_display();	//show the new scene on monitor

				frames++;
				redraw--;
			}
		}
	}

done:

	sprintf(buff,"\n%.1f FPS\n", frames / (al_get_time() - start));
	al_show_native_message_box(0,"FPS",buff,"",NULL,0);
	al_destroy_event_queue(queue);

	return 0;
}
