// HeightData.cpp: implementation of the HeightData class.
//
//////////////////////////////////////////////////////////////////////

#include "HeightData.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

#include <cstdio>

#include "texture.h"
#include "logger.h"
#include <string>
using std::string;

// Constructor
// bmpFile is the bitmap file containing the height data,
// cols and rows are the resolution of the height data
HeightData::HeightData( const char* rawFile, int x, int z  )
{
	// Both cols and rows must a multiple of nine
//	if( x % 9 != 0 || z % 9 != 0 )
//		throw new InvalidParameterException( "HeightData::Constructor" );

	// Set the rows and columns
	m_iCols = z;
	m_iRows = x;

	// Allocate memory for the height data
	m_iSize = x * z;
	m_ucData = new unsigned char[ m_iSize ];

	// Open a file pointer to the raw file and read the data in
	FILE* f = fopen( rawFile, "rb" );

	// Make sure that the file was opened
//	if( ! f )
//		throw new FileException( "HeightData::Constructor: Error opening " + 
//									string(rawFile) );

	// Read in the height data
	fread( m_ucData, 1, m_iSize, f );
	
	// Close the file pointer
	fclose( f );
}// End HeightData::Constructor


// Destructor
HeightData::~HeightData()
{
	// Delete the height data
	if( m_ucData )
		delete[] m_ucData;
}// End HeightData::Destructor



// Returns the number of rows in this height map
int HeightData::getRows() const
{
	return m_iRows;
}// End HeightData::getRows


// Returns the number of columns in this height map
int HeightData::getColumns() const
{
	return m_iCols;
}// End HeightData::getColumns


// Height data access parameters
unsigned char HeightData::operator[] ( int n ) const
{
	// Make sure that the argument is in range
//	if( n > m_iSize )
//		throw new IndexOutOfBoundsException( "HeightData::operator[int]" );
	return m_ucData[n];
}// End HeightData::operator[]( int )

/*
// This treats the single array like a 2d array
unsigned char HeightData::at( int x, int z ) const
{
	// Make sure that the argument is in range
	if( z > m_iCols || x > m_iRows )
	{
		logger::event( "HeightData::at", "m_iCols", m_iCols );
		logger::event( "HeightData::at", "z", z );
		logger::event( "HeightData::at", "m_iRows", m_iRows );
		logger::event( "HeightData::at", "x", x );

		throw new IndexOutOfBoundsException( "HeightData::at(int,int)" );
	}// End if


	return m_ucData[ z * m_iCols + x];
}// End HeightData::operator[] ( int, int )
*/
