title: SDL2_image rendering - Images render properly only when source SDL_Rect's properties are printed
body: <p>My C++/SDL2 program uses a <code>Sprites</code> class with a std::map of pointers to <code>SDL_Texture</code>s and member functions for loading and rendering images, however when the program ran none of the loaded images showed up in the window.</p>
  <p>I've found that the Sprites instance is given a correct pointer to the renderer and <code>IMG_LoadTexture</code> did not return 0. The renderer is also working properly since primitives like SDL_RenderDrawLine are drawn just fine.</p>
  <p>Interestingly when I wanted to check if <code>SDL_QueryTexture</code> gave correct dimensions by printing the source rectangle's width and height, the program rendered the images properly. The printed dimensions were also correct, and trying to print the x and y fixed it too.</p>
  <p>Why does this happen?</p>
  <p>Here's the two important member functions in the Sprites class.</p>
  <pre class="lang-cpp prettyprint-override"><code>void Sprites::load(std::string id, std::string filename, int length) {
      SDL_Texture* tex = IMG_LoadTexture(renderer, filename.c_str());
      if (tex == 0) {
          printf(&quot;Texture %s could not be loaded\n&quot;, filename.c_str());
      }
  
      sprmap[id] = tex;
      lengthmap[id] = length;
      printf(&quot;%s should be loaded now\n&quot;, id.c_str());
  }
  
  void Sprites::render(std::string id, float x, float y, int frame) {
      SDL_Rect src;
      SDL_Rect dest;
      SDL_QueryTexture(sprmap[id], NULL, NULL, &amp;src.w, &amp;src.h);
      src.w /= lengthmap[id];
      printf(&quot;src x: %s, src y %s\n&quot;, std::to_string(src.x).c_str(), std::to_string(src.y).c_str()); 
      //Images do not render when above line is omitted
      src.x = frame*src.w;
      dest.x = x;
      dest.y = y;
      dest.w = src.w;
      dest.h = src.h;
      SDL_RenderCopy(renderer, sprmap[id], &amp;src, &amp;dest);
  }
  </code></pre>