title: SFML load, modify, display bitmap
body: <p>I had to work all day to figure out how to load, modify, and display a bitmap.  Here's my working program  (Lubuntu, CodeBlocks 16.01).  I had to add these libraries:-lsfml-graphics,-lsfml-window, and -lsfml-system to project&gt; build options&gt; linker settings&gt; other linker options.  Any suggestions for improvements?</p>
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;SFML/Graphics.hpp&gt;
  
  using namespace std;
  using namespace sf;
  
  struct pix {
      unsigned char r;  // could use Uint8
      unsigned char g;
      unsigned char b;
      unsigned char t;
  };
  
  // globals
  
  pix pixarray[640][480];
  Texture tex;
  Sprite spr;
  
  // functions
  
  void loadbitmap();
  void modifybitmap();
  void displaybitmap();
  
  int main() {
  
      RenderWindow window(VideoMode(640, 480), &quot;Modify Bitmap&quot;);
  
      loadbitmap();
      modifybitmap();
      displaybitmap();
  
      while (window.isOpen()) {
  
          Event event;
          while (window.pollEvent(event)) {
  
              if (event.type == Event::Closed)
                  window.close();
          }
  
          window.clear();
  
          window.draw(spr);
  
          window.display();
      }
  
      return 0;
  }
  
  //-------------------------------------------------------
  
  void loadbitmap() {
  
      const unsigned char *pixptr;
      Image img;
      int i, j;
  
      img.loadFromFile(&quot;pic.bmp&quot;);
      pixptr = img.getPixelsPtr();   // notice its Pixels not Pixel
  
      // pixptr is read only, move pixels to your array
  
      for (i = 0; i &lt; 640; ++i) {
          for (j = 0; j &lt; 480; ++j) {
              pixarray[i][j].r = *pixptr++;
              pixarray[i][j].g = *pixptr++;
              pixarray[i][j].b = *pixptr++;
              pixarray[i][j].t = 255;
              pixptr++;
          }
      }
      return;
  }
  
  //---------------------------------------------------------
  
  void modifybitmap() {
  
  // turn up red in top half of your array
  
      int i, j;
  
      for (i = 0; i &lt; 320; ++i) {
          for (j = 0; j &lt; 480; ++j) {
              pixarray[i][j].r = 255;
          }
      }
      return;
  }
  
  //-------------------------------------------------------------
  
  void displaybitmap() {
  
      Image img;
      unsigned char *ptr1;
  
      ptr1 = &amp;pixarray[0][0].r;  // point to first byte
  
      img.create(640, 480, ptr1);
      tex.loadFromImage(img);
      spr.setTexture(tex);
  
      return;
  }
  </code></pre>