1
0
Fork 0

Boost de puissance

This commit is contained in:
MrDev023 2015-07-15 21:52:39 +02:00
parent 5ccf998834
commit 69577a9f6a
3 changed files with 83 additions and 29 deletions

View file

@ -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

View file

@ -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();
}
}

View file

@ -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<Chunk> chunks = new ArrayList<Chunk>();
@ -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;