Fastest Method of Drawing a Tilemap

All of the methods I have used to draw a tilemap seem to be a bit slow
for smooth gameplay. I often loop through the map, and draw each tile
based on the value, but when it comes to drawing, I must be doing
something wrong. I load a surface with the tile, and blit it to the
proper place on the screen, then I update the screen at the end of the
loop. There has got to be a faster method of doing this… I’m running
WinXP using VC++ 6.0. Could someone supply me with some
psuedocode/algorithm of a proper, fast tilemap drawing function? No
need to worry about anything but the drawing of it. Thanks so much.

All of the methods I have used to draw a tilemap seem to be a bit slow
for smooth gameplay. I often loop through the map, and draw each tile
based on the value, but when it comes to drawing, I must be doing
something wrong. I load a surface with the tile, and blit it to the
proper place on the screen,

From the way you say that, it sounds like you’re loading the tile from
disk every time you blit it. That’s going to be hellish slow. Please
tell me I’m misinterpreting what you’re doing.

then I update the screen at the end of the
loop. There has got to be a faster method of doing this… I’m running
WinXP using VC++ 6.0. Could someone supply me with some
psuedocode/algorithm of a proper, fast tilemap drawing function? No
need to worry about anything but the drawing of it. Thanks so much.

Ron Steinke

“The sound of gunfire, off in the distance. I’m getting used to it now.”
– Talking Heads> From: Colin Hart

Do you convert tile surfaces to the display format? Do you use
RLE encoding?On Sun, May 18, 2003 at 01:56:06AM -0400, Colin Hart wrote:

All of the methods I have used to draw a tilemap seem to be a bit slow
for smooth gameplay. I often loop through the map, and draw each tile
based on the value, but when it comes to drawing, I must be doing
something wrong. I load a surface with the tile, and blit it to the
proper place on the screen, then I update the screen at the end of the
loop. There has got to be a faster method of doing this…


Ivan Stankovic, @Ivan_Stankovic

From the way you say that, it sounds like you’re loading the tile from
disk every time you blit it. That’s going to be hellish slow. Please
tell me I’m misinterpreting what you’re doing.

Yeah I think I actually am doing that… :frowning:

Do you convert tile surfaces to the display format? Do you use
RLE encoding?

I don’t do either…

Thanks…

I’m sorry if this is too obvious, but your post isn’t clear - do you loop through the whole map and rely on SDL for clipping the invisible tiles? Looping only the visible tiles is easy if you use a top down view and only slightly harder if you use a 30? isometric view and it will improve performance by an order of magnitude.

Lic. Gabriel Gambetta
ARTech - GeneXus Development Team
ggambett at artech.com.uy> ----- Original Message -----

From: Colin Hart [mailto:xempest@comcast.net]
Sent: Domingo, 18 de Mayo de 2003 02:56 a.m.
To: SDL Message Board
Subject: [SDL] Fastest Method of Drawing a Tilemap…

All of the methods I have used to draw a tilemap seem to be a bit slow
for smooth gameplay. I often loop through the map, and draw each tile
based on the value, but when it comes to drawing, I must be doing
something wrong. I load a surface with the tile, and blit it to the
proper place on the screen, then I update the screen at the end of the
loop. There has got to be a faster method of doing this… I’m running
WinXP using VC++ 6.0. Could someone supply me with some
psuedocode/algorithm of a proper, fast tilemap drawing function? No
need to worry about anything but the drawing of it. Thanks so much.


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Something like this should work, and be pretty fast if you get hardware
surfaces:

unsigned char map[25][25];

/* load all surfaces */
s[0] = IMG_Load(…);

/* draw tiles /
for (j = 0; j < 25; j++) {
for (i = 0; i < 25; i++) {
/
draw tile map[i][j] */
}
}

Is that what you’re looking for? I think from reading other responses
your problem may just be that you should load and convert surfaces ahead
of time and keep them in memory or video memory before you need to draw
them. I dunno, I could be misunderstanding.

– chris (@Christopher_Thielen)On Mon, 2003-05-19 at 06:05, Gabriel Gambetta wrote:

I’m sorry if this is too obvious, but your post isn’t clear - do you loop through the whole map and rely on SDL for clipping the invisible tiles? Looping only the visible tiles is easy if you use a top down view and only slightly harder if you use a 30? isometric view and it will improve performance by an order of magnitude.

Lic. Gabriel Gambetta
ARTech - GeneXus Development Team
ggambett at artech.com.uy

-----Original Message-----
From: Colin Hart [mailto:xempest at comcast.net]
Sent: Domingo, 18 de Mayo de 2003 02:56 a.m.
To: SDL Message Board
Subject: [SDL] Fastest Method of Drawing a Tilemap…

All of the methods I have used to draw a tilemap seem to be a bit slow
for smooth gameplay. I often loop through the map, and draw each tile
based on the value, but when it comes to drawing, I must be doing
something wrong. I load a surface with the tile, and blit it to the
proper place on the screen, then I update the screen at the end of the
loop. There has got to be a faster method of doing this… I’m running
WinXP using VC++ 6.0. Could someone supply me with some
psuedocode/algorithm of a proper, fast tilemap drawing function? No
need to worry about anything but the drawing of it. Thanks so much.


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

I almost forgot; you could have a look at the smoothscroll and
parallax examples found here:

http://olofson.net/mixed.html

Note that the smoothscroll example also includes OpenGL rendering.

//David Olofson - Programmer, Composer, Open Source Advocate

.- The Return of Audiality! --------------------------------.
| Free/Open Source Audio Engine for use in Games or Studio. |
| RT and off-line synth. Scripting. Sample accurate timing. |
`-----------------------------------> http://audiality.org -’
http://olofson.nethttp://www.reologica.se —On Sunday 18 May 2003 07.56, Colin Hart wrote:

All of the methods I have used to draw a tilemap seem to be a bit
slow for smooth gameplay. I often loop through the map, and draw
each tile based on the value, but when it comes to drawing, I must
be doing something wrong. I load a surface with the tile, and blit
it to the proper place on the screen, then I update the screen at
the end of the loop. There has got to be a faster method of doing
this… I’m running WinXP using VC++ 6.0. Could someone supply me
with some
psuedocode/algorithm of a proper, fast tilemap drawing function? No
need to worry about anything but the drawing of it. Thanks so much.

Actually, all I am working with right now is tilemaps that fit to the
screen size, so clipping and smooth-scrolling aren’t in scope. I’ll
worry about drawing only on-screen tiles later, so no off-the-screen
SDL clipping needed. It’s a standard top-down view, no isometrix.
Thank you, David Olofson, I did actually take a look at some of your
smooth-scrolling examples, but I’m looking for no OGL support. So what
should I do about drawing the map? Load the tileset image into a
surface once, and blit all the needed tiles when I draw it? Any help
would be appreciated.

Thanks.

Actually, all I am working with right now is tilemaps that fit to
the screen size, so clipping and smooth-scrolling aren’t in scope.
I’ll worry about drawing only on-screen tiles later, so no
off-the-screen SDL clipping needed. It’s a standard top-down view,
no isometrix. Thank you, David Olofson, I did actually take a look
at some of your smooth-scrolling examples, but I’m looking for no
OGL support.

Only smoothscroll supports OpenGL. I suggested looking at that because
if you disregard the OpenGL rendering layer, it’s probably simpler
than any of the parallax examples. (It’s been a while since I looked
at any of that code, though.)

So what should I do about drawing the map? Load the
tileset image into a surface once, and blit all the needed tiles
when I draw it? Any help would be appreciated.

Why, yes, that’s the normal way. There might be other ways that are
better in some special cases, but in pretty much every game that uses
tiled graphics, you basically do something like what I’m doing in the
scrolling examples. If there’s no scrolling, you can simplify the
rendering loop into something like this (based on smoothscroll):

void video_render(void)
{
int x, y;
for(y = 0; y < vscreen_h; y += TILE_H)
for(x = 0; x < vscreen_w; x += TILE_W)
video_draw_tile(x, y, map[y * MAP_W + x]);
}

In smoothscroll, video_draw_tile is a function pointer. When using SDL
rendering, it points to this function:

void draw_tile_sdl(float x, float y, int tile)
{
SDL_Rect source_rect, dest_rect;

source_rect.x = tile % PALETTE_TW * TILE_W;
source_rect.y = tile / PALETTE_TW * TILE_H;
source_rect.w = TILE_W;
source_rect.h = TILE_H;

dest_rect.x = (int)floor(x);
dest_rect.y = (int)floor(y);

SDL_BlitSurface(tiles, &source_rect, screen, &dest_rect);

}

The reason for the float coordinates is that smoothscroll is really
all about demonstrating sub-pixel accurate rendering with OpenGL. The
SDL layer is just there to demonstrate the difference.

The calculations around source_rect are about finding out where in the
tileset surface the requested tile is. If you prefer to put each tile
in it’s own surface (faster if you use RLE encoded surfaces with
colorkey or alpha), just pick the right surface from an array or
something, and pass NULL for the source rect to SDL_BlitSurface().

//David Olofson - Programmer, Composer, Open Source Advocate

.- The Return of Audiality! --------------------------------.
| Free/Open Source Audio Engine for use in Games or Studio. |
| RT and off-line synth. Scripting. Sample accurate timing. |
`-----------------------------------> http://audiality.org -’
http://olofson.nethttp://www.reologica.se —On Tuesday 20 May 2003 21.09, Colin Hart wrote: