gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Coding > Converting from tiled to bitmap

#122115 - Lick - Sat Mar 17, 2007 2:08 am

I'm trying to convert a 8x8 tilemap that consists of 4x4 pixels into a 32x32 bitmap.
I've tried, but all the looping has given me a headache. Could someone think of some code that would do this?

Thanks. I'm going to get some sleep now.
_________________
http://licklick.wordpress.com

#122125 - tepples - Sat Mar 17, 2007 4:09 am

Do you want to do this conversion on the GBA, on the DS, or on the PC?
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#122155 - Cearn - Sat Mar 17, 2007 1:50 pm

Code:


// Yer average bitmap blitter
void bmpBlit(BITMAP *dst, int dstX, int dstY, int width, int height,
    const BITMAP *src, int srcX, int srcY)
{
    // Assuming BPPs are the same and no clipping is necessary,
    // for simplicity's sake

    int ix, iy;
    int bpp= dst->bpp;

    BYTE *dstD= dst->data[dstY*dst->pitch + dstX*bpp/8];
    BYTE *srcD= src->data[srcY*src->pitch + srcX*bpp/8];
    int pitch= width*bpp/8;

    for(iy=0; iy<height; iy++)
        for(ix=0; ix<pitch; ix++)
            dstD[iy*dst->pitch + ix]= srcD[iy*src->pitch + ix];
}


// In draw map function

int itx, ity;               // loop indices for the map
int px, py;                 // pixel coords of the tile in the tileset
int tid;                    // tile index
BITMAP *bmp;                // Destination bitmap
TILEMAP *map;               // Tilemap

BITMAP *tiles= map->tileSet; // Tileset bitmap
int tw= map->tileWidth, th= map->tileHeight;

for(ity=0; ity<map->height; ity++)
{
    for(itx=0; itx<map->width; itx++)
    {
        tid= mapGetTileId(map, itx, ity);
        px= tid%map->width * tw;
        py= tid/map->width * th;

        bmpBlit(bmp, itx*tw, ity*th, tw, th, tiles, px, py);
    }
}


Something like that anyway. The basic steps:
  • loop over all map entries (itx, ity)
  • for each entry, get the tile index and translate that to coordinates on the tileset.
  • Now simply blit rectangle (px, py, px+tw, py+th) of the tileset onto the destination bitmap at (itx*tw, ity*th).

There are plenty of possible optimizations here, of course. If your sizes are fixed, you can hardcode those. If your tileset is one tile wide, you don't need the divisions: just use px=0 and py= tid*th. If your bpp is fixed, you can cast to the right type and copy using those instead of in bytes. In fact, depending on width and bpp of the tiles, you can probably copy a whole tile scanline in one assignment. And then possibly integrate bmpBlip into the main drawing routine, or make it it an inline function.

But do these things after you get the basic thing working.

#122157 - Lick - Sat Mar 17, 2007 2:03 pm

Thanks for the help!
_________________
http://licklick.wordpress.com

#122163 - Lick - Sat Mar 17, 2007 2:47 pm

I wrestled with the code and came up with the following code, it converts the DS Cartridge Icon to a paletted bitmap (16 color).
Code:
        for(u32 q=0; q<16; q++) {
          u32 tof = (q&3)*(8/2) + (q>>2)*(8*8/2*4);
          u32 sof = (q)*(8*8/2);
          for(u32 y=0; y<8; y++) {
            for(u32 x=0; x<8/2; x++) {
              dst[tof+y*(32/2)+x] = src[sof+y*(8/2)+x];
            }
          }
        }


Using the output in dst I created a texture and it shows up nicely. Yay! =D
_________________
http://licklick.wordpress.com