1

i'm trying to make a map loader for tiled, on android.. so far i could parse the tmx file, grab all the tile data, and put them in a 2dimensional array, like this: Bitmap tiles[x][y] ... it works and i can render tiled maps on android now, but only by interating through that tiles[][] array, like shown below..

how can i merge together in one single bitmap the content of a bitmap array ?

here's my render method:

//here's what i have:
for (int x = 0; x < MapLoader.width; x++) {
  for (int y = 0; y < MapLoader.height; y++) {
    g.drawBitmap( picSource[x][y], posX, posY, BitmapPaint);
  }
}

//and here's what i'd like to have:
g.drawBitmap( picDest, posX, posY, BitmapPaint);

i would like to itterate through picSource[x][y] grab all the bitmaps and put them all in picDest. so i can get 1 single big pic, representing the map i've loaded and constructed from tiled tmx file..

( note that no bitmap contained in the picSource[][] array is located a the same position .. there's no bitmap on top of any other, they're just displayed in a grid each is a 32x32 bitmap in a 4x3 grid for example.. each its own spot on the grid .. )

thanks for the help

freeaks
  • 487
  • 2
  • 6
  • 14
  • 1
    this kind of operation on lists is called "folding" in functional programming (combining the elements using a common operation), you might find something in the standard library that does it for you – bandi Jun 28 '10 at 06:31
  • How big are this tiles? For image processing and manipulation I usually use native methods with NDK. I don't know if speed can be an issue in your case. I hope it helps, bye. – SlowTree Mar 30 '11 at 15:54

2 Answers2

1

I made the following version that would work using only the Bitmap library. It works with one-dimensional arrays but change it for working with matrices would be trivial.

private Bitmap rebuildBitmapFromPatches(Bitmap [] patches, int finalWidth, int finalHeight){
        int width = patches[0].getWidth();
        int height = patches[0].getHeight();
        Bitmap image = Bitmap.createBitmap(finalWidth,finalHeight,patches[0].getConfig());
        int i=0;
        int [] pixelsAux = new int [width*height];
        for(int x = 0; x<finalWidth; x+=width){
            for(int y = 0; y<finalHeight; y+=height){
                patches[i].getPixels(pixelsAux,0,width,0,0,width,height);
                image.setPixels(pixelsAux,0, width,x,y,width, height);
                i++;
            }
        }
        return image;
Haru Kaeru
  • 41
  • 3
0

In case someone else is wondering, i'm posting how i did it:

MapLoader class filled the array "block[x][y] with an int value for each tile.

we loop through that array, looking for all tiles at pos:x,y with value 169.

when one is found, the corresponding bitmap, taken from the tilesheet (alway the same one..at same position on the tilesheet: 0,0,16,16 it's a 16x16 red tile used for collisions), is drawn into a temporary bitmap at the same pos.x,y that the array loop is at.

when the loop is exited, collisionPicDest bitmap have been built, out of lots of smaller parts blended together in 1 final pic.

collisionSheet = BitmapFactory.decodeResource(ctx.getResources(), R.drawable.collision16);
collisionPic = new Bitmap[width][height];
collisionPicDest = Bitmap.createBitmap(width*tileSize, height*tileSize, Bitmap.Config.RGB_565);
collisionCanvas = new Canvas(collisionPicDest());
// ===============
// collision layer
// ===============
for (int y = 0; y < height; y++) {
  for (int x = 0; x < width; x++) {

    tileNbr = MapLoader.block[x][y];


    switch(tileNbr){
    case 169:
      // ============
      // normal block
      // ============
      collisionPic[x][y]= Bitmap.createBitmap(collisionSheet, 0, 0, tileSize, tileSize);
      collisionCanvas.drawBitmap(collisionPic[x][y],x*tileSize,y*tileSize,bitmapPaint);
      break;

      // do other cases..
    }
  }
freeaks
  • 487
  • 2
  • 6
  • 14