2
\$\begingroup\$

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.

\$\endgroup\$
8
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Oct 19, 2011 at 16:22
  • \$\begingroup\$ MayDraw is updated in BackGround class, if the picture is not in the game play scene it is not drawed. \$\endgroup\$
    – icaptan
    Commented Oct 19, 2011 at 16:36
  • 1
    \$\begingroup\$ 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);. \$\endgroup\$ Commented Oct 19, 2011 at 16:37
  • \$\begingroup\$ Ops! You detect a nice point ! thanks ! I fix it now ! --> Either the problem does not dissappeared \$\endgroup\$
    – icaptan
    Commented Oct 19, 2011 at 16:45
  • \$\begingroup\$ You should update your question with the new code. So, how many background images can fit on the screen at once? Three? \$\endgroup\$ Commented Oct 19, 2011 at 17:18

1 Answer 1

1
\$\begingroup\$

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.

You also seem to render all the background images every frame, which is potentially wasteful as probably at most two (given their sizes) will be onscreen at once.

You may want to check out this resource on 2D scrolling background in XNA.

\$\endgroup\$
1
  • \$\begingroup\$ i've checked the website, and i use same algorithm with there. I've tested my code on a different machine works with a graphic card (i'm using onboard graphic card) the flickers has ended. has really graphic card a huge effect on simple texture looping ? \$\endgroup\$
    – icaptan
    Commented Oct 19, 2011 at 16:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .