title: Infinite Scrolling Background in XNA
body: <p>I'm drawing a ground texture in a sidescrolling platformer like this:</p>
  
  <pre><code>spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, _camera.GetViewMatrix(Parallax));
  foreach (Sprite sprite in Sprites)
       sprite.Draw(spriteBatch);
  spriteBatch.End();
  </code></pre>
  
  <p>In Sprite.Draw()</p>
  
  <pre><code>        public void Draw(SpriteBatch spriteBatch)
          {
              if (Texture != null)
                  spriteBatch.Draw(Texture, Position, new Rectangle(0, 0, Texture.Width, Texture.Height), Color.White, Rotation, Origin, Scale, SpriteEffects.None, ZIndex);
          }
  </code></pre>
  
  <p><strong>How can I get the texture to repeat in the X Direction infinitely?</strong></p>
  
  <p>Any resources explaining how to do this would be great.  Thanks!</p>
  
  <p>My implementation of camera is based on this:  <a href="http://www.david-gouveia.com/2d-camera-with-parallax-scrolling-in-xna/" rel="nofollow noreferrer">http://www.david-gouveia.com/2d-camera-with-parallax-scrolling-in-xna/</a></p>
  
  <h2>EDIT</h2>
  
  <p>This is what I tried, I made a separate draw for the background like this:</p>
  
  <pre><code>//Draw Background
  spriteBatch.Begin(SpriteSortMode.Immediate, null, SamplerState.LinearWrap, null, null,null, camera.GetViewMatrix(Vector2.One));
  groundSprite.Draw(spriteBatch, (int)camera.Position.X - (int)camera.Origin.X / 2);
  spriteBatch.End();
  </code></pre>
  
  <p>and in Sprite.Draw():</p>
  
  <pre><code>public void Draw(SpriteBatch spriteBatch, int scrollX = 0)
  {
      if (Texture != null)
         spriteBatch.Draw(Texture, Position, new Rectangle(scrollX, 0, Texture.Width, Texture.Height), Color.White, Rotation, Origin, Scale, SpriteEffects.None, ZIndex);
  }
  </code></pre>
  
  <p>still have some issues.</p>
  
  <p><img src="https://i.stack.imgur.com/hmMaI.jpg" alt="This is the start">
  <img src="https://i.stack.imgur.com/b6DHB.jpg" alt="This is the issue now with the camera matrix."></p>
  
  <h2>Second Edit</h2>
  
  <p>It just came to me.  I had the parallax set to 1 on the ground.  It should be 0 so that it never actually moves.  It just looks like it moves when you use the answer I selected below.</p>
  
  <p>Thanks!</p>