Game Development Community

dev|Pro Game Development Curriculum

Masked Bitmap Button

by Sasha Djurovic · 09/10/2001 (8:28 pm) · 1 comments

Download Code File

It works like this: you make a bitmap button the way Harold prescribed (you can get his mod here, or from this forum thread.). You add another .png file, which serves as a mask. From within a script you then call two functions added to GuiBitmapCtrl::setMask (name) which sets the mask for button where mouse is, and getMask (x,y) which gives you "red green blue alpha" string representing color of point underneath the cursor. These two functions are separated because we have to check for color within onMouseMove routine and we don't want to reload mask bmp every time we move a mouse. Once you have a color at certain point you can use it for many things: to execute commands, switch bitmaps etc.

In order to get everything working unzip the file and install Harold's mod in /example directory. Replace /example/Labrat/ui/ MainMenuGui.gui with one included in zip file and put player-***.jpg files in /example/Labrat/gui directory (you should use .png files tho, I used jpg for posting reasons.

Start your compiler and open GuiBitmapCtrl.cc file. Around line 48 add:
static bool cBitmapSetMask(SimObject *obj, S32, const char **argv)
{
   GuiBitmapCtrl *ctrl = static_cast<GuiBitmapCtrl*>(obj);
   return(ctrl->setMask(argv[2]));
}

const char* cBitmapGetMask(SimObject *obj, S32, const char **argv)
{
   GuiBitmapCtrl *ctrl = static_cast<GuiBitmapCtrl*>(obj);
   return(ctrl->getMask(dAtoi(argv[2]), dAtoi(argv[3])));
}
Next, around line 60 within:
void GuiBitmapCtrl::consoleInit()
{
...
}
add the following:
Con::addCommand("GuiBitmapCtrl", "setMask",  cBitmapSetMask,   "guiBitmapCtrl.SetMask(name)", 3, 3);
   Con::addCommand("GuiBitmapCtrl", "getMask",  cBitmapGetMask,   "guiBitmapCtrl.GetMask(xmouse,ymouse)", 4, 4);

Around line 100 add:
bool GuiBitmapCtrl::setMask(const char *name)
{
	bool Mipped=false;
	ResourceObject *obj;
	//load name.bmp
	obj= ResourceManager->load(name,false);
	if (!obj)
		return NULL;
	bmp = (GBitmap*) obj->mInstance;
	if(bmp==NULL)
		return false;
	//check for mip levels
	if (bmp->numMipLevels>1)
		Mipped=true;
	//allocate bmp so we can play with it
	bmp->allocateBitmap(bmp->getWidth(), bmp->getHeight(), Mipped, bmp->internalFormat);
	return true;
}
 
const char *GuiBitmapCtrl::getMask(const U32 x, const U32 y)
{
	//empty previous buffer, should we use memset?
	dSprintf (StringBuffer,sizeof(StringBuffer),"");
	//we can't call getColor on NULL bmp
	if(bmp==NULL)
		return &StringBuffer[0];
	ColorI rColor;
	bmp->getColor(x,y,rColor);	
	dSprintf(StringBuffer,sizeof(StringBuffer),"%d %d %d %d", rColor.red, rColor.green, rColor.blue, rColor.alpha);
	const char* color=&StringBuffer[0];
	return color;
}

Now open GuiBitmapCtrl.h and add:

Protected:
char StringBuffer[1024];
GBitmap *bmp;
And also:
Public:
bool setMask(const char *name);
const char *getMask(const U32 x, const U32 y);

Compile it and start mod with command line parameter -mod LabRat

Regards Sasha

#1
02/03/2005 (12:42 pm)
i found this resource a bit lacking and out of date, so i wrote a similar solution that meshes well with the rest of torques gui stuff
http://www.garagegames.com/mg/forums/result.thread.php?qt=25683