⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 image.cpp

📁 game programing code
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//==============================================================
//==============================================================
//= image.cpp ==================================================
//= Original coders: Trent Polack (trent@voxelsoft.com) and	   =
//-					 Evan Pipho	  (evan@codershq.com)		   =
//==============================================================
//= Image/texture loadings ops								   =
//==============================================================
//==============================================================


//--------------------------------------------------------------
//--------------------------------------------------------------
//- HEADERS AND LIBRARIES --------------------------------------
//--------------------------------------------------------------
//--------------------------------------------------------------
#include <stdio.h>
#include <memory.h>

#include "image.h"
#include "log.h"


//--------------------------------------------------------------
//--------------------------------------------------------------
//- GLOBALS ----------------------------------------------------
//--------------------------------------------------------------
//--------------------------------------------------------------
unsigned char g_ucUTGAcompare[12]= {	0, 0, 2,  0, 0, 0, 0, 0, 0, 0, 0, 0	};
unsigned char g_ucCTGAcompare[12]= {	0, 0, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0	};


//--------------------------------------------------------------
//--------------------------------------------------------------
//- DEFINITIONS ------------------------------------------------
//--------------------------------------------------------------
//--------------------------------------------------------------

//--------------------------------------------------------------
// Name:			CIMAGE::Create - public
// Description:		Create space for use with a new texture
// Arguments:		-uiWidth, uiHeight: the dimensions of the new image
//					-uiBPP: the bits per pixel for the new image
// Return Value:	A boolean variable: -true: memory was successfully allocated
//									    -false: memory was not successfully allocated
//--------------------------------------------------------------
bool CIMAGE::Create( unsigned int uiWidth, unsigned int uiHeight, unsigned int uiBPP )
{
	//set the member variables
	m_uiWidth = uiWidth;
	m_uiHeight= uiHeight;
	m_uiBPP   = uiBPP;

	//allocate memory
	m_ucpData= new unsigned char [uiWidth*uiHeight*( m_uiBPP/8 )];
	if( !m_ucpData )
	{
		g_log.Write( LOG_FAILURE, "Out of memory for image memory allocation" );
		return false;
	}

	//set the loaded flag
	m_bIsLoaded= true;
	return true;
}

//--------------------------------------------------------------
// Name:			CIMAGE::LoadData - public
// Description:		Load only the data for a new image (do not create an
//					OpenGL texture)
// Arguments:		-szFilename: the file to load in
// Return Value:	A boolean variable: -true: data was successfully loaded
//									    -false: data was not successfully loaded
//--------------------------------------------------------------
bool CIMAGE::LoadData( char* szFilename )
{
	FILE* pFile;
	int iStart;
	int iEnd;
	int iSize;

	//open the file for reading (in binary mode)
	pFile= fopen( szFilename, "rb" );

	//check to see if we were able to open the file
	if( pFile==NULL )
	{
		g_log.Write( LOG_FAILURE, "Could not open file %s.", szFilename );
		return false;
	}
		
	//Get file length
	fseek( pFile, 0, SEEK_END );
	iEnd = ftell( pFile );
	fseek( pFile, 0, SEEK_SET );
	iStart = ftell( pFile );
	iSize = iEnd - iStart;

	//allocate the data buffer (temporary)
	m_ucpData= new unsigned char [iSize];

	//read the image's data into the buffer
	if( fread( m_ucpData, sizeof( unsigned char ), iSize, pFile ) != ( unsigned )iSize )
	{
		//the file is corrupted
		g_log.Write( LOG_FAILURE, "%s is corrupted, could not read all data\n", szFilename );
		if( m_ucpData )
		{
			delete[] m_ucpData;
			m_ucpData = NULL;
		}
		return false;
	}

	//check to see if the file is in the BMP format
	if( memcmp( m_ucpData, "BM", 2 )==0 )
	{
		//load the BMP using the BMP-loading routine
		if( !LoadBMP( ) )
		{
			//could not load the BMP file
			g_log.Write( LOG_FAILURE, "Could not load BMP %s.", szFilename );
			if( m_ucpData )
			{
				delete[] m_ucpData;
				m_ucpData= NULL;
			}
			return false;
		}
	}

	//check to see if the file is an uncompressed TGA
	else if( memcmp( m_ucpData, g_ucUTGAcompare, 12 )==0 )
	{
		//load the file using the uncompressed TGA Loader
		if( !LoadUncompressedTGA( ) )
		{
			//could not load the file
			g_log.Write( LOG_FAILURE, "Could not load uncompressed TGA %s\n", szFilename );
			if( m_ucpData )
			{
				delete[] m_ucpData;
				m_ucpData= NULL;
			}
			return false;
		}
	}
	
	//check to see if the file is a compressed TGA
	else if( memcmp( m_ucpData, g_ucCTGAcompare, 12 )==0 )
	{
		//load the file using the compressed TGA loader
		if( !LoadCompressedTGA( ) )
		{
			//could not load the file
			if( m_ucpData )
			{
				delete[] m_ucpData;
				m_ucpData= NULL;
			}
			g_log.Write( LOG_FAILURE, "Could not load compressed TGA %s\n", szFilename );
			return false;
		}
	}

	//the file is not supported by our image loader
	else
	{
		g_log.Write( LOG_FAILURE, "%s is not a supported image type\n", szFilename );
		if( m_ucpData )
		{
			delete[] m_ucpData;
			m_ucpData= NULL;
		}
		return false;
	}

	//the file's data was successfully loaded
	m_bIsLoaded= true;
	g_log.Write( LOG_SUCCESS, "Loaded %s correctly\n", szFilename );
	return true;
}

//--------------------------------------------------------------
// Name:			CIMAGE::Load - public
// Description:		Completely setup a new texture, first load the data in,
//					and then setup the texture for use with OpenGL.  This
//					function supports both the BMP and the TGA formats
// Arguments:		-szFilename: the file to load in
//					-fMinFilter/fMaxFilter: OpenGL filter (GL_LINEAR is most common)
//					-bMipmap: create mipmaps for the texture being created
// Return Value:	A boolean variable: -true: texture was successfully loaded
//									    -false: texture was not successfully loaded
//--------------------------------------------------------------
bool CIMAGE::Load( char* szFilename, float fMinFilter, float fMaxFilter, bool bMipmap )
{
	int	iType;

	//load the file's data in
	if( !LoadData( szFilename ) )
		return false;

	//set the image's OpenGL BPP type
	if( m_uiBPP==24 )
		iType= GL_RGB;
	else
		iType= GL_RGBA;

	//build the texture for use with OpenGL
	glGenTextures( 1, &m_ID );
	glBindTexture( GL_TEXTURE_2D, m_ID );
	glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, fMinFilter );
	glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, fMaxFilter );						

	//create the texture normally
	if( !bMipmap )
		glTexImage2D( GL_TEXTURE_2D, 0, iType, m_uiWidth, m_uiHeight, 
					  0, iType, GL_UNSIGNED_BYTE, m_ucpData );

	//create a mipmapped texture
	else
		gluBuild2DMipmaps( GL_TEXTURE_2D, iType, m_uiWidth, m_uiHeight, 
						   iType, GL_UNSIGNED_BYTE, m_ucpData );

	//the image has been successfully loaded
	m_bIsLoaded= true;
	return true;
}

//--------------------------------------------------------------
// Name:			CIMAGE::Unload - public
// Description:		Unload the texture that is currently loaded
// Arguments:		None
// Return Value:	None
//--------------------------------------------------------------
void CIMAGE::Unload( void )
{
	if( m_bIsLoaded )
	{
		delete[] m_ucpData;

		m_uiWidth = 0;
		m_uiHeight= 0;
		m_uiBPP   = 0;

		m_bIsLoaded= false;
	}
}

//--------------------------------------------------------------
// Name:			CIMAGE::Save - public
// Description:		Save the current image information to a file.
//					This function only supports the BMP format.
// Arguments:		-szFilename: the filename of the file to be saved
// Return Value:	A boolean variable: -true: image was saved
//									    -false: image was not saved
//--------------------------------------------------------------
bool CIMAGE::Save( char* szFilename )
{
	//save the file as a windows bitmap (.BMP)
	if( !SaveBMP( szFilename ) )
	{
		g_log.Write( LOG_FAILURE, "Could not save %s.", szFilename );
		return false;
	}

	//the file has been successfully saved
	g_log.Write( LOG_SUCCESS, "%s has been successfully saved", szFilename );
	return true;
}

//--------------------------------------------------------------
// Name:			CIMAGE::LoadBMP - private
// Description:		The routine to load a windows bitmap (BMP)
// Arguments:		None
// Return Value:	A boolean variable: -true: BMP was loaded
//									    -false: BMP was not loaded
//--------------------------------------------------------------
bool CIMAGE::LoadBMP( void )
{
	BMPFileHeader* fileHeader;
	BMPInfoHeader* infoHeader;
	unsigned int iImageSize;
	unsigned char uiTempRGB;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -