how to add an existing image in SFML ?

http://www.sfml-dev.org/tutorials/2.1/graphics-sprite.php

A simple google or perusing the tutorial at the SFML site would show you what to do.
Imagine that you've already saved your image file in your hard disk as image.png. Then assuming that you've correctly included SFML/Graphics.hpp and enabled all requirements for running SFML, here's a snippet that might help you:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
int main()
{
  .
  .
  .
  sf::RenderWindow window(sf::VideoMode(640, 480, 32), "SFML");
  .
  .
  sf::Texture imageSource;
  if(!imageSource.loadFromFile("image.png"))
      return EXIT_FAILURE;
  sf::Sprite imageSprite;
  imageSprite.setTexture(imageSource);
  .
  .
  .
  while(window.isOpen())
  {
     sf::Event event;
     while(window.pollEvent(event))
     {
        . 
        .
     }

     window.draw(imageSprite);
     window.display();
  }

  return EXIT_SUCCESS;
}


Hope that helps!

I also recommend the tutorials at codingmadeeasy.ca
thanks for your help mirage , but its is not working well , it is giving of error! i.e. "project.exe has stop working" when i compile it it in "debug solution configuration ",

But when i compile it in "Release solution configuration " it is working properly ! :)
what is the issue ? should i make my whole project in "Release solution configuration" ???
Where are you keeping the image , during development keep it in the root directory of your project and only when you distribute it keep it with the executable.
thanks for your help mirage , but its is not working well , it is giving of error! i.e. "project.exe has stop working" when i compile it it in "debug solution configuration ",


Make sure you're linking to the debug libraries in your debug configuration. (They typically end with -d.)
amhndu i am not getting you , where should i paste/place my image ... ?
i am working on SFML 2.0 in Visual studio C++ 2010
cire i have done all linking in all, debug , and release configuration , but dont know why , this is still producing an error, :( .. any can help !?
Paste your image where your main project file is located .
For instance, Say i have my project file 'foo.vcproj' in c:/cpp/foo/
- this is the root directory for your project , so if i place my image on that directory as well i would refer to it with the address 'image.png' instead of the absolute path 'c:/cpp/foo/image.png', now if the image is further kept in the c:/cpp/foo/data/ directory i would refer to my image as 'data/image.png'.
EDIT: Wait, you said you are using vs2010 but this is the unix section , move this thread to windows section.
Last edited on
SFML (Simple and Fast Multimedia Library) is a portable and easy-to-use API for multimedia programming. SFML provides 2D graphics that are hardware accelerated with OpenGL and provides different modules made to ease programming games and multimedia applications. If you want to add image to your SFML program, you may need to download the image from the url and save it to your hard disk at first. Here is a tutorial that tells you how to download the image from the url:
http://www.rasteredge.com/how-to/vb-net-imaging/download-from-url/
Topic archived. No new replies allowed.