Game Development Reference
In-Depth Information
return (rhs.ID() == ID());
}
bool operator != (const grid_game_item& rhs) const
{
return (rhs.ID() != ID());
}
unsigned int ID() const { return m_id; }
private:
std::shared_ptr<render::texture> m_texture;
unsigned int m_id;
};
The important part about the drawing is in the calculation of the position where to draw.
The slot's x s ,y s position is given by:
Where x g ,y g the top, left screen coordinates of the grid itself. The slot's index is given by
m,n and the width and height of each slot by w,h . In this example the width and height are
extracted from the texture, this means that we could have a problem if the texture sizes for
each texture used is not the same. Ideally, the slot size should be configurable within the
grid class itself, and on construction of every grid item, we pass a constant reference to the
grid that owns it. Upon drawing, we can query the size from the grid, and scale our im-
age accordingly. On interesting benefit of this approach is that if we were to move an item
between grids of different sizes, the item would still draw correctly regardless of which
grid it is on.
When we draw the grid, we pass a reference to the slot down to the items' draw function,
this allows us to fetch some information from the slot in case we want to display it. After
drawing the grid's item, we display on its top, left corner its count, or how many instances
of the item are held by this slot.
std::wostringstream ss;
ss << slot.Count();
std::wstring itemCount = ss.str();
font->DrawString(spriteBatch.get(), itemCount.c_str() rc.Position());
First we use the std::wostringstream class to format a string that only contains the item
count in that slot, then we draw the string at the top, left corner of the item.
Search WWH ::




Custom Search