#include <string.h>
#include <sstream>
#include <iostream>
#include "engine.h"
#include "guiapply.h"
#include "graphics.h"
#include "input.h"
#include "fftfile.h"

extern Engine *engine;
extern FFTInput *fftinput;
extern Preferences *prefs;

int RunStartingGUI(int w, int h)
{
	SDL_Event event;
	SDL_Surface *screen=SDL_GetVideoSurface();
	StarField *stars = new StarField;
	StarField *stars2 = new StarField;
	GUI *startingGUI=new GUI;
	stars->Init(w, h);
	stars2->Init(w, h);
	startingGUI->Init();
	startingGUI->MakeWindow(60, 120, w-120, 220);
	int sNewGame=startingGUI->MakeButton(100,160,w-200, "New Game");
	int sLoadGame=startingGUI->MakeButton(100,200,w-200, "Load Game");
	int sOptions=startingGUI->MakeButton(100,240,w-200, "Options");
	int sQuit=startingGUI->MakeButton(100,280,w-200, "Quit");

	SDL_Color white = { 0xFF, 0xFF, 0xFF, 0 };
	TTF_Font *font=TTF_OpenFont("fonts/arial.ttf", 14);
	int done=0;
	int getx, gety, temp;
	int click=-1;
	double x=0;
	double y=0;
	int starx=0;
	int stary=0;
	int starx2=0;
	int stary2=0;
	int angle=90;
	while(!done)
	{
		x+=50*-(sin(angle*(PI/180)));
		y+=50*(cos(angle*(PI/180)));
		starx=(int)x/10;
		stary=(int)y/10;
		starx2=(int)x/20;
		stary2=(int)y/20;
		Slock();
		while (SDL_PollEvent(&event))
		{
			// an event was found
			switch (event.type) {
			// close button clicked
			case SDL_QUIT:
				TTF_CloseFont(font);
				delete startingGUI;
				delete stars;
				delete stars2;
				engine->Quit();
				break;

			// handle the keyboard
			case SDL_KEYDOWN:
				switch (event.key.keysym.sym) {
				case SDLK_RETURN:
					done = 1;
					break;
				default:
					break;
				}
				break;
			case SDL_MOUSEMOTION:
				SDL_GetMouseState(&getx, &gety);
				startingGUI->SendMouseMotion(getx,gety);
				break;
			case SDL_MOUSEBUTTONDOWN:
				SDL_GetMouseState(&getx, &gety);
				click = startingGUI->SendMouseClick(getx, gety);
				break;
			case SDL_MOUSEBUTTONUP:
				SDL_GetMouseState(&getx, &gety);
				if (click!=-1 && click==startingGUI->SendMouseClick(getx, gety))
				{
					SDL_FillRect(screen,0,0);
					stars2->DisplayWarp(starx2, stary2, angle, 25);
					stars->DisplayWarp(starx, stary, angle, 25);
					centerString(font, w/2, 40, "Final Frontier Trader", white);
					startingGUI->Draw();
					Sulock();
					SDL_Flip(screen);
					if (click==sNewGame)
					{
						done=2;
					}
					if (click==sLoadGame)
					{
						//nothing yet
					}
					if (click==sOptions)
					{
						temp=RunStartingOptions(w, h);
						if (temp==-1) done=-1;
					}
					if (click==sQuit)
					{
						done = AskToQuit(w, h);
					}
				}
				startingGUI->UpdateButtons();
				break;
			default:
				break;
			}
		}
		SDL_FillRect(screen,0,0);
		stars2->DisplayWarp(starx2, stary2, angle, 25);
		stars->DisplayWarp(starx, stary, angle, 25);
		centerString(font, w/2, 40, "Final Frontier Trader", white);
		startingGUI->Draw();
		Sulock();
		SDL_Flip(screen);
	}
	TTF_CloseFont(font);
	delete startingGUI;
	delete stars;
	delete stars2;
	return done;
}
int ControlChanging(int w, int h, SDL_Surface *bg, bool fill)
{
	SDL_Event event;
	SDL_Surface *screen=SDL_GetVideoSurface();
	int click=-1;
	int getx,gety,newkey;
	int done=0;
	GUI *controlsGUI=new GUI;
	GUI *Notify=0;
	controlsGUI->Init();
	controlsGUI->MakeWindow(90, 30, w-180, 430);
	controlsGUI->MakeStaticLeft(110, 45, 40, 20, "Fire:");
	int sFire1=controlsGUI->MakeButton(230, 40, 80, fftinput->GetFire1Str());
	int sFire2=controlsGUI->MakeButton(330, 40, 80, fftinput->GetFire2Str());
	controlsGUI->MakeStaticLeft(110, 75, 40, 20, "Warp:");
	int sWarp1=controlsGUI->MakeButton(230, 70, 80, fftinput->GetWarp1Str());
	int sWarp2=controlsGUI->MakeButton(330, 70, 80, fftinput->GetWarp2Str());
	controlsGUI->MakeStaticLeft(110, 105, 40, 20, "Accelerate:");
	int sUp=controlsGUI->MakeButton(230, 100, 80, fftinput->GetUpStr());
	controlsGUI->MakeStaticLeft(110, 135, 40, 20, "Decelerate:");
	int sDown=controlsGUI->MakeButton(230, 130, 80, fftinput->GetDownStr());
	controlsGUI->MakeStaticLeft(110, 165, 40, 20, "Left turn:");
	int sLeft=controlsGUI->MakeButton(230, 160, 80, fftinput->GetLeftStr());
	controlsGUI->MakeStaticLeft(110, 195, 40, 20, "Right turn:");
	int sRight=controlsGUI->MakeButton(230, 190, 80, fftinput->GetRightStr());
	controlsGUI->MakeStaticLeft(110, 225, 40, 20, "All Stop:");
	int sAllStop=controlsGUI->MakeButton(230, 220, 80, fftinput->GetAllStopStr());
	controlsGUI->MakeStaticLeft(110, 255, 40, 20, "Cancel:");
	int sCancelC=controlsGUI->MakeButton(230, 250, 80, fftinput->GetCancelStr());
	controlsGUI->MakeStaticLeft(110, 285, 40, 20, "Minimap Zoom in:");
	int sMMZoomIn=controlsGUI->MakeButton(230, 280, 80, fftinput->GetMiniMapZoomInStr());
	controlsGUI->MakeStaticLeft(110, 315, 40, 20, "Minimap Zoom out:");
	int sMMZoomOut=controlsGUI->MakeButton(230, 310, 80, fftinput->GetMiniMapZoomOutStr());
	controlsGUI->MakeStaticLeft(110, 345, 40, 20, "Pause:");
	int sPause=controlsGUI->MakeButton(230, 340, 80, fftinput->GetPauseStr());
	controlsGUI->MakeStaticLeft(110, 375, 40, 20, "Zoom in:");
	int sZoomIn=controlsGUI->MakeButton(230, 370, 80, fftinput->GetZoomInStr());
	controlsGUI->MakeStaticLeft(110, 405, 40, 20, "Zoom out:");
	int sZoomOut=controlsGUI->MakeButton(230, 400, 80, fftinput->GetZoomOutStr());
	controlsGUI->MakeStaticLeft(330, 105, 40, 20, "Previous Weapon:");
	int sPrevW=controlsGUI->MakeButton(430, 100, 80, fftinput->GetPrevWeaponStr());
	controlsGUI->MakeStaticLeft(330, 135, 40, 20, "Next Weapon:");
	int sNextW=controlsGUI->MakeButton(430, 130, 80, fftinput->GetNextWeaponStr());
	controlsGUI->MakeStaticLeft(330, 165, 40, 20, "Dock:");
	int sDock=controlsGUI->MakeButton(430, 160, 80, fftinput->GetDockStr());
	controlsGUI->MakeStaticLeft(330, 195, 40, 20, "Previous Target:");
	int sPrevT=controlsGUI->MakeButton(430, 190, 80, fftinput->GetPrevTargetStr());
	controlsGUI->MakeStaticLeft(330, 225, 40, 20, "Next Target:");
	int sNextT=controlsGUI->MakeButton(430, 220, 80, fftinput->GetNextTargetStr());
	int sControlCancel=controlsGUI->MakeButton(140,430,w-280, "Back to Previous Menu");
	std::string keyinuse="";
	unsigned int showticks=SDL_GetTicks();
	bool showinuse=false;
	bool wasinuse=false;
	SDL_Color white = { 0xFF, 0xFF, 0xFF, 0 };
	TTF_Font *font=TTF_OpenFont("fonts/arial.ttf", 14);

	while(!done)
	{
		Slock();
		while (SDL_PollEvent(&event))
		{
			// an event was found
			switch (event.type) {
			// close button clicked
			case SDL_QUIT:
				done=-1;
				break;

			// handle the keyboard
			case SDL_KEYDOWN:
				switch (event.key.keysym.sym) {
				case SDLK_ESCAPE:
					done = 1;
					break;
				default:
					break;
				}
				break;
			case SDL_MOUSEMOTION:
				SDL_GetMouseState(&getx, &gety);
				controlsGUI->SendMouseMotion(getx,gety);
				break;
			case SDL_MOUSEBUTTONDOWN:
				SDL_GetMouseState(&getx, &gety);
				click = controlsGUI->SendMouseClick(getx, gety);
				break;
			case SDL_MOUSEBUTTONUP:
				SDL_GetMouseState(&getx, &gety);
				newkey=0;
				if (click!=-1 && click==controlsGUI->SendMouseClick(getx, gety))
				{
					if (fill)
					{
						SDL_FillRect(screen,0,0);
						centerString(font, w/2, 10, "Controls", white);
					}
					else if (bg) DrawIMG(bg,0,0);
					controlsGUI->Draw();
					if (showinuse && Notify)
					{
						Notify->Draw();
						if (SDL_GetTicks()>=showticks+2000)
						{
							delete Notify;
							showinuse=false;
						}
					}
					Sulock();
					SDL_Flip(screen);
					if (click==sFire1)
					{
						newkey=ChangeControl(w, h, bg, fill);
						if(newkey>0)
						{
							if(!fftinput->KeyInUse(newkey))
							{
								fftinput->SetFire1(newkey);
								controlsGUI->NewString(sFire1, fftinput->GetFire1Str());
							}
							else if (newkey!=fftinput->GetFire1()) wasinuse=true;
						}
						else if(newkey==-1) done=-1;
					}
					if (click==sFire2)
					{
						newkey=ChangeControl(w, h, bg, fill);
						if(newkey>0)
						{
							if(!fftinput->KeyInUse(newkey))
							{
								fftinput->SetFire2(newkey);
								controlsGUI->NewString(sFire2, fftinput->GetFire2Str());
							}
							else if (newkey!=fftinput->GetFire2()) wasinuse=true;
						}
						else if(newkey==-1) done=-1;
					}
					if (click==sWarp1)
					{
						newkey=ChangeControl(w, h, bg, fill);
						if(newkey>0)
						{
							if(!fftinput->KeyInUse(newkey))
							{
								fftinput->SetWarp1(newkey);
								controlsGUI->NewString(sWarp1, fftinput->GetWarp1Str());
							}
							else if (newkey!=fftinput->GetWarp1()) wasinuse=true;
						}
						else if(newkey==-1) done=-1;
					}
					if (click==sWarp2)
					{
						newkey=ChangeControl(w, h, bg, fill);
						if(newkey>0)
						{
							if(!fftinput->KeyInUse(newkey))
							{
								fftinput->SetWarp2(newkey);
								controlsGUI->NewString(sWarp2, fftinput->GetWarp2Str());
							}
							else if (newkey!=fftinput->GetWarp2()) wasinuse=true;
						}
						else if(newkey==-1) done=-1;
					}
					if (click==sUp)
					{
						newkey=ChangeControl(w, h, bg, fill);
						if(newkey>0)
						{
							if(!fftinput->KeyInUse(newkey))
							{
								fftinput->SetUp(newkey);
								controlsGUI->NewString(sUp, fftinput->GetUpStr());
							}
							else if (newkey!=fftinput->GetUp()) wasinuse=true;
						}
						else if (newkey!=fftinput->GetUp()) if(newkey==-1) done=-1;
					}
					if (click==sDown)
					{
						newkey=ChangeControl(w, h, bg, fill);
						if(newkey>0)
						{
							if(!fftinput->KeyInUse(newkey))
							{
								fftinput->SetDown(newkey);
								controlsGUI->NewString(sDown, fftinput->GetDownStr());
							}
							else if (newkey!=fftinput->GetDown()) wasinuse=true;
						}
						else if(newkey==-1) done=-1;
					}
					if (click==sLeft)
					{
						newkey=ChangeControl(w, h, bg, fill);
						if(newkey>0)
						{
							if(!fftinput->KeyInUse(newkey))
							{
								fftinput->SetLeft(newkey);
								controlsGUI->NewString(sLeft, fftinput->GetLeftStr());
							}
							else if (newkey!=fftinput->GetLeft()) wasinuse=true;
						}
						else if(newkey==-1) done=-1;
					}
					if (click==sRight)
					{
						newkey=ChangeControl(w, h, bg, fill);
						if(newkey>0)
						{
							if(!fftinput->KeyInUse(newkey))
							{
								fftinput->SetRight(newkey);
								controlsGUI->NewString(sRight, fftinput->GetRightStr());
							}
							else if (newkey!=fftinput->GetRight()) wasinuse=true;
						}
						else if(newkey==-1) done=-1;
					}
					if (click==sAllStop)
					{
						newkey=ChangeControl(w, h, bg, fill);
						if(newkey>0)
						{
							if(!fftinput->KeyInUse(newkey))
							{
								fftinput->SetAllStop(newkey);
								controlsGUI->NewString(sAllStop, fftinput->GetAllStopStr());
							}
							else if (newkey!=fftinput->GetAllStop()) wasinuse=true;
						}
						else if(newkey==-1) done=-1;
					}
					if (click==sCancelC)
					{
						newkey=ChangeControl(w, h, bg, fill);
						if(newkey>0)
						{
							if(!fftinput->KeyInUse(newkey))
							{
								fftinput->SetCancel(newkey);
								controlsGUI->NewString(sCancelC, fftinput->GetCancelStr());
							}
							else if (newkey!=fftinput->GetCancel()) wasinuse=true;
						}
						else if(newkey==-1) done=-1;
					}
					if (click==sMMZoomIn)
					{
						newkey=ChangeControl(w, h, bg, fill);
						if(newkey>0)
						{
							if(!fftinput->KeyInUse(newkey))
							{
								fftinput->SetMiniMapZoomIn(newkey);
								controlsGUI->NewString(sMMZoomIn, fftinput->GetMiniMapZoomInStr());
							}
							else if (newkey!=fftinput->GetMiniMapZoomIn()) wasinuse=true;
						}
						else if(newkey==-1) done=-1;
					}
					if (click==sMMZoomOut)
					{
						newkey=ChangeControl(w, h, bg, fill);
						if(newkey>0)
						{
							if(!fftinput->KeyInUse(newkey))
							{
								fftinput->SetMiniMapZoomOut(newkey);
								controlsGUI->NewString(sMMZoomOut, fftinput->GetMiniMapZoomOutStr());
							}
							else if (newkey!=fftinput->GetMiniMapZoomOut()) wasinuse=true;
						}
						else if(newkey==-1) done=-1;
					}
					if (click==sPause)
					{
						newkey=ChangeControl(w, h, bg, fill);
						if(newkey>0)
						{
							if(!fftinput->KeyInUse(newkey))
							{
								fftinput->SetPause(newkey);
								controlsGUI->NewString(sPause, fftinput->GetPauseStr());
							}
							else if (newkey!=fftinput->GetPause()) wasinuse=true;
						}
						else if(newkey==-1) done=-1;
					}
					if (click==sZoomIn)
					{
						newkey=ChangeControl(w, h, bg, fill);
						if(newkey>0)
						{
							if(!fftinput->KeyInUse(newkey))
							{
								fftinput->SetZoomIn(newkey);
								controlsGUI->NewString(sZoomIn, fftinput->GetZoomInStr());
							}
							else if (newkey!=fftinput->GetZoomIn()) wasinuse=true;
						}
						else if(newkey==-1) done=-1;
					}
					if (click==sZoomOut)
					{
						newkey=ChangeControl(w, h, bg, fill);
						if(newkey>0)
						{
							if(!fftinput->KeyInUse(newkey))
							{
								fftinput->SetZoomOut(newkey);
								controlsGUI->NewString(sZoomOut, fftinput->GetZoomOutStr());
							}
							else if (newkey!=fftinput->GetZoomOut()) wasinuse=true;
						}
						else if(newkey==-1) done=-1;
					}
					if (click==sPrevW)
					{
						newkey=ChangeControl(w, h, bg, fill);
						if(newkey>0)
						{
							if(!fftinput->KeyInUse(newkey))
							{
								fftinput->SetPrevWeapon(newkey);
								controlsGUI->NewString(sPrevW, fftinput->GetPrevWeaponStr());
							}
							else if (newkey!=fftinput->GetPrevWeapon()) wasinuse=true;
						}
						else if(newkey==-1) done=-1;
					}
					if (click==sNextW)
					{
						newkey=ChangeControl(w, h, bg, fill);
						if(newkey>0)
						{
							if(!fftinput->KeyInUse(newkey))
							{
								fftinput->SetNextWeapon(newkey);
								controlsGUI->NewString(sNextW, fftinput->GetNextWeaponStr());
							}
							else if (newkey!=fftinput->GetNextWeapon()) wasinuse=true;
						}
						else if(newkey==-1) done=-1;
					}
					if (click==sDock)
					{
						newkey=ChangeControl(w, h, bg, fill);
						if(newkey>0)
						{
							if(!fftinput->KeyInUse(newkey))
							{
								fftinput->SetDock(newkey);
								controlsGUI->NewString(sDock, fftinput->GetDockStr());
							}
							else if (newkey!=fftinput->GetDock()) wasinuse=true;
						}
						else if(newkey==-1) done=-1;
					}
					if (click==sPrevT)
					{
						newkey=ChangeControl(w, h, bg, fill);
						if(newkey>0)
						{
							if(!fftinput->KeyInUse(newkey))
							{
								fftinput->SetPrevTarget(newkey);
								controlsGUI->NewString(sPrevT, fftinput->GetPrevTargetStr());
							}
							else if (newkey!=fftinput->GetPrevTarget()) wasinuse=true;
						}
						else if(newkey==-1) done=-1;
					}
					if (click==sNextT)
					{
						newkey=ChangeControl(w, h, bg, fill);
						if(newkey>0)
						{
							if(!fftinput->KeyInUse(newkey))
							{
								fftinput->SetNextTarget(newkey);
								controlsGUI->NewString(sNextT, fftinput->GetNextTargetStr());
							}
							else if (newkey!=fftinput->GetNextTarget()) wasinuse=true;
						}
						else if(newkey==-1) done=-1;
					}
					if (click==sControlCancel)
					{
						done=1;
					}
					if (wasinuse && !showinuse)
					{
						keyinuse="The ";
						keyinuse+=fftinput->KeyCodeToString(newkey);
						keyinuse+=" key is in use already!";
						Notify=new GUI;
						Notify->Init();
						Notify->MakeWindow(200, h/2-50, w-400, 100);
						Notify->MakeStatic(200, h/2-7, w-400, 20, keyinuse);
						showinuse=true;
						showticks=SDL_GetTicks();
					}
				}
				controlsGUI->UpdateButtons();
				break;
			default:
				break;
			}
		}
		if (fill)
		{
			SDL_FillRect(screen,0,0);
			centerString(font, w/2, 10, "Controls", white);
		}
		else if (bg) DrawIMG(bg,0,0);
		controlsGUI->Draw();
		if (showinuse && Notify)
		{
			Notify->Draw();
			if (SDL_GetTicks()>=showticks+2000)
			{
				showinuse=false;
				wasinuse=false;
			}
		}
		Sulock();
		SDL_Flip(screen);
	}
	TTF_CloseFont(font);
	delete controlsGUI;
	if (showinuse) { delete Notify; Notify=0; }
	if (done!=1 && done!=-1) done=0;
	prefs->ChangeInput(fftinput);
	prefs->Save();
	return done;
}
int RunStartingOptions(int w, int h)
{
	SDL_Event event;
	SDL_Surface *screen=SDL_GetVideoSurface();
	GUI *optionsGUI=new GUI;
	optionsGUI->Init();
	optionsGUI->MakeWindow(60, 120, w-120, 220);
	int sControls=optionsGUI->MakeButton(100,160,w-200, "Controls");
	int sCancel=optionsGUI->MakeButton(100,280,w-200, "Back to Main Menu");

	SDL_Color white = { 0xFF, 0xFF, 0xFF, 0 };
	TTF_Font *font=TTF_OpenFont("fonts/arial.ttf", 14);
	int done=0;
	int getx, gety;
	int click=-1;
	while(!done)
	{
		Slock();
		while (SDL_PollEvent(&event))
		{
			// an event was found
			switch (event.type) {
			// close button clicked
			case SDL_QUIT:
				done=-1;
				break;

			// handle the keyboard
			case SDL_KEYDOWN:
				switch (event.key.keysym.sym) {
				case SDLK_ESCAPE:
					done = 1;
					break;
				default:
					break;
				}
				break;
			case SDL_MOUSEMOTION:
				SDL_GetMouseState(&getx, &gety);
				optionsGUI->SendMouseMotion(getx,gety);
				break;
			case SDL_MOUSEBUTTONDOWN:
				SDL_GetMouseState(&getx, &gety);
				click = optionsGUI->SendMouseClick(getx, gety);
				break;
			case SDL_MOUSEBUTTONUP:
				SDL_GetMouseState(&getx, &gety);
				if (click!=-1 && click==optionsGUI->SendMouseClick(getx, gety))
				{
					SDL_FillRect(screen,0,0);
					centerString(font, w/2, 40, "Options", white);
					optionsGUI->Draw();
					Sulock();
					SDL_Flip(screen);
					if (click==sControls)
					{
						done=ControlChanging(w,h);
					}
					if (click==sCancel)
					{
						done=1;
					}
				}
				optionsGUI->UpdateButtons();
				break;
			default:
				break;
			}
		}
		SDL_FillRect(screen,0,0);
		centerString(font, w/2, 40, "Options", white);
		optionsGUI->Draw();
		Sulock();
		SDL_Flip(screen);
	}
	TTF_CloseFont(font);
	delete optionsGUI;
	return done;
}
int ChangeControl(int w, int h, SDL_Surface *bg, bool fill)
{
	int key=0;
	SDL_Event event;
	SDL_Surface *screen=SDL_GetVideoSurface();
	GUI *cGUI=new GUI;
	cGUI->Init();
	cGUI->MakeWindow(60, 220, w-120, 60);
	cGUI->MakeStatic(60, 240, w-120, 20, "Press the new key");
	int done=0;
	while(!done)
	{
		Slock();
		while (SDL_PollEvent(&event))
		{
			// an event was found
			switch (event.type) {
			// close button clicked
			case SDL_QUIT:
				done=-1;
				break;

			// handle the keyboard
			case SDL_KEYDOWN:
				key=event.key.keysym.sym;
				done=1;
				break;
			default:
				break;
			}
		}
		if (fill) SDL_FillRect(screen,0,0);
		else if (bg!=0) DrawIMG(bg,0,0);
		cGUI->Draw();
		Sulock();
		SDL_Flip(screen);
	}
	delete cGUI;
	return key;
}
int AskToQuit(int SCREEN_WIDTH, int SCREEN_HEIGHT)
{
	SDL_Event event;
	SDL_Surface *screen=SDL_GetVideoSurface();
	SDL_Surface *bg = SDL_CreateRGBSurface(0, screen->w, screen->h, screen->format->BitsPerPixel, screen->format->Rmask, screen->format->Gmask, screen->format->Bmask, screen->format->Amask);
	SDL_Rect dest;
	dest.x = 0;
	dest.y = 0;
	SDL_BlitSurface(screen, NULL, bg, &dest);
	GUI *gui3=new GUI;
	gui3->Init();
	gui3->MakeWindow(120,200,SCREEN_WIDTH-240,110);
	gui3->MakeStatic(30,206,SCREEN_WIDTH-60,20,"Are you sure you want to exit?");
	gui3->MakeButton(150,270,40,"Yes");
	gui3->MakeButton(SCREEN_WIDTH-190,270,40,"No");
	int yesButton=gui3->GetButtonID(150,270,40,"Yes");
	int noButton=gui3->GetButtonID(SCREEN_WIDTH-190,270,40,"No");
	int done=0;
	int getx, gety;
	int click=-1;
	while(!done)
	{
		Slock();
		while (SDL_PollEvent(&event))
		{
			// an event was found
			switch (event.type) {
			// close button clicked
			case SDL_QUIT:
				delete gui3;
				SDL_FreeSurface(bg);
				engine->Quit();
				break;

			// handle the keyboard
			case SDL_KEYDOWN:
				switch (event.key.keysym.sym) {
				case SDLK_RETURN:
					done = 1;
					break;
				default:
					break;
				}
				break;
			case SDL_MOUSEMOTION:
				SDL_GetMouseState(&getx, &gety);
				gui3->SendMouseMotion(getx,gety);
				break;
			case SDL_MOUSEBUTTONDOWN:
				SDL_GetMouseState(&getx, &gety);
				click = gui3->SendMouseClick(getx, gety);
				break;
			case SDL_MOUSEBUTTONUP:
				SDL_GetMouseState(&getx, &gety);
				if (click!=-1 && click==gui3->SendMouseClick(getx, gety))
				{
					if (click==yesButton)
					{
						SDL_FillRect(screen,0,0);
						DrawIMG(bg,0,0);
						gui3->Draw();
						Sulock();
						SDL_Flip(screen);
						SDL_Delay(100);
						done=-1;
					}
					if (click==noButton)
					{
						SDL_FillRect(screen,0,0);
						DrawIMG(bg,0,0);
						gui3->Draw();
						Sulock();
						SDL_Flip(screen);
						SDL_Delay(100);
						done=1;
					}
				}
				gui3->UpdateButtons();
				break;
			default:
				break;
			}
		}
		SDL_FillRect(screen,0,0);
		DrawIMG(bg,0,0);
		gui3->Draw();
		Sulock();
		SDL_Flip(screen);
	}
	delete gui3;
	SDL_FreeSurface(bg);
	if (done!=-1) done=0;
	return done;
}
int AskToQuitGame(int SCREEN_WIDTH, int SCREEN_HEIGHT)
{
	SDL_Event event;
	SDL_Surface *screen=SDL_GetVideoSurface();
	SDL_Surface *bg = SDL_CreateRGBSurface(0, screen->w, screen->h, screen->format->BitsPerPixel, screen->format->Rmask, screen->format->Gmask, screen->format->Bmask, screen->format->Amask);
	SDL_Rect dest;
	dest.x = 0;
	dest.y = 0;
	SDL_BlitSurface(screen, NULL, bg, &dest);
	GUI *gui3=new GUI;
	gui3->Init();
	gui3->MakeWindow(120,200,SCREEN_WIDTH-240,110);
	gui3->MakeStatic(30,206,SCREEN_WIDTH-60,20,"Are you sure you want to quit this game?");
	gui3->MakeButton(150,270,40,"Yes");
	gui3->MakeButton(SCREEN_WIDTH-190,270,40,"No");
	int yesButton=gui3->GetButtonID(150,270,40,"Yes");
	int noButton=gui3->GetButtonID(SCREEN_WIDTH-190,270,40,"No");
	int done=0;
	int getx, gety;
	int click=-1;
	while(!done)
	{
		Slock();
		while (SDL_PollEvent(&event))
		{
			// an event was found
			switch (event.type) {
			// close button clicked
			case SDL_QUIT:
				delete gui3;
				SDL_FreeSurface(bg);
				engine->Quit();
				break;

			// handle the keyboard
			case SDL_KEYDOWN:
				switch (event.key.keysym.sym) {
				case SDLK_RETURN:
					done = 1;
					break;
				default:
					break;
				}
				break;
			case SDL_MOUSEMOTION:
				SDL_GetMouseState(&getx, &gety);
				gui3->SendMouseMotion(getx,gety);
				break;
			case SDL_MOUSEBUTTONDOWN:
				SDL_GetMouseState(&getx, &gety);
				click = gui3->SendMouseClick(getx, gety);
				break;
			case SDL_MOUSEBUTTONUP:
				SDL_GetMouseState(&getx, &gety);
				if (click!=-1 && click==gui3->SendMouseClick(getx, gety))
				{
					if (click==yesButton)
					{
						SDL_FillRect(screen,0,0);
						DrawIMG(bg,0,0);
						gui3->Draw();
						Sulock();
						SDL_Flip(screen);
						SDL_Delay(100);
						done=-1;
					}
					if (click==noButton)
					{
						SDL_FillRect(screen,0,0);
						DrawIMG(bg,0,0);
						gui3->Draw();
						Sulock();
						SDL_Flip(screen);
						SDL_Delay(100);
						done=1;
					}
				}
				gui3->UpdateButtons();
				break;
			default:
				break;
			}
		}
		SDL_FillRect(screen,0,0);
		DrawIMG(bg,0,0);
		gui3->Draw();
		Sulock();
		SDL_Flip(screen);
	}
	delete gui3;
	SDL_FreeSurface(bg);
	if (done!=-1) done=0;
	return done;
}
int MsgBox(int SCREEN_WIDTH, int SCREEN_HEIGHT, std::string message)
{
	SDL_Event event;
	SDL_Surface *screen=SDL_GetVideoSurface();
	SDL_Surface *bg = SDL_CreateRGBSurface(0, screen->w, screen->h, screen->format->BitsPerPixel, screen->format->Rmask, screen->format->Gmask, screen->format->Bmask, screen->format->Amask);
	SDL_Rect dest;
	dest.x = 0;
	dest.y = 0;
	SDL_BlitSurface(screen, NULL, bg, &dest);

	//multiple lines
	std::string lines[100];
	char c;
	int i=0;
	int done=0;
	int end=0;
	c=message[i];
	std::string line;
	while(!done) {
		if(end>=100) done=1;
		if(c != '\n') {
			line += c;
		}
		else
		{
			lines[end]=line;
			end++;
			line="";
		}
		i++;
		if (i>=(int)message.size())
		{
			lines[end]=line;
			end++;
			done=1;
		}
		c=message[i];
	}
	int height=(12*end)+70;

	GUI *gui3=new GUI;
	gui3->Init();
	gui3->MakeWindow(120,(SCREEN_HEIGHT/2)-(height/2),SCREEN_WIDTH-240,height);
	int cur=(SCREEN_HEIGHT/2)-(height/2)+6;
	for(i=0; i<end; i++)
	{
		gui3->MakeStatic(30,cur,SCREEN_WIDTH-60,20,lines[i]);
		cur+=12;
	}
	gui3->MakeButton(150,cur+24,SCREEN_WIDTH-300,"OK");
	int okButton=gui3->GetButtonID(150,cur+24,SCREEN_WIDTH-300,"OK");
	done=0;
	int getx, gety;
	int click=-1;
	while(!done)
	{
		Slock();
		while (SDL_PollEvent(&event))
		{
			// an event was found
			switch (event.type) {
			// close button clicked
			case SDL_QUIT:
				delete gui3;
				SDL_FreeSurface(bg);
				engine->Quit();
				break;

			// handle the keyboard
			case SDL_KEYDOWN:
				switch (event.key.keysym.sym) {
				case SDLK_RETURN:
					done = 1;
					break;
				default:
					break;
				}
				break;
			case SDL_MOUSEMOTION:
				SDL_GetMouseState(&getx, &gety);
				gui3->SendMouseMotion(getx,gety);
				break;
			case SDL_MOUSEBUTTONDOWN:
				SDL_GetMouseState(&getx, &gety);
				click = gui3->SendMouseClick(getx, gety);
				break;
			case SDL_MOUSEBUTTONUP:
				SDL_GetMouseState(&getx, &gety);
				if (click!=-1 && click==gui3->SendMouseClick(getx, gety))
				{
					if (click==okButton)
					{
						SDL_FillRect(screen,0,0);
						DrawIMG(bg,0,0);
						gui3->Draw();
						Sulock();
						SDL_Flip(screen);
						SDL_Delay(100);
						done=1;
					}
				}
				gui3->UpdateButtons();
				break;
			default:
				break;
			}
		}
		SDL_FillRect(screen,0,0);
		DrawIMG(bg,0,0);
		gui3->Draw();
		Sulock();
		SDL_Flip(screen);
	}
	delete gui3;
	SDL_FreeSurface(bg);
	return done;
}
int YesNoBox(int SCREEN_WIDTH, int SCREEN_HEIGHT, std::string message)
{
	SDL_Event event;
	SDL_Surface *screen=SDL_GetVideoSurface();
	SDL_Surface *bg = SDL_CreateRGBSurface(0, screen->w, screen->h, screen->format->BitsPerPixel, screen->format->Rmask, screen->format->Gmask, screen->format->Bmask, screen->format->Amask);
	SDL_Rect dest;
	dest.x = 0;
	dest.y = 0;
	SDL_BlitSurface(screen, NULL, bg, &dest);

	//multiple lines
	std::string lines[100];
	char c;
	int i=0;
	int done=0;
	int end=0;
	c=message[i];
	std::string line;
	while(!done) {
		if(end>=100) done=1;
		if(c != '\n') {
			line += c;
		}
		else
		{
			lines[end]=line;
			end++;
			line="";
		}
		i++;
		if (i>=(int)message.size())
		{
			lines[end]=line;
			end++;
			done=1;
		}
		c=message[i];
	}
	int height=(12*end)+70;

	GUI *gui3=new GUI;
	gui3->Init();
	gui3->MakeWindow(120,(SCREEN_HEIGHT/2)-(height/2),SCREEN_WIDTH-240,height);
	int cur=(SCREEN_HEIGHT/2)-(height/2)+6;
	for(i=0; i<end; i++)
	{
		gui3->MakeStatic(30,cur,SCREEN_WIDTH-60,20,lines[i]);
		cur+=12;
	}
	gui3->MakeButton(150,cur+24,40,"Yes");
	gui3->MakeButton(SCREEN_WIDTH-190,cur+24,40,"No");
	int yesButton=gui3->GetButtonID(150,cur+24,40,"Yes");
	int noButton=gui3->GetButtonID(SCREEN_WIDTH-190,cur+24,40,"No");
	done=0;
	int getx, gety;
	int click=-1;
	while(!done)
	{
		Slock();
		while (SDL_PollEvent(&event))
		{
			// an event was found
			switch (event.type) {
			// close button clicked
			case SDL_QUIT:
				delete gui3;
				SDL_FreeSurface(bg);
				engine->Quit();
				break;

			// handle the keyboard
			case SDL_KEYDOWN:
				switch (event.key.keysym.sym) {
				case SDLK_RETURN:
					done = 1;
					break;
				default:
					break;
				}
				break;
			case SDL_MOUSEMOTION:
				SDL_GetMouseState(&getx, &gety);
				gui3->SendMouseMotion(getx,gety);
				break;
			case SDL_MOUSEBUTTONDOWN:
				SDL_GetMouseState(&getx, &gety);
				click = gui3->SendMouseClick(getx, gety);
				break;
			case SDL_MOUSEBUTTONUP:
				SDL_GetMouseState(&getx, &gety);
				if (click!=-1 && click==gui3->SendMouseClick(getx, gety))
				{
					if (click==yesButton)
					{
						SDL_FillRect(screen,0,0);
						DrawIMG(bg,0,0);
						gui3->Draw();
						Sulock();
						SDL_Flip(screen);
						SDL_Delay(100);
						done=1;
					}
					if (click==noButton)
					{
						SDL_FillRect(screen,0,0);
						DrawIMG(bg,0,0);
						gui3->Draw();
						Sulock();
						SDL_Flip(screen);
						SDL_Delay(100);
						done=-1;
					}
				}
				gui3->UpdateButtons();
				break;
			default:
				break;
			}
		}
		SDL_FillRect(screen,0,0);
		DrawIMG(bg,0,0);
		gui3->Draw();
		Sulock();
		SDL_Flip(screen);
	}
	delete gui3;
	SDL_FreeSurface(bg);
	if (done!=1) done=0;
	return done;
}
int ExitToMenuBox(int SCREEN_WIDTH, int SCREEN_HEIGHT, std::string message)
{
	SDL_Event event;
	SDL_Surface *screen=SDL_GetVideoSurface();
	SDL_Surface *bg = SDL_CreateRGBSurface(0, screen->w, screen->h, screen->format->BitsPerPixel, screen->format->Rmask, screen->format->Gmask, screen->format->Bmask, screen->format->Amask);
	SDL_Rect dest;
	dest.x = 0;
	dest.y = 0;
	SDL_BlitSurface(screen, NULL, bg, &dest);

	//multiple lines
	std::string lines[100];
	char c;
	int i=0;
	int done=0;
	int end=0;
	c=message[i];
	std::string line;
	while(!done) {
		if(end>=100) done=1;
		if(c != '\n') {
			line += c;
		}
		else
		{
			lines[end]=line;
			end++;
			line="";
		}
		i++;
		if (i>=(int)message.size())
		{
			lines[end]=line;
			end++;
			done=1;
		}
		c=message[i];
	}
	int height=(12*end)+20+70;

	GUI *gui3=new GUI;
	gui3->Init();
	gui3->MakeWindow(120,(SCREEN_HEIGHT/2)-(height/2),SCREEN_WIDTH-240,height);
	int cur=(SCREEN_HEIGHT/2)-(height/2)+6;
	for(i=0; i<end; i++)
	{
		gui3->MakeStatic(30,cur,SCREEN_WIDTH-60,20,lines[i]);
		cur+=12;
	}
	gui3->MakeStatic(30,cur+12,SCREEN_WIDTH-60,20,"Click OK to exit to Main Menu");
	gui3->MakeButton(150,cur+44,SCREEN_WIDTH-300,"OK");
	int okButton=gui3->GetButtonID(150,cur+44,SCREEN_WIDTH-300,"OK");
	done=0;
	int getx, gety;
	int click=-1;
	while(!done)
	{
		Slock();
		while (SDL_PollEvent(&event))
		{
			// an event was found
			switch (event.type) {
			// close button clicked
			case SDL_QUIT:
				delete gui3;
				SDL_FreeSurface(bg);
				engine->Quit();
				break;

			// handle the keyboard
			case SDL_KEYDOWN:
				switch (event.key.keysym.sym) {
				case SDLK_RETURN:
					done = 1;
					break;
				default:
					break;
				}
				break;
			case SDL_MOUSEMOTION:
				SDL_GetMouseState(&getx, &gety);
				gui3->SendMouseMotion(getx,gety);
				break;
			case SDL_MOUSEBUTTONDOWN:
				SDL_GetMouseState(&getx, &gety);
				click = gui3->SendMouseClick(getx, gety);
				break;
			case SDL_MOUSEBUTTONUP:
				SDL_GetMouseState(&getx, &gety);
				if (click!=-1 && click==gui3->SendMouseClick(getx, gety))
				{
					if (click==okButton)
					{
						SDL_FillRect(screen,0,0);
						DrawIMG(bg,0,0);
						gui3->Draw();
						Sulock();
						SDL_Flip(screen);
						SDL_Delay(100);
						done=1;
					}
				}
				gui3->UpdateButtons();
				break;
			default:
				break;
			}
		}
		SDL_FillRect(screen,0,0);
		DrawIMG(bg,0,0);
		gui3->Draw();
		Sulock();
		SDL_Flip(screen);
	}
	delete gui3;
	SDL_FreeSurface(bg);
	return done;
}
int GameOverBox(int SCREEN_WIDTH, int SCREEN_HEIGHT)
{
	SDL_Event event;
	SDL_Surface *screen=SDL_GetVideoSurface();
	SDL_Surface *bg = SDL_CreateRGBSurface(0, screen->w, screen->h, screen->format->BitsPerPixel, screen->format->Rmask, screen->format->Gmask, screen->format->Bmask, screen->format->Amask);
	SDL_Rect dest;
	dest.x = 0;
	dest.y = 0;
	SDL_BlitSurface(screen, NULL, bg, &dest);
	GUI *gui3=new GUI;
	gui3->Init();
	gui3->MakeWindow(120,200,SCREEN_WIDTH-240,110);
	gui3->MakeStatic(30,206,SCREEN_WIDTH-60,20,"Game Over!");
	gui3->MakeStatic(30,226,SCREEN_WIDTH-60,20,"Click OK to exit to Main Menu");
	gui3->MakeButton(150,270,SCREEN_WIDTH-300,"OK");
	int okButton=gui3->GetButtonID(150,270,SCREEN_WIDTH-300,"OK");
	int done=0;
	int getx, gety;
	int click=-1;
	while(!done)
	{
		Slock();
		while (SDL_PollEvent(&event))
		{
			// an event was found
			switch (event.type) {
			// close button clicked
			case SDL_QUIT:
				delete gui3;
				SDL_FreeSurface(bg);
				engine->Quit();
				break;

			// handle the keyboard
			case SDL_KEYDOWN:
				switch (event.key.keysym.sym) {
				case SDLK_RETURN:
					done = 1;
					break;
				default:
					break;
				}
				break;
			case SDL_MOUSEMOTION:
				SDL_GetMouseState(&getx, &gety);
				gui3->SendMouseMotion(getx,gety);
				break;
			case SDL_MOUSEBUTTONDOWN:
				SDL_GetMouseState(&getx, &gety);
				click = gui3->SendMouseClick(getx, gety);
				break;
			case SDL_MOUSEBUTTONUP:
				SDL_GetMouseState(&getx, &gety);
				if (click!=-1 && click==gui3->SendMouseClick(getx, gety))
				{
					if (click==okButton)
					{
						SDL_FillRect(screen,0,0);
						DrawIMG(bg,0,0);
						gui3->Draw();
						Sulock();
						SDL_Flip(screen);
						SDL_Delay(100);
						done=1;
					}
				}
				break;
			default:
				break;
			}
		}
		SDL_FillRect(screen,0,0);
		DrawIMG(bg,0,0);
		gui3->Draw();
		Sulock();
		SDL_Flip(screen);
	}
	delete gui3;
	SDL_FreeSurface(bg);
	return done;
}
GUI *MakeCenterOKWindow(std::string &message)
{
	SDL_Surface *screen=SDL_GetVideoSurface();
	GUI *gui=new GUI;
	gui->Init();
	gui->MakeWindow(120,170,screen->w-240,170);
	std::istringstream stream(message);
	std::string line;
	int i=0;
	while(std::getline(stream, line))
	{
		if (line[0]=='\t' || line[0]=='\r') line.erase(0,1);
		if (line[line.size()-1]=='\t' || line[line.size()-1]=='\r') line.erase(line.size()-1);
		gui->MakeStatic(30,176+(i*12),screen->w-60,20,line);
		i++;
	}
	gui->MakeButton(150,300,screen->w-300,"Go!");
	return gui;
}

