Cet article appartient à une série de tutoriels dédiés à SFML. Le lecteur est invité à se référer aux autres sections de ce tutoriel en suivant ce lien : SFML tutoriel.
Cette page présente comment obtenir toutes les résolutions disponibles avec SFML. L'exemple présenté sur cette page a été réalisé avec les versions suivantes :
Cette vidéo vous donne un aperçu du résultat :
La fonction getFullscreenModes() renvoit un tableau (std::vector)
qui contient tous les modes vidéos supportés en mode plein écran.
Une fois que la fonction a été appellée, le tableau est complété et nous pouvons simplement accéder aux attributs suivants :
widht: largeur du mode vidéo, en pixels.height: hauteur du mode vidéo, en pixels. bitsPerPixel: profondeur de couleur, en bits par pixel.Il est aussi possible d'obtenir la résolution courante du bureau avec la function getDesktopMode().
Cette fonction retourne un unique VideoMode
contenant les mêmes attributs
que précédemment.
Le programme suivant liste toutes les résolutions disponibles en mode plein écran at affiche, à la fin, la résolution actuelle du bureau.
/*!
* \file main.cpp
* \brief start SFML in full screen and display a sprite (https://lucidar.me/en/sfml/sfml-part-3-display-a-sprite/)
* \author Philippe Lucidarme
* \version 1.0
* \date 13/07/2018
*/
// SFML libraries
#include <SFML/Graphics.hpp>
#include <iostream>
int main()
{
// _____________________
// ::: Create window :::
// Create a window with the same pixel depth as the desktop
sf::VideoMode desktopMode = sf::VideoMode::getDesktopMode();
sf::RenderWindow window(sf::VideoMode( desktopMode.width,
desktopMode.height,
desktopMode.bitsPerPixel),
"SFML part 3",
sf::Style::Fullscreen);
// Enable vertical sync. (vsync)
window.setVerticalSyncEnabled (true);
// ____________________
// ::: Load texture :::
// Create texture from PNG file
sf::Texture texture;
if (!texture.loadFromFile("../sfml-icon-small.png"))
{
std::cerr << "Error while loading texture" << std::endl;
return -1;
}
// Enable the smooth filter. The texture appears smoother so that pixels are less noticeable.
texture.setSmooth(true);
// _______________________________________
// ::: Create sprite and apply texture :::
// Create the sprite and apply the texture
sf::Sprite sprite;
sprite.setTexture(texture);
sf::FloatRect spriteSize=sprite.getGlobalBounds();
sprite.setOrigin(spriteSize.width/2.,spriteSize.height/2.);
// _________________
// ::: Main loop :::
sf::Clock timer;
while (window.isOpen())
{
// Display fps
std::cout << int(1./timer.restart().asSeconds()) << " fps" << std::endl;
// Process events
sf::Event event;
while (window.pollEvent(event))
{
// Close the window if a key is pressed or if requested
if (event.type == sf::Event::Closed) window.close();
if (event.type == sf::Event::KeyPressed) window.close();
}
// Clear the window and apply grey background
window.clear( sf::Color(127,127,127));
// Draw the sprite in the middle of the screen
sprite.setPosition(window.getSize().x/2., window.getSize().y/2.);
window.draw(sprite);
// Update display and wait for vsync
window.display();
}
return 0;
}
Le projet Qt pour cet exemple peut être téléchargé ci-dessous :