/** SGX

The   sss    gggg x   x   
     s      g      x x    Engine
      sss   g  gg   x
         s  g   g  x x          Copyright 1995-2009.
     ssss    gggg x   x         Steven Goodwin. All Rights Reserved.
    
This version has been released under the GNU Public License v2.0.

Please see the license files for details.

File: sgx/src/sgx/graphics/surfacecache.cpp

*/

#include "sgx/core/core.hpp"
#include "sgx/graphics/graphics.hpp"

void
SurfaceCache::getCacheID(SurfaceCacheID &id, const sgxString &filename)
{
	// TODO: get Unique ID from filename? to check for same filename wioth relative/abs path
	id = filename;
}

void
SurfaceCache::getCacheID(SurfaceCacheID &id, const sgxString &filename, const sgxString &alpha_filename)
{
	id = filename + alpha_filename;
}

CSGXSurface	*
SurfaceCache::getSurface(const sgxString &filename)
{
	SurfaceCacheID id;
	
	getCacheID(id, filename);
    SurfaceCacheMap::iterator it = m_Cache.find(id);

    if (it == m_Cache.end()) {
		return NULL;
	}
	// TODO: Remove ref counting, or change function name
		++(*it).second.iCount;

	return (*it).second.pSurface;
}


CSGXSurface	*
SurfaceCache::getSurface(const sgxString &filename, const sgxString &alpha_filename)
{
	SurfaceCacheID id;
	
	getCacheID(id, filename, alpha_filename);
    SurfaceCacheMap::iterator it = m_Cache.find(id);

    if (it == m_Cache.end()) {
		return NULL;
	}
	// TODO: Remove ref counting, or change function name
		++(*it).second.iCount;

	return (*it).second.pSurface;
}
	
void		
SurfaceCache::addSurface(const SurfaceCacheID &id, CSGXSurface *pSurface)
{
	m_Cache.insert(SurfaceCacheMap::value_type(id, sgxRefCache(pSurface)));
}

	// TODO:add a RemoveSurface(, IsInCache, GetRefCount