1
\$\begingroup\$

These are two screenshots from my new game, a top-down RTS with tiles. The first one is from a normal view with no zoom, the second one is a zoomed out view of the whole map:

normal view(no zoom)

zoomed out view

In the first image, the tiles are rendered perfectly. However, when I zoom out, some of the tiles are rendered with lines in between them, while others clump into larger groups with their own lines. Here is the code that draws the tiles:

            Rectangle rect;

            if ((int)(32 * gameInfo.Game.ZoomFactor) > 0)
            { rect = new Rectangle(0, 0, (int)(32 * gameInfo.Game.ZoomFactor), (int)(32 * gameInfo.Game.ZoomFactor)); }
            else
            { rect = new Rectangle(0, 0, 1, 1); }

            Texture2D buffer = new Texture2D(this.GraphicsDevice, rect.Width, rect.Height);

            for (int x = 0; x < gameInfo.Game.TileMap.GetLength(0); x++)
            {
                for (int y = 0; y < gameInfo.Game.TileMap.GetLength(1); y++)
                {
                    rect.X = (int)(gameInfo.Game.ZoomFactor * ((x * 32) - gameInfo.Game.ViewOffset.X));
                    rect.Y = (int)(gameInfo.Game.ZoomFactor * ((y * 32) - gameInfo.Game.ViewOffset.Y));
                    buffer = EnvironmentTextures.ENTileTextures[gameInfo.Game.TileMap[x, y].TileType];
                    spriteBatch.Draw(buffer, rect, Color.White);
                }
            }

The gameInfo.Game.ZoomFactor is a Single which is modified when the scroll wheel is changed, in increments of 0.2f. Has anyone had this problem before, and how can I fix it?

\$\endgroup\$
3
\$\begingroup\$

The gaps are caused when you are truncating the rect width and height. Instead of:

rect = new Rectangle(0, 0, (int)(32 * gameInfo.Game.ZoomFactor), (int)(32 * gameInfo.Game.ZoomFactor));

You should use:

rect = new Rectangle(0, 0, (int)((32 * gameInfo.Game.ZoomFactor) + 0.5), (int)((32 * gameInfo.Game.ZoomFactor) + 0.5));

This should ensure that the width and height of the tiles will be sufficient to cover the canvas and you'll eliminate the gaps.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.