
#include "game.h"


//using namespace std;
//#include <iostream>

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



computerId=0;
text_mode(-1);
 
	// install a MIDI sound driver 
	if (install_sound(DIGI_AUTODETECT, MIDI_AUTODETECT, NULL) != 0) 
	{
		allegro_message("Error initialising sound system\n%s\n", allegro_error);
		return 1;
	}
	////////////////////##### i put this in for fullsreen F2 togoll

	set_color_depth(16);
	if (set_gfx_mode(GFX_AUTODETECT_WINDOWED,640,480,0,0)!=0)
	{
		allegro_message("Error initialising video system\n%s\n", allegro_error);
		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;
}

//BITMAP *buffer;








///////////////////////////////////////	
 volatile int speed_counter = 0;

      void increment_speed_counter()
      {
         speed_counter++;
      }

      END_OF_FUNCTION(increment_speed_counter)
/////////////////////////////////////////////////////////////

int main(int argc, char **argv)
{

	if (initAll()<0) return 0;




	//	bool graphic = true;//turn of an on sever grapics F6

	bool done=false;
	Game newgame;

	ClientDatabase clients = ClientDatabase();
	clients.load();

	//newgame.play_song(MIDI_BEAT);
	/////////////////////////////////////////////////networing

   

	int loginbutton=99;
	int loadbutton=99; 
	bool draw= true;
	LOCK_VARIABLE(speed_counter);
    LOCK_FUNCTION(increment_speed_counter);

    install_int_ex(increment_speed_counter, BPS_TO_TIMER(60));

	while (done==false)
	{
	
			rest(1)	; //take 100%cpu or runs slow


			//newgame.do_game_networking();
		if (speed_counter > 0) 
		{
			
			if(loadbutton==99||loadbutton==0)
			{
				blit((BITMAP*)newgame.data[BACK].dat,newgame.get_views()[0].get_buffer(),0,0,0,0,640,480);
				if(loginbutton!=2)
				{
					loginbutton=draw_login(newgame.get_views()[0].get_buffer());
				}
			
				if(loginbutton==-1)
				{
					done=true;
				}
				else if (loginbutton==2)
				{
			
					loadbutton= draw_loadmenu(newgame.get_views()[0].get_buffer());
					if(loadbutton==1)
					{
						username = "j";
						netId++;
	
						password = ""; //

   	
						charnetId=netId;
						inboxbuffer += "9999|"+NumberToString(netId)+"|006|"+NumberToString(netId)+","+NumberToString(csock)+","+username+","+password+"`~"; 

					}
					else if(loadbutton==2)
					{
						username = "p";
						netId++;
	
						password = ""; //

   	
						charnetId=netId;
						inboxbuffer += "9999|"+NumberToString(netId)+"|006|"+NumberToString(netId)+","+NumberToString(csock)+","+username+","+password+"`~"; 

					}
			
				}
			}
			else
			{



				if(-1<socketcl)
				{
				//for propor login off 


					clients.numlogedon--;
					socketcl=-1;
				}

				if (clientloginflag == true)
				{
					clients.logon();
					clients.numlogedon++;
					newgame.play_song(MIDI_SPIKKO);
				}
				if (clientlogoffflag == true)
				{
					//clients.save();
					clients.logoff2();
	
	
				}
				if (newclientflag == true)
				{
					clients.insert(newclientstring);
					newclientflag = false;

				}

				newgame.do_game_logic();
				
				
			}

	
			//acquire_screen();
////////////////////////////////////////////////////
			//turn of an on sever grapics F6
		   speed_counter--;
		}
		else if(speed_counter==0)
		{
			draw = true;
			graphic = true;
		}

		if(draw == true)
		{
scare_mouse();
			newgame.do_game_gfx();
unscare_mouse();

			draw= false;
				//rest(1);
		}

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

	}
//allegro_exit();
	return 0;
}
END_OF_MAIN();



/*


	LOCK_VARIABLE(timer);
	LOCK_FUNCTION(inc_timer);

 	install_int(inc_timer,1000);

volatile int timer=1;

void inc_timer(void)
{
    if (!game_pause)
    {
        timer++;
    }
}
END_OF_FUNCTION(inc_timer);
*/