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.

I'm not a completly beginner to java, so I decided to write a map loader and a tileset loader myself instead of using a tilemap editor like Tiled. I'm using Slick & LWJGL for my game.

The plan was to load every map and tileset from external files. When creating a new map, you'd be able to assign tilesets to your map which define the tiles used to paint the map.

I've already written a tileset loader which creates tiles from an external file. For example:

0|floor|tiles/floor.png;

is a tile where id=0, floor means the player in the game would be able to walk on that tile, tiles/floor.png is the texture of the tile.

The tiles are stored in an array in the tileset class.

But I don't know how to create the map loader. Example for a map:

1, 1, 1, 1, 1;

1, 0, 0, 0, 1;

1, 0, 2, 0, 1;

etc. The numbers symbolize the id's of the tiles.

Is there any way to realize my plans? Or is there a better way?

Thanks in advance.

PS: Sorry for my english, I'm no native speaker.


My current solution:

public void createTiles() {
    for(int y = 0; y < map.length; y++) {
        for(int x = 0; x < map[y].length; x++) {
            for(int i = 0; i < tileSets.length; i++) {
                for(int j = 0; j < tileSets[i].tiles.length; j++) {
                    if(map[y][x] == j) {
                        Tile tile = new Tile(tileSets[i].tiles[j].texture, x, y, xOff, yOff, tileSets[i].tiles[j].isWall);
                        tileList.add(tile);
                    }
                }
            }
        }
    }
}
share|improve this question
    
Your 4 nested for loops would explain the frame rate drop you're experiencing. What is tileSets exactly? Is that an array of all of your possible tiles that you set to your map grid? If so, do the contents and length of tileSet change randomly, at runtime? –  Neeko Aug 21 '12 at 21:34
    
I've accidently written this snippet into my draw() method and the tiles were created every frame. Now they're only created once when the map is loading. The frame rate is now way higher. –  Chris Aug 21 '12 at 21:38
add comment

2 Answers

Usually you use a 2D array of map tiles for this; and simply you assign them an ID. Then you can simply iterate over the tiles and draw the specific tile.

Usually, you can compute this similarly as such: x = tileID % tilesetWidth y = tileID / tilesetWidth;

Then, your source rectangle is simply (x, y, tileWidth, tileHeight)

share|improve this answer
add comment

You're on the right track! You'd want to iterate through your map (which is almost aways a 2D Array) and for each tile in the 2D Array do something similar to the pseudocode below;

for (int tileY = 0; tileY < map.length; ++tileY) {
    for (int tileX = 0; tileX < map[tileY].length; ++tileX) {
        switch (map[tileX][tileY]) {
            case 0:
                tile.setImage(someTileImage1.png);
                tile.setSomeOtherProperty(true);
                break;
            case 1:
                tile.setImage(someTileImage2.png);
                tile.setSomeOtherProperty(true);
                break;
            etc...
            default
                break;
        }
    }
}

the case statements representing each type of tile you can have in your map.

That said, it's sometimes good to be lazy (I mean that in a good way :) ). Why not make use of a great tile editor like Tiled and just setup a map loader to parse the Tiled generated XML or JSON file? You know the old saying, don't reinvent the wheel, but I can respect if you'd really like to know how exactly that wheel is created!

share|improve this answer
    
Thanks for your code snippet, but I've already found a solution without using switch. It's very dynamic though, you can add as many tiles as you want, 100 tiles per tileset and 16 tilesets per map. The map size is also editable. It's working very well, but the fps drops from around 200 to 20. The reason why I didn't use Tiled is my idea to create random worlds and I don't know how to do so using Tiled. –  Chris Aug 21 '12 at 19:57
    
Ah very good then. I would post the solution you found as a new answer, and then accept it so that others may learn from it as well, or maybe even edit your original question with it and ask for ideas on how to improve performance. –  Neeko Aug 21 '12 at 20:06
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.