
#include<allegro5\allegro5.h> 
#include<allegro5\allegro_native_dialog.h> 
#include<allegro5\allegro_primitives.h>
#include<allegro5\allegro_image.h>
#include<fstream>
#include<string>
#include<sstream>
#include<vector>


#define ScreenWidth 1024
#define ScreenHeight 768
#define BlockSize 40

int loadCounterX = 0, loadCounterY = 0, mapSizeX, mapSizeY;


// %%%%%  TILE MAP LOADING TXT FILES  %%%%%//
void LoadMap(const char *filename, std::vector< std::vector <int> > &map)
{
	std::fstream openfile(filename);
	if(openfile.is_open())
	{
		std::string line, value;
		int space;

		while(!openfile.eof())
		{
			std::getline(openfile, line);

			std::stringstream str(line);
			std::vector<int> tempVector;

			while(!str.eof())
			{
				std::getline(str, value, ' ');
				if(value.length() > 0)
					tempVector.push_back(atoi(value.c_str()));
			}
			map.push_back(tempVector);
		}
	}
	else
	{
	}

}

	// %%%%%  CAMERA SCROLLING  %%%%%//
void cameraUpdate(float *cameraPosition, float x, float y, int width, int height)
{
	cameraPosition[0] = -(ScreenWidth / 2) + (x + width / 2);
	cameraPosition[1] = -(ScreenWidth / 2) + (y + height / 2);

	if(cameraPosition[0] < 0)
		cameraPosition[0] = 0;
	if(cameraPosition[1] < 0)
		cameraPosition[1] = 0;

}
void DrawMap(std::vector <std::vector <int> > map);

int main()
{
	// %%%%%  IMPORTANT SETTINGS  %%%%%//
	ALLEGRO_DISPLAY *display; 
	const float FPS = 60.0;
	const float frameFPS = 10.0;
	enum Direction { DOWN, LEFT, RIGHT, UP };




	// %%%%%  ERROR CODES, ALLEGRO AND DISPLAY  %%%%%//
	if(!al_init())
		al_show_native_message_box(NULL, "Error", NULL, "Could not Initialize Allegro", NULL, NULL); 

	display = al_create_display(ScreenWidth, ScreenHeight);

	if(!display)
		al_show_native_message_box(NULL, "Error", NULL, "Could not create Allegro Display", NULL, NULL);


	// %%%%%  WINDOW SETTINGS  %%%%%//
	
	al_set_new_display_flags(ALLEGRO_WINDOWED);
	al_set_window_position(display, 200, 200);
	al_set_window_title(display, "PRE-alpha Test");
	
	ALLEGRO_COLOR playerColor = al_map_rgb(255, 0, 255);


	// %%%%%  Movement SPEED  %%%%%//
	bool done = false, draw = true, active = false;
	float x = 512, y = 384, moveSpeed = 3;
	int dir = DOWN, sourceX = 64, sourceY = 96;
	float cameraPosition[2] = { 0, 0 };
	                                      

	// %%%%%  ADDONS  %%%%%//
	al_init_primitives_addon(); 
	al_install_keyboard();
	al_init_primitives_addon();
	al_install_mouse();
	al_init_image_addon();


	

	ALLEGRO_BITMAP *player = al_load_bitmap("player.png");
	/*ALLEGRO_BITMAP *background = al_load_bitmap("backgroundtest.png");*/
	al_convert_mask_to_alpha(player, al_map_rgb(255, 0, 255));
	ALLEGRO_KEYBOARD_STATE KeyState;
	ALLEGRO_TRANSFORM camera;


	// %%%%%  EVENTS installation & TIMERS %%%%%//
	ALLEGRO_TIMER *timer = al_create_timer(1.0 / FPS);
	ALLEGRO_TIMER *frametimer = al_create_timer(1.0 / frameFPS);

	ALLEGRO_EVENT_QUEUE *event_queue = al_create_event_queue();
	al_register_event_source(event_queue, al_get_keyboard_event_source());
	al_register_event_source(event_queue, al_get_timer_event_source(timer));
	al_register_event_source(event_queue, al_get_timer_event_source(frametimer));
	al_register_event_source(event_queue, al_get_display_event_source(display));
	al_register_event_source(event_queue, al_get_mouse_event_source());

	

	

	// %%%%%  TIMER, NO installations UNDER THIS  %%%%%//
	al_start_timer(timer);
	al_start_timer(frametimer);

	// %%%%%  Beginning of Events %%%%%//


	std::vector< std::vector <int> > map;
	LoadMap("map.txt", map);
	
	while(!done)
	{
		ALLEGRO_EVENT events; 
		al_wait_for_event(event_queue, &events);
		al_get_keyboard_state(&KeyState);


	 // %%%%%  TILE MAP IN THE EVENT & TIMER CODE %%%%%//
	
			if(events.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
		{
			done = true;
		}
		else if (events.type == ALLEGRO_EVENT_TIMER)
		{
			
		}

		// %%%%%  KEYBOARD INPUT AND EVENTS AND PLAYER IMAGE AND CAMERA SCROLLING FOR CHARACTER %%%%%//
		if(events.type == ALLEGRO_EVENT_KEY_UP)
		{
			switch(events.keyboard.keycode)
			{
			case ALLEGRO_KEY_ESCAPE:
				done = true;
			}
		}
		else if(events.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
		{
			done = true;
		}
		else if (events.type = ALLEGRO_EVENT_TIMER)
		{
			if(events.timer.source == timer)
			{
			active = true;
			if(al_key_down(&KeyState, ALLEGRO_KEY_S))
			{
			 y += moveSpeed;
			 dir = DOWN;
			}
			else if(al_key_down(&KeyState, ALLEGRO_KEY_W))
			{
			 y -= moveSpeed;
			 dir = UP;
			}
			else if(al_key_down(&KeyState, ALLEGRO_KEY_D))
			{
				x += moveSpeed;
				dir = RIGHT;
			}
			else if(al_key_down(&KeyState, ALLEGRO_KEY_A))
			{
				x -= moveSpeed;
				dir = LEFT;
			}
			else
				active = false;

			cameraUpdate(cameraPosition, x, y, 64, 96);
			al_identity_transform(&camera);
			al_translate_transform(&camera, -cameraPosition[0], -cameraPosition[1]);
			al_use_transform(&camera);

			}

			else if(events.timer.source == frametimer)
			{

			if(active)
			 sourceX += al_get_bitmap_width(player) / 4;

			else
				sourceX = 64;	

			if(sourceX >= al_get_bitmap_width(player))
				sourceX = 0;

			sourceY = dir;
			}
				draw = true;
		}

		if(draw)
		{


		/*al_draw_bitmap(background, 0, 0, NULL);*/
		
		al_draw_bitmap_region(player, sourceX, dir * al_get_bitmap_height(player) / 4, 64, 96, x, y, NULL);

		DrawMap(map);
		al_flip_display();
		al_clear_to_color(al_map_rgb(0, 0, 0));


		}

				
		// %%%%%  GAME UI SHELL  %%%%%//
		/*al_draw_rectangle(1, 1, 800, 100, al_map_rgb(255, 50, 50), 9.0);*/
		
		// %%%%%  Destroy ENDING  %%%%%//
	}
	
	
	al_destroy_display(display);
	al_destroy_timer(timer);
	al_destroy_bitmap(player);
	/*al_destroy_bitmap(background);*/
	al_destroy_event_queue(event_queue);
	
	
	return 0;
}


	 //%%%%%  RITAR UPP TILES / FÄRG PÅ FYRKANTERNA  %%%%%//
void DrawMap(std::vector <std::vector <int> > map)
	{
		for(int i = 0; i < map.size(); i++)
		{
			for(int j = 0; j < map.size(); j++)
			{
				if(map[i][j] == 0)
					al_draw_filled_rectangle(j * BlockSize, i  * BlockSize,
					j * BlockSize + BlockSize, i = BlockSize + BlockSize, al_map_rgb(255, 100, 255));
				else
					al_draw_filled_rectangle(j * BlockSize, i  * BlockSize,
					j * BlockSize + BlockSize, i * BlockSize + BlockSize, al_map_rgb(0, 255, 0));
			
			}
		}
	}