0
\$\begingroup\$

So I am trying to make a tile based game, it is rendering about 40 tiles on the screen at once. I get about 40 FPS on an okay computer. When I tell it to not render the FPS jumps to around 400 so I know its something with the rendering. I was wondering if there is a more efficient way to render a lot of tiles. Here is the code for my Tile class :

public class Tile { public static Tile empty; public static Tile rock; public static Tile flower; public static Tile grass;

public static Tile[] tiles; 

protected boolean solid,breakable;

public static final int TILE_SIZE = 32;

public static final int VOID_TILE = 0, ROCK_TILE = 1,GRASS_TILE = 2,FLOWER_TILE = 3;

private int width = 16,height = 16;

private SpriteSheet image;
private int xCrop,yCrop;
private String path;
private boolean toRender = true;
private int ID;

public Tile(int ID) {
    this.ID = ID;
    path = ("res/spritesheet.png");
    if(ID == VOID_TILE) {
        solid = true;
        toRender = false;
        width = height = 16;
    }

    if(ID == ROCK_TILE) {
        solid = true;
        breakable = true;

        xCrop = yCrop = 1;
        width = height = 16;

        path = ("res/spritesheet.png");
    }

    if(ID == GRASS_TILE) {
        xCrop = yCrop = 0;
        width = height = 16;

        path = ("res/spritesheet.png");
    }

    if(ID == FLOWER_TILE) {
        xCrop = 1;
        yCrop = 0;
        width = height = 16;

        path = ("res/spritesheet.png");
    }
    if(toRender) {
        try {
            image = new SpriteSheet(path,width,height);
        } catch (SlickException e) {
            System.out.println("Error Loading File : " + path);
            e.printStackTrace();
        }
    }

}

public static void initTiles() {
    empty = new Tile(VOID_TILE);
    rock = new Tile(ROCK_TILE);
    grass = new Tile(GRASS_TILE);
    flower = new Tile(FLOWER_TILE);

    tiles = new Tile[] {empty,rock,grass,flower};
}



public void render(int x,int y) {
    if(image!=null)image.getSubImage(xCrop, yCrop).draw(x,y,TILE_SIZE,TILE_SIZE);
}

public void update() {

}

public int getID() {
    return ID;
}

public void setID(int iD) {
    ID = iD;
}

}

\$\endgroup\$
  • \$\begingroup\$ Profile your code, check where your slow downs are. Look at the duplicate, see how you can use those strategies to help optimize your code. \$\endgroup\$ – MichaelHouse Sep 14 '15 at 15:42
-1
\$\begingroup\$

Jumping from 400 to 40 fps means something is really going wrong.

image.getSubImage(xCrop, yCrop)

I don't actually know much of slick, but I think that this loads the image to the graphics card. Try batching this (by making an instance variable in your class). I hope it helps!

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

My suggestion is to used TiledMap to do so:

https://bitbucket.org/kevglass/slick/src/aa6e7b6dce00d9725ef7e4c4fb6a99107f87c218/trunk/Slick/src/org/newdawn/slick/tiled/TiledMap.java?at=development&fileviewer=file-view-default

You can also see how tiledMap store and render tiles!

\$\endgroup\$
  • \$\begingroup\$ Just pointing to a source file and saying "do it like this", isn't helpful. Your answer should say what they're doing and how they're doing it. \$\endgroup\$ – MichaelHouse Sep 14 '15 at 15:40
  • \$\begingroup\$ I agree my contribution for stackoverflow standard could be better. But I believe that study slick2d and follow author's approach totally is always best that follow a random guy like me on stackoverflow. I will elaborate better when I will have time \$\endgroup\$ – Vokail Sep 15 '15 at 6:43

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