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


		#include "script_squirrel/cobject/cobject.h"
		#include "script_squirrel/engine_vm.h"
		#include "raytracer/raytracer_core.h"
		#include "automation/automation_source_group.h"
		#include "scene3d/scene.h"
		#include "scene3d/group.h"
		#include "scene3d/memitter.h"
		#include "scene3d/mcamera.h"
		#include "scene3d/mobject.h"
		#include "scene3d/mtrigger.h"
		#include "scene3d/mlight.h"
		#include "scene3d/instance.h"
		#include "ui/ui_cursor.h"
		#include "core/raster_font.h"
		#include "input/input_device.h"

		using namespace GS;
		using namespace GS::Script;


//------------------------------------------------------------------------------
void	CObject::Initialize(CObjectType t, void *o, bool m)
{
	Release();

	type = t;
	native = o;
	managed = m;

	if	(native)
		switch (type)
		{
			case typetag_Picture:			((Picture *)native)->AddRef();					break;
			case typetag_Texture:			((Render::Texture *)native)->AddRef();			break;
			case typetag_Geometry:			((Render::Geometry *)native)->AddRef();			break;
			case typetag_Material:			((Render::Material *)native)->AddRef();			break;
			case typetag_InputDevice:		((Input::Device *)native)->AddRef();			break;
			case typetag_AutomationSource:	((Automation::Source *)native)->AddRef();		break;

			default: break;
		}
}
void	CObject::Release()
{
	if	(native == NULL)
		return;

	switch (type)
	{
		case typetag_Picture:				((Picture *)native)->RemoveRef();				break;
		case typetag_Texture:				((Render::Texture *)native)->RemoveRef();		break;
		case typetag_Geometry:				((Render::Geometry *)native)->RemoveRef();		break;
		case typetag_Material:				((Render::Material *)native)->RemoveRef();		break;
		case typetag_InputDevice:			((Input::Device *)native)->RemoveRef();			break;
		case typetag_AutomationSource:		((Automation::Source *)native)->RemoveRef();	break;

		default: break;
	}

	if	(managed)
		switch (type)
		{
			case typetag_Metafile:				delete ((NML::File *)native);				break;
			case typetag_Raytracer:				delete ((Raytrace::Raytracer *)native);		break;
			case typetag_RasterFont:			delete ((Render::RasterFont *)native);		break;
			case typetag_AutomationSourceGroup:	delete ((Automation::SourceGroup *)native);	break;
			case typetag_Group:					delete ((S3D::Group *)native);				break;
			case typetag_Scene3d:				delete ((S3D::Scene *)native);				break;
			case typetag_UICursor:				delete ((S2D::Cursor *)native);				break;

			default:
				__LOG_E__ << "Type " << CObjectTypeToString(type) << " should not be managed by the VM.\n";
				break;
		}

	native = NULL;
}
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
CObject::CObject(EngineVM *v, CObjectType t, void *o, bool managed) : vm(v)
{
	list_item = vm->cobjects.Add(this);

	native = NULL;
	Initialize(t, o, managed);
}
CObject::~CObject()
{
	if	(list_item)
	{
		vm->cobjects.Remove(list_item);
//		g_flog << "Native object alive count: " << vm->cobjects.GetCount() << "\n";
	}
	Release();
}
//------------------------------------------------------------------------------
