Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 21 additions & 27 deletions examples/nes_emulator/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ const script = registerScript({
});

const File = Java.type("java.io.File");
const Files = Java.type("java.nio.file.Files");
const RenderShortcutsKt = Java.type("net.ccbluex.liquidbounce.render.RenderShortcutsKt");
const NativeImageBackedTexture = Java.type("net.minecraft.client.texture.NativeImageBackedTexture");
const RenderSystem = Java.type("com.mojang.blaze3d.systems.RenderSystem");
const Vec3d = Java.type("net.minecraft.util.math.Vec3d");
const RenderPipelines = Java.type("net.minecraft.client.renderer.RenderPipelines");
const Render2DKt = Java.type("net.ccbluex.liquidbounce.render.Render2DKt")
const TextureSetup = Java.type("net.minecraft.client.gui.render.TextureSetup");
const DynamicTexture = Java.type("net.minecraft.client.renderer.texture.DynamicTexture");
const FileUtils = Java.type("org.apache.commons.io.FileUtils");
const StandardCharsets = Java.type("java.nio.charset.StandardCharsets");
const Thread = Java.type("java.lang.Thread");
Expand All @@ -26,12 +25,12 @@ function readRom(name) {
}

function getInstalledRoms() {
return ["None", ...new File(ROMS_BASE_PATH).listFiles().map(f => f.getName())];
return ["None", ...(new File(ROMS_BASE_PATH).listFiles() ?? []).map(f => f.getName())];
}

const installedRoms = getInstalledRoms();

const texture = new NativeImageBackedTexture(SCREEN_WIDTH, SCREEN_HEIGHT, false);
const texture = new DynamicTexture("NESEmulator Texture", SCREEN_WIDTH, SCREEN_HEIGHT, false);

script.registerModule({
name: "NESEmulator",
Expand Down Expand Up @@ -147,7 +146,7 @@ script.registerModule({

nes.loadROM(readRom(mod.settings.rom.value));

UnsafeThread.run(() => {
AsyncUtil.launch(() => {
while (runEmulator) {
nes.frame();
Thread.sleep((1000 / targetFramerate) | 0);
Expand All @@ -163,7 +162,7 @@ script.registerModule({
const key = e.getKey();

for (const k of controller) {
if (k.setting.value.getTranslationKey() === key.getTranslationKey()) {
if (k.setting.value.getName() === key.getName()) {
if (e.getAction() === 1 || e.getAction() === 2) {
nes.buttonDown(1, k.emulatorKey);
} else {
Expand All @@ -175,25 +174,20 @@ script.registerModule({

mod.on("overlayRender", e => {
const context = e.getContext();
const x = ~~(context.guiWidth() / 2 - SCREEN_WIDTH / 2);
const y = ~~(context.guiHeight() / 2 - SCREEN_HEIGHT / 2);

const x = context.getScaledWindowWidth() / 2 - SCREEN_WIDTH / 2;
const y = context.getScaledWindowHeight() / 2 - SCREEN_HEIGHT / 2;

RenderShortcutsKt.renderEnvironmentForGUI(context.getMatrices(), renderEnvironment => {
texture.bindTexture();

if (dirty) {
texture.upload();
dirty = false;
}

RenderSystem.bindTexture(texture.getGlId());
RenderSystem.setShaderTexture(0, texture.getGlId());

RenderShortcutsKt.drawTextureQuad(renderEnvironment, new Vec3d(x, y, 0), new Vec3d(x + SCREEN_WIDTH, y + SCREEN_HEIGHT, 0));
if (dirty) {
texture.upload();
dirty = false;
}

RenderSystem.setShaderTexture(0, 0);
});
Render2DKt.drawBlitOnCurrentLayer(
context,
TextureSetup.singleTexture(texture.getTextureView(), texture.getSampler()),
x, y,
x + SCREEN_WIDTH, y + SCREEN_HEIGHT,
0, 0, 1, 1, -1, RenderPipelines.GUI_TEXTURED,
)
});
});