Skip to content

Instantly share code, notes, and snippets.

@Cleroth
Created February 11, 2016 21:15
Show Gist options
  • Save Cleroth/5dbf5c4c03ddee9e59ef to your computer and use it in GitHub Desktop.
Save Cleroth/5dbf5c4c03ddee9e59ef to your computer and use it in GitHub Desktop.
const auto & font = app->_fonts[0];
auto prev_view = tgt->getView();
// GetPos() gets the unit's world position (shape world position for you)
auto world_pos = GetPos();
world_pos.y -= 11 + font.size;
auto screen_pos = tgt->mapCoordsToPixel(world_pos);
auto view = tgt->getDefaultView();
view.setSize({(float)app->GetResolution().x, (float)app->GetResolution().y});
tgt->setView(view);
auto pos = tgt->mapPixelToCoords(screen_pos);
// Draw name
sf::Text text(GetName(), font.font, font.size);
auto bounds = text.getGlobalBounds();
text.setOrigin({bounds.width / 2, bounds.height});
text.setColor({255, 255, 255});
text.setPosition(pos);
tgt->draw(text);
tgt->setView(prev_view);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment