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 BackGroundObject
s 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 BackGroundObject
s. 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.
bgo.Draw(spriteBatch)
in your Draw function? It seems to me yourMayDraw
is never considered based on the code above. \$\endgroup\$MayDraw
into account, it just drawsbgo.Picture
. You should be callingbgo.Draw(spriteBatch);
instead ofspriteBatch.Draw(bgo.Picture, bo.Position, Color.White);
. \$\endgroup\$