Skip to content

Commit 31fa64e

Browse files
committed
up
1 parent c962e08 commit 31fa64e

File tree

3 files changed

+69
-16
lines changed

3 files changed

+69
-16
lines changed

src/comcrafthd/ChunkList.java

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,39 @@ private int getInitialSize() {
2828
return (int) (ComcraftPrefs.instance.chunkRenderDistance * ComcraftPrefs.instance.chunkRenderDistance * 3.2f) + 1;
2929
}
3030

31-
public void triggerRenderChunks(final ComcraftRenderer renderer) {
31+
public void triggerRenderClosestChunk(final ComcraftRenderer renderer, int blockX, int blockZ) {
32+
final short originChunkX = (short) (blockX >> Chunk.BLOCK_TO_CHUNK_SHIFT);
33+
final short originChunkZ = (short) (blockZ >> Chunk.BLOCK_TO_CHUNK_SHIFT);
34+
35+
Chunk closest = null;
36+
int min = 0;
37+
3238
for (final Enumeration e = chunks.elements(); e.hasMoreElements();) {
3339
final Chunk chunk = (Chunk) e.nextElement();
34-
renderer.renderChunkListCallback(chunk);
40+
41+
if (!areNeighboursLoaded(chunk.chunkX, chunk.chunkZ)) {
42+
continue;
43+
}
44+
45+
synchronized (chunk.renderCache) {
46+
if (chunk.renderCache.done || chunk.renderCache.isCacheBeingGenerated) {
47+
continue;
48+
}
49+
}
50+
51+
final int x = chunk.chunkX - originChunkX;
52+
final int z = chunk.chunkZ - originChunkZ;
53+
54+
final int dist = x * x + z * z;
55+
56+
if (closest == null || dist < min) {
57+
closest = chunk;
58+
min = dist;
59+
}
60+
}
61+
62+
if (closest != null) {
63+
renderer.renderChunkListCallback(closest);
3564
}
3665
}
3766

@@ -76,9 +105,9 @@ public void dropChunk(final Chunk chunk) {
76105
return;
77106
}
78107
}
79-
108+
80109
chunks.remove(getChunkKey(chunk.chunkX, chunk.chunkZ));
81-
110+
82111
ComcraftGame.instance.renderer.dropChunkListCallback(chunk);
83112
}
84113

@@ -109,5 +138,21 @@ public Chunk getChunk(int chunkX, int chunkZ) {
109138
private void addChunk(int chunkX, int chunkZ, Chunk chunk) {
110139
chunks.put(getChunkKey(chunkX, chunkZ), chunk);
111140
}
141+
142+
public boolean areNeighboursLoaded(int chunkX, int chunkZ) {
143+
if (!chunkExists(chunkX - 1, chunkZ)) {
144+
return false;
145+
}
146+
if (!chunkExists(chunkX + 1, chunkZ)) {
147+
return false;
148+
}
149+
if (!chunkExists(chunkX, chunkZ - 1)) {
150+
return false;
151+
}
152+
if (!chunkExists(chunkX, chunkZ + 1)) {
153+
return false;
154+
}
155+
return true;
156+
}
112157

113158
}

src/comcrafthd/client/ComcraftRenderer.java

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import comcrafthd.ChunkList;
1010
import comcrafthd.ComcraftGame;
1111
import comcrafthd.Log;
12+
import comcrafthd.MathHelper;
1213
import comcrafthd.client.midlets.ComcraftMIDPCanvas;
1314
import java.io.IOException;
1415
import javax.microedition.lcdui.Graphics;
@@ -58,11 +59,9 @@ public void initialize() {
5859
public void stop() {
5960
chunkRendererThread.stop();
6061
}
61-
62+
6263
public void render() {
63-
if (chunkRendererThread.hasVaccancy) {
64-
ComcraftGame.instance.chunkList.triggerRenderChunks(this);
65-
}
64+
renderChunksCache();
6665

6766
int hints = Graphics3D.OVERWRITE;
6867

@@ -74,24 +73,33 @@ public void render() {
7473
comcraftCanvas.flushGraphics();
7574
}
7675

76+
private void renderChunksCache() {
77+
if (chunkRendererThread.hasVaccancy) {
78+
final CameraMovement cameraMovement = ComcraftGame.instance.cameraMovement;
79+
80+
final int centerBlockX = MathHelper.roundToInt(cameraMovement.positionX);
81+
final int centerBlockZ = MathHelper.roundToInt(cameraMovement.positionZ);
82+
83+
ComcraftGame.instance.chunkList.triggerRenderClosestChunk(this, centerBlockX, centerBlockZ);
84+
}
85+
}
86+
7787
public synchronized void chunkRendererThreadCallback(final Chunk chunk) {
7888
world.addChild(chunk.renderCache.node);
7989
}
8090

8191
public void renderChunkListCallback(final Chunk chunk) {
82-
if (chunk.renderCache.done == false) {
83-
clearChunkRenderCache(chunk);
92+
clearChunkRenderCache(chunk);
8493

85-
if (chunkRendererThread.hasVaccancy) {
86-
chunkRendererThread.enqueue(chunk);
87-
}
94+
if (chunkRendererThread.hasVaccancy) {
95+
chunkRendererThread.enqueue(chunk);
8896
}
8997
}
9098

9199
public void dropChunkListCallback(final Chunk chunk) {
92100
clearChunkRenderCache(chunk);
93101
}
94-
102+
95103
private void clearChunkRenderCache(final Chunk chunk) {
96104
if (chunk.renderCache.node != null) {
97105
world.removeChild(chunk.renderCache.node);

src/comcrafthd/client/midlets/ComcraftMIDlet.vmd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,10 +265,10 @@
265265
</Document>
266266
<FlowScene version="1">
267267
<Node componentID="25" descriptorID="componentNode25" x="62" y="305"/>
268-
<Node componentID="75" descriptorID="componentNode75" x="231" y="841"/>
269-
<Node componentID="22" descriptorID="componentNode22" x="511" y="436"/>
270268
<Node componentID="65" descriptorID="componentNode65" x="442" y="684"/>
269+
<Node componentID="75" descriptorID="componentNode75" x="231" y="841"/>
271270
<Node componentID="45" descriptorID="componentNode45" x="61" y="467"/>
271+
<Node componentID="22" descriptorID="componentNode22" x="511" y="436"/>
272272
<Node componentID="2" descriptorID="componentNode2" x="35" y="160"/>
273273
</FlowScene>
274274
</VisualDesign>

0 commit comments

Comments
 (0)