Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Displaying An Image Using ::Image  (Read 11631 times)

0 Members and 1 Guest are viewing this topic.

DhimanOmega

  • Newbie
  • *
  • Posts: 5
    • View Profile
Displaying An Image Using ::Image
« on: October 23, 2015, 08:59:56 am »
I am trying to Load and display  an image from file and then  changing all its pixels one by one to black. After changing all pixels program should display the new image. But it only shows the original image and nothing else.
Where am i doing wrong?

Image Dimensions are 1280 720



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


int main()
{  
       
        sf::RenderWindow window(sf::VideoMode(800, 600), "SFML ");
       
        sf::Image image;
        if (!(image.loadFromFile("map.jpg")))
                std::cout << "Cannot load image";   //Load Image
       
        sf::Texture texture;
        texture.loadFromImage(image);  //Load Texture from image

        sf::Sprite sprite;
        sprite.setTexture(texture);    


        while (window.isOpen())
        {
               
                sf::Event event;
               
                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                                window.close();
                       
                        }//Event handling done
                       

                for (int i = 0; i < 1280; i++)
                {
                        for (int j = 0; j < 720; j++)
                                image.setPixel(i, j, sf::Color::Black);
                }

                window.clear();
                window.draw(sprite);
                window.display();
                }


        return 0;
}


 

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Displaying An Image Using ::Image
« Reply #1 on: October 23, 2015, 09:06:29 am »
An sf::Image doesn't stay "connected" to an sf::Texture. If you want the texture to update, you need to load the image again into a texture. Keep in mind that this is a heavy operation. If you just want to do some pixel manipulation it's better to use a shader instead.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

DhimanOmega

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Displaying An Image Using ::Image
« Reply #2 on: October 23, 2015, 09:16:47 am »
Okay , So i did this and it works also

Are you saying that this is not performance effective?


                for (int i = 0; i < 1280; i++)
                {
                        for (int j = 0; j < 720; j++)
                                image.setPixel(i, j, sf::Color::Black);
                }

                texture.loadFromImage(image);            //loading the image again into texture
               

                window.clear();
                window.draw(sprite);
                window.display();
                }


        return 0;
}
 

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Displaying An Image Using ::Image
« Reply #3 on: October 23, 2015, 10:27:55 am »
A texture lives in the VRAM while an image lives in the RAM. Every time you update a texture your PC has to move the data from the CPU to the GPU which can be limiting. It's fast enough for a lot of things, but it's still something to keep in mind, so one doesn't build a big system around syncing lots of images etc. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

DhimanOmega

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Displaying An Image Using ::Image
« Reply #4 on: October 23, 2015, 12:30:49 pm »
Thank you , I will keep that in mind.