MrDrProfessorTyler
MrDrProfessorTyler

Reputation: 423

Java 2D: Render a very large amount of tiles on screen quickly

Another quick question if anyone is up to it. I have this code:

        for(int cy = 0; cy < 3; cy++) {
            for(int cx = 0; cx < 3; cx++) {
                for(int y = 0; y < 16; y++) {
                    for(int x = 0; x < 16; x++) {
                        g2D.drawImage(Tiles.tileImages.get(C0.chunk[x][y][cx][cy]), 
                                C0.cX[cx][cy] * cspcr + (blckspcr * x) + width - pXspcr,
                                C0.cY[cx][cy] * cspcr + (blckspcr * y) + height - pYspcr + (int)(24.25 * zoom),
                                    blckspcr, blckspcr, null);
                        if(C0.chunk[x][y][cx][cy].equals("a05")) {
                            g2D.drawImage(Tiles.treetop, 
                                    C0.cX[cx][cy] * cspcr + (blckspcr * x) + width - pXspcr,
                                    C0.cY[cx][cy] * cspcr + (blckspcr * y) + height - pYspcr + (int)(24.25 * zoom) - blckspcr,
                                    blckspcr, blckspcr, null);
                        }
                    }
                }
            }
        }

but my problem is, it creates an incredibly large amount lag in my application. Is there any way where I could avoid using the large amounts of loops I have or possibly speed it up? The image it is drawing first is grabbing blocks from chunks in a 4D array of strings and gets images from a hashmap based on the string and then it draws the image using a bunch of numbers. If anyone can help I would greatly appreciate it. Thanks!

Upvotes: 0

Views: 281

Answers (1)

Dhaivat Pandya
Dhaivat Pandya

Reputation: 6536

You need to store your data in a different way (a 4D array is almost always a bad idea).

You can begin replacing those 4D array of strings by maybe a hash if its fits your usecase (then you'd only have one loop).

Upvotes: 1

Related Questions