#include <iostream>
#include <SFML/Graphics.hpp>
int main()
{
sf::Vector2u windowSize{ 1920u, 1080u };
sf::Font font;
if (!font.loadFromFile("D:/resources/fonts/arial.ttf"))
{
std::cerr << "ERROR LOADING FONT" << std::endl;
return EXIT_FAILURE;
}
sf::Text text;
text.setFont(font);
text.setCharacterSize(30u);
text.setFillColor(sf::Color::White);
text.setString("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
const sf::FloatRect textLocalBounds{ text.getLocalBounds() };
text.setOrigin(textLocalBounds.left + (textLocalBounds.width / 2.f), textLocalBounds.top + (textLocalBounds.height / 2.f));
text.setPosition(sf::Vector2f(windowSize / 2u));
sf::Text feedbackText;
feedbackText.setFont(font);
feedbackText.setCharacterSize(36u);
feedbackText.setFillColor(sf::Color::Green);
feedbackText.setPosition({ 5.f, 5.f });
sf::RenderWindow window(sf::VideoMode(windowSize.x, windowSize.y), "");
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
switch (event.type)
{
case sf::Event::Closed:
window.close();
break;
case sf::Event::KeyPressed:
switch (event.key.code)
{
case sf::Keyboard::Escape:
window.close();
break;
}
break;
}
}
char characterUnderMouse = '\n';
const sf::Vector2f mousePosition{ window.mapPixelToCoords(sf::Mouse::getPosition(window)) };
if (text.getGlobalBounds().contains(mousePosition))
{
for (std::size_t i{ 0u }; i < text.getString().getSize(); ++i)
{
if (text.findCharacterPos(i).x > mousePosition.x)
break;
characterUnderMouse = text.getString()[i];
}
}
if (characterUnderMouse != '\n')
feedbackText.setString(std::string("Character Under Mouse: ") + characterUnderMouse);
else
feedbackText.setString("");
window.clear();
window.draw(text);
window.draw(feedbackText);
window.display();
}
}
NOTE: You'd obviously need to change the font location and possibly the size of the window (I'm using a 4K resolution) and texts.