www.pudn.com > final_6.rar > BitmapTexture.cpp


// BitmapTexture.cpp: implementation of the CBitmapTexture class. 
// 
////////////////////////////////////////////////////////////////////// 
 
#include "stdafx.h" 
#include "CG.h" 
#include "BitmapTexture.h" 
#include "math.h" 
#include "base.h" 
 
#ifdef _DEBUG 
#undef THIS_FILE 
static char THIS_FILE[]=__FILE__; 
#define new DEBUG_NEW 
#endif 
////////////////////////////////////////////////////////////////////// 
// Construction/Destruction 
////////////////////////////////////////////////////////////////////// 
 
IMPLEMENT_SERIAL(CBitmapTexture,CObject,1) 
 
CBitmapTexture::CBitmapTexture() 
{ 
	imgFile = ""; 
	img = NULL; 
	lpbyImgData32 = NULL; 
	TexType = 1; 
} 
 
CBitmapTexture::CBitmapTexture(CString filename) 
{ 
	CFileFind find; 
	if(!find.FindFile(filename)) 
	{ 
		imgFile = ""; 
		img = NULL; 
		lpbyImgData32 = NULL; 
		TexType = 1; 
		return; 
	} 
	else 
	{ 
		imgFile = filename; 
        imgID=0; 
		img = new CDib(); 
		img->Load(filename); 
		width = img->Width(); 
		height = img->Height(); 
		lpbyImgData32 = new BYTE[width * height * 4]; 
		img->GetDdbData32(lpbyImgData32); 
		TexType = 1; 
	} 
} 
 
CBitmapTexture::CBitmapTexture(int PicNo) 
{ 
	if(PicNo>4&&PicNo<0) 
	{ 
		imgID=0; 
		img = NULL; 
		lpbyImgData32 = NULL; 
		TexType = 1; 
		return; 
	} 
	else 
	{ 
    	imgID=IDB_BITMAP0; 
		imgFile = ""; 
	    img = new CDib(); 
	    img->LoadFromResource(imgID); 
	    width = img->Width(); 
	    height = img->Height(); 
	    lpbyImgData32 = new BYTE[width * height * 4]; 
	    img->GetDdbData32(lpbyImgData32); 
	    TexType = 1; 
	} 
} 
 
CBitmapTexture::~CBitmapTexture() 
{ 
	if(img) 
	{ 
		delete img; 
		img = NULL; 
	} 
	if(lpbyImgData32) 
	{ 
		delete[] lpbyImgData32; 
		lpbyImgData32 = NULL; 
	} 
} 
 
COLORREF CBitmapTexture::GetColor(CGPoint pt) 
{ 
	long w, h; 
	w = long(fabs(pt.x)); 
	h = long(fabs(pt.y)); 
	w = w % width; 
	h = h % height; 
	BYTE r, g, b, a; 
	long index = h * width * 4 + 4 * w; 
	 
	if(img==NULL) 
	{ 
		AfxMessageBox("位图没有载入,不可贴图!"); 
	} 
	b = lpbyImgData32[index++]; 
	g = lpbyImgData32[index++]; 
	r = lpbyImgData32[index++]; 
	a = lpbyImgData32[index++]; 
	return RGB(r,g,b); 
} 
 
void CBitmapTexture::Serialize(CArchive &ar) 
{ 
	if(ar.IsStoring()) 
	{ 
		ar << width << height << imgFile<> width >> height >> imgFile>>imgID; 
		img = new CDib(); 
		CFileFind find; 
		if(!find.FindFile(imgFile)) 
		{ 
			img->LoadFromResource(imgID); 
		} 
		else 
		{    
			img->Load(imgFile);  
		} 
		width = img->Width(); 
		height = img->Height(); 
		lpbyImgData32 = new BYTE[width * height * 4]; 
		img->GetDdbData32(lpbyImgData32); 
	} 
}