# How can I seamlessly loop a background texture in XNA?

Using XNA, I made a looping background with several textures (whose widths are over 1024px). There are fewer than five of them. All the pictures are different, held in a list and their positions are looped. When it reaches the end of the loop, it flickers.

In my Update method I do this:

dt = (float)gameTime.ElapsedTime.TotalSeconds;
foreach(BackGroundObject bgo in myList)
{
bgo.Position += speed * dt;
}


So that it works based on gameTime. Then, in my drawing function is this code:

foreach(BackGroundObject bgo in myList)
{
spriteBatch.Draw(bgo.Picture, bo.Position, Color.White);
}


Here is the BackGroundObject class. The game holds a list of BackGroundObjects and Update changes their positions. If any object goes off screen, its position is set again; a BackGroundObject is loaded once and the game always uses same BackGroundObjects. MayDraw is a property of the picture's position. I.e. If in the screen or not.

public class BackGroundObject
{
public Texture2D Picture;
public Vector2 Position, DrawPosition;
public bool MayDraw;

public BackGroundObject(Vector2 position, Texture2D picture)
{
this.Picture = picture;
this.Position = position;
this.MayDraw = true;
}

public virtual void Draw(SpriteBatch spriteBatch)
{
if (MayDraw == true)
{
spriteBatch.Draw(Picture, Position, Color.White);
}
}
}


Does the flicker problem occur because of the large image sizes? Could I do this with a shader instead?

My background code works on with a graphic card with 60 FPS, but on a onboard graphic card with 30 FPS.

I have 4 background layers, each layer has 4 pictures, in total about 15pictures are drawen with widths of 1920.

• Why don't you use bgo.Draw(spriteBatch) in your Draw function? It seems to me your MayDraw is never considered based on the code above. – Richard Marskell - Drackir Oct 19 '11 at 16:22
• MayDraw is updated in BackGround class, if the picture is not in the game play scene it is not drawed. – icaptan Oct 19 '11 at 16:36
• You posted some code above after saying "So that it works based on gameTime. Then, in my drawing function is this code:". That code doesn't take MayDraw into account, it just draws bgo.Picture. You should be calling bgo.Draw(spriteBatch); instead of spriteBatch.Draw(bgo.Picture, bo.Position, Color.White);. – Richard Marskell - Drackir Oct 19 '11 at 16:37
• Ops! You detect a nice point ! thanks ! I fix it now ! --> Either the problem does not dissappeared – icaptan Oct 19 '11 at 16:45
• You should update your question with the new code. So, how many background images can fit on the screen at once? Three? – Richard Marskell - Drackir Oct 19 '11 at 17:18

It's unclear to me what BackGroundObject is, and how its implemented -- it's possible the problem lies in there, or elsewhere, as the code you've provided doesn't seem terribly suspect. The biggest thing that stands out to me is that you never seem to actually loop (reset) the position of the background images at all. The "Position" value will increase without bound, unless there's a modulus operation that occurs in the setter for that property or something.