#include "Gos.pch"
#include "Graphics.h"
#include "d3dx8tex.h"

GosSurface*	(*GosSurface::CreateEmpty)( UINT, UINT, D3DFORMAT ) = Surface::CreateEmpty;
GosSurface*	(*GosSurface::CreateEmptyDepthStencil)( UINT, UINT, D3DFORMAT, D3DMULTISAMPLE_TYPE ) = Surface::CreateEmptyDepthStencil;
GosSurface* (*GosSurface::CreateRenderTarget)( UINT, UINT, D3DFORMAT, D3DMULTISAMPLE_TYPE, BOOL ) = Surface::CreateRenderTarget;

GosSurface* Surface::CreateEmpty( UINT Width, UINT Height, D3DFORMAT Format )
{
	LOG_API("Surface::CreateEmpty");
	IDirect3DSurface8 *pD3DSurface;
	HRESULT rc = Graphics::D3DDevice()->CreateImageSurface( Width, Height, Format, &pD3DSurface );
	if( FAILED(rc) )
		STOP(("Failed to create D3D surface"));
	return (GosSurface *)(new Surface(pD3DSurface));
}

GosSurface* Surface::CreateEmptyDepthStencil( UINT Width, UINT Height, D3DFORMAT Format, D3DMULTISAMPLE_TYPE MultiSample )
{
	LOG_API("Surface::CreateEmptyDepthStencil");
	IDirect3DSurface8 *pD3DSurface;
	HRESULT rc = Graphics::D3DDevice()->CreateDepthStencilSurface( Width, Height, Format, MultiSample, &pD3DSurface );
	if( FAILED(rc) )
		STOP(("Failed to create D3D Depth Stencil"));
	return (GosSurface *)(new Surface(pD3DSurface));
}

GosSurface* Surface::CreateRenderTarget( UINT Width, UINT Height, D3DFORMAT Format, D3DMULTISAMPLE_TYPE MultiSample, BOOL Lockable )
{
	LOG_API("Surface::CreateRenderTarget");
	IDirect3DSurface8 *pD3DSurface;
	HRESULT rc = Graphics::D3DDevice()->CreateRenderTarget( Width, Height, Format, MultiSample, Lockable, &pD3DSurface );
	if( FAILED(rc) )
		STOP(("Failed to create D3D Render Target"));
	return (GosSurface *)(new Surface(pD3DSurface));
}

HRESULT Surface::LoadFromFile( TCHAR *pName )
{
	LOG_API("Surface::LoadFromFile");
	HRESULT rc;
	rc = D3DXLoadSurfaceFromFile( m_pD3DSurface, NULL, NULL, pName, NULL, D3DX_FILTER_BOX, 0, NULL );
	CHECK_RC;
}

HRESULT	Surface::Lock( D3DLOCKED_RECT *pLockedRect, RECT *pRect )
{
	LOG_API("Surface::Lock");
	HRESULT rc;
	rc = m_pD3DSurface->LockRect( pLockedRect, pRect, 0 );
	CHECK_RC;
}

HRESULT Surface::Unlock()
{
	LOG_API("Surface::Unlock");
	HRESULT rc;
	rc = m_pD3DSurface->UnlockRect();
	CHECK_RC;
}

HRESULT Surface::Desc( D3DSURFACE_DESC *pDesc )
{
    LOG_API("Surface::Desc");
    HRESULT rc;
    rc = m_pD3DSurface->GetDesc( pDesc );
    CHECK_RC;
}

Surface::Surface( IDirect3DSurface8 *pD3DSurface )
{
	m_pD3DSurface = pD3DSurface;
}

Surface::~Surface()
{
	LOG_API("Surface::~Surface");
	if( m_pD3DSurface )
		m_pD3DSurface->Release();
}

GosSurface::~GosSurface()
{
	;
}




