From 51e0f84c3fef60a83c902d0c84bd5d3e53ebe349 Mon Sep 17 00:00:00 2001 From: JeremyStarTM Date: Sat, 15 Jun 2024 22:24:37 +0200 Subject: [PATCH] Update graphics subsystem and OpenGL API --- .../graphics/opengl/OpenGlManagement.java | 2 +- .../graphics/opengl/OpenGlSubsystem.java | 28 +++++++++-- .../sosengine/graphics/GraphicsSubsystem.java | 1 - .../events/GraphicsApiErrorEvent.java | 49 +++++++++++++++++++ 4 files changed, 73 insertions(+), 7 deletions(-) create mode 100644 graphics/src/main/java/de/staropensource/sosengine/graphics/events/GraphicsApiErrorEvent.java diff --git a/graphics/opengl/src/main/java/de/staropensource/sosengine/graphics/opengl/OpenGlManagement.java b/graphics/opengl/src/main/java/de/staropensource/sosengine/graphics/opengl/OpenGlManagement.java index 157e4fab..3ec78131 100644 --- a/graphics/opengl/src/main/java/de/staropensource/sosengine/graphics/opengl/OpenGlManagement.java +++ b/graphics/opengl/src/main/java/de/staropensource/sosengine/graphics/opengl/OpenGlManagement.java @@ -30,7 +30,7 @@ import org.jetbrains.annotations.NotNull; * @since 1-alpha0 */ @SuppressWarnings({ "unused" }) -public class OpenGlManagement implements ApiManagementClass { +public final class OpenGlManagement implements ApiManagementClass { /** {@inheritDoc} */ @NotNull @Override diff --git a/graphics/opengl/src/main/java/de/staropensource/sosengine/graphics/opengl/OpenGlSubsystem.java b/graphics/opengl/src/main/java/de/staropensource/sosengine/graphics/opengl/OpenGlSubsystem.java index ef5a4cd5..7f1d97b3 100644 --- a/graphics/opengl/src/main/java/de/staropensource/sosengine/graphics/opengl/OpenGlSubsystem.java +++ b/graphics/opengl/src/main/java/de/staropensource/sosengine/graphics/opengl/OpenGlSubsystem.java @@ -21,12 +21,14 @@ package de.staropensource.sosengine.graphics.opengl; import de.staropensource.sosengine.base.annotations.EventListener; import de.staropensource.sosengine.base.classes.EventPriority; +import de.staropensource.sosengine.base.classes.helpers.EventHelper; import de.staropensource.sosengine.base.logging.LoggerInstance; import de.staropensource.sosengine.base.types.LogIssuer; import de.staropensource.sosengine.base.utility.Miscellaneous; import de.staropensource.sosengine.graphics.GraphicsSubsystem; import de.staropensource.sosengine.graphics.classes.ApiMainClass; import de.staropensource.sosengine.graphics.classes.ApiManagementClass; +import de.staropensource.sosengine.graphics.events.GraphicsApiErrorEvent; import de.staropensource.sosengine.graphics.opengl.events.GraphicsErrorEvent; import lombok.Getter; import org.lwjgl.glfw.GLFWErrorCallback; @@ -88,10 +90,15 @@ public final class OpenGlSubsystem implements ApiMainClass { instance.logger.crash("The subsystem tried to initialize twice"); } - // Register Graphics API - GraphicsSubsystem.getInstance().registerGraphicsApi(this); + long initTime = Miscellaneous.measureExecutionTime(() -> { + // Precompute event listeners + EventHelper.precomputeEventListeners(GraphicsErrorEvent.class); - logger.info("Initialized subsystem"); + // Register Graphics API + GraphicsSubsystem.getInstance().registerGraphicsApi(this); + }); + + logger.info("Initialized subsystem in " + initTime + "ms"); } /** {@inheritDoc} */ @@ -140,14 +147,25 @@ public final class OpenGlSubsystem implements ApiMainClass { } /** - * Called when a graphics error occurs. This method just logs the error and returns. + * Called when an OpenGL error occurs. This method just logs the error and returns. * * @param error graphics error * @since 1-alpha0 */ @EventListener(event = GraphicsErrorEvent.class, priority = EventPriority.EXTREMELY_IMPORTANT) public static void onGraphicsError(String error) { - instance.logger.error("An error occurred: " + error); + instance.logger.error("OpenGL returned an error: " + error); + } + + /** + * Called when a Graphics API error occurs. This method just logs the error and returns. + * + * @param error Graphics API error + * @since 1-alpha0 + */ + @EventListener(event = GraphicsApiErrorEvent.class, priority = EventPriority.EXTREMELY_IMPORTANT) + public static void onGraphicsApiError(String error) { + instance.logger.error("Graphics API returned an error: " + error); } /** diff --git a/graphics/src/main/java/de/staropensource/sosengine/graphics/GraphicsSubsystem.java b/graphics/src/main/java/de/staropensource/sosengine/graphics/GraphicsSubsystem.java index 6c2126c6..a32cdf97 100644 --- a/graphics/src/main/java/de/staropensource/sosengine/graphics/GraphicsSubsystem.java +++ b/graphics/src/main/java/de/staropensource/sosengine/graphics/GraphicsSubsystem.java @@ -33,7 +33,6 @@ import de.staropensource.sosengine.graphics.events.GraphicsApiShutdownEvent; import de.staropensource.sosengine.graphics.events.GraphicsErrorEvent; import lombok.Getter; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; import java.util.*; diff --git a/graphics/src/main/java/de/staropensource/sosengine/graphics/events/GraphicsApiErrorEvent.java b/graphics/src/main/java/de/staropensource/sosengine/graphics/events/GraphicsApiErrorEvent.java new file mode 100644 index 00000000..ea3f75b0 --- /dev/null +++ b/graphics/src/main/java/de/staropensource/sosengine/graphics/events/GraphicsApiErrorEvent.java @@ -0,0 +1,49 @@ +package de.staropensource.sosengine.graphics.events; + +import de.staropensource.sosengine.base.classes.Event; +import de.staropensource.sosengine.base.events.LogEvent; +import de.staropensource.sosengine.base.types.LogIssuer; +import de.staropensource.sosengine.base.types.LogLevel; +import de.staropensource.sosengine.base.classes.helpers.EventHelper; +import org.jetbrains.annotations.NotNull; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +/** + * Called when a graphics error occurs. + * + * @since 1-alpha0 + */ +@SuppressWarnings({ "unused" }) +public class GraphicsApiErrorEvent implements Event { + /** + * Constructor. + */ + public GraphicsApiErrorEvent() {} + + /** + * {@inheritDoc} + * @deprecated use the {@code callEvent} method with arguments + * @see LogEvent#callEvent(LogLevel, LogIssuer, String) + */ + @Deprecated + @Override + public void callEvent() {} + + /** + * Calls the event and notifies all annotated methods. + * + * @param error graphics error + * @since 1-alpha0 + */ + public void callEvent(@NotNull String error) { + EventHelper.logCall(getClass()); + + for (Method method : EventHelper.getAnnotatedMethods(getClass())) { + try { + method.invoke(null, error); + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NullPointerException | ExceptionInInitializerError ignored) {} + } + } +}