From 69577a9f6a92e9f0dfcaa69b45aea692d42cb221 Mon Sep 17 00:00:00 2001 From: MrDev023 Date: Wed, 15 Jul 2015 21:52:39 +0200 Subject: [PATCH] Boost de puissance --- VBO/src/main/Main.java | 16 ++++++++ VBO/src/world/Chunk.java | 84 ++++++++++++++++++++++++++++------------ VBO/src/world/World.java | 12 +++--- 3 files changed, 83 insertions(+), 29 deletions(-) diff --git a/VBO/src/main/Main.java b/VBO/src/main/Main.java index 1aa4842..d2fa85f 100644 --- a/VBO/src/main/Main.java +++ b/VBO/src/main/Main.java @@ -15,6 +15,8 @@ public class Main { private static final String TITLE = "Test VBO"; private static final int width = 1280, height = 720; + + private static Thread[] threadArray; private static Game game; @@ -29,12 +31,26 @@ public class Main { Display.setResizable(true); Mouse.setGrabbed(true); Display.create(); + threadArray = new Thread[Runtime.getRuntime().availableProcessors()]; game = new Game(); loop(); } catch (Exception e) { } } + + public static Thread addThread(Thread t){ + for(Thread c : threadArray){ + c = t; + try { + c.join(); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + return t; + } /** * @Info Boucle principal avec Timer diff --git a/VBO/src/world/Chunk.java b/VBO/src/world/Chunk.java index d48a841..289e549 100644 --- a/VBO/src/world/Chunk.java +++ b/VBO/src/world/Chunk.java @@ -6,12 +6,14 @@ import math.*; public class Chunk { - public final static int SIZE = 4; + public final static int SIZE = 8; private int x, y, z; private VBO vbo; private World world; Block[][][] blocks; private boolean IsLoad = false; + private boolean IsGenerated = false; + private boolean IsCurrentGenerate = false; public Chunk(int x, int y, int z, World world) { this.x = x; @@ -27,26 +29,17 @@ public class Chunk { } public void update() { - + } public void createChunk(World world) { this.world = world; - for (int i = 0; i < SIZE; i++) { - for (int j = 0; j < 2; j++) { - for (int k = 0; k < SIZE; k++) { - blocks[i][j][k] = Block.GRASS; - } - } - } - for (int i = 0; i < SIZE; i++) { - for (int j = 0; j < 2; j++) { - for (int k = 0; k < SIZE; k++) { - loopChunk(i, j, k); - } - } - } + Main.addThread((new Thread(new Generate(this, world)))).start(); + IsCurrentGenerate = true; + } + + public void loadBufferData() { vbo.bufferData(); IsLoad = true; } @@ -79,20 +72,20 @@ public class Chunk { return z; } - public Vector3f getPosition(){ - return new Vector3f(x,y,z); + public Vector3f getPosition() { + return new Vector3f(x, y, z); } - + public Block getBlock(int x, int y, int z) { if (x < 0 || y < 0 || z < 0 || x >= SIZE || y >= SIZE || z >= SIZE) return null; return blocks[x][y][z]; } - public void destroyChunk(){ + public void destroyChunk() { vbo.destroyVBO(); } - + public void loopChunk(int x, int y, int z) { int xx = this.x * SIZE + x; int yy = this.y * SIZE + y; @@ -255,7 +248,50 @@ public class Chunk { public boolean isLoaded() { return IsLoad; } - - - + + public boolean isGenerated() { + return IsGenerated; + } + + public void setGenerated(boolean g) { + IsGenerated = g; + } + + public boolean isCurrentGenerate(){ + return IsCurrentGenerate; + } +} + +class Generate implements Runnable { + + private Chunk chunk; + private World world; + + public Generate(Chunk chunk, World world) { + this.chunk = chunk; + this.world = world; + } + + public void run() { + for (int i = 0; i < chunk.SIZE; i++) { + for (int j = 0; j < 2; j++) { + for (int k = 0; k < chunk.SIZE; k++) { + chunk.blocks[i][j][k] = Block.GRASS; + } + } + } + synchronized (world.chunks) { + for (int i = 0; i < chunk.SIZE; i++) { + for (int j = 0; j < 2; j++) { + for (int k = 0; k < chunk.SIZE; k++) { + chunk.loopChunk(i, j, k); + } + } + } + } + chunk.setGenerated(true); + System.out.println(Thread.currentThread().getName()); + Thread.currentThread().stop(); + } + } diff --git a/VBO/src/world/World.java b/VBO/src/world/World.java index 3d10e1b..0fd9751 100644 --- a/VBO/src/world/World.java +++ b/VBO/src/world/World.java @@ -11,7 +11,7 @@ public class World { public long seed; public final int SIZE = 1,HEIGHT = 1; public static final float GRAVITY = 1; - public static final int VIEW_CHUNK = 2; + public static final int VIEW_CHUNK = 4; public ArrayList chunks = new ArrayList(); @@ -34,16 +34,15 @@ public class World { int za = (int)((Camera.getPosition().getZ()-((float)Chunk.SIZE/2.0f))/(float)Chunk.SIZE) - VIEW_CHUNK; int zb = (int)((Camera.getPosition().getZ()-((float)Chunk.SIZE/2.0f))/(float)Chunk.SIZE) + VIEW_CHUNK; + int delta_x = xb - xa; int delta_z = zb - za; - boolean isC = false; for(int i = 0; i <= delta_x;i++){ for(int k = 0;k <= delta_z;k++){ for(int j = 0; j < HEIGHT; j++){ if(getChunk((xa + i), 0, (za + k)) == null){ Chunk ch = new Chunk((xa + i),j,(za + k),this); chunks.add(ch); - isC = true; } } } @@ -57,7 +56,8 @@ public class World { } } for(Chunk c : chunks){ - if(!c.isLoaded())c.createChunk(this); + if(!c.isLoaded() && !c.isGenerated() && !c.isCurrentGenerate())c.createChunk(this); + if(!c.isLoaded() && c.isGenerated())c.loadBufferData(); c.update(); } System.gc(); @@ -72,7 +72,9 @@ public class World { public Chunk getChunk(int xc, int yc, int zc) { Chunk c = null; - for(Chunk ch : chunks){ + Object[] chunk = chunks.toArray(); + for(int i = 0;i < chunk.length;i++){ + Chunk ch = (Chunk)chunk[i]; if(ch.getX() == xc && ch.getY() == yc && ch.getZ() == zc){ c = ch; break;