Game Development Community

GBitmap getColor problem

by Stefan Beffy Moises · in Torque Game Engine · 05/19/2006 (5:17 am) · 1 replies

Hi,
I've got a problem with GBitmap always returning plain white (255 255 255) when trying to get the color of a bitmap...
This takes place in a custom GUI object:
void GuiGenericBarHud::setBitmap(const char *name)
{
   mRefBitmapName = StringTable->insert(name);
   if (*mRefBitmapName) {
      mRefTextureHandle = TextureHandle(mRefBitmapName, new GBitmap(8, 64, false, GBitmap::RGB));
   }
   else
      mRefTextureHandle = NULL;
}

void GuiGenericBarHud::onRender(Point2I offset, const RectI &updateRect)
{
   S32 time = Platform::getVirtualMilliseconds();
   S32 dt = time - mLastRenderTime;
   mLastRenderTime = time;

   if (mCurValue > mValue)
   {
      mCurValue -= (mMaxValue - mMinValue) * (dt * mDrainSpeed) / 1000.f;

      if (mCurValue < mValue)
         mCurValue = mValue;
   }
   else if (mCurValue < mValue)
   {
      mCurValue += (mMaxValue - mMinValue) * (dt * mFillSpeed) / 1000.f;

      if (mCurValue > mValue)
         mCurValue = mValue;
   }

   // use custom color from a reference bitmap?
   if(dStrlen(mRefBitmapName) > 0)
   {
      GBitmap* bmp;
      ColorI rColor;

      if(!mRefTextureHandle)
         mRefTextureHandle = TextureHandle(mRefBitmapName, new GBitmap(8, 64, false, GBitmap::RGB));
      if(mRefTextureHandle)
      {
         bmp = mRefTextureHandle.getBitmap();
         //bmp = (GBitmap*)ResourceManager->loadInstance( mRefBitmapName );
         if(!bmp)
         {
            Con::errorf("Unable to load bitmap!");
            setBitmap(mRefBitmapName);
            bmp = mRefTextureHandle.getBitmap();
         }
         if(bmp)
         {
            bool foundColor = bmp->getColor(bmp->getWidth()-1, bmp->getHeight()-1, rColor);
            //bmp->getColor(4, (U32)(mCurValue * 100), rColor);
            // set colors
            mFillColor.red = rColor.red / 255;
            mFillColor.green = rColor.green / 255;
            mFillColor.blue = rColor.blue / 255;
            mFillColor.alpha = rColor.alpha / 255;
            //Con::printf("foundColor %i - mCurValue: %i - ColorI: %i %i %i %i - ColorF: %f %f %f %f", foundColor,(U32)(mCurValue*100),rColor.red,rColor.green,rColor.blue,rColor.alpha,mFillColor.red,mFillColor.green,mFillColor.blue,mFillColor.alpha);
         }
      }
      else
         Con::warnf("No mRefTextureHandle!");
   }

...

bool foundColor = bmp->getColor(bmp->getWidth()-1, bmp->getHeight()-1, rColor);

returns true, yet the color is always "white", no matter what bitmap I'm using (even tried standard terrain textures to make sure it isnt my texture)...
any ideas anyone??
Thx!

#1
05/19/2006 (9:21 am)
Ok, problem solved, it seemed to be that the texture wasn't kept in memory using GBitmap or BitmapTexture... using BitmapKeepTexture like this did the trick:
void GuiGenericBarHud::setBitmap(const char *name)
{
   mRefBitmapName = StringTable->insert(name);
   if (*mRefBitmapName)
   {
      mRefTextureHandle = TextureHandle(mRefBitmapName, BitmapKeepTexture);
      if(mRefTextureHandle)
         mBmp = mRefTextureHandle.getBitmap();
      else
         Con::errorf("Failed loading bitmap!");
   }
   else
      mRefTextureHandle = NULL;
}

void GuiGenericBarHud::onRender(Point2I offset, const RectI &updateRect)
{
   ...
   // use custom color from a reference bitmap?
   if (!mRefTextureHandle)
   {
      mRefTextureHandle = TextureHandle(mRefBitmapName, BitmapKeepTexture);
   }
   ColorI rColor;
   if(mRefTextureHandle)
   {
      if(!mBmp)
      {
         setBitmap(mRefBitmapName);
         mBmp = mRefTextureHandle.getBitmap();
      }
      if(mBmp)
      {
         F32 bHeight = mBmp->getHeight()-1;
         bool foundColor = mBmp->getColor(4, (U32)(bHeight-(mCurValue * bHeight)), rColor);
         // set colors
         mFillColor.red = rColor.red / 255.f;
         mFillColor.green = rColor.green / 255.f;
         mFillColor.blue = rColor.blue / 255.f;
         mFillColor.alpha = rColor.alpha / 255.f;
      }
   }
...

Thanks to the IRC guys for the help! ;)