Game Development Reference
In-Depth Information
rectangle textureRect = rectangle(0.f, 0.f, m_background->Width(), m_background->Height());
float zoom = element.Zoom();
rectangle sourceRect = rectangle(0.f, 0.f, textureRect.Width() / zoom, textureRect.Height() / zoom);
auto halfTextureWidth = (textureRect.Width() / 2);
auto halfTextureHeight = (textureRect.Height() / 2);
sourceRect.Left() = halfTextureWidth + (playerPos2D.x() - (sourceRect.Width() / 2));
sourceRect.Top() = halfTextureHeight + (playerPos2D.y() - (sourceRect.Height()/2));
The source rectangle 's size will be the texture size divided by the zoom factor, this means
that when the zoom factor is 1, the source rectangle has the same size as the texture and
will be drawn in a 1:1 scale.
Calculating the source rectangle's top/left coordinates is slightly more complicated. If the
player's world position is at the origin (defined by its 2D coordinates in a top-down view),
we can calculate the top/left corner of the source rectangle by offsetting the player's pos-
ition by half the size of the source rectangle , this gives us the top/left corner still in what
would be world space coordinates. We then offset this coordinate by half of the texture's
width (or height, respectively), this gives us the correct top/left coordinates within the map
texture's space.
Having set the position of the player we will draw the background using the destination
rectangle and the source rectangle we calculated, we will also rotate it using the player's
angle.
auto destinationRect = m_rectangle;
destinationRect.Offset(m_rectangle.Width()*0.5f, m_rectangle.Height()*0.5f);
DirectX::CommonStates states(device->GetDevice());
m_spriteBatch->Begin(DirectX::SpriteSortMode_Deferred, states.NonPremultiplied());
{
m_spriteBatch->Draw(*m_background->GetView(), destinationRect, &((RECT)sourceRect), render::color::WHITE, playerAngle, Direc-
tX::XMFLOAT2(sourceRect.Width()*0.5f, sourceRect.Height()*0.5f));
}
Now that we have drawn the background we will begin iterating over all the entities in our
world in order to gather enough information from them and draw them in our mini map.
for (auto& entity : element.World().Entities())
{
if (entity->Type() == game::entity::EntityType::Player)
continue;
vector2 entityPos2D = vector2(entity->Position().x(), -entity->Position().z());
vector2 directionToEntityXZ = (entityPos2D - playerPos2D) * (zoom/4);
directionToEntityXZ = rotationMatrix.Rotate(directionToEntityXZ, playerAngle );
Search WWH ::




Custom Search