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
This is the Init() code
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
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
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; } }