/*  $Id: outline_provider_file_generic.cpp,v 1.2 2004/04/05 09:51:58 sphair 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/Core/IOData/inputsource_provider.h"
#include "API/Core/IOData/inputsource.h"
#include "API/Core/System/clanstring.h"
#include "API/Core/System/error.h"
#include "outline_provider_file_generic.h"

/////////////////////////////////////////////////////////////////////////////
// CL_OutlineProviderFile_Generic construction:

CL_OutlineProviderFile_Generic::CL_OutlineProviderFile_Generic(
	const std::string &filename,
	CL_InputSourceProvider *_provider)
{
	if( _provider == 0 )
		provider = CL_InputSourceProvider::create_file_provider(".");
	else
		provider = _provider->clone();
	
	load(filename);
}

CL_OutlineProviderFile_Generic::~CL_OutlineProviderFile_Generic()
{
	delete provider;
}

/////////////////////////////////////////////////////////////////////////////
// CL_OutlineProviderFile_Generic operations:

void CL_OutlineProviderFile_Generic::load(const std::string &filename)
{
	CL_InputSource *input_source = provider->open_source(filename);

	// file type & version identifiers
	int type = input_source->read_uint32();
	unsigned char version = input_source->read_uint8();

	if( type != 0x03042004  )
		throw CL_Error(CL_String::format("File is not a collision outline file: '%1'", filename) );
	if( version != 1 )
		throw CL_Error(CL_String::format("Unsupported version of outline format: %1 in file '%2'. Supported versions: 1.", version, filename) );

	// num contours
	int num_contours = input_source->read_uint32();

	for( int cc=0; cc < num_contours; ++cc )
	{
		CL_Contour contour;

		int num_points = input_source->read_uint32();

		for( int pp=0; pp < num_points; ++pp )
		{
			CL_Pointf point(0,0);
			point.x = input_source->read_float32();
			point.y = input_source->read_float32();

			contour.points.push_back(point);
		}
		
		contours.push_back(contour);
	}

	input_source->close();

	delete input_source;
}
