/*
	Lan Wars.
	Copyright (c) 2009, Darragh Coy.
	All rights reserved.
	Original work(s), source code & author(s) details available at: http://www.darraghcoy.com

	Redistribution and use in source and binary forms, with or without modification, are permitted 
	provided that the following conditions are met:

		* 	Redistributions of source code must retain the above copyright notice, this list of 
			conditions and the following disclaimer.
		* 	Redistributions in binary form must reproduce the above copyright notice, this list of 
			conditions and the following disclaimer in the documentation.
		* 	Neither the name of the author nor the names of contributors may be used to endorse or 
			promote products derived from this software without specific prior written permission.
		* 	The use of any third party libraries, modules or data files provided with the source 
			form of the program must follow and respect the individual licensing terms of these 
			components. You do not have permission to modify or circumvent the licensing terms for 
			provided third party components unless the third party licenses specifically allow this. 
			If you do not agree to the terms of provided third party components then you do not have 
			permission to use them in your derived work(s).
		* 	The terms of this license do not apply to separate (but related) distributions if supplied 
			by the author(s), only to the contents of this source distribution, derived works, and the 
			binaries produced by either modified or unmodified versions of the source code. The terms 
			of this license do not also apply to any third party libraries or components provided with 
			the source distribution. Such third party elements are licensed under their own separate 
			agreements.

	THIS SOFTWARE IS PROVIDED BY DARRAGH COY ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, 
	BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
	ARE DISCLAIMED. IN NO EVENT SHALL DARRAGH COY BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
	EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
	OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 
	OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
	IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

//=====================================================================================================================
/// @brief		This is a base class for all graphics resources that can have their resources lost when the graphics
///				device resets. These include render targets, vertex buffers, index buffers and state blocks.
///				The display maintains an internal list of these resources and asks each evicted resource to 
///				firstly release itself (before a device reset) and then to recreate it's resources after a device
///				has successfully been regained.
//=====================================================================================================================

#include "Precompiled.h"
#include "Graphics\DisplayEvictable.h"
#include "Graphics\Display.h"

using namespace Game;

//=====================================================================================================================
/// @brief		Creates the resource that can be evicted by the graphics display 
/// @param		parent_display - parent display this resource belongs to
/// @pre		The parent display must not be null
/// @pre		The parent display must be valid
//=====================================================================================================================
				
DisplayEvictable::DisplayEvictable( Display * parent_display )
{
	// Make sure ok:

	ENSURE( parent_display != 0			, "The parent display must not be null" )
	ENSURE( parent_display->IsValid()	, "The parent display must be valid"	)
	
	// Save display:
	
	m_display = parent_display;
	
	// Add a reference to the device:
	
	m_display->GetDevice()->AddRef();
	
	// Insert to the head of the evictable resources list on the graphics device:
	
	if ( m_display->m_evictable_head )
	{
		// Link with the head with us
	
		m_display->m_evictable_head->m_evictable_prev = this;
		
		// Link us with the head:
		
		m_evictable_next = m_display->m_evictable_head;
	}
	else
	{
		// No head: therefore we are the new tail
		
		m_display->m_evictable_tail = this;
		
		// Nothing after us in the list:
		
		m_evictable_next = 0;
	}	
	
	// We are the head of the list:
	
	m_display->m_evictable_head = this;
	
	// Nothing before us yet:
	
	m_evictable_prev = 0;
	
	// This resource is not valid first
	
	m_is_valid = false;
}

//=====================================================================================================================
/// @brief		Destroys the evictable resource 
/// @pre		Derived objects should have released all of their resources
//=====================================================================================================================

DisplayEvictable::~DisplayEvictable()
{
	// Make sure ok:
	
	ENSURE( IsValid() == false , "Derived objects should have released all of their resources" )

	//=================================================================================================================
	// Need to remove this item from the list of evictable resources on the display. It no longer needs to be 
	// managed by the display in the case of device resets.
	//=================================================================================================================
	
	// See if anything before us in the list:

	if ( m_evictable_prev )
	{
		// Set it's next pointer
		
		m_evictable_prev->m_evictable_next = m_evictable_next;
	}
	else
	{
		// Our own next item is the new head:
		
		m_display->m_evictable_head = m_evictable_next;
	}
	
	// See if anything after us in the list:
	
	if ( m_evictable_next )
	{
		// Set it's previous pointer:
		
		m_evictable_next->m_evictable_prev = m_evictable_prev;
	}
	else
	{
		// Item before us is the new tail:
		
		m_display->m_evictable_tail = m_evictable_prev;
	}
	
	// Remove reference to our device:
	
	m_display->GetDevice()->Release();
}
