Best way to draw a tile (a floor) all over the screen?

I wanted a tip, a guidance perhaps to know the best way to draw a tile all over the screen. This tile is a 16x16 tile that should be the floor in my top-down map. I set my resolution to 1280x720 and i know i should make a foreach and a for loop ( i looked around in the foruns to find this) but couldn’t make it work on my own. I just finished a monogame course and i’m trying to make a game on my own to really acknowledge the things i’ve learned, but i wanted a help with it if anyone could. I wanted to do the same thing to draw things repetitively on the screen (like trees, rocks, and this kind of stuff). My tile is called “floor”, it’s imported alright and it is loaded too with backGroundFloor = Content.Load<Texture2D>("background/floor");

Anyone know a way to solve this? Also, would this be the best way to do it, or should i just draw a full map and place it on screen? I think this way would be more complicated with itens on the screen to handle collisions and etc

You could try

It’s a shader that draws a tile infinitely.

If you want to draw a repeating tile, and your tile texture is in its own file (i.e. not in some kind of texture pack) you can simply use SpriteBatch and a single draw call. Just set the source rectangle to be the same size as the destination rectangle when you call Draw.

Thanks for the advice! I’m just a little confused to understand the “rectangle”. I searched for it online but didn’t get it right… Could you help me specify it in a code example?

Thank you for the help, was able to make it work with this:

    for(int position = 0; position <= 1920; position += 32)
    {
        for(int positionY = 0; positionY <=1080; positionY += 32)
        {
            _spriteBatch.Draw(backGroundFloor, new Rectangle(position, positionY, 32, 32), Color.White);
        }
    }

That method will probably be a lot more inefficient than what I suggested. SpriteBatch.Draw has multiple overloads, and in some of them you can specify a source rectangle—in other words, the specific portion of the texture to use when drawing. Using your code above, here’s how you would accomplish what I described (with no for loops):

Rectangle destination = new Rectangle(0, 0, 1920, 1080);
Rectangle source = destination;
_spriteBatch.Draw(backGroundFloor, destination, source, Color.White);

Now let’s say you wanted a destination rectangle that wasn’t located at (0, 0). In that case: you would do the following:

Rectangle destination = new Rectangle(x, y, 1920, 1080); // where x and y are the desired location of the destination
Rectangle source = destination;
source.Location = Point.Zero;
_spriteBatch.Draw(backGroundFloor, destination, source, Color.White);

The source rectangle describes the portion of the texture that will be used. If no source is specified, it will use a rectangle that exactly matches the boundaries of the texture. If a source rectangle is specified but it goes beyond the boundaries of the texture, the behavior will differ depending on what SamplerState the spriteBatch is using. If a “wrap” type of SamplerState is used, then the texture will repeat.

If the source and destination rectangles have different sizes, then stretching will occur.

You’ll need to use a PointWrap, LinearWrap, or AnisotropicWrap samplerstate with the SpriteBatch for this to work properly.

_spriteBatch.begin(samplerState: SamplerState.LinearWrap);
1 Like