snipplets.dev/projects/SFML/fullscreen.cpp

59 lines
1.5 KiB
C++
Raw Permalink Normal View History

2024-02-24 19:29:12 +03:00
#include <iostream>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Graphics/Text.hpp>
int main() {
bool isFullscreen = false;
int screenWidth = sf::VideoMode::getDesktopMode().width;
int screenHeight = sf::VideoMode::getDesktopMode().height;
sf::Font font;
std::string fontPath = "/usr/share/fonts/truetype/ubuntu/Ubuntu-R.ttf";
if (!font.loadFromFile(fontPath)) {
std::cout << "Error loading font: " << fontPath << std::endl;
exit(EXIT_FAILURE);
}
sf::Text text;
text.setFont(font);
text.setCharacterSize(24);
text.setFillColor(sf::Color::White);
text.setString("Press F keyboard button to toggle fullscreen mode");
sf::RenderWindow window;
window.create(sf::VideoMode(640, 480), "Fullscreen example");
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed)
window.close();
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
window.close();
if (sf::Keyboard::isKeyPressed(sf::Keyboard::F)) {
isFullscreen = !isFullscreen;
window.close();
if (isFullscreen)
window.create(sf::VideoMode(screenWidth, screenHeight),
"Fullscreen example", sf::Style::Fullscreen);
else
window.create(sf::VideoMode(640, 480), "Fullscreen example",
sf::Style::Default);
}
}
window.clear(sf::Color::Black);
window.draw(text);
window.display();
}
return EXIT_SUCCESS;
}