Epic failures in the game development field.

This blog covers funny/weird issues and bugs from games that happened during development.

Send your own contributions to igetyourfail at repi.se (can be anonymous if specified)

Sunday, January 27, 2013

Strings - a fundamental component of floating point math


namespace uiVehicleComp
{
      //Convert double, float to a string with given number of decimals.
      template  T>
      eastl::string toStrDec(T const& val, const int decimals)
      {
            MallocScope ms;
            std::ostringstream ostr;
            ostr << std::fixed << std::setprecision(decimals) << val;
            return ostr.str().c_str();
      }
}


void UIVehicleComp::updateVehicleYawRotations(const GameInfo& gameInfo)
{
      using namespace uiVehicleComp;

      if (gameInfo.vehicle && gameInfo.hudData && gameInfo.hudData->getUseWeaponOrientations())
      {
            eastl::string currentYaw = toStrDec(gameInfo.vehicleYawAngle, 1);

            if(0 != currentYaw.compare(m_yawAngle))
            {
                  IUISystem::getInstance()->setData(UI_UIVEHICLECOMP_YAWANGLE, (double)atof(currentYaw.c_str()));
                  m_yawAngle = currentYaw;
            }
      }
}


7 comments:

NeARAZ said...

My eyes!

bionic said...

Dynamic strings FTW! Best way ever to round a number. Only costs... probably around 40k cycles or so, perhaps only about three orders of magnitude more than the engage-your-brain method of doing it!

Big McLargeHuge said...

You should be thankful he didn't do it with boost::lexical_cast(). I've seen a game which dragged the whole of boost along so it could use lexical_cast and the "cross-platform" thread handling crap.

Mircea Marghidanu said...

so sad... :(

dep said...

Best part - returning c_str() from a temporary which is destroyed before the function returns..

Jasper Bekkers said...

@dep you missed the part where it gets converted back into a eastl::string implicitly

nevyn said...

Memory management - you're supposed to treat it like magic, right?

Followers