How to open an image in C ++ using SFML?

3

I'm still at the beginning and need to open an image in C ++ using SFML, at the moment I'm using a code that uses the image address, but all this via code and what I need is that the address where the image is be informed by the user.

Example:

c:\desktop\teste.png

The code I want to modify is this:

    sf::Image imagem;
if (!imagem.loadFromFile("imagem.png")) {
    cout<<"Erro ao abrir a imagem!"<<endl;
}
    
asked by anonymous 06.05.2014 / 23:33

2 answers

4

SFML alone offers no way to prompt the user to select an image. But, you can use an Image Selection Dialog from a toolkit such as Qt, wxWidgets or use the API of your program's own target system.

For Windows (using WIN32 API) you can open a window to select an image using the following function:

#include <Windows.h> // Para a janela de selecionar o arquivo
#include <string>
#include <cstring>

#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>

sf::Texture LoadTexture(void)
{
    OPENFILENAME ofn;

    ZeroMemory(&ofn, sizeof(ofn));

    ofn.lStructSize = sizeof(ofn);
    ofn.hwndOwner = 0;
    ofn.lpstrDefExt = 0;
    ofn.lpstrFile = new TCHAR[512];
    ofn.lpstrFile[0] = '
sf::Texture textura = LoadTexture(); // chama a função anterior.

// Vincula a texture ao sprite.
sf::Sprite sprite;
sprite.setTexture(textura);
'; ofn.nMaxFile = 512; ofn.lpstrFilter = NULL; ofn.nFilterIndex = 0; ofn.lpstrInitialDir = NULL; ofn.lpstrTitle = L"Selecione uma Imagem"; ofn.Flags = 0; GetOpenFileName(&ofn); // Converte para std::string. std::wstring wstr = ofn.lpstrFile; std::string str(wstr.begin(), wstr.end()); // Armazenará a imagem. sf::Texture texture; // Verifica se o arquivo é válido e se foi possível carregar. if (!texture.loadFromFile(str)) { MessageBox(NULL, L"Imagem inválida!", L"Erro", MB_OK); } return texture; }

Then, bind to sf::Sprite :

window.draw(sprite);

And finally, just draw:

#include <Windows.h> // Para a janela de selecionar o arquivo
#include <string>
#include <cstring>

#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>

sf::Texture LoadTexture(void)
{
    OPENFILENAME ofn;

    ZeroMemory(&ofn, sizeof(ofn));

    ofn.lStructSize = sizeof(ofn);
    ofn.hwndOwner = 0;
    ofn.lpstrDefExt = 0;
    ofn.lpstrFile = new TCHAR[512];
    ofn.lpstrFile[0] = '
sf::Texture textura = LoadTexture(); // chama a função anterior.

// Vincula a texture ao sprite.
sf::Sprite sprite;
sprite.setTexture(textura);
'; ofn.nMaxFile = 512; ofn.lpstrFilter = NULL; ofn.nFilterIndex = 0; ofn.lpstrInitialDir = NULL; ofn.lpstrTitle = L"Selecione uma Imagem"; ofn.Flags = 0; GetOpenFileName(&ofn); // Converte para std::string. std::wstring wstr = ofn.lpstrFile; std::string str(wstr.begin(), wstr.end()); // Armazenará a imagem. sf::Texture texture; // Verifica se o arquivo é válido e se foi possível carregar. if (!texture.loadFromFile(str)) { MessageBox(NULL, L"Imagem inválida!", L"Erro", MB_OK); } return texture; }

    
07.05.2014 / 02:58
4

I do not know SFML in depth, but because it is a library for accessing multimedia resources I do not think it has the resources to make it easy to request the filename for the user (or at least it should be very difficult to build this using class Window ).

One alternative is to use a graphical library such as Qt . You could build your own graphical interface for this, but it is quite common (and makes it easier for users accustomed to the operating system) to use the default file selection window.

With Qt you can do the following:

#include <QFileDialog>
QString arquivo = QFileDialog::getOpenFileName(this, "Abrir imagem", ".", "Arquivos PNG (*.png);; Todos os arquivos (*.*)"));
if(arquivo.length())
{
    sf::Image imagem;
    if (!imagem.loadFromFile(arquivo.toStdString())) {
        cout<<"Erro ao abrir a imagem!"<<endl;
    }
}

This code will open a file selection window like this below (example played of that OS thread in English - I did not run the code here), where the user can select the existing file or even type the path. When you click the OK button (in the image it is in another language, but it does not matter), the window is closed and the QFileDialog::getOpenFileName method returns a string with the path and file name selected. Just use it in the call of the 'Image :: loadFromFile' method of the SFML library, as shown above.

    
07.05.2014 / 00:29