#include <string>
$Include "Common.bi"
$Include "GameState.bi"
$Include "ppLevel.bi"
$Include "ppAliens.bi"
$Include "ppPlayer.bi"
$Include "StateStart.bi"
$Include "StatePlay.bi"
$Include "StateEnd.bi"

Raw As EGameState m_eCurrentState = EGS_Start
Raw As CGameState PTR m_ppStates[EGS_StateCount] = {}
Raw As CLevel PTR m_pScenery = 0L
Raw As CAliens PTR m_pAliens = 0L
Raw As CPlayer PTR m_pPlayer = 0L

Sub AppBegin()
	agk::SetFolder("/media")
	agk::SetWindowTitle("Space Shooter")
	agk::SetWindowSize(agk::GetMaxDeviceWidth(),agk::GetMaxDeviceHeight(),0)
	agk::SetVirtualResolution(550,400)
	agk::SetOrientationAllowed(0,0,1,1)
	agk::SetScissor(0,0,0,0)
	agk::LoadImage(IMGID_PARTICLES, "space/particle.png")
	agk::LoadImage(IMGID_GAMEFONT, "space/ascii.png")
	agk::LoadImage(IMGID_ALIENSHIP, "space/alienshipleft.png")
	agk::LoadImage(IMGID_ALIENPARTICLE, "space/shrapnel3.png")
	agk::LoadImage(IMGID_BULLET, "space/laser.png")
	
	agk::LoadSound(SFXID_LASER, "space/laser.wav")
	agk::LoadSound(SFXID_EXPLOSION1, "space/explode1.wav")
	agk::LoadSound(SFXID_EXPLOSION2, "space/explode2.wav")
	agk::LoadSound(SFXID_EXPLOSION3, "space/explode3.wav")
	agk::LoadSound(SFXID_HIT, "space/playerhit.wav")
	
	agk::SetTextDefaultFontImage(IMGID_GAMEFONT)
	
	Raw As CStateStart PTR HandleStart = CREATE(CStateStart())
	Raw As CStatePlay PTR HandleGame = CREATE(CStatePlay())
	Raw As CStateEnd PTR HandleEnd = CREATE(CStateEnd())
	
	m_pScenery = CREATE(CLevel())
	m_pAliens = CREATE(CAliens())
	m_pPlayer = CREATE(CPlayer())
	
	m_pPlayer->SetReferences(m_pScenery, m_pAliens)
	m_pPlayer->HidePlayer()
	
	HandleStart->m_refAliens = m_pAliens
	
	HandleGame->m_refAliens = m_pAliens
	HandleGame->m_refPlayer = m_pPlayer
	
	HandleEnd->m_refAliens = m_pAliens
	
	m_ppStates[EGS_Start] = HandleStart
	m_ppStates[EGS_Play] = HandleGame
	m_ppStates[EGS_End] = HandleEnd
	
	m_ppStates[m_eCurrentState]->Reset()
End Sub

Function AppLoop()
	Raw As EGameState state = m_ppStates[m_eCurrentState]->Run(agk::GetFrameTime())
	If agk::GetRawKeyState(27) Then End
	If state <> m_eCurrentState Then
		m_ppStates[state]->Reset()
		m_eCurrentState = state
	EndIf
	
	agk::Sync()
	Function = 0
End Function

Sub AppEnd()
	For integer i = 0 to EGS_StateCount - 1
		If m_ppStates[i] <> NULL Then RELEASE(m_ppStates[i])
	Next
	
	RELEASE(m_pPlayer)
	RELEASE(m_pAliens)
	RELEASE(m_pScenery)
End Sub
