//
// Asura_Controller.cpp
//
// Created by KOR - 05 October 2006
//
// Copyright (C) 2006 Rebellion, All Rights Reserved.
//

#include "Core/Asura.h"

// This file's header.
#include "Input/Asura_Controller.h"

// Asura headers
#include "Core/Memory/Asura_Mem.h"

Asura_Controller::Asura_Controller(u_int uControllerIndex, u_int uNumKeys)
: m_uControllerIndex(uControllerIndex)
, m_uNumKeys(uNumKeys)
{
	m_pfCurrentKeys = new float[uNumKeys];
	m_pfPreviousKeys = new float[uNumKeys];
	Asura_Mem::Set(m_pfCurrentKeys, 0, uNumKeys * sizeof(float));
	Asura_Mem::Set(m_pfPreviousKeys, 0, uNumKeys * sizeof(float));
	Asura_Input::RegisterController(m_uControllerIndex, this);
} 

Asura_Controller::~Asura_Controller()
{
	// Deallocate arrays
	delete [] m_pfCurrentKeys;
	delete [] m_pfPreviousKeys;
	Asura_Input::UnregisterController(m_uControllerIndex);
}

void Asura_Controller::Update()
{
	// Copy current keys into previous keys.
	// This should be the fastest way of doing it.
	Asura_Mem::Copy(m_pfPreviousKeys, m_pfCurrentKeys, m_uNumKeys * sizeof(float));
	// Now update inputs. This may be done in this function or the Asura_Input::Platform_Update,
	// depending on the structure of the platform libraries.
	Platform_Update();
}

bool Asura_Controller::HasAnyKeyBeenPressed() const
{
	for(u_int uKey = 0; uKey < m_uNumKeys; ++uKey)
	{
		if(m_pfCurrentKeys[uKey] > 0.f)
		{
			return true;
		}
	}
	return false;
}

bool Asura_Controller::DebouncedHasAnyKeyBeenPressed() const
{
	for(u_int uKey = 0; uKey < m_uNumKeys; ++uKey)
	{
		if( m_pfCurrentKeys[uKey] > 0.f && !(m_pfPreviousKeys[uKey] > 0.f) )
		{
			return true;
		}
	}
	return false;
}
