title: Rendering text with SDL2, problems updating texture / output
body: <p>I have recently started using SDL2 in a C++ project, which is mainly used as a training project so I can learn using SDL2.
  One of the things I tried, was to write a simple label class. The Idea is that the label is created with given position and dimensions at start. Then when I call my 'setText()' function, the text would be rendered and shown on window. At first I used this approach:</p>
  
  <pre><code>// get a surface from given text    
  m_surf = TTF_RenderText_Solid(m_font, txt.c_str(), fgCol);
  // get a texture from the previous surface  
  m_texture = SDL_CreateTextureFromSurface(r, m_surf);
  // render it in 'r' which is the window renderer
  SDL_RenderCopy(r, m_texture, NULL, &amp;rect);
  </code></pre>
  
  <p>This works for me. So I thought to remove the overhead of creating textures every time text changes, then destroy them, and use <code>SDL_UpdateTexture()</code>. 
  Well, this never worked! It always produces a corrupt output on screen, even though the call is successful:</p>
  
  <pre><code>// Once at ctor create a texture
  m_texture = SDL_CreateTexture(r, SDL_PIXELFORMAT_RGBA32,SDL_TEXTUREACCESS_STATIC, rect.w, rect.h);
  // ...
  // later update when setText() is called get a surface from given text and update texture   
  m_surf = TTF_RenderText_Solid(m_font, txt.c_str(), fgCol);
  rc = SDL_UpdateTexture(m_texture,  NULL, m_surf-&gt;pixels, m_surf-&gt;pitch);
  </code></pre>
  
  <p>My first thought was that it's something wrong with the pixel formatting... so I also used a format convert before updating:
  <code>m_surf =  SDL_ConvertSurfaceFormat(ts, SDL_PIXELFORMAT_RGBA32, 0);</code>
  where 'ts' is a temp surface where the text was rendered, and constant SDL_PIXELFORMAT_RGBA32 is same as the one I used to create the m_texture.</p>
  
  <p>here  is what I get on window when using 'SDL_UpdateTexture()'
  <a href="https://i.stack.imgur.com/xvSPJ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xvSPJ.png" alt="corrupt output"></a></p>
  
  <p>Any Ideas ?</p>
  
  <p>Thank you,
  Alex</p>
  
  <p>Code Snip:
  I have written a compact piece of code that is related to reproduce problem</p>
  
  <pre><code>// label class declaration
  class label
  {
      friend class panel;
      public:
          label(SDL_Window* const w, TTF_Font* const fnt, SDL_Rect rect);
          ~label();
  
          int draw(SDL_Rect&amp; rect);
          int draw(SDL_Rect&amp; rect,const std::string&amp; txt, SDL_Color fgCol, int quality);
  
      private:
      std::string     m_txt;
      TTF_Font* const m_font = nullptr;
      SDL_Surface     *m_surf = nullptr;
      SDL_Texture     *m_texture = nullptr;
      SDL_Rect        m_rect{0,0,0,0};
      SDL_Renderer    *m_ren;
      SDL_Window      *m_wnd;
      Uint32          m_pixForm;
  };
  
  // label class constructor
  label::label(SDL_Window* const w, TTF_Font* const fnt, SDL_Rect rect):m_wnd(w),m_font(fnt),m_surf (nullptr), m_rect(rect)
  {
  // save window info locally for label object
      m_ren =SDL_GetRenderer(m_wnd);
      m_pixForm =SDL_GetWindowPixelFormat(m_wnd);
      m_texture = SDL_CreateTexture(m_ren, m_pixForm,SDL_TEXTUREACCESS_STATIC, rect.w, rect.h);
      m_surf = nullptr;
      std::cout &lt;&lt; "PixelFormat " &lt;&lt; m_pixForm &lt;&lt; " : " &lt;&lt; SDL_GetPixelFormatName(m_pixForm) &lt;&lt;  std::endl;
  }
  
  // label class methods to render/draw text on screen
  int label::setText(const std::string&amp; txt, SDL_Color fgCol, int quality=0)
  {
      int w,h;
      SDL_Surface *ts;
      if(m_surf!=nullptr)SDL_FreeSurface(m_surf);
      switch(quality)
      {
          case 1:
          {
              ts = TTF_RenderText_Shaded(m_font, txt.c_str(), fgCol, SDL_Color{0,0,0,0});
              break;
          }
          case 2:
          {
              ts = TTF_RenderText_Blended(m_font,txt.c_str(), fgCol);
              break;
          }
          default:
          {
              ts = TTF_RenderText_Solid(m_font, txt.c_str(), fgCol);
              break;
          }
      }
      std::cout &lt;&lt; "Before pixFormat ts " &lt;&lt; SDL_GetPixelFormatName(ts-&gt;format-&gt;format)&lt;&lt; std::endl;
      m_surf =  SDL_ConvertSurfaceFormat(ts, m_pixForm, 0);
      std::cout &lt;&lt; "After pixFormat surf : " &lt;&lt;  SDL_GetPixelFormatName(m_surf-&gt;format-&gt;format) &lt;&lt; " ts " &lt;&lt; SDL_GetPixelFormatName(ts-&gt;format-&gt;format)&lt;&lt; std::endl;
      SDL_FreeSurface(ts);
      TTF_SizeText(m_font, txt.c_str(), &amp;w, &amp;h);
      std::cout &lt;&lt; "Set text '" &lt;&lt; txt &lt;&lt; "' w: " &lt;&lt; w &lt;&lt; " h: " &lt;&lt; h &lt;&lt; std::endl;
  }
  
  int label::draw(SDL_Rect&amp; rect)
  {
      // HERE is the one that draw the noise on screen!
      {
          int rc;
          rc = SDL_UpdateTexture(m_texture,  NULL, m_surf-&gt;pixels, m_surf-&gt;pitch);
          if(rc)
          {
              std::cout &lt;&lt; "Error updating texture " &lt;&lt; rc &lt;&lt;std::endl;
              return(rc);
          }
      }
      // if it is replaced with the following it works:
      /*
      {
          if(m_texture!=nullptr)SDL_DestroyTexture(m_texture);
          m_texture = SDL_CreateTextureFromSurface(r, m_surf);
      }
      */
  
      SDL_RenderCopy(m_ren, m_texture, NULL, &amp;rect);
  }
  
  int label::draw(SDL_Rect&amp; rect,const std::string&amp; txt, SDL_Color fgCol, int q)
  {
      setText(txt,fgCol,q);
      draw(rect);
  }
  
  // main functiom. Init SDL, create window, create label and draw it:
  int main( int argc, char * argv[] )
  {
      SDL_Window *wnd ;
      SDL_Renderer *wrend;
      int i;
  
      wnd = SDL_CreateWindow("TestWin",SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,500,300,0);
      wrend = SDL_CreateRenderer(wnd,-1,SDL_RENDERER_ACCELERATED);
      SDL_SetRenderDrawBlendMode(wrend,SDL_BLENDMODE_ADD);
      SDL_SetRenderDrawColor(wrend,0,0,0,255);
      SDL_RenderClear(wrend);
  
      SDL_Color fcol{255,255,0,128};
      SDL_Rect lblBox;
      lblBox.h = 120;
      lblBox.w = 280;
      lblBox.x = 1;
      lblBox.y = 1;
  
      label lbl(wnd, getCurrentFont(), lblBox); // create a label (getCurrentFont returns a valid TTF_Font pointer)
      lbl.draw(lblBox,std::string("Text Test"), fcol, 2);
      SDL_RenderPresent(wrend);
      SDL_ShowWindow(wnd);
      // .. more code here
  }
  </code></pre>