/*
Solar Conquest
Copyright (C) 2006 Greg Beaman

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "MenuItem.h"
#include "Button.h"
#include "TextBox.h"
#include "ListBox.h"
#include "TextItem.h"
#include "BarChart.h"
#include "BigTextItem.h"

class CMenuScreen
{
public:
	int menuResult;

	void (*menuFunction)(void);

	CMenuItem* firstItem;
	CMenuItem* selectedItem;

	CMenuScreen();
	~CMenuScreen();

	void MouseClick(int mx, int my);

	void DrawMenuScreen();
	void DrawMenuScreenText();

	CButton* AddButton(int id, int x, int y, char* txt);
	CTextBox* AddTextBox(int id, int x, int y, int w);
	CListBox* AddListBox(int id, int x, int y, int w, int h);
	CTextItem* AddText(int id, int x, int y, char* txt);
	CBarChart* AddBarChart(int id, int x, int y, int w, int h);
	CBigTextItem* AddBigText(int id, int x, int y, char* txt);

	CMenuItem* FindItem(int id);
};

CMenuScreen::CMenuScreen()
{
	firstItem = NULL;
	selectedItem = NULL;

	menuFunction = NULL;

	menuResult = 0;
}

CMenuScreen::~CMenuScreen()
{
	CMenuItem* curItem;
	CMenuItem* tempItem;

	curItem = firstItem;
	while (curItem != NULL)
	{
		tempItem = curItem->nextItem;
		delete curItem;
		curItem = tempItem;
	}
}

void CMenuScreen::MouseClick(int mx, int my)
{
	int x,y,x2,y2;

	CMenuItem* curItem;
	curItem = firstItem;
	while (curItem != NULL)
	{
		x = curItem->GetX();
		y = curItem->GetY();
		x2 = curItem->GetWidth() + x;
		y2 = curItem->GetHeight() + y;

		if (mx >= x && mx <= x2 && my >= y && my <= y2)
		{
			if (curItem->itemType == MENU_ITEM_SCREENBUTTON)
				menuResult = curItem->GetId();
			else if (curItem->itemType == MENU_ITEM_TEXTBOX)
				selectedItem = curItem;
			curItem = NULL;
		}
		else
			curItem = curItem->nextItem;
	}
}

void CMenuScreen::DrawMenuScreen()
{
	CMenuItem* curItem;

	//Draw the items
	curItem = firstItem;
	while (curItem != NULL)
	{
		if (selectedItem == curItem)
			curItem->DrawItem(true);
		else
			curItem->DrawItem(false);
		curItem = curItem->nextItem;
	}
}

void CMenuScreen::DrawMenuScreenText()
{
	if (g_MainFont)
		g_MainFont->Enable();

	CMenuItem* curItem;

	//Draw the items
	curItem = firstItem;
	while (curItem != NULL)
	{
		if (selectedItem == curItem)
			curItem->DrawItemText(true);
		else
			curItem->DrawItemText(false);
		curItem = curItem->nextItem;
	}

	if (g_MainFont)
		g_MainFont->Disable();
}

CMenuItem* CMenuScreen::FindItem(int id)
{
	CMenuItem* item;
	item = firstItem;
	while (item)
	{
		if (item->GetId() == id)
			return item;
		item = item->nextItem;
	}
	return NULL;
}

CButton* CMenuScreen::AddButton(int id, int x, int y, char* txt)
{
	CMenuItem* curItem;

	int w,h;
	w = strlen(txt) * (FONT_SIZE_X + FONT_SPACING) + 12;
	h = FONT_SIZE_Y + 12;

	if (firstItem == NULL)
	{
		firstItem = new CButton(id,x,y,w,h);
		firstItem->SetText(txt);
		return (CButton*)firstItem;
	}
	else
	{
		curItem = firstItem;
		while (curItem->nextItem != NULL)
		{
			curItem = curItem->nextItem;
		}
		curItem->nextItem = new CButton(id,x,y,w,h);
		curItem->nextItem->SetText(txt);
		return (CButton*)curItem->nextItem;
	}
	return NULL;
}

CTextBox* CMenuScreen::AddTextBox(int id, int x, int y, int w)
{
	CMenuItem* curItem;
	int h = FONT_SIZE_Y + 12;

	if (firstItem == NULL)
	{
		firstItem = new CTextBox(id,x,y,w,h);
		return (CTextBox*)firstItem;
	}
	else
	{
		curItem = firstItem;
		while (curItem->nextItem != NULL)
		{
			curItem = curItem->nextItem;
		}
		curItem->nextItem = new CTextBox(id,x,y,w,h);
		return (CTextBox*)curItem->nextItem;
	}
	return NULL;
}

CListBox* CMenuScreen::AddListBox(int id, int x, int y, int w, int h)
{
	CMenuItem* curItem;

	if (firstItem == NULL)
	{
		firstItem = new CListBox(id,x,y,w,h);
		return (CListBox*)firstItem;
	}
	else
	{
		curItem = firstItem;
		while (curItem->nextItem != NULL)
		{
			curItem = curItem->nextItem;
		}
		curItem->nextItem = new CListBox(id,x,y,w,h);
		return (CListBox*)curItem->nextItem;
	}
	return NULL;
}

CTextItem* CMenuScreen::AddText(int id, int x, int y, char* txt)
{
	CMenuItem* curItem;

	if (firstItem == NULL)
	{
		firstItem = new CTextItem(id,x,y);
		firstItem->SetText(txt);
		return (CTextItem*)firstItem;
	}
	else
	{
		curItem = firstItem;
		while (curItem->nextItem != NULL)
		{
			curItem = curItem->nextItem;
		}
		curItem->nextItem = new CTextItem(id,x,y);
		curItem->nextItem->SetText(txt);
		return (CTextItem*)curItem->nextItem;
	}
	return NULL;
}

CBarChart* CMenuScreen::AddBarChart(int id, int x, int y, int w, int h)
{
	CMenuItem* curItem;

	if (firstItem == NULL)
	{
		firstItem = new CBarChart(id,x,y,w,h);
		return (CBarChart*)firstItem;
	}
	else
	{
		curItem = firstItem;
		while (curItem->nextItem != NULL)
		{
			curItem = curItem->nextItem;
		}
		curItem->nextItem = new CBarChart(id,x,y,w,h);
		return (CBarChart*)curItem->nextItem;
	}
	return NULL;
}

CBigTextItem* CMenuScreen::AddBigText(int id, int x, int y, char* txt)
{
	CMenuItem* curItem;

	if (firstItem == NULL)
	{
		firstItem = new CBigTextItem(id,x,y);
		firstItem->SetText(txt);
		return (CBigTextItem*)firstItem;
	}
	else
	{
		curItem = firstItem;
		while (curItem->nextItem != NULL)
		{
			curItem = curItem->nextItem;
		}
		curItem->nextItem = new CBigTextItem(id,x,y);
		curItem->nextItem->SetText(txt);
		return (CBigTextItem*)curItem->nextItem;
	}
	return NULL;
}

CMenuScreen* g_curMenuScreen = NULL;
CMenuScreen* g_respawnScreen = NULL;
CMenuScreen* g_escapeScreen = NULL;
CMenuScreen* g_actionInfoScreen = NULL;
CMenuScreen* g_empireInfoScreen = NULL;
CMenuScreen* g_mainMenuScreen = NULL;
CMenuScreen* g_difficultyMenuScreen = NULL;
CMenuScreen* g_optionsMenuScreen = NULL;
CMenuScreen* g_planetControlScreen = NULL;
CMenuScreen* g_skirmishScreen = NULL;
CMenuScreen* g_introScreen = NULL;
CMenuScreen* g_victoryScreen = NULL;
CMenuScreen* g_defeatScreen = NULL;
CMenuScreen* g_loadLevelScreen = NULL;

int g_introState = 0;
int g_menuNum = -1;

void DestroyScreens()
{
	g_curMenuScreen = NULL;

	if (g_respawnScreen)
		delete g_respawnScreen;
	g_respawnScreen = NULL;

	if (g_escapeScreen)
		delete g_escapeScreen;
	g_escapeScreen = NULL;

	if (g_actionInfoScreen)
		delete g_actionInfoScreen;
	g_actionInfoScreen = NULL;

	if (g_empireInfoScreen)
		delete g_empireInfoScreen;
	g_empireInfoScreen = NULL;

	if (g_mainMenuScreen)
		delete g_mainMenuScreen;
	g_mainMenuScreen = NULL;

	if (g_difficultyMenuScreen)
		delete g_difficultyMenuScreen;
	g_difficultyMenuScreen = NULL;

	if (g_optionsMenuScreen)
		delete g_optionsMenuScreen;
	g_optionsMenuScreen = NULL;

	if (g_planetControlScreen)
		delete g_planetControlScreen;
	g_planetControlScreen = NULL;

	if (g_skirmishScreen)
		delete g_skirmishScreen;
	g_skirmishScreen = NULL;

	if (g_introScreen)
		delete g_introScreen;
	g_introScreen = NULL;

	if (g_victoryScreen)
		delete g_victoryScreen;
	g_victoryScreen = NULL;

	if (g_defeatScreen)
		delete g_defeatScreen;
	g_defeatScreen = NULL;
	
	if (g_loadLevelScreen)
		delete g_loadLevelScreen;
	g_loadLevelScreen = NULL;
}

void LoadBreifing(int level)
{
	if (!g_introScreen)
		return;
		
	CMenuItem* item = g_introScreen->FindItem(2);
	if (item)
	{
		/*
		Mission 1: Enlistment\n\n
		Our situation is extremely dire. Reinforcements from AeonTech and the Earth Space Force\n
		will not arrive until a few months. We have to keep Moretti's forces at bay until then.\n
		\n\n
		You will be piloting the ESF-R4 Advance Fighter ships. They are armed with two plasma\n
		guns and are remote controlled. You will be able to use as many as you wish during your\n
		missions, as they can quickly be reproduced by our carriers. However, these fighters are\n
		only trusted to human pilots, as these fighters can cause too much damage in the event of\n
		a software glitch.\n
		\n\n
		Since you are new, you will be sent to clear small field of turret stations. You will\n
		be escorted by a carrier of ESF-R2 Fighters. These fighters are AI operated, and the\n
		carrier will produce more of them every so often, along with a repair ship.\n
		\n\n
		Destroy all of the turret stations to win. You can destroy them by shooting the block\n
		in the center of the station. In normal situations, losing all of your carriers will cost\n
		you the mission, but the enemy does not have any offensive units here.
		*/
		if (level == 1)
		{
			item->SetText("Mission 1: Enlistment\n\nOur situation is extremely dire. Reinforcements from AeonTech and the Earth Space Force\nwill not arrive until a few months. We have to keep Moretti's forces at bay until then.\n\n\nYou will be piloting the ESF-R4 Advance Fighter ships. They are armed with two plasma\nguns and are remote controlled. You will be able to use as many as you wish during your\nmissions, as they can quickly be reproduced by our carriers. However, these fighters are\nonly trusted to human pilots, as these fighters can cause too much damage in the event of\na software glitch.\n\n\nSince you are new, you will be sent to clear small field of turret stations. You will\nbe escorted by a carrier of ESF-R2 Fighters. These fighters are AI operated, and the\ncarrier will produce more of them every so often, along with a repair ship.\n\n\nDestroy all of the turret stations to win. You can destroy them by shooting the block\nin the center of the station. In normal situations, losing all of your carriers will cost\nyou the mission, but the enemy does not have any offensive units here.");
		}
		/*
		Mission 2: Violent Minority\n\n
		The rebellion has little manpower. To give ourselves an edge, we will have to take one of\n
		the new space stations. This will require a series of swift strikes across the Morettian\n
		ships in the area.\n
		\n\n
		Your strike force will attack one of the nearby bomber squadrons. You will be assisted by\n
		pilots Ashley and Rodney. Both pilots have their own fighter carriers. We can not spare\n
		any extra carriers, so you will have to make do with the available forces.\n
		\n\n
		Similar to the previous mission, you will have to destroy all enemy carriers by destroying\n
		the block in the center of the ship. The block is a generator which powers the entire ship\n
		or station it is on, and helps produce ammo or even other ships.\n
		\n\n
		Good luck.
		*/
		else if (level == 2)
		{
			item->SetText("Mission 2: Violent Minority\n\nThe rebellion has little manpower. To give ourselves an edge, we will have to take one of\nthe new space stations. This will require a series of swift strikes across the Morettian\nships in the area.\n\n\nYour strike force will attack one of the nearby bomber squadrons. You will be assisted by\npilots Ashley and Rodney. Both pilots have their own fighter carriers. We can not spare\nany extra carriers, so you will have to make do with the available forces.\n\n\nSimilar to the previous mission, you will have to destroy all enemy carriers by destroying\nthe block in the center of the ship. The block is a generator which powers the entire ship\nor station it is on, and helps produce ammo or even other ships.\n\n\nGood luck.");
		}
		/*
		Mission 3: Life of a Vulture\n\n
		The space station has been captured, but the defenses were not finished. As a result, we\n
		must finish construction ourselves, but this will require many resources. We can get these\n
		resources by scavenging debris from destroyed carriers. The recent battles met most of the\n
		requirements, but we still need about 5 carriers worth of resources.\n
		\n\n
		Admiral Moretti is preparing dozens of carriers to launch a counterstrike, before\n\n
		construction is finished. You and Rodney will be sent in with several carriers to eliminate\n
		some of the carriers heading to the rendezvous point. Destroy everything, the wreckage crews\n
		can take care of the scavenging. There should be enough carriers to finish the construction.\n
		\n\n
		Time is of the essence. With Moretti's power, the space station will not hold without those\n
		supplies. Good luck.\n
		*/
		else if (level == 3)
		{
			item->SetText("Mission 3: Life of a Vulture\n\nThe space station has been captured, but the defenses were not finished. As a result, we\nmust finish construction ourselves, but this will require many resources. We can get these\nresources by scavenging debris from destroyed carriers. The recent battles met most of the\nrequirements, but we still need about 5 carriers worth of resources.\n\n\nAdmiral Moretti is preparing dozens of carriers to launch a counterstrike, before\nconstruction is finished. You and Rodney will be sent in with several carriers to eliminate\nsome of the carriers heading to the rendezvous point. Destroy everything, the wreckage crews\ncan take care of the scavenging. There should be enough carriers to finish the construction.\n\n\nTime is of the essence. With Moretti's power, the space station will not hold without those\nsupplies. Good luck.\n");
		}
		/*
		Mission 4: Dance of Death\n\n
		Admiral Moretti is launching a counterstrike with the intent of destroying the space station.\n
		The defenses are finished, but they are far from enough to hold off the assault. The situation\n
		is dire enough to justify modifications to the factories on the space station. The station will\n
		now produce AI operated Advance Fighters.  In addition to that, every rebel carrier has been\n
		called to the defense of the station.\n
		\n\n
		The station must survive the attack. If the station is lost, we have no hope of surviving until\n
		reinforcements arrive.\n
		*/
		else if (level == 4)
		{
			item->SetText("Mission 4: Dance of Death\n\nAdmiral Moretti is launching a counterstrike with the intent of destroying the space station.\nThe defenses are finished, but they are far from enough to hold off the assault. The situation\nis dire enough to justify modifications to the factories on the space station. The station will\n	now produce AI operated Advance Fighters.  In addition to that, every rebel carrier has been\ncalled to the defense of the station.\n\n\nThe station must survive the attack. If the station is lost, we have no hope of surviving until\nreinforcements arrive.\n");
		}
		/*
		Mission 5: Bit the hand that fed you
		\n\n
		The remarkable display of power shown by the Advance Fighters has caused some worry to\n
		spread among both rebel and Morettian forces. At the same time, it has caused a great\n
		divide in Moretti's empire.\n
		\n\n
		Many more people are joining up with the rebellion, and have hijacked many of Moretti's\n
		carriers to assist our cause. However, too many of the original crew see the rebellion's\n
		actions as fanatical and careless. This has actually thrown people to support the\n
		Admrial's cause and pick up arms against us.\n
		\n\n
		The Admrial himself seems to be in a slight state of panic. He has pushed the research\n
		of a new weapon into field testing, mounting it on some of the ESF-R2 Fighters. In\n
		addition to that, he has loaded the ESF-R1 Advance Bomber plans on some carriers.\n
		\n\n
		We must grab some of Admrial Moretti's new technology, to level the playing field. You\n
		will be sent with both Rodney and Ashley to ambush a group of carriers testing the new\n
		tech. Our carriers will be outfitted to produce Advance Fighters. Good luck.
		*/
		else if (level == 5)
		{
			item->SetText("Mission 5: Bit the hand that fed you\n\nThe remarkable display of power shown by the Advance Fighters has caused some worry to\nspread among both rebel and Morettian forces. At the same time, it has caused a great\ndivide in Moretti's empire.\n\n\nMany more people are joining up with the rebellion, and have hijacked many of Moretti's\ncarriers to assist our cause. However, too many of the original crew see the rebellion's\nactions as fanatical and careless. This has actually thrown people to support the\nAdmrial's cause and pick up arms against us.\n\n\nThe Admrial himself seems to be in a slight state of panic. He has pushed the research\nof a new weapon into field testing, mounting it on some of the ESF-R2 Fighters. In\naddition to that, he has loaded the ESF-R1 Advance Bomber plans on some carriers.\n\n\nWe must grab some of Admrial Moretti's new technology, to level the playing field. You\nwill be sent with both Rodney and Ashley to ambush a group of carriers testing the new\ntech. Our carriers will be outfitted to produce Advance Fighters. Good luck.");
		}
	}
}

void UpdateEscapeMenu()
{
	if (g_escapeScreen->menuResult != 0)
	{
		int result = g_escapeScreen->menuResult;
		if (result == 1)
		{
			//Return to game
			g_curMenuScreen = NULL;
		}
		else if (result == 2)
		{
			//Info
			if (g_curScreen == GAME_SCREEN_EMPIRE)
				g_curMenuScreen = g_empireInfoScreen;
			else if (g_curScreen == GAME_SCREEN_ACTION)
				g_curMenuScreen = g_actionInfoScreen;
		}
		else if (result == 3)
		{
			//Options
			g_curMenuScreen = g_optionsMenuScreen;
		}
		else if (result == 4)
		{
			//Main Menu
			if (g_curScreen == GAME_SCREEN_ACTION)
			{
				DeleteAllObjects();
				DeleteAllFactories();
				DeleteAllProjectiles();
				
				if (g_particles)
					g_particles->DeleteAllParticles();
				if (g_Map)
					g_Map->ResetMap();
			}
			g_curScreen = GAME_SCREEN_MENU;
			g_curMenuScreen = g_mainMenuScreen;
		}
		else if (result == 5)
		{
			//Exit
			progRunning = false;
		}

		if (!g_curMenuScreen)
			paused = false;
		g_escapeScreen->menuResult = 0;
	}
}

void UpdateMainMenu()
{
	if (g_mainMenuScreen->menuResult != 0)
	{
		int result = g_mainMenuScreen->menuResult;
		if (result == 1)
		{
			//New game
			if (g_introScreen)
			{
				g_level = 1;
				g_introState = 5;
				LoadBreifing(g_level);
				g_curMenuScreen = g_introScreen;
			}
			else { printf("ERROR: MENU SCREEN MISSING! CAN NOT START NEW GAME!\n"); }
		}
		else if (result == 2)
		{
			//Load Level
			g_curMenuScreen = g_loadLevelScreen;
		}
		else if (result == 3)
		{
			//Options
			g_curMenuScreen = g_optionsMenuScreen;
		}
		else if (result == 4)
		{
			//Exit
			progRunning = false;
		}
		else if (result == 5)
		{
			//Skirmish
			g_curMenuScreen = g_skirmishScreen;
		}

		if (!g_curMenuScreen)
			paused = false;
		g_mainMenuScreen->menuResult = 0;
	}
}

void UpdateDifficultyMenu()
{
	if (g_difficultyMenuScreen->menuResult != 0)
	{
		int result = g_difficultyMenuScreen->menuResult;
		if (result == 5)
		{
			//Exit to menu
			g_curMenuScreen = g_mainMenuScreen;
		}
		else
		{
//			BuildSolarSystem(result-1);
			cameraPos = CreateVector(0,0);
//			g_curScreen = GAME_SCREEN_EMPIRE;
			g_level = 1;
			ActionScreen_Start();
			g_curMenuScreen = NULL;
		}

		if (!g_curMenuScreen)
			paused = false;
		g_difficultyMenuScreen->menuResult = 0;
	}
}

void UpdateOptionsMenuText()
{
/*	CTextItem* text = (CTextItem*)g_optionsMenuScreen->FindItem(100);
	if (text)
	{
		if (g_resolution == 0)
			text->SetText("640x480x16");
		else if (g_resolution == 1)
			text->SetText("800x600x16");
		else if (g_resolution == 2)
			text->SetText("1024x768x16");
		else if (g_resolution == 3)
			text->SetText("640x480x32");
		else if (g_resolution == 4)
			text->SetText("800x600x32");
		else if (g_resolution == 5)
			text->SetText("1024x768x32");
	}
*/
	CTextItem* text;
	text = (CTextItem*)g_optionsMenuScreen->FindItem(101);
	if (text)
	{
		if (g_fullscreen)
			text->SetText("Full Screen");
		else
			text->SetText("Windowed");
	}
	text = (CTextItem*)g_optionsMenuScreen->FindItem(102);
	if (text)
	{
		if (g_soundEnabled)
			text->SetText("Sound: Enabled");
		else
			text->SetText("Sound: Disabled");
	}
	text = (CTextItem*)g_optionsMenuScreen->FindItem(103);
	if (text)
	{
		char txt[MAX_STRING_SIZE];
		char num[MAX_STRING_SIZE];
		gcvt(g_gameSpeed,2,num);
		strcpy(txt,"Speed: ");
		strncat(txt,num,3);
		text->SetText(txt);
	}
}

void UpdateOptionsMenu()
{
	if (g_optionsMenuScreen->menuResult != 0)
	{
		int result = g_optionsMenuScreen->menuResult;
/*		if (result == 1)
		{
			//Decrease resolution
			g_resolution--;
			if (g_resolution < 0)
				g_resolution = 5;
			CTextItem* text = (CTextItem*)g_optionsMenuScreen->FindItem(100);
			if (text)
			{
				if (g_resolution == 0)
					text->SetText("640x480x16");
				else if (g_resolution == 1)
					text->SetText("800x600x16");
				else if (g_resolution == 2)
					text->SetText("1024x768x16");
				else if (g_resolution == 3)
					text->SetText("640x480x32");
				else if (g_resolution == 4)
					text->SetText("800x600x32");
				else if (g_resolution == 5)
					text->SetText("1024x768x32");
			}
		}
		else if (result == 2)
		{
			//Increase resolution
			g_resolution++;
			if (g_resolution > 7)
				g_resolution = 0;
			CTextItem* text = (CTextItem*)g_optionsMenuScreen->FindItem(100);
			if (text)
			{
				if (g_resolution == 0)
					text->SetText("640x480x16");
				else if (g_resolution == 1)
					text->SetText("800x600x16");
				else if (g_resolution == 2)
					text->SetText("1024x768x16");
				else if (g_resolution == 3)
					text->SetText("1280x1024x16");
				else if (g_resolution == 4)
					text->SetText("640x480x32");
				else if (g_resolution == 5)
					text->SetText("800x600x32");
				else if (g_resolution == 6)
					text->SetText("1024x768x32");
				else if (g_resolution == 7)
					text->SetText("1280x1024x32");
			}
		}
*/		if (result == 3 || result == 4)
		{
			//Toggle Windowed Mode
			if (g_fullscreen)
				g_fullscreen = false;
			else
				g_fullscreen = true;

			CTextItem* text = (CTextItem*)g_optionsMenuScreen->FindItem(101);
			if (text)
			{
				if (g_fullscreen)
					text->SetText("Full Screen");
				else
					text->SetText("Windowed");
			}
		}
		else if (result == 5 || result == 6)
		{
			//Toggle Sound
			if (g_soundEnabled)
				g_soundEnabled = false;
			else
				g_soundEnabled = true;

			CTextItem* text = (CTextItem*)g_optionsMenuScreen->FindItem(102);
			if (text)
			{
				if (g_soundEnabled)
					text->SetText("Sound: Enabled");
				else
					text->SetText("Sound: Disabled");
			}
		}
		else if (result == 7)
		{
			//Decrease game speed
			g_gameSpeed -= 0.1;
			if (g_gameSpeed < 0.1)
				g_gameSpeed = 0.1;

			char txt[MAX_STRING_SIZE];
			char num[MAX_STRING_SIZE];
			gcvt(g_gameSpeed,2,num);
			strcpy(txt,"Speed: ");
			strncat(txt,num,3);

			CTextItem* text = (CTextItem*)g_optionsMenuScreen->FindItem(103);
			if (text)
				text->SetText(txt);
		}
		else if (result == 8)
		{
			//Increase game speed
			g_gameSpeed += 0.1;
			if (g_gameSpeed > 2.0)
				g_gameSpeed = 2.0;

			char txt[MAX_STRING_SIZE];
			char num[MAX_STRING_SIZE];
			gcvt(g_gameSpeed,2,num);
			strcpy(txt,"Speed: ");
			strncat(txt,num,3);

			CTextItem* text = (CTextItem*)g_optionsMenuScreen->FindItem(103);
			if (text)
				text->SetText(txt);
		}
		else if (result == 9)
		{
			//Exit options
			if (g_curScreen == GAME_SCREEN_MENU)
				g_curMenuScreen = g_mainMenuScreen;
			else
				g_curMenuScreen = g_escapeScreen;

			CMenuItem* item;
			item = g_optionsMenuScreen->FindItem(1);
			if (item)
				g_screenWidth = atoi(item->caption);
			item = g_optionsMenuScreen->FindItem(2);
			if (item)
				g_screenHeight = atoi(item->caption);
			item = g_optionsMenuScreen->FindItem(100);
			if (item)
				g_screenDepth = atoi(item->caption);

			g_updateResolution = true;
			SaveGameSettings();
		}

		if (!g_curMenuScreen)
			paused = false;
		g_optionsMenuScreen->menuResult = 0;
	}
}

void UpdateInfoMenu()
{
	if (g_curMenuScreen->menuResult != 0)
	{
		int result = g_curMenuScreen->menuResult;
		g_curMenuScreen->menuResult = 0;
		if (result == 1)
		{
			//Exit to escape screen
			g_curMenuScreen = g_escapeScreen;
		}

		if (!g_curMenuScreen)
			paused = false;
	}
}

void UpdateIntroScreen()
{
	if (g_introScreen->menuResult != 0)
	{
		int result = g_introScreen->menuResult;
		if (result == 1)
		{
			g_introState++;

			if (g_introState == 2)
			{
				//Done with intro, goto main menu
				g_curMenuScreen->menuResult = 0;
				g_curMenuScreen = g_mainMenuScreen;
				return;
			}
			if (g_introState == 6)
			{
				//Start mission
				g_curMenuScreen->menuResult = 0;
				g_curMenuScreen = NULL;
				paused = false;
				LoadLevel(g_level);
				return;
			}
/*
			CTextItem* text = (CTextItem*)g_introScreen->FindItem(2);
			if (text)
			{
				if (g_introState == 1)
				{
					text->SetText("By the year 2053, AeonTech was finally ready to start the voyage. A crew of 500 was selected to travel with a fleet of A.I. operated ships. The trip would take them all to a recently discovered solar system, well beyond colonized space. Years of research begain to pay off, as people's spirits soared and the crew left Earth.\n\nNear the beginning of the year 2054, the crew reached the solar system. Immediately, they started probing the planets and sending snapshots of the system back to Earth. The scientists quickly discovered new materials that could produce superior equipment.\n\nThat was when everything turned around. Admiral Moretti, head of the entire fleet, slowly started producing militaristic machines, and prepared to start his own empire. In a few months, he ");
				}
			}
*/
		}
	}
}

void UpdateSkirmishScreen()
{
	if (g_skirmishScreen->menuResult != 0)
	{
		int result = g_skirmishScreen->menuResult;
		if (result == 9) //Exit Skirmish Screen
			g_curMenuScreen = g_mainMenuScreen;
		else if (result == 10) //Start skirmish round
		{
			CMenuItem* text;
			
			text = g_skirmishScreen->FindItem(1);
			if (text)
				g_alliedFighterCarriers = atoi(text->caption);
			text = g_skirmishScreen->FindItem(2);
			if (text)
				g_alliedBomberCarriers = atoi(text->caption);
			text = g_skirmishScreen->FindItem(3);
			if (text)
				g_enemyFighterCarriers = atoi(text->caption);
			text = g_skirmishScreen->FindItem(4);
			if (text)
				g_enemyBomberCarriers = atoi(text->caption);

			g_level = -1;
			ActionScreen_Start();
			g_curMenuScreen = NULL;
		}

		if (!g_curMenuScreen)
			paused = false;
		g_skirmishScreen->menuResult = 0;
	}
}

void UpdateLoadLevelScreen()
{
	if (g_loadLevelScreen->menuResult != 0)
	{
		int result = g_loadLevelScreen->menuResult;
		if (result == 100) //Exit Load Level Screen
			g_curMenuScreen = g_mainMenuScreen;
		else //Load Level
		{
			g_level = result;
			g_introState = 5;
			LoadBreifing(g_level);
			g_curMenuScreen = g_introScreen;
		}

		if (!g_curMenuScreen)
			paused = false;
		g_loadLevelScreen->menuResult = 0;
	}
}
