#include "Gos.pch"
#include "Gos1Legacy.h"

Gos1Environment Environment;
Gos1SurfaceDesc BackBufferddsd;

char* Gos1DescribeKey( DWORD Key )
{
	_asm int 3;
	return "Key Description";
}

DWORD Gos1EnableSetting( Gos1Setting Setting, DWORD Value )
{
	_asm int 3;
	return 0;
}
void Gos1Video_GetResourceInfo( GosVideo *pVideo, Gos1Video_ResourceInfo* gvi )
{
	_asm int 3;
}
void Gos1Video_Command( GosVideo *pVideo, enum Gos1Video_Command vc, float x, float y )
{
	_asm int 3;
}
void Gos1Video_SetPlayMode( GosVideo *pVideo, enum Gos1Video_PlayMode gvpm )
{
	_asm int 3;
}
void Gos1Audio_GetResourceInfo( GosSound *pSound, Gos1Audio_ResourceInfo* sri )
{
	_asm int 3;
}
void Gos1Audio_AllocateChannelSliders( int Channel, DWORD properties)
{
	_asm int 3;
}
void Gos1Lock( GosTexture *pTexture, LPRECT lpDestRect, Gos1SurfaceDesc *pDesc, DWORD dwFlags, HANDLE hEvent )
{
	_asm int 3;
}



GosFont3D * FontHandle;
Gos1TLVertex FontVertices[128];
DWORD FontIndex;
DWORD FontClipping;
DWORD FontColor;
bool FontStartOfLine;
bool FontDisableCodes;
float FontSize;
bool OddSize;
bool FontWordWrap;
DWORD FontWrapType;
bool FontProportional;
bool FontBold;
bool FontItalic;
int FontX,FontY;
float FontMinX,FontMinY,FontMaxX,FontMaxY;
bool FontNoCache;
bool DontFastPath;						// Set to 1 to disable 'fast paths'
char* ErrorTitle;
__int64 TimeScriptsCallbacks=0;
//
// 0=Normal character, 1=DBCS lead byte in current locale
//
BYTE LeadByteTable[256];
DWORD	ModalScripts=0;			// ModalScripts
DWORD gCurrentHeapStackLevel = 0;


void* DecodeBMP( const char* FileName, BYTE* Data, DWORD DataSize, DWORD* TextureWidth, DWORD* TextureHeight, bool TextureLoad )
{
	_asm int 3;
	return (void *)NULL;
}
void* DecodeJPG( const char* FileName, BYTE* Data, DWORD DataSize, DWORD* TextureWidth, DWORD* TextureHeight, bool TextureLoad )
{
	_asm int 3;
	return (void *)NULL;
}
void* DecodePNG( const char* FileName, BYTE* Data, DWORD DataSize, DWORD* TextureWidth, DWORD* TextureHeight, bool TextureLoad )
{
	_asm int 3;
	return (void *)NULL;
}
void* DecodeTGA( const char* FileName, BYTE* Data, DWORD DataSize, DWORD* TextureWidth, DWORD* TextureHeight, bool TextureLoad )
{
	_asm int 3;
	return (void *)NULL;
}
void HandleTags(char ** lplpMessage)
{
	;	// i.e. tags disabled
}

const int MAX_LINE_LENGTH=256;		// Maximum characters per line in a file (shown as error line)

//
// If the file exists, return a string containing the line given
// Used in _DEBUG mode to return line with exception or error
//
TCHAR* GetLineFromFile( TCHAR* tempLine, TCHAR* FileName, int LineNumber )
{
//
// If line number too large, ignore it
//
	if( LineNumber>65000 )
		return 0;
//
// Is the file name pointer valid? (NT 5 often is not)
//
	TCHAR* Test=FileName;
	do
	{
		if( IsBadReadPtr( Test++, 1 ) )
		{
			return 0;
		}

	} while( *Test!=0 );
//
// Filename cannot be correct if too long
//
	if( Test-FileName>MAX_PATH )
	{
		return 0;
	}
//
// Open File
//
	HANDLE hFile = CreateFile( FileName, GENERIC_READ, FILE_SHARE_READ,NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL );
	if( hFile==INVALID_HANDLE_VALUE )
	{
		return 0;
	}
//
// Read in whole file
//
	unsigned long dwBytesRead;
	int Length=GetFileSize( hFile, NULL );
	TCHAR* Buffer=(TCHAR*)malloc( Length*2 );
	ReadFile(hFile,(LPVOID)Buffer, Length, &dwBytesRead, NULL);  
    CloseHandle( hFile );
//
// Find line or end of file
//
	TCHAR*Source=Buffer;
	while( Source<(Buffer+Length) && LineNumber>1 )
	{
		while( *Source!=0xa && Source<(Buffer+Length) )
			Source++;
		Source++;
		LineNumber--;
	}
//
// Copy line to buffer and remove tabs
//
	TCHAR* Dest=tempLine;

	while( (*Source==' ' || *Source==9) && Source<(Buffer+Length))		// Skip leading spaces or tabs
		Source++;

	while( *Source!=0xa && *Source!=0xd && Source<(Buffer+Length))
	{
		if( *Source==9 )
		{
			*Dest++=' ';												// Copy line, making tabs single spaces
			Source++;
		}
		else
		{
			*Dest++=*Source++;
		}
		if( Dest > tempLine+MAX_LINE_LENGTH-4 )
			break;
	}
//
// Append newline
//
	*Dest++='\n';
	*Dest=0;
//
// Remove file and return
//
	free( Buffer );
	return tempLine;
}

GosHeap * ClientHeap;

DWORD CurrentFontTexture=-1;


void SetupFontRenderstates()
{
//
// Setup text font states - fastest performance, reading but not updating Z Buffer
//
/*
	if (FontHandle)
	{
		CurrentFontTexture=FontHandle->Texture[0];
		Gos3D::SetTexture( 0,(GosTexture *)( CurrentFontTexture ));
	}
*/
	{Gos3D::SetTextureStageState( 0, D3DTSS_MAGFILTER,D3DTEXF_NONE);Gos3D::SetTextureStageState( 0, D3DTSS_MINFILTER,D3DTEXF_NONE);Gos3D::SetTextureStageState( 0, D3DTSS_MIPFILTER,D3DTEXF_NONE);}
	Gos3D::SetRenderState( D3DRS_ZENABLE, D3DZB_FALSE);
	Gos3D::SetRenderState( D3DRS_ZWRITEENABLE,  0  );
	Gos3D::SetRenderState( D3DRS_ALPHATESTENABLE,  1  );
//	Gos3D::SetRenderState( D3DRS_TEXTUREPERSPECTIVE,  0  );
	Gos3D::SetRenderState( D3DRS_SPECULARENABLE,  0  );
	Gos3D::SetRenderState( D3DRS_DITHERENABLE,  0  );
	Gos3D::SetRenderState( D3DRS_CLIPPING, FALSE );
	Gos3D::SetRenderState( D3DRS_FILLMODE, ( 0 )?D3DFILL_WIREFRAME:D3DFILL_SOLID );
	Gos3D::SetRenderState( D3DRS_CULLMODE,  D3DCULL_NONE  );
	(void)0;
	Gos3D::SetRenderState( D3DRS_WRAP0, D3DWRAP_U | D3DWRAP_V );
	Gos3D::SetRenderState( D3DRS_SHADEMODE,  D3DSHADE_FLAT  );
	Gos3D::SetRenderState( D3DRS_FOGTABLEMODE, D3DFOG_NONE );
	Gos3D::SetRenderState( D3DRS_STENCILENABLE,  0  );
	if( 0xffffffff==FontColor )
	{
		{Gos3D::SetTextureStageState(0,D3DTSS_COLOROP,D3DTOP_SELECTARG1);Gos3D::SetTextureStageState(0,D3DTSS_ALPHAOP,D3DTOP_SELECTARG1);}
	}
	else
	{
		if( 0xff000000==(FontColor&0xff000000) )
			{Gos3D::SetTextureStageState(0,D3DTSS_COLOROP,D3DTOP_MODULATE);Gos3D::SetTextureStageState(0,D3DTSS_ALPHAOP,D3DTOP_SELECTARG1);}
		else
			{Gos3D::SetTextureStageState(0,D3DTSS_COLOROP,D3DTOP_MODULATE);Gos3D::SetTextureStageState(0,D3DTSS_ALPHAOP,D3DTOP_MODULATE);}
	}
//
// Alpha mode depends on vertex alpha - because of alpha test ONEZERO can be used for transparent text
//
	if( (FontColor&0xff000000)==0xff000000 /* && !FontHandle->Aliased */ )
		Gos3D::SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE);
	else
		{Gos3D::SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE);Gos3D::SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA );Gos3D::SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA );}
}

void __stdcall ExitGameOS()
{
	_asm int 3;
}