title: Loading multiple textures of the same image in SDL2
body: <p>How does this work? I had trouble finding tutorials/documentation on using the same loaded image for many textures.</p>
  
  <p>What I did at first was use something like <code>Object obj("image.png");</code>, which wraps an <code>SDL_Texture</code> in an Object. When calling Object's constructor, it loads "image.png" each time. Obviously, this is bad design if you want multiple sprites using the same image. It should only need to be loaded once. </p>
  
  <p>So what I have now is a <code>Sprite</code> class, which wraps an <code>SDL_Texture</code> inside of it, like so:</p>
  
  <pre><code>struct Sprite {
      SDL_Surface* sprite = NULL;
  
      Sprite(const char* filename) {
          sprite = IMG_Load(filename);
      }
  };
  </code></pre>
  
  <p>And I also have an <code>Instance</code> class, which uses a <code>Sprite</code> to create textures:</p>
  
  <pre><code>struct Instance {
      Sprite* sprite = NULL;
      SDL_Texture* texture = NULL;
  
      Instance(Sprite* spr, WindowWrapper* wr) {
          texture = SDL_CreateTextureFromSurface(wr-&gt;renderer, sprite-&gt;sprite);
          SDL_FreeSurface(sprite-&gt;sprite);
      }
  }
  </code></pre>
  
  <p>So then what I do in main is load sprites into Sprite objects:</p>
  
  <pre><code>vector&lt;Sprite*&gt; sprites;
  sprites.push_back(new Sprite("image1.png"));
  sprites.push_back(new Sprite("image2.png"));
  sprites.push_back(new Sprite("image3.png"));
  </code></pre>
  
  <p>...and then load these into <code>Instance</code> objects.</p>
  
  <pre><code>Instance* i0 = new Instance(sprites[0], &amp;theWindow);
  Instance* i1 = new Instance(sprites[0], &amp;theWindow);
  </code></pre>
  
  <p>But this kills the program.</p>
  
  <p>It's not a problem with me rendering it, it doesn't even get to the rendering. It just kills the program when I call <code>new Instace()</code>. </p>
  
  <p>I suspect that you can't load the same <code>SDL_Surface</code> into different <code>SDL_Textures</code>. So then how do I make it so that I only have to load a certain sprite <em>once</em> and then I can use that same sprite over and over again for different textures?</p>