Directly using VirtualBox from Java #5

Merged
NorbiPeti merged 60 commits from directvb into master 2019-04-18 23:29:21 +00:00
4 changed files with 27 additions and 7 deletions
Showing only changes of commit 18e1981a2a - Show all commits

View file

@ -8,6 +8,7 @@ import org.bukkit.command.CommandSender;
import org.virtualbox_6_0.*;
import sznp.virtualcomputer.events.MachineEventHandler;
import sznp.virtualcomputer.events.VBoxEventHandler;
import sznp.virtualcomputer.renderer.GPURenderer;
import sznp.virtualcomputer.renderer.GPURendererInternal;
import sznp.virtualcomputer.renderer.MCFrameBuffer;
import sznp.virtualcomputer.util.Scancode;
@ -207,7 +208,7 @@ public final class Computer {
sendMessage(sender, "§eComputer powered off."); //This block runs later
}
});
GPURendererInternal.setPixels(new byte[1], 0, 0); //Black screen
GPURenderer.update(new byte[1], 0, 0, 0, 0, 640, 480); //Black screen
stopEvents();
MouseLockerPlayerListener.computerStop();
}

View file

@ -16,8 +16,10 @@ public class GPURenderer extends MapRenderer implements IRenderer {
private byte[] buffer;
private GPURendererInternal kernel;
private WorldMap wmap;
private int mapx, mapy;
//Store at central location after conversion
private static int[] colors_;
private static int changedX, changedY, changedWidth, changedHeight;
public GPURenderer(short id, World world, int mapx, int mapy) throws Exception {
MapView map = IRenderer.prepare(id, world);
@ -32,6 +34,8 @@ public class GPURenderer extends MapRenderer implements IRenderer {
colors_[i] = cs[i].getRGB();
}
}
this.mapx = mapx;
this.mapy = mapy;
Field field = map.getClass().getDeclaredField("worldMap");
field.setAccessible(true);
wmap = (WorldMap) field.get(map);
@ -46,19 +50,34 @@ public class GPURenderer extends MapRenderer implements IRenderer {
public void render(MapView map, MapCanvas canvas, Player player) {
Timing t = new Timing();
try {
if (kernel.isRendered()) return; //TODO: Stop rendering after computer is stopped
if (kernel.isRendered()) return;
if (buffer == null) { //The buffer remains the same, as the canvas remains the same
Field field = canvas.getClass().getDeclaredField("buffer");
field.setAccessible(true);
buffer = (byte[]) field.get(canvas);
}
if (changedX >= (mapx + 1) * 128 || changedY >= (mapy + 1) * 128
|| changedX + changedWidth < mapx * 128 || changedY + changedHeight < mapy * 128)
return; //No change for this map - TODO: Test
int x = changedX % 128;
int y = changedY % 128;
int w = x + changedWidth >= 128 ? 128 - x - 1 : changedWidth;
int h = y + changedHeight >= 128 ? 128 - y - 1 : changedHeight;
kernel.render(buffer);
wmap.flagDirty(0, 0);
wmap.flagDirty(127, 127); // Send the whole image - TODO: Only send changes
wmap.flagDirty(x, y);
wmap.flagDirty(x + w, y + h); // Send the changes only
} catch (Exception e) {
e.printStackTrace();
}
if (t.elapsedMS() > 60)
System.out.println("Map rendering took " + t.elapsedMS() + "ms");
}
public static void update(byte[] pixels, int width, int height, int changedX, int changedY, int changedWidth, int changedHeight) {
GPURenderer.changedX = changedX;
GPURenderer.changedY = changedY;
GPURenderer.changedWidth = changedWidth;
GPURenderer.changedHeight = changedHeight;
GPURendererInternal.setPixels(pixels, width, height);
}
}

View file

@ -95,7 +95,7 @@ public class GPURendererInternal extends Kernel {
private static final int BLUE = 0;
@SuppressWarnings("Convert2Lambda") //Aparapi fails with lambdas
public static void setPixels(byte[] pixels, int width, int height) {
static void setPixels(byte[] pixels, int width, int height) {
renderers.forEach(new Consumer<GPURendererInternal>() {
@Override //IT'S THE LAMBDAS (exception)
public void accept(GPURendererInternal r) {

View file

@ -106,7 +106,7 @@ public class MCFrameBuffer implements IFramebuffer {
pointer = new Pointer(ptr[0]);
this.width = (int) w[0];
this.height = (int) h[0];
GPURendererInternal.setPixels(pointer.getByteArray(0L, (int) (w[0] * h[0] * 4)), (int) w[0], (int) h[0]);
GPURenderer.update(pointer.getByteArray(0L, (int) (w[0] * h[0] * 4)), (int) w[0], (int) h[0], 0, 0, this.width, this.height);
} else {
PluginMain.allpixels = new Pointer(ptr[0]).getByteBuffer(0L, width * height * 4);
if (width * height > 640 * 480)
@ -129,7 +129,7 @@ public class MCFrameBuffer implements IFramebuffer {
@Override
public void notifyUpdate(long x, long y, long width, long height) {
Timing t = new Timing();
GPURendererInternal.setPixels(pointer.getByteArray(0L, (this.width * this.height * 4)), this.width, this.height); //TODO: Only copy changed part
GPURenderer.update(pointer.getByteArray(0L, this.width * this.height * 4), this.width, this.height, (int) x, (int) y, (int) width, (int) height);
if (t.elapsedMS() > 60) //Typically 1ms max
System.out.println("Update took " + t.elapsedMS() + "ms");
}