DEV Community

dwarfŧ
dwarfŧ

Posted on

5

displaying images in sfml

Image description

step 1
first of all you need to install SFML. I am using kali linux and i found the linux installation guide here.

step 2
next you need to make a window i made a guide here but i will give you the code anyway. Enter this code into your preferred text/code editor:



#include <SFML/Graphics.hpp>

int main()
{
    // create the window
    sf::RenderWindow window(sf::VideoMode(800, 600), "Hello window!");

    // run the program as long as the window is open
    while (window.isOpen())
    {
        // check all the window's events that were triggered since the last iteration of the loop
        sf::Event event;
        while (window.pollEvent(event))
        {
            // "close requested" event: we close the window
            if (event.type == sf::Event::Closed)
                window.close();
        }

        // clear the window with black color
        window.clear(sf::Color::Black);

        // draw everything here...
        // window.draw(...);

        // end the current frame
        window.display();
    }

    return 0;
}


Enter fullscreen mode Exit fullscreen mode

then check that the window will display by compiling it with:g++ main.cpp -o main -lsfml-graphics -lsfml-window -lsfml-system and run it with: ./main

step 3
first you will need to find an image to use. i will be using this one:
mushroom

step 4
now we will implement the code. this is what the code for displaying the image on the window should look like:

note: on line 8 change the Mushroom.png to your file of choice.



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

int main(){
        int x = 100;
        int y = 100;
        sf::RenderWindow window(sf::VideoMode(800, 600), "Hello Window");
        sf::Texture texture;
        if (!texture.loadFromFile("Mushroom.png")) {
                std::cout << "Could not load enemy texture" << std::endl;
                return 0;
        }
        while (window.isOpen()) {
                sf::Event event;
                while (window.pollEvent(event)) {
                        if (event.type == sf::Event::Closed)
                                window.close();
                }

                window.clear(sf::Color::Black);

                sf::Sprite image;
                image.setTexture(texture);
                image.setPosition(sf::Vector2f(x,y));
                image.scale(sf::Vector2f(1,1.5));
                window.clear();
                window.draw(image);

                window.display();

        }

        return 0;
}


Enter fullscreen mode Exit fullscreen mode

step 4
we will compile this by doing: g++ -o main main.cpp -lsfml-graphics -lsfml-window -lsfml-system and then run it with: ./main

step 5
enjoy and have a good day.

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay