This is the actual Android example. You don't have to write any platform specific code, unless you want to use things not directly exposed. ('vibrate()' in this example; undefine 'USE_JNI' above to disable it)
| 80 | // specific code, unless you want to use things not directly exposed. |
| 81 | // ('vibrate()' in this example; undefine 'USE_JNI' above to disable it) |
| 82 | int main(int argc, char* argv[]) |
| 83 | { |
| 84 | sf::VideoMode screen(sf::VideoMode::getDesktopMode()); |
| 85 | |
| 86 | sf::RenderWindow window(screen, ""); |
| 87 | window.setFramerateLimit(30); |
| 88 | |
| 89 | const sf::Texture texture("image.png"); |
| 90 | |
| 91 | sf::Sprite image(texture); |
| 92 | image.setPosition(sf::Vector2f(screen.size) / 2.f); |
| 93 | image.setOrigin(sf::Vector2f(texture.getSize()) / 2.f); |
| 94 | |
| 95 | const sf::Font font("tuffy.ttf"); |
| 96 | |
| 97 | sf::Text text(font, "Tap anywhere to move the logo.", 64); |
| 98 | text.setFillColor(sf::Color::Black); |
| 99 | text.setPosition({10, 10}); |
| 100 | |
| 101 | sf::View view = window.getDefaultView(); |
| 102 | |
| 103 | sf::Color background = sf::Color::White; |
| 104 | |
| 105 | // We shouldn't try drawing to the screen while in background |
| 106 | // so we'll have to track that. You can do minor background |
| 107 | // work, but keep battery life in mind. |
| 108 | bool active = true; |
| 109 | |
| 110 | while (window.isOpen()) |
| 111 | { |
| 112 | while (const std::optional event = active ? window.pollEvent() : window.waitEvent()) |
| 113 | { |
| 114 | if (event->is<sf::Event::Closed>() || |
| 115 | (event->is<sf::Event::KeyPressed>() && |
| 116 | event->getIf<sf::Event::KeyPressed>()->code == sf::Keyboard::Key::Escape)) |
| 117 | { |
| 118 | window.close(); |
| 119 | } |
| 120 | |
| 121 | else if (const auto* resized = event->getIf<sf::Event::Resized>()) |
| 122 | { |
| 123 | const auto size = sf::Vector2f(resized->size); |
| 124 | view.setSize(size); |
| 125 | view.setCenter(size / 2.f); |
| 126 | window.setView(view); |
| 127 | } |
| 128 | else if (event->is<sf::Event::FocusLost>()) |
| 129 | { |
| 130 | background = sf::Color::Black; |
| 131 | } |
| 132 | else if (event->is<sf::Event::FocusGained>()) |
| 133 | { |
| 134 | background = sf::Color::White; |
| 135 | } |
| 136 | // On Android MouseLeft/MouseEntered are (for now) triggered, |
| 137 | // whenever the app loses or gains focus. |
| 138 | else if (event->is<sf::Event::MouseLeft>()) |
| 139 | { |
nothing calls this directly
no test coverage detected