0
\$\begingroup\$

What i need help with is how can i get a chunk of a large 2d char array and update it so i am only drawing enough tiles to cover the screen?

Currently, i load all the chars from a text file and store them in an array. I then create a smaller array:char[][] toDraw = new char[height][width] which gets the first height*width area of chars from the larger array like this:

  public void filterMap() {
    for(int i = 0; i < height; i++) {
        for(int j = 0; j < width; j++) {
            toDraw[i][j] = MapLoader.mapData[i][j];
        }
    }
}

I then draw the map using this drawMap method:

public void drawMap(Canvas canvas) {
    for(int i = 0; i < height; i++) {
        for(int j = 0; j < width; j++) {
            if(getBitmap(toDraw[i][j]) != null) {
            canvas.drawBitmap(getBitmap(toDraw[i][j]), x + (j*tileSize), y + (i*tileSize), null);
            }
        }
    }
}

note the int x and y are what i increment to move the map, getBitmap(char c) returns a Bitmap from a char, MapLoader.mapData is the 2d array that the text file loads into.

I have tried a few ways of using an offset in the toDraw arrays index to scroll the data relevant to the x,y location but they have been buggy/failed.

So, does anybody have a link to how things like this can be done (I have tried but have found none that help) or could someone with experience in 2d games please tell/show me how this should be done.

Currently my code will get a area from the larger array and draw it to the screen, the only problem is updating the data in the toDraw array. thanks.

The game is made for android but is not included in tags because i dont feel it is relevant to the problem

\$\endgroup\$
1
\$\begingroup\$

If you only want to work with a small chunk, then only iterate through that chunk.

for(int i = BOTTOM_BOUND; i < TOP_BOUND; i++) {
    for(int j = LEFT_BOUND; j < RIGHT_BOUND; j++) {
        // Do something with the chunk
        toDraw[i][j] = MapLoader.mapData[i][j];
    }
}

That loop will only update a small rectangle formed by the constants, anytime you want to update just the chunk just iterate using the same four points.

\$\endgroup\$
  • \$\begingroup\$ Thank you for your answer, i had just reached the almost same result but in a more messy way! works perfectly, thanks \$\endgroup\$ – user3256254 Feb 2 '14 at 6:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.