Game Development Community

GuiClockHud updated T3D 1.0.1

by Bryce² · 11/18/2009 (3:47 pm) · 9 comments

This will give your guiClockHud hours minutes seconds milliseconds and an alarm!

the file is at
./T3D/fps/guiClockHud.cpp

It was more of a learning experience for me, so it may not really be useful to anybody.

Here are the changes

change class GuiClockHud : public GuiControl to...
class GuiClockHud : public GuiControl
{
   typedef GuiControl Parent;

   bool     mShowFrame;
   bool     mShowFill;
   bool		mShowExtendedTime; //changed by Bryce

   ColorF   mFillColor;
   ColorF   mFrameColor;
   ColorF   mTextColor;

   S32      mTimeOffset;
   S32		mAlarmVal;
   S32		mLast; //changed by Bryce

public:
   GuiClockHud();

   void setTime(F32 newTime);
   F32  getTime();
   F32	getTimeMS();

   void onRender( Point2I, const RectI &);
   static void initPersistFields();
   DECLARE_CONOBJECT( GuiClockHud );
   DECLARE_CATEGORY( "Gui Game" );
};

change GuiClockHud::GuiClockHud()
GuiClockHud::GuiClockHud()
{
   mShowFrame = mShowExtendedTime = mShowFill = true;
   mFillColor.set(0, 0, 0, 0.5);
   mFrameColor.set(0, 1, 0, 1);
   mTextColor.set( 0, 1, 0, 1 );

   mTimeOffset = 0;

   mAlarmVal = -1;

   mLast = 0;
}

change void GuiClockHud::initPersistFields()
void GuiClockHud::initPersistFields()
{
   addGroup("Misc");		
   addField( "showFill", TypeBool, Offset( mShowFill, GuiClockHud ) );
   addField( "showFrame", TypeBool, Offset( mShowFrame, GuiClockHud ) );
   addField( "fillColor", TypeColorF, Offset( mFillColor, GuiClockHud ) );
   addField( "frameColor", TypeColorF, Offset( mFrameColor, GuiClockHud ) );
   addField( "textColor", TypeColorF, Offset( mTextColor, GuiClockHud ) );
   endGroup("Misc");

   addGroup("Settings");
   addField( "showExtendedTime", TypeBool, Offset( mShowExtendedTime, GuiClockHud ) );
   addField( "alarm", TypeF32, Offset( mAlarmVal, GuiClockHud ) );
   endGroup("Settings");

   Parent::initPersistFields();
}

change void GuiClockHud::onRender(Point2I offset, const RectI &updateRect)
void GuiClockHud::onRender(Point2I offset, const RectI &updateRect)
{
   // Background first
   if (mShowFill)
      GFX->getDrawUtil()->drawRectFill(updateRect, mFillColor);

   // Convert ms time into hours, minutes and seconds.
   S32 time = S32(getTime());
   S32 timeMS = S32(getTimeMS());
   S32 milli = timeMS % 1000;
   S32 secs = time % 60;
   S32 mins = (time % 3600) / 60;
   S32 hours = (time % 216000) / 3600;
   S32 sAlarm = time % 60;
   S32 sAlarm2 = sAlarm - mLast;
	if(sAlarm2 == mAlarmVal)
	{
		sAlarm = 0;
		Con::executef( "onAlarm" );
		mLast = time % 60;
	}


   // "Currently only displays min/sec" haha not anymore (=P)
   char buf[256];
   if(mShowExtendedTime == true){
	   dSprintf(buf,sizeof(buf), "%02d:%02d:%02d:%03d",hours,mins,secs,milli);}
   else{
	   dSprintf(buf,sizeof(buf), "%02d:%02d",mins,secs);}

   // Center the text
   offset.x += (getWidth() - mProfile->mFont->getStrWidth((const UTF8 *)buf)) / 2;
   offset.y += (getHeight() - mProfile->mFont->getHeight()) / 2;
   GFX->getDrawUtil()->setBitmapModulation(mTextColor);
   GFX->getDrawUtil()->drawText(mProfile->mFont, offset, buf);
   GFX->getDrawUtil()->clearBitmapModulation();

   // Border last
   if (mShowFrame)
      GFX->getDrawUtil()->drawRect(updateRect, mFrameColor);
}

add this
F32 GuiClockHud::getTimeMS()
{
	//Return elapsed time in seconds.
	return F32(mTimeOffset + Platform::getVirtualMilliseconds());
}

and there you have it, an improved guiClockhud

to use the alarm all you need to do is make a function like this
function alarm();
{
   """stuff"""
}

About the author

Nimansoft, http://www.nimansoft.com/


#2
11/18/2009 (10:40 pm)
nice, i have a bomb!!!! ;D
#3
11/19/2009 (12:28 am)
Wow... nice to see this is actually useful to people :D
#4
11/19/2009 (11:53 am)
Updated the code, here are the changes:
dSprintf(buf,sizeof(buf), "%02d:%02d:%02d:%03d",hours,mins,secs,milli);}


mLast = time % 60;


S32 milli = timeMS % 1000;

F32		mAlarmVal;

addField( "alarm", TypeF32, Offset( mAlarmVal, GuiClockHud ) );

#5
11/20/2009 (1:25 pm)
Also, screenshots :P

nice idea for games with clocks.
#6
01/14/2010 (9:13 am)
Awesome, thank a ton man :)
#7
02/11/2010 (5:28 pm)
Cool, got the clock working showing milliseconds. Just a little fuzzy on how you set the starttime and alarm. When I added the clock it starts at 25 min and then just counted up. Any help would be appreciated, thanks
#8
02/14/2010 (3:46 am)
Yea its a little bug, but it only happens when you add the clock, if you restart your game the bug should be gone.
#9
02/14/2010 (5:53 am)
Thanks and sorry for the newbie question, but how do you get the alarm to show something and then stop the clock.