How to open a C++ image using SFML?

Asked

Viewed 1,301 times

3

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

Example:

c:\desktop\teste.png

the code I intend to modify and this:

    sf::Image imagem;
if (!imagem.loadFromFile("imagem.png")) {
    cout<<"Erro ao abrir a imagem!"<<endl;
}
  • Your question is not very clear. It seems that the question is about how to ask the user to choose a file, not how to open a C++ image with SFML. That’s it?

2 answers

4

I don’t know the SFML in depth, but because it is a library for access to multimedia resources I believe it has no resources to facilitate the request of the file name to the user (or at least it must be very difficult to build it using the class Window).

An 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 facilitates the use of users accustomed to the operating system) to utilize the standard file selection window.

With Qt you can do it this way:

#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 the one below (example reproduced of that OS thread in English - I didn’t get to execute the code here), where the user can select the existing file or even type the path. By clicking the OK button (the image is in another language, but it doesn’t matter), the window is closed and the method QFileDialog::getOpenFileName returns a string with the path and name of the selected file. Just use it in the `Image::loadFromFile' method of the SFML library, as illustrated above.

inserir a descrição da imagem aqui

  • 2

    As much as it’s a correct, easy and portable solution across multiple platforms, I don’t think it’s worth including Qt dependency just for a feature that can be implemented by other means. That gives a 15MB more dlls in windows.

  • 1

    @Guilhermebernal It is true. But in practice this will depend on factors not mentioned in the question, especially if the OP product needs to be portable or not and if the need for its graphical interface goes beyond just requesting a file name. Anyway, even if it’s not the best answer, it still seems nice to keep it for reference.

  • 1

    Thank you, I will take a look at both the library indicated and the other commented form. except that my need is that at least the image is loaded/ indicated by the user, this can be in code in the terminal or as Luiz published behind a library. Att

4

The SFML alone offers no way to ask the user to select an image. But, you can use an Image Selection Dialog from a Toolkit such as Qt, wxWidgets or using 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] = '\0';
    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 link to a sf::Sprite:

sf::Texture textura = LoadTexture(); // chama a função anterior.

// Vincula a texture ao sprite.
sf::Sprite sprite;
sprite.setTexture(textura);

And finally, just draw:

window.draw(sprite);

inserir a descrição da imagem aqui

  • 2

    Hahaha Meme very well placed! :)

  • This, this way I had seen in the forum of the library :D, I do not know if you would know me but I know that there is the possibility to search each pixel using the same library. what I’m doing is this, this correct: const sf::Uint8* pixels = imagem.getPixelsPtr(); Note: My goal is to traverse using a loop find pixels and then replace with characters from the ASCII table Thanks.Att

  • @user3609858 I don’t understand very well. Do you want to write a text on the image? Or replace certain pixels by another? If possible, ask a new question explaining exactly what you are not able to do, as solving this by the comments becomes complicated.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.