Skip to content

Commit

Permalink
fix: Window not being available on instantiation on Fabric
Browse files Browse the repository at this point in the history
---

Fabric loads earlier than I expected.
The Minecraft instance is available but the MainWindow isn't.
Refactored to not use fixed fields but method calls.

TODO: Change back once Lazy Loading is done as it should then be safe.

Fix #25

Signed-off-by: AterAnimAvis <AterAnimAvis@gmail.com>
  • Loading branch information
AterAnimAvis committed Feb 14, 2021
1 parent 9dfbc75 commit e2eeaa1
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.unascribed.blockrenderer.fabric.client.varia.rendering;

import com.mojang.blaze3d.systems.RenderSystem;
import com.unascribed.blockrenderer.varia.logging.Log;
import com.unascribed.blockrenderer.varia.logging.Markers;
import com.unascribed.blockrenderer.varia.rendering.GLI;
import net.minecraft.client.MainWindow;
import net.minecraft.client.Minecraft;
Expand All @@ -15,18 +13,6 @@ public class GL implements GLI {

public static final GLI INSTANCE = new GL();

Minecraft client;
MainWindow window;

@SuppressWarnings("ConstantConditions")
public GL() {
client = Minecraft.getInstance();
window = client != null ? client.getMainWindow() : null;

if (client == null) Log.warn(Markers.ROOT, "Minecraft Instance isn't present. If your not in a data-gen context this is an error!");
if (client != null && window == null) throw new AssertionError("Minecraft Instance is present but the Window isn't.");
}

/* ================================================================================================== Matrix ==== */

@Override
Expand Down Expand Up @@ -115,42 +101,42 @@ public void clearDepthBuffer() {

@Override
public void unbindFBO() {
client.getFramebuffer().unbindFramebuffer();
client().getFramebuffer().unbindFramebuffer();
}

@Override
public void flipFrame() {
window.flipFrame();
window().flipFrame();
}

@Override
public void rebindFBO() {
client.getFramebuffer().bindFramebuffer(false);
client().getFramebuffer().bindFramebuffer(false);
}

@Override
public int getScaledWidth() {
return window.getScaledWidth();
return window().getScaledWidth();
}

@Override
public int getScaledHeight() {
return window.getScaledHeight();
return window().getScaledHeight();
}

@Override
public int getFramebufferWidth() {
return window.getFramebufferWidth();
return window().getFramebufferWidth();
}

@Override
public int getFramebufferHeight() {
return window.getFramebufferHeight();
return window().getFramebufferHeight();
}

@Override
public double getScaleFactor() {
return window.getGuiScaleFactor();
return window().getGuiScaleFactor();
}

/* ============================================================================================= Projections ==== */
Expand All @@ -165,4 +151,14 @@ public void matrixModeModelView() {
RenderSystem.matrixMode(GL11.GL_MODELVIEW);
}

/* ================================================================================================= Utility ==== */

private Minecraft client() {
return Minecraft.getInstance();
}

private MainWindow window() {
return Minecraft.getInstance().getMainWindow();
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.unascribed.blockrenderer.forge.client.varia.rendering;

import com.mojang.blaze3d.systems.RenderSystem;
import com.unascribed.blockrenderer.varia.logging.Log;
import com.unascribed.blockrenderer.varia.logging.Markers;
import com.unascribed.blockrenderer.varia.rendering.GLI;
import net.minecraft.client.MainWindow;
import net.minecraft.client.Minecraft;
Expand All @@ -15,18 +13,6 @@ public class GL implements GLI {

public static final GLI INSTANCE = new GL();

Minecraft client;
MainWindow window;

@SuppressWarnings("ConstantConditions")
public GL() {
client = Minecraft.getInstance();
window = client != null ? client.getMainWindow() : null;

if (client == null) Log.warn(Markers.ROOT, "Minecraft Instance isn't present. If your not in a data-gen context this is an error!");
if (client != null && window == null) throw new AssertionError("Minecraft Instance is present but the Window isn't.");
}

/* ================================================================================================== Matrix ==== */

@Override
Expand Down Expand Up @@ -115,42 +101,42 @@ public void clearDepthBuffer() {

@Override
public void unbindFBO() {
client.getFramebuffer().unbindFramebuffer();
client().getFramebuffer().unbindFramebuffer();
}

@Override
public void flipFrame() {
window.flipFrame();
window().flipFrame();
}

@Override
public void rebindFBO() {
client.getFramebuffer().bindFramebuffer(false);
client().getFramebuffer().bindFramebuffer(false);
}

@Override
public int getScaledWidth() {
return window.getScaledWidth();
return window().getScaledWidth();
}

@Override
public int getScaledHeight() {
return window.getScaledHeight();
return window().getScaledHeight();
}

@Override
public int getFramebufferWidth() {
return window.getFramebufferWidth();
return window().getFramebufferWidth();
}

@Override
public int getFramebufferHeight() {
return window.getFramebufferHeight();
return window().getFramebufferHeight();
}

@Override
public double getScaleFactor() {
return window.getGuiScaleFactor();
return window().getGuiScaleFactor();
}

/* ============================================================================================= Projections ==== */
Expand All @@ -165,4 +151,14 @@ public void matrixModeModelView() {
RenderSystem.matrixMode(GL11.GL_MODELVIEW);
}

/* ================================================================================================= Utility ==== */

private Minecraft client() {
return Minecraft.getInstance();
}

private MainWindow window() {
return Minecraft.getInstance().getMainWindow();
}

}
2 changes: 1 addition & 1 deletion patches/forge2fabric/client/varia/rendering/GL.java.patch
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
+package com.unascribed.blockrenderer.fabric.client.varia.rendering;

import com.mojang.blaze3d.systems.RenderSystem;
import com.unascribed.blockrenderer.varia.logging.Log;
import com.unascribed.blockrenderer.varia.rendering.GLI;

0 comments on commit e2eeaa1

Please sign in to comment.