Wrapping the map?

Aug 12, 2011 at 3:46 AM

Since the actual spritebatch calls for the map don't seem to be available without modifying the source, is there any way to wrap the map so that it tiles forever without modifying the source? Specifically, I'm talking about doing a 256x256 RPG overworld that wraps back at each edge. The whole map would be 4096^2, so I don't think drawing it to a rendertarget and wrapping that would be feasible...

Aug 12, 2011 at 9:41 AM

Map wrapping isn't supported directly but it may be possible by rendering multiple times with different viewports. Basically, if the viewport exceeds the right hand side, you can do another map render using another viewport set at -map.Width away. The first render will place all the tiles up to the right edge of the map, leaving the right side of the display empty; the second render will render nothing on the left side (because the additional viewport would overstep the left edge of the map), but will render the left side of the map on the remaining right side of the screen. For the vertical direction you would need another render, and if the bottom corner is exceeded you would need up to four renders.

I'm not sure if I explained the idea clearly enough, let me know if you want me to expand on this.

I think it would be a good idea to support this natively within the engine however.. I'll definitely look into it!

Aug 12, 2011 at 12:36 PM

That seems to be the way to do it. For an RPG overworld, you'd need a total of 8 additional viewports to fill the 3x3 grid. I have my character position reset to the opposite map limit when it passes over, so it only ever draws half-a-screen of any particular side viewport, so there's no need for additional renders beyond the one on each side and the four corners.

Thing is, how is this going to affect performance? I don't have an XNA subscription at the moment, so I have no idea how fast it would run on 360. On my system, it hovers around 30-36Mb in RAM and I get a pretty constant framerate when I limit it to 60fps. Is the addition of 8 viewports going to inflate my resource usage? Sorry if this is a stupid question, but I'm still really new to XNA...

Aug 12, 2011 at 5:20 PM
Edited Aug 12, 2011 at 5:20 PM

If you think about it, you can get away with rendering 4 viewports in a 2x2 grid since at no point you need to render more than 2 across and 2 down. either way, it would affect performance slightly since you would be doing several calls to the Map.Draw method instead of one.

But fear not; a wrap-around option is in the works! :) The extended version of the Draw method will feature a "wraparound" boolean flag so you don't need to jump through hoops to implement map wrapping. For such a map to be consistent you will need to make sure that all the layers in the map can individually wrap around nicely - the parallax computations are done in such a way that the layers align at the edges when the viewport is at the edge of a map.

Aug 12, 2011 at 7:18 PM
Edited Aug 12, 2011 at 7:18 PM

To use a total of four viewports, the map would have to be big enough that you don't see two opposite edges at once. If you want to do a small map, it has to fill beyond the edge of the screen. It's fine for large maps, but not if a map is smaller than the screen size.

If possible, I'd like separate X and Y wrap flags to allow for maps that wrap one way, but not the other. It's easy to do with the viewports, but if you're going to add it to the library...

Aug 12, 2011 at 11:48 PM

Well, that's true.. 2x2 would be enough in the case where the viewport is smaller than the map, which I daresay would be the rule, rather than the exception. Anyhow, the latest update should address this kind of scenario, although you might experience some weird scrolling due to the parallax computations (if you decide to use viewports larger than the map). I did not however provide separate flags for vertical and horizontal wrapping as it would complicate the code. For the time being normal and wrapped map rendering run in different loops to avoid repeated condition checks.

Aug 12, 2011 at 11:53 PM

I tried it this evening when I got home and the new wrap flag is fine. I can provide my own "one and not the other" wrap modes as needed by adding a left and right or top and bottom viewport. Thanks!