From 60c85ec4f670eb1c7211f07829e6e650d00a8e46 Mon Sep 17 00:00:00 2001 From: Jozufozu Date: Wed, 4 Aug 2021 14:34:13 -0700 Subject: [PATCH] Fix nullpointer in ModelRenderer - Somehow managed to only do a check in a subclass - Add #empty() helper method to IModel --- .../backend/model/ArrayModelRenderer.java | 8 ++--- .../flywheel/backend/model/ModelRenderer.java | 31 +++++++++---------- .../jozufozu/flywheel/core/model/IModel.java | 8 +++++ 3 files changed, 25 insertions(+), 22 deletions(-) diff --git a/src/main/java/com/jozufozu/flywheel/backend/model/ArrayModelRenderer.java b/src/main/java/com/jozufozu/flywheel/backend/model/ArrayModelRenderer.java index 17bcff621..48359821c 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/model/ArrayModelRenderer.java +++ b/src/main/java/com/jozufozu/flywheel/backend/model/ArrayModelRenderer.java @@ -16,7 +16,7 @@ public class ArrayModelRenderer extends ModelRenderer { @Override public void draw() { - if (!isInitialized()) init(); + if (!initialized) init(); if (!isValid()) return; vao.bind(); @@ -26,16 +26,12 @@ public class ArrayModelRenderer extends ModelRenderer { vao.unbind(); } - private boolean isValid() { - return model != null && model.valid(); - } - @Override protected void init() { initialized = true; IModel model = modelSupplier.get(); - if (model.vertexCount() <= 0) return; + if (model.empty()) return; this.model = new IndexedModel(model); diff --git a/src/main/java/com/jozufozu/flywheel/backend/model/ModelRenderer.java b/src/main/java/com/jozufozu/flywheel/backend/model/ModelRenderer.java index e0a2e97b9..72a11d601 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/model/ModelRenderer.java +++ b/src/main/java/com/jozufozu/flywheel/backend/model/ModelRenderer.java @@ -19,30 +19,29 @@ public class ModelRenderer { * Renders this model, checking first if there is anything to render. */ public void draw() { - - if (!isInitialized()) init(); - if (!model.valid()) return; + if (!initialized) init(); + if (!isValid()) return; model.setupState(); model.drawCall(); model.clearState(); } - protected void init() { - initialized = true; - IModel model = modelSupplier.get(); - - if (model.vertexCount() <= 0) return; - - this.model = new IndexedModel(model); - } - - public boolean isInitialized() { - return initialized; - } - public void delete() { if (model != null) model.delete(); } + + protected void init() { + initialized = true; + IModel model = modelSupplier.get(); + + if (model.empty()) return; + + this.model = new IndexedModel(model); + } + + protected boolean isValid() { + return model != null && model.valid(); + } } diff --git a/src/main/java/com/jozufozu/flywheel/core/model/IModel.java b/src/main/java/com/jozufozu/flywheel/core/model/IModel.java index 7bc41a9ae..b7ffd1a1b 100644 --- a/src/main/java/com/jozufozu/flywheel/core/model/IModel.java +++ b/src/main/java/com/jozufozu/flywheel/core/model/IModel.java @@ -63,4 +63,12 @@ public interface IModel { default int size() { return vertexCount() * format().getStride(); } + + /** + * Is there nothing to render? + * @return true if there are no vertices. + */ + default boolean empty() { + return vertexCount() == 0; + } }