Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

In my 2d platformer (made with Java and Slick2d), random maps are made by combining different segments together and displaying them one after the other. My problem is that I can't load too many segments or the game will run out of memory, so I want to load n number of segments at a time in chunks, then load the next chunk when the player comes near the end of one.

I've attempted to do this for a couple of hours now, but I just can't get it to work at all.

This is my chunk generation function where chunkLoad is the number of segments to load and BLOCK_WIDTH is the number of blocks/tiles each segment is across. Chunk1 and map are arrays of segments.

Random r    = new Random();
for(int i=0; i<chunkLoad; i++) {
    int id  = r.nextInt(4)+2;
    chunk1[i]   = new BlockMap("res/window/map"+id+".tmx", i*BLOCK_WIDTH);
}
map = chunk1;
chunksLoaded++;

The map is then drawn on the screen like this. tmap is a TiledMap object and each block/tile is 16 pixels wide

for(int i=0; i<chunkLoad; i++) {
    map[i].tmap.render((i * BLOCK_WIDTH * 16) + (cameraX), 0);
}

I can successfully load new chunks, but I can't display them in the correct position, nor the hitboxes.

Any suggestions? Thanks.

share|improve this question
    
Is the problem really having everything in memory at the same time? Or is the problem drawing everything each refresh? Theoretically, the virtual memory manager ought to be taking care of swapping out the different segments. –  Raceimaztion Aug 25 '12 at 6:19
add comment

1 Answer 1

up vote 0 down vote accepted

Only display the squares the user will see. If your game 'map' is 100 X 100 and the user can only see 10 X 10 then theres no sense in displaying all 100 squares. Heres some code that might answer your question or push you in the right direction:

public void draw()
{
    //Tile tiles[][] = new Tile[100][100]; // map array

    // player cords
    int playerX = 0;
    int playerY = 0;

    // display the map appropriatly

    int viewRow = 15;
    int viewColumn = 15; // 15 By 15 game board
    int tileSize = 10;

    int startX = (playerX % tileSize);
    int startY = (playerY % tileSize);

    int startRow = (playerY / tileSize) - 1; // -1 is for when the player is 'in between' tiles
    int startColumn = (playerX / tileSize) - 1;

    for(int row = 0;row < viewRow;row++)
    {
        for(int column = 0; column < viewColumn; column++)
        {
            int displayRow = row + startRow;
            int displayColumn = column + startColumn;
            // these are your row/column variables. This is where you would do your drawing.


            //ex
            //Rect rect = new Rect(row * tileSize - startX, column * tileSize - startY, ...);
            //canvas.draw(rect);

            //if you want to draw a specific tile or whatnot, you could do this:
            //canvas.draw(tiles[displayRow][displayColumn]);

        }
    }
}

That example is a little rough around the edges but i hope it helped

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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