# My 2D Tile Game Lag (Caused by loop) Looking for an alternate method of rendering

• January 18th, 2014, 10:21 PM
Cekeh
My 2D Tile Game Lag (Caused by loop) Looking for an alternate method of rendering
So the following is my code for the Terrian Generation of my game, however the way i have it rendering all the tile rectangles as opposed to just rendering whats visable on my JFrame causes lots of lag. All suggestions welcomed! :)

These are the variables and Rectangles inside _TerrianGen.class
Code Java:

``` //CHUNK static int chunkx =2048; static int chunky =2048;   //RECTANGLES public static Rectangle[][] tile = new Rectangle[64][64]; static int[][] blockType = new int[64][64]; //0=Grass 1=Dirt 2=Stone 10=Brick 11=Coal 12=Iron 21=Gold 22=Diamond -1=null```

This is the Init() code
Code Java:

```for (int x =0; x < chunkx-32; x+=32){ for (int y=0; y < chunky-32; y+=32){ tile[y/32][x/32] = new Rectangle(x, y, 32, 32); blockType[y/32][x/32] = -1; }   } for (int i =0; i < 20; i++){ blockType[10][i] = 0; }```

Then my render code... where i believe the problem is
Code Java:

```public static void render(Graphics g){ for (int x =0 ; x < chunkx-32; x++){ for (int y =0; y < chunky-32; y++){     //CREATING BLOCKS // if (_Mouse.mouse.intersects(tile[x/32][y/32]) && delete && blockType[x/32][y/32] == -1 && _Inventory.hand){ // blockType[x/32][y/32] = 10; // } // //DELETE BLOCKS // if (_Mouse.mouse.intersects(tile[x/32][y/32]) && delete && blockType[x/32][y/32] != -1 && _Inventory.pick){ // blockType[x/32][y/32] = -1; // }   //SPAWNING BLOCKS switch (blockType[x/32][y/32]){ case 0: g.drawImage(_LoadImages.image[0], tile[x/32][y/32].x+(Main.PLAYEROSx*2), tile[x/32][y/32].y+(Main.PLAYEROSy*2), 32, 32, null); break; case 1: g.drawImage(_LoadImages.image[1], tile[x/32][y/32].x+(Main.PLAYEROSx*2), tile[x/32][y/32].y+(Main.PLAYEROSy*2), 32, 32, null); break; case 2: g.drawImage(_LoadImages.image[2], tile[x/32][y/32].x+(Main.PLAYEROSx*2), tile[x/32][y/32].y+(Main.PLAYEROSy*2), 32, 32, null); break; case 10: g.drawImage(_LoadImages.image[10], tile[x/32][y/32].x+(Main.PLAYEROSx*2), tile[x/32][y/32].y+(Main.PLAYEROSy*2), 32, 32, null); break; case 11: g.drawImage(_LoadImages.image[11], tile[x/32][y/32].x+(Main.PLAYEROSx*2), tile[x/32][y/32].y+(Main.PLAYEROSy*2), 32, 32, null); break; case 12: g.drawImage(_LoadImages.image[12], tile[x/32][y/32].x+(Main.PLAYEROSx*2), tile[x/32][y/32].y+(Main.PLAYEROSy*2), 32, 32, null); break; case 21: g.drawImage(_LoadImages.image[21], tile[x/32][y/32].x+(Main.PLAYEROSx*2), tile[x/32][y/32].y+(Main.PLAYEROSy*2), 32, 32, null); break; case 22: g.drawImage(_LoadImages.image[22], tile[x/32][y/32].x+(Main.PLAYEROSx*2), tile[x/32][y/32].y+(Main.PLAYEROSy*2), 32, 32, null); break; }   } }   }```

Main.java
Code java:

```package main;   import main.controls._KeyBoard; import main.controls._Mouse; import main.player._Jump; import main.player._Player; import main.terrian.*;   import java.awt.Canvas; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Rectangle; import java.awt.image.BufferStrategy;   import javax.swing.JFrame;   public class Main extends Canvas implements Runnable{ private static final long serialVersionUID = 1L;   //PLAYER Rectangle p1; public static double windowROT =0; public static int PLAYEROSx=0; public static int PLAYEROSy=0; private static _Player player;   //JFRAME public static int windowWidth = 600; public static int windowHeight = 400; public static int scale = 2;   //SCRIPT public static boolean running = false; public Thread gameThread;   //FPS public int currentFPS = 0; public static int FPS = 0; public long start = 0;   public void init(){ this.addKeyListener(new _KeyBoard()); this.addMouseListener(new _Mouse()); player = new _Player(); new _LoadImages(); new _TerrianGen(); } public synchronized void start(){ if(running)return; running = true; gameThread = new Thread(this); gameThread.start(); } public synchronized void stop(){ if(!running)return; running = false; try { gameThread.join(); } catch (InterruptedException e) { e.printStackTrace();   } } public void run() { init();   long lastTime = System.nanoTime(); final double amountOfTicks = 60D; double ns = 1000000000 / amountOfTicks; double delta = 0;   while(running){ long now = System.nanoTime(); delta += (now - lastTime) / ns; lastTime = now; if(delta >= 1){ tick();delta--;   } render();   } stop(); } public void tick(){ _Player.tick();   _Jump.tick();     currentFPS++; if(System.currentTimeMillis() - start >= 1000) { FPS = currentFPS;currentFPS = 0; start = System.currentTimeMillis(); } } public void render(){   BufferStrategy bs = this.getBufferStrategy(); if(bs == null){ createBufferStrategy(3); return; } Graphics g = bs.getDrawGraphics(); //RENDER HERE   _Background.render(g);   _Player.render(g);   //END RENDER g.dispose(); bs.show(); } public static void main (String[] args) { Main game = new Main(); game.setPreferredSize(new Dimension(windowWidth * scale, windowHeight * scale)); game.setMaximumSize(new Dimension(windowWidth * scale, windowHeight * scale)); game.setMinimumSize(new Dimension(windowWidth * scale, windowHeight * scale));   JFrame frame = new JFrame("Void v2.0 Alpha"); frame.setSize(windowWidth * scale, windowHeight * scale); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setResizable(false); frame.add(game); frame.setVisible(true); frame.setLocationRelativeTo(null);   game.start(); } public static _Player getPlayer(){ return player; } public static int getFPS() { return FPS; }     }```
• January 19th, 2014, 01:58 AM
GregBrannon
Re: My 2D Tile Game Lag (Caused by loop) Looking for an alternate method of rendering
Have you used a profiler to determine that the code you've posted is the source of the lag, or are you guessing?
• January 19th, 2014, 04:00 AM
Cekeh
Re: My 2D Tile Game Lag (Caused by loop) Looking for an alternate method of rendering
When I remove the render chunk of the code, the lag disappears. So yeah I suppose you could say its a guess. I can post all my code if it would be easier.

Also I'm fairly new to java, only learning off of tutorials and some java books I have lying around the house. So if you have any tips, please state them as I would love to learn more.
• January 19th, 2014, 04:53 AM
GregBrannon
Re: My 2D Tile Game Lag (Caused by loop) Looking for an alternate method of rendering
So you have some evidence that's the source of the lag. Didn't want to waste time looking at code that wasn't the problem. The key areas to investigate are: large nested for loops, in the tens of thousands, the images are being loaded each time from a slow resource, and/or the thread that does the drawing is being slept somewhere else.

Do you have a loop that controls the pace of the game and/or "sleeps the thread" somewhere? If so post that. What I'm asking for might be your game loop. Either way, post that.

If one reduces the code you posted to code that actually does something, there's a nested for loop and a switch statement. How large can that nested for loop be? What's the maximum (chunkx - 32) * (chunky - 32)? Then in the switch statement, what does _LoadImages do? Are the images being loaded already in memory or are they being obtained from a laggy resource like a hard drive or the internet? Why do you even need the switch() statement? It seems the only difference in the statement in the case blocks is the index which is the same as the switch parameter. Why not use that parameter to indicate the desired image and replace the entire switch() logic with a single line of code. That code will be more elegant, but it won't change performance.

Everybody's "new to Java" and writing (or wanting to write) a complex graphical game. Don't get me started.
• January 19th, 2014, 07:01 PM
Cekeh
Re: My 2D Tile Game Lag (Caused by loop) Looking for an alternate method of rendering
_LoadImages, just simply stores a buffered image that was inside the init() method. Yes my game has a loop timer of 60fps.
• January 20th, 2014, 03:41 AM
GregBrannon
Re: My 2D Tile Game Lag (Caused by loop) Looking for an alternate method of rendering
It's interesting that I asked several questions, suggested a couple places to look, and asked for code and you gave such a short answer that covers about 20% of what I asked. How are we supposed to help with so little information?
• January 20th, 2014, 06:05 AM
Cekeh
Re: My 2D Tile Game Lag (Caused by loop) Looking for an alternate method of rendering
Im sorry I'm very busy and not at my computer much lately, I've been posting from my phone. I will answer more as soon as I get a good look at the code myself.