Jump to content
  • Advertisement
Sign in to follow this  
mrtie_dye

BitMapObject

This topic is 4812 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I was fooling around with the BitMapObject class found in the Tetris Clone tutorial, trying to find a better way to make transparancey in bitmaps. The second Load function below is my way of doing this. Please take a look at it and give me some suggestions. Thanks. You are looking for the following function: void BitMapObject::Load(COLORREF crTransparent, WORD lpszFilename) EDIT: I worked on this code a little bit. Made the Collision function make more sense with the new transparancy stuff. I also added a few comments to help make a few things clear in the original Load function. I appologize for the lack of comments in the new Load function, but to be honest, I am not entirely sure what is going on in there. I just know that it works. If anyone has any suggestions, or wants to explain exaclty what each line does, I would greatly appreciate it. BitMapObject.cpp
/*******************************************************************************
The BitMapObject class was borrowed from Evil_Greven's "Tetris clone in an
hour with C++" tutorial on gamedev.net. You can find it here...
http://www.gamedev.net/community/forums/topic.asp?topic_id=192483
I have modified it slightly to suit my needs
********************************************************************************/

#include "bitmapobject.h"

BitMapObject::BitMapObject()
{
	hdcMemory=NULL;
	hbmNewBitMap=NULL;
	hbmOldBitMap=NULL;
	iWidth=0;
	iHeight=0;
	hotPink = RGB(255,0,255);
}

BitMapObject::~BitMapObject()
{
	//if the hdcMemory hasn't been destroyed, do so
	if(hdcMemory)
		Destroy();
}

void BitMapObject::Load(HWND hwnd, WORD lpszFilename, BOOL trans)
{
	if(hdcMemory)
		Destroy();
	
	BITMAP bmp;
	PAINTSTRUCT ps;

	HDC hdc = BeginPaint(hwnd, &ps);

	hdcMemory = CreateCompatibleDC(hdc);
	
	hbmNewBitMap=LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(lpszFilename));
	//shove the image into the dc
	hbmOldBitMap=(HBITMAP)SelectObject(hdcMemory,hbmNewBitMap);
	//grab the bitmap's properties
	GetObject(hbmNewBitMap,sizeof(BITMAP),(LPVOID)&bmp);
	//grab the width & height
	iWidth=bmp.bmWidth;
	iHeight=bmp.bmHeight;
	
	//if this image is to be loaded with transparancy, we need to take some
	//additional steps
	if(trans)
	{
		//define the color black
		COLORREF black = RGB(0,0,0);
		
		//resize VCollision so that it has enough deminsions to hold info
		//for all pixles in this object
		VCollision.resize(iWidth); 
		for(int i =0;i < iWidth;++i)
		{
			VCollision.resize(iHeight); 
		}
		
		//Now go through each pixel
		for(int a = 0; a < iWidth; a++)
		{
			for(int b = 0; b < iHeight; b++)
			{
				//if this pixel is hotPink...
				if(GetPixel(hdcMemory, a, b) == hotPink)
				{
					//set collision for this pixel to 0
					VCollision[a] = 0;
					
					//Now, make that pixel black so our transparancy
					//will look right
					SetPixel(hdcMemory,a,b,black);
				}
				else
				{
					//otherwise, set collision to 1 
					VCollision[a] = 1;
				}
			}
		}		
	}
	
	SelectObject(hdc, hbmOldBitMap);
	DeleteDC(hdc);
	EndPaint(hwnd, &ps);
}

void BitMapObject::Load(WORD lpszFilename)
{
	if(hdcMemory)
		Destroy();
	
	HDC hdcMem;
	HBITMAP hbmColor;
	BITMAP bmp;
	
	hbmColor=LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(lpszFilename));

	GetObject(hbmColor, sizeof(BITMAP), &bmp);
	hbmNewBitMap = CreateBitmap(bmp.bmWidth, bmp.bmHeight, 1, 1, NULL);

	hdcMem = CreateCompatibleDC(0);
	hdcMemory = CreateCompatibleDC(0);

	SelectObject(hdcMem, hbmColor);
	SelectObject(hdcMemory, hbmNewBitMap);

	SetBkColor(hdcMem, hotPink);

	BitBlt(hdcMemory, 0, 0, bmp.bmWidth, bmp.bmHeight, hdcMem, 0, 0, SRCCOPY);

	BitBlt(hdcMem, 0, 0, bmp.bmWidth, bmp.bmHeight, hdcMemory, 0, 0, SRCINVERT);
	
	hbmOldBitMap=(HBITMAP)SelectObject(hdcMemory,hbmNewBitMap);
	//grab the bitmap's properties
	GetObject(hbmNewBitMap,sizeof(BITMAP),(LPVOID)&bmp);
	//grab the width & height
	iWidth=bmp.bmWidth;
	iHeight=bmp.bmHeight;

	SelectObject(hdcMem, hbmOldBitMap);
	DeleteDC(hdcMem);
}

void BitMapObject::Create(HDC hdcCompatible, int width, int height)
{
	//if hdcMemory isn't null, blow it up!
	if(hdcMemory)
		Destroy();

	//create the memory dc.
	hdcMemory=CreateCompatibleDC(hdcCompatible);
	//create the bitmap
	hbmNewBitMap=CreateCompatibleBitmap(hdcCompatible, width, height);
	//shove the image into the dc
	hbmOldBitMap=(HBITMAP)SelectObject(hdcMemory, hbmNewBitMap);
	//change the width and height.
	iWidth=width;
	iHeight=height;
}

void BitMapObject::Destroy()
{
	//restore old bitmap.
	SelectObject(hdcMemory, hbmOldBitMap);
	//delete new bitmap.
	DeleteObject(hbmNewBitMap);
	//delete device context.
	DeleteDC(hdcMemory);
	//set members to 0/NULL
	hdcMemory=NULL;
	hbmNewBitMap=NULL;
	hbmOldBitMap=NULL;
	iWidth=0;
	iHeight=0;
}

BOOL BitMapObject::Collision(int x, int y)
{
	if(VCollision[x][y] == 0)
	{
		return FALSE;
	}
	else if(VCollision[x][y] == 1)
	{
		return TRUE;
	}
	return FALSE;
}
BitMapObject::operator HDC()
{
	//return hdcMemory.
	return(hdcMemory);
}

int BitMapObject::GetWidth()
{
	//return width
	return(iWidth);
}

int BitMapObject::GetHeight()
{
	//return height
	return(iHeight);
}




BitMapObject.h
/*******************************************************************************
The BitMapObject class was borrowed from Evil_Greven's "Tetris clone in an
hour with C++" tutorial on gamedev.net. You can find it here...
http://www.gamedev.net/community/forums/topic.asp?topic_id=192483
I have modified it slightly to suit my needs
********************************************************************************/

#ifndef BITMAPOBJECT_H
#define BITMAPOBJECT_H

#include <windows.h>
#include <vector>

class BitMapObject
{
private:
	//new bitmap!
	HBITMAP hbmNewBitMap;
	//old bitmap.
	HBITMAP hbmOldBitMap;	
	//width & height as integers.
	int iWidth;
	int iHeight;
	
	//This will be our transparent color
	COLORREF hotPink;
	
public:
	
	//memory dc
	HDC hdcMemory;
	//constructor
	BitMapObject();

	//destructor
	~BitMapObject();

	//loads bitmap from a file
	void Load(HWND hwnd, WORD lpszFilename, BOOL trans=FALSE);
	
	//load bitmap mask
	void Load(WORD lpszFilename);

	//creates a blank bitmap
	void Create(HDC hdcCompatible, int width, int height);
	
	//test for collision based on transparancy of pixel
	BOOL Collision(int x, int y);

	//destroys bitmap and dc
	void Destroy();

	//return width
	int GetWidth();

	//return height
	int GetHeight();

	//converts to HDC
	operator HDC();
	
	//a vector to hold collison info
	std::vector< std::vector<int> > VCollision;
};

#endif




[Edited by - mrtie_dye on June 12, 2005 11:37:42 PM]

Share this post


Link to post
Share on other sites
Advertisement
Quote:

/*******************************************************************************
The BitMapObject class was borrowed from Evil_Greven's "Tetris clone in an
hour with C++" tutorial on gamedev.net. You can find it here...
http://www.gamedev.net/community/forums/topic.asp?topic_id=192483
I have modified it slightly to suit my needs
********************************************************************************/

clicky

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!