

#include "Unit.h"

int initAll()
{
	allegro_init();
	install_keyboard();
	install_timer();
	install_mouse();


	// install a MIDI sound driver 
//if (install_sound(DIGI_AUTODETECT, MIDI_AUTODETECT, NULL) != 0) 
//	{
//		return 1;
//	}
	////////////////////##### i put this in for fullsreen F2 togoll

	set_color_depth(24);
	if (set_gfx_mode(GFX_AUTODETECT,640,480,0,0)!=0)
	{
		return -1;
	}
		set_display_switch_mode(SWITCH_BACKGROUND); //allows game to run in back ground

	/*
  BITMAP *buffer;


    buffer=create_bitmap(SCREEN_W,SCREEN_H);
	*/

	return 0;
}
void blitBuffer(BITMAP *bmp)
{
	
	acquire_screen();
	blit(bmp,screen,0,0,0,0,SCREEN_W,SCREEN_H);
	release_screen();
}
volatile int speed_counter = 0;

      void increment_speed_counter()
      {
         speed_counter++;
      }

      END_OF_FUNCTION(increment_speed_counter)

int main()
{
	if (initAll()<0)
	{ 
		return 0;
	}
	bool done= false;
bool step = false;
DATAFILE *data;

data = load_datafile("data.dat");
BITMAP *buffer;
	buffer=create_bitmap(SCREEN_W,SCREEN_H); // double buffering

blit((BITMAP*)data[BACK].dat,buffer,0,0,0,0,640,480);
blitBuffer(buffer);

SpritePartconfigList list1;
list1.load("charparts.txt");
SpriteList players;
players.load("sprits.txt");
//Sprites

for( int i = 1; i < (players.GetNumItems()+1); i++)
{
players.Find_Sprite_id(i).loadSpriteParts(&list1);
}



show_mouse( buffer );
	

LOCK_VARIABLE(speed_counter);
         LOCK_FUNCTION(increment_speed_counter);

         install_int_ex(increment_speed_counter, BPS_TO_TIMER(60));

players.Find_Sprite_id(1).draw(buffer,data);
int speed=0;
while (done==false)
{

while (speed_counter > 0) 
{
       


			

if (speed == 20) 
{
speed=0;
		if(step == false)
		{
			players.Find_Sprite_id(1).switchPart(1,5);
			for( int i = 1; i < (players.GetNumItems()+1); i++)
			{
				players.Find_Sprite_id(i).loadSpriteParts(&list1);
			}
		players.Find_Sprite_id(1).set_spriteXOffsets(1,8);
		players.Find_Sprite_id(1).set_spriteYOffsets(1,-40);
			step= true;
		}
		else
		{
			players.Find_Sprite_id(1).switchPart(5,1);
			for( int i = 1; i < (players.GetNumItems()+1); i++)
			{
				players.Find_Sprite_id(i).loadSpriteParts(&list1);
			}
			players.Find_Sprite_id(1).set_spriteXOffsets(1,0);
			step= false;
		}
}
else
{
speed++;
}


	if (mouse_b & 1)
	{
		

		players.Find_Sprite_id(1).set_X(mouse_x);
		players.Find_Sprite_id(1).set_Y(mouse_y);
		players.Find_Sprite_id(1).get_spriteParts(0)->set_dest_X(players.Find_Sprite_id(1).get_X());
		players.Find_Sprite_id(1).get_spriteParts(0)->set_dest_Y(players.Find_Sprite_id(1).get_Y());
	
	}
        speed_counter--;
}
	       
	    
	blit((BITMAP*)data[BACK].dat,buffer,0,0,0,0,640,480);
	players.Find_Sprite_id(1).draw(buffer,data);
	blitBuffer(buffer);

	if (key[KEY_F12])
	{
		done = true;
	}

}




	return 0;
}
END_OF_MAIN();