/* -----------------------------------------------------------------------------
	GSFramework
	Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
----------------------------------------------------------------------------- */


		#include "binding_helpers.h"
		#include "script_squirrel/cobject/matrix_decl.h"
		#include "core/resource_factories.h"
		#include "core/graphic_resource_factory.h"
		#include "core/render_resource_factory.h"
		#include "core/mixer_resource_factory.h"
		#include "core/raster_font.h"

		using namespace GS;
		using namespace GS::Core;
		using namespace GS::Script;
		using namespace GS::Render;


//------------------------------------------------------------------------------
SQInteger			ResourceFactoryLoadPicture(HSQUIRRELVM vm)
{
	__SQ_GETSTART(2)
	__SQ_GETSAFEPTR(f, ResourceFactories, typetag_ResourceFactories)
	__SQ_GETSTRING(name)
	Picture *p = f->graphic->LoadPicture(name);
	__SQ_GETEND
	__SQ_RETURNSAFEPTR(p, typetag_Picture)
}
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
SQInteger			ResourceFactoryLoadRasterFont(HSQUIRRELVM vm)
{
	__SQ_GETSTART(3)
	__SQ_GETSAFEPTR(f, ResourceFactories, typetag_ResourceFactories)
	__SQ_GETSTRING(base)
	__SQ_GETSTRING(name)
	RasterFont *font = new RasterFont;
	if	(!font)
		return sq_throwerror(vm, "Failed to allocate raster font object.");
	font->Load(*f->render, base, name);
	__SQ_GETEND
	__SQ_RETURNMANAGEDSAFEPTR(font, typetag_RasterFont)
}
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
SQInteger			ResourceFactoryNewTexture(HSQUIRRELVM vm)
{
	__SQ_GETSINGLESAFEPTR(f, ResourceFactories, typetag_ResourceFactories)
	Texture *t = f->render->NewTexture();
	__SQ_RETURNSAFEPTR(t, typetag_Texture)
}
SQInteger			ResourceFactoryLoadTexture(HSQUIRRELVM vm)
{
	__SQ_GETSTART(2)
	__SQ_GETSAFEPTR(f, ResourceFactories, typetag_ResourceFactories)
	__SQ_GETSTRING(name)
	Texture *t = f->render->LoadTexture(name);
	if	(!t)
		return sq_throwerror(vm, String::Format("Texture '%s' not found.", name));
	__SQ_GETEND
	__SQ_RETURNSAFEPTR(t, typetag_Texture)
}
SQInteger			ResourceFactoryLoadTextureEx(HSQUIRRELVM vm)
{
	__SQ_GETSTART(3)
	__SQ_GETSAFEPTR(f, ResourceFactories, typetag_ResourceFactories)
	__SQ_GETSTRING(name)
	__SQ_GETBOOL(bypass_cache)
	Texture *t = f->render->LoadTexture(name, asbool(bypass_cache));
	if	(!t)
		return sq_throwerror(vm, String::Format("Texture '%s' not found.", name));
	__SQ_GETEND
	__SQ_RETURNSAFEPTR(t, typetag_Texture)
}
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
SQInteger			ResourceFactoryLoadShader(HSQUIRRELVM vm)
{
	__SQ_GETSTART(2)
	__SQ_GETSAFEPTR(f, ResourceFactories, typetag_ResourceFactories)
	__SQ_GETSTRING(name)
	Render::Shader *s = f->render->LoadShader(name);
	__SQ_GETEND
	__SQ_RETURNSAFEPTR(s, typetag_Shader)
}
SQInteger			ResourceFactoryLoadShaderEx(HSQUIRRELVM vm)
{
	__SQ_GETSTART(3)
	__SQ_GETSAFEPTR(f, ResourceFactories, typetag_ResourceFactories)
	__SQ_GETSTRING(name)
	__SQ_GETBOOL(bypass_cache)
	Render::Shader *s = f->render->LoadShader(name, asbool(bypass_cache));
	__SQ_GETEND
	__SQ_RETURNSAFEPTR(s, typetag_Shader)
}
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
SQInteger			ResourceFactoryLoadGeometry(HSQUIRRELVM vm)
{
	__SQ_GETSTART(2)
	__SQ_GETSAFEPTR(f, ResourceFactories, typetag_ResourceFactories)
	__SQ_GETSTRING(name)
	Render::Geometry *g = f->render->LoadGeometry(name);
	__SQ_GETEND
	__SQ_RETURNSAFEPTR(g, typetag_Geometry)
}
SQInteger			ResourceFactoryLoadGeometryEx(HSQUIRRELVM vm)
{
	__SQ_GETSTART(3)
	__SQ_GETSAFEPTR(f, ResourceFactories, typetag_ResourceFactories)
	__SQ_GETSTRING(name)
	__SQ_GETBOOL(bypass_cache)
	Render::Geometry *g = f->render->LoadGeometry(name, asbool(bypass_cache));
	__SQ_GETEND
	__SQ_RETURNSAFEPTR(g, typetag_Geometry)
}
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
SQInteger			ResourceFactoryLoadMaterial(HSQUIRRELVM vm)
{
	__SQ_GETSTART(2)
	__SQ_GETSAFEPTR(f, ResourceFactories, typetag_ResourceFactories)
	__SQ_GETSTRING(name)
	Render::Material *m = f->render->LoadMaterial(name);
	__SQ_GETEND
	__SQ_RETURNSAFEPTR(m, typetag_Material)
}
SQInteger			ResourceFactoryLoadMaterialEx(HSQUIRRELVM vm)
{
	__SQ_GETSTART(3)
	__SQ_GETSAFEPTR(f, ResourceFactories, typetag_ResourceFactories)
	__SQ_GETSTRING(name)
	__SQ_GETBOOL(bypass_cache)
	Render::Material *m = f->render->LoadMaterial(name, asbool(bypass_cache));
	__SQ_GETEND
	__SQ_RETURNSAFEPTR(m, typetag_Material)
}
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
SQInteger			ResourceFactoryLoadSound(HSQUIRRELVM vm)
{
	__SQ_GETSTART(2)
	__SQ_GETSAFEPTR(f, ResourceFactories, typetag_ResourceFactories)
	__SQ_GETSTRING(name)
	Audio::Sound *s = f->audio->LoadSound(name);
	__SQ_GETEND
	__SQ_RETURNSAFEPTR(s, typetag_Sound)
}
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
SQInteger			ResourceFactoryPurge(HSQUIRRELVM vm)
{
	__SQ_GETSINGLESAFEPTR(f, ResourceFactories, typetag_ResourceFactories)
	uint purged = 0;
	purged += f->graphic->PurgeCache();
	purged += f->render->PurgeCache();
//	purged += f->mixer->PurgeCache();
	__SQ_RETURNINT(purged)
}
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
void		RegisterResourceFactoryBinding(HSQUIRRELVM vm)
{
/*#
	Topic: Resource Factory
	Desc: A resource factory caches all graphic and render resources used by a project. You can access your project resource factory from any script by using the global variable <b>g_factory</b>.
	Type: ResourceFactory
#*/

/*#
	Section: ResourceFactoryGeneral
	Desc: Resource Factory General
#*/
	/*#
		Func: ResourceFactoryPurge
		Proto: int:ResourceFactory
		Desc: Call this function to unload from memory all cached resources that are not currently in use.
		Example:
local purged_count = ResourceFactoryPurge(g_factory)
print("Resource unloaded: " + purged_count)
	#*/
	sq_register(vm, ResourceFactoryPurge, "ResourceFactoryPurge", _SC(".x"));

/*#
	Section: GraphicResourceFactory
	Desc: Graphic Resources
#*/
	/*#
		Func: ResourceFactoryLoadPicture
		Proto: Picture:ResourceFactory,String name
		Desc: Load a picture from a graphic resource factory, this function use the resource cache to prevent redundant loads of the same resource.
		See: PictureLoad
	#*/
	sq_register(vm, ResourceFactoryLoadPicture, "ResourceFactoryLoadPicture", _SC(".xs"));

/*#
	Section: RenderResourceFactory
	Desc: Render Resources
#*/
	/*#
		Func: ResourceFactoryLoadRasterFont
		Proto: RasterFont:ResourceFactory,String name
		Desc: Load a raster font from a resource factory.
	#*/
	sq_register(vm, ResourceFactoryLoadRasterFont, "ResourceFactoryLoadRasterFont", _SC(".xss"));

	/*#
		Func: ResourceFactoryNewTexture
		Proto: Texture:ResourceFactory
		Desc: Create a new texture from a resource factory.
	#*/
	sq_register(vm, ResourceFactoryNewTexture, "ResourceFactoryNewTexture", _SC(".x"));

	/*#
		Func: ResourceFactoryLoadShader
		Proto: Shader:ResourceFactory,String name
		Desc: Load a shader from a resource factory.
	#*/
	sq_register(vm, ResourceFactoryLoadShader, "ResourceFactoryLoadShader", _SC(".xs"));
	/*#
		Func: ResourceFactoryLoadShaderEx
		Proto: Shader:ResourceFactory,String name,bool bypass_cache
		Desc: Load a shader from a resource factory, optionally bypassing the factory cache.
	#*/
	sq_register(vm, ResourceFactoryLoadShaderEx, "ResourceFactoryLoadShaderEx", _SC(".xsb"));

	/*#
		Func: ResourceFactoryLoadTexture
		Proto: Texture:ResourceFactory,String name
		Desc: Load a texture from a resource factory.
	#*/
	sq_register(vm, ResourceFactoryLoadTexture, "ResourceFactoryLoadTexture", _SC(".xs"));
	/*#
		Func: ResourceFactoryLoadTextureEx
		Proto: Texture:ResourceFactory,String name,bool bypass_cache
		Desc: Load a texture from a resource factory, optionally bypassing the factory cache.
	#*/
	sq_register(vm, ResourceFactoryLoadTextureEx, "ResourceFactoryLoadTextureEx", _SC(".xsb"));

	/*#
		Func: ResourceFactoryLoadGeometry
		Proto: Texture:ResourceFactory,String name
		Desc: Load a geometry from a resource factory.
	#*/
	sq_register(vm, ResourceFactoryLoadGeometry, "ResourceFactoryLoadGeometry", _SC(".xs"));
	/*#
		Func: ResourceFactoryLoadGeometryEx
		Proto: Texture:ResourceFactory,String name,bool bypass_cache
		Desc: Load a geometry from a resource factory, optionally bypassing the factory cache.
	#*/
	sq_register(vm, ResourceFactoryLoadGeometryEx, "ResourceFactoryLoadGeometryEx", _SC(".xsb"));

	/*#
		Func: ResourceFactoryLoadMaterial
		Proto: Material:ResourceFactory,String name
		Desc: Load a material from a resource factory.
	#*/
	sq_register(vm, ResourceFactoryLoadMaterial, "ResourceFactoryLoadMaterial", _SC(".xs"));

	/*#
		Func: ResourceFactoryLoadMaterialEx
		Proto: Material:ResourceFactory,String name,bool bypass_cache
		Desc: Load a material from a resource factory, optionally bypassing the factory cache.
	#*/
	sq_register(vm, ResourceFactoryLoadMaterialEx, "ResourceFactoryLoadMaterialEx", _SC(".xsb"));

/*#
	Section: MixerResourceFactory
	Desc: Mixer Resources
#*/
	/*#
		Func: ResourceFactoryLoadSound
		Proto: Sound:ResourceFactory,String name
		Desc: Load a sound from a resource factory.
	#*/
	sq_register(vm, ResourceFactoryLoadSound, "ResourceFactoryLoadSound", _SC(".xs"));
}
//------------------------------------------------------------------------------
