Game Development Reference
In-Depth Information
thatbothcomputesthedistanceandnormalizesthevector,asitis,wearecurrentlycomput-
ing the vector's magnitude twice. This is a reason why it is important to understand what
the math functions are doing under the hood.
std::wstring distanceStr = helper::stringutils::FormatStringW(L"%.2f m", distance);
vector2 textExtents = vector2(m_font->MeasureString(distanceStr.c_str()));
m_spriteBatch->Begin();
m_font->DrawString(m_spriteBatch.get(), distanceStr.c_str(), screenPosition - (textExtents * 0.5f), m_textColor);
m_spriteBatch->End();
m_iconSprite->Play(m_animations[Default]);
auto frameSize = m_iconSprite->FrameSize() / 2;
m_iconSprite->Draw(screenPosition - frameSize, entity->Color());
In our example, each game unit represents one meter, so we format a string to display a
floating point value forthe distance, wemeasure it inordertocenter the text onthe projec-
ted screen position and finally we draw the icon's sprite centered as well.
This is the bare minimum code we need in order to render any type of information from
the world space projected onto the screen. We can take this further by instead of simply
ignoring items that are beyond the screen extents to constraining them either with a rect-
angle within the screen or an ellipse that encompasses the majority of the screen space.
Depending onthe type ofgame, this is a useful navigation aid fortracking targets orpoints
of interest that is easier to follow than mini maps may be.
Search WWH ::




Custom Search