/*  $Id: setupdisplay.cpp,v 1.14 2004/03/13 23:04:13 mbn Exp $
**
**  ClanLib Game SDK
**  Copyright (C) 2003  The ClanLib Team
**  For a total list of contributers see the file CREDITS.
**  
**  This library is free software; you can redistribute it and/or
**  modify it under the terms of the GNU Lesser General Public
**  License as published by the Free Software Foundation; either
**  version 2.1 of the License, or (at your option) any later version.
**
**  This library is distributed in the hope that it will be useful,
**  but WITHOUT ANY WARRANTY; without even the implied warranty of
**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
**  Lesser General Public License for more details.
**
**  You should have received a copy of the GNU Lesser General Public
**  License along with this library; if not, write to the Free Software
**  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
*/

#include "Display/display_precomp.h"
#include "API/Display/setupdisplay.h"
#include "API/Display/Providers/provider_type_register.h"
#include "API/Display/Providers/pcx_provider.h"
#include "API/Display/Providers/targa_provider.h"
#include "API/Display/Providers/jpeg_provider.h"
#include "API/Display/Providers/png_provider.h"
#include "API/Core/Resources/resource_manager.h"
#include "resourcedata_surface.h"
#include "resourcedata_sprite.h"
#include "resourcedata_sprite_description.h"
#include "resourcedata_font.h"
#include "resourcedata_text_styler.h"

static int init_count = 0;
static CL_Slot slot_resource_added;

static void clandisplay_resource_added(CL_Resource &resource)
{
	std::string type = resource.get_type();
	
	if (type == "surface") new CL_ResourceData_Surface(resource);
	if (type == "sprite") new CL_ResourceData_Sprite(resource);
	if (type == "description") new CL_ResourceData_SpriteDescription(resource);
	if (type == "font") new CL_ResourceData_Font(resource);
	if (type == "text_styler") new CL_ResourceData_TextStyler(resource);
}

CL_SetupDisplay::CL_SetupDisplay(bool register_resources_only)
{
	CL_SetupDisplay::init(register_resources_only);
}

CL_SetupDisplay::~CL_SetupDisplay()
{
	CL_SetupDisplay::deinit();
}

void CL_SetupDisplay::init(bool register_resources_only)
{
	init_count++;
	if (init_count != 1) return;

	slot_resource_added = CL_ResourceManager::sig_resource_added().connect(&clandisplay_resource_added);

	new CL_ProviderType_Register<CL_PCXProvider>("pcx");
	new CL_ProviderType_Register<CL_TargaProvider>("tga");
	new CL_ProviderType_Register<CL_TargaProvider>("targa");
	new CL_ProviderType_Register<CL_JPEGProvider>("jpeg");
	new CL_ProviderType_Register<CL_JPEGProvider>("jpg");
	new CL_ProviderType_Register<CL_PNGProvider>("png");
	
	if (register_resources_only) return;
}

void CL_SetupDisplay::deinit()
{
	init_count--;
	if (init_count != 0) return;

	slot_resource_added = CL_Slot();
}
