[SDL] correct way of map scrolling

Sami Näätänen sn.ml at bayminer.com
Mon Aug 13 11:39:50 PDT 2007


On Friday 10 August 2007, Scott Harper wrote:
> > If you're doing a true tiling system (say, 32x32 tiles or similar),
> > then ya might consider just recreating the background every frame.
> > You'll undoubtedly be animating EVERY tile, so you'll have to
> > redraw it anyway...
>
> For me, since I use SDL as a cheap way to set up and OpenGL context
> on whatever platform, I'm pretty sure I have to redraw all the tiles
> each frame anyway, as a result of using 3D acceleration for drawing.
> It should be relatively easy, however, to figure out what is on-
> screen or not (test bounds) so you don't have to draw EVERYTHING...
> Though with OGL, I recall that if something's offscreen it'll PROCESS
> the vertices you give it, but won't waste time drawing if nothing
> shows on-screen...  My general idea is to break larger maps down into
> smaller map segments, and do hit-testing on them for if they're near
> the screen edges, that way I may lose SOME cycles each redraw to
> wasted tiles drawn, but probably not as many as would be wasted with
> small hit-checks on each tile.  Kind of like a hand-made quad-tree,
> only without the even breaks.
>
> Wow, that was scatterbrained! >.<  Sorry for the unintelligible
> response! =)

If one can use shaders, one can manage with only one quad that covers 
the whole tile scrolling area. This way the shaders can do all the hard 
work for you. Well vertex shader only calculates the needed texture 
coordinates and map position etc but it is still usefull addition.

One needs texture(s) for the tile GFX and at least one texture for the 
map itself. One can use a single color texture as a map for a maximum 
of 256 different tiles. Using multi color map the possibilities are 
endless. For example multiple tile layers, more tiles or combination of 
these etc.

For added bonus this method will give a free zooming for the tile 
scroller (Simply use wider range of texture coordinate values). The 
scroller can even use maps that are smaller than the scrolling area (if 
the coordinates are out of the map simply discard the fragment) or maps 
that wrap around (map texture can be repeated). So these all come with 
no speed penalties, because the fragment shader is allways drawing at 
most the scrolling areas worth of pixels.

Only downside is the need for own texture filtering, because one screen 
pixel can belong to multiple (max 4) tile texels and these can be up to 
four different tiles (assuming of course that the tile cannot be 
smaller than one pixel). This downside can be eliminated if one doesn't 
allow zooming and the scrolling is allways done in pixel coordinates, 
but I think this would look quite ugly.


So the basic idea is that the texture coordinates are calculated from 
the camera position using the tile size and the wanted tile resolution.
This way the fragment part of the texture coordinate passed to the 
fragment shader tells the position of the fragment inside the tile and 
the floored coordinate gives the indices to the map, so that we know 
which tile to use.

PS. One should arrange the tiles for the tile GFX texture using the tile 
number so that the tile position can be calculated with ease.


More information about the SDL mailing list