/* 
 * 
 * Confidential Information of Telekinesys Research Limited (t/a Havok).  Not for disclosure or distribution without Havok's
 * prior written consent.This software contains code, techniques and know-how which is confidential and proprietary to Havok.
 * Level 2 and Level 3 source code contains trade secrets of Havok. Havok Software (C) Copyright 1999-2008 Telekinesys Research Limited t/a Havok. All Rights Reserved. Use of this software is subject to the terms of an end user license agreement.
 * 
 */
#include <Demos/demos.h>
#include <Demos/DemoCommon/Utilities/Replay/DemoReplay.h>

#include <Common/Base/Reflection/hkClass.h>
#include <Common/Base/System/Io/IStream/hkIStream.h>
#include <Common/Base/System/Io/OStream/hkOStream.h>

hkDemoReplayUtility::hkDemoReplayUtility()
{
	start("replay/frame");
}

void hkDemoReplayUtility::start(const char* filename)
{
	m_currentFileName = filename;
	m_currentFrame = 0;
}


void hkDemoReplayUtility::recordFrame( hkDemoEnvironment* env )
{

	Frame f;
	f.m_pad = *( env->m_gamePad ); //pseudo pad

#ifdef HK_PLATFORM_WIN32
	f.m_keys = env->m_window->getKeyboard();
#endif

	//f.m_timestep = m_timestep;

	hkgCamera* cam = env->m_window->getViewport(0)->getCamera();
	hkgVec3Copy( f.m_camera.m_from, cam->getFromPtr() );
	hkgVec3Copy( f.m_camera.m_to, cam->getToPtr() );
	hkgVec3Copy( f.m_camera.m_up, cam->getUpPtr() );

	const hkgMouse& m = env->m_window->getMouse();
	f.m_mouseX = m.getPosX();
	f.m_mouseY = m.getPosY();

	env->m_textDisplay->outputText("RECORDING", 10,10,0x7F7F0000, 1);

	char buffer[256];
	hkString::snprintf(buffer, 250, "%s_%i", m_currentFileName, m_currentFrame);
	hkOstream ostream( buffer );
	if ( !ostream.isOk() )
	{
		return;
	}

	ostream.write((const char*)&f, sizeof(f));
	m_currentFrame++;
}


void hkDemoReplayUtility::playbackFrame( hkDemoEnvironment* env )
{
	Frame f;
	{
		char buffer[256];
		hkString::snprintf(buffer, 250, "%s_%i", m_currentFileName, m_currentFrame);
		hkIstream ostream( buffer );
		if ( !ostream.isOk() )
		{
			return;
		}

		ostream.read((char*)&f, sizeof(f));
		m_currentFrame++;
	}

	// reflect in the local pseudo pad (pc keypoard interpretation etc)
	*(env->m_gamePad) = f.m_pad;

#ifdef HK_PLATFORM_WIN32
	const_cast<hkgKeyboard&>( env->m_window->getKeyboard() ) = f.m_keys;
#endif

	// overwrite camera
	hkgCamera* cam = env->m_window->getViewport(0)->getCamera();
	cam->setFrom(f.m_camera.m_from);
	cam->setTo(f.m_camera.m_to);
	cam->setUp(f.m_camera.m_up);
	cam->computeModelView();
	cam->computeProjection();

	env->m_window->setMousePosition(f.m_mouseX, f.m_mouseY);

	env->m_textDisplay->outputText("REPLAY", 10,10,0x7F7F0000, 1);
}


/*
* Havok SDK - NO SOURCE PC DOWNLOAD, BUILD(#20080529)
*
* Confidential Information of Havok.  (C) Copyright 1999-2008 
* Telekinesys Research Limited t/a Havok. All Rights Reserved. The Havok
* Logo, and the Havok buzzsaw logo are trademarks of Havok.  Title, ownership
* rights, and intellectual property rights in the Havok software remain in
* Havok and/or its suppliers.
*
* Use of this software for evaluation purposes is subject to and indicates 
* acceptance of the End User licence Agreement for this product. A copy of 
* the license is included with this software and is also available at
* www.havok.com/tryhavok
*
*/
