Update init logic of base, graphics and glfw

This commit is contained in:
JeremyStar™ 2024-07-23 20:24:57 +02:00
parent a07dc6db84
commit 5909a948b5
Signed by: JeremyStarTM
GPG key ID: E366BAEF67E4704D
4 changed files with 26 additions and 55 deletions

View file

@ -24,9 +24,7 @@ import de.staropensource.sosengine.base.classes.SubsystemMainClass;
import de.staropensource.sosengine.base.classes.helpers.EventHelper;
import de.staropensource.sosengine.base.data.info.EngineInformation;
import de.staropensource.sosengine.base.data.versioning.StarOpenSourceVersioningSystem;
import de.staropensource.sosengine.base.events.EngineCrashEvent;
import de.staropensource.sosengine.base.events.EngineShutdownEvent;
import de.staropensource.sosengine.base.events.LogEvent;
import de.staropensource.sosengine.base.events.*;
import de.staropensource.sosengine.base.exceptions.dependency.UnmetDependenciesException;
import de.staropensource.sosengine.base.internal.events.InternalEngineShutdownEvent;
import de.staropensource.sosengine.base.internal.types.DependencySubsystemVector;
@ -241,7 +239,9 @@ public final class Engine implements SubsystemMainClass {
// General events
EventHelper.precomputeEventListeners(EngineCrashEvent.class);
EventHelper.precomputeEventListeners(EngineShutdownEvent.class);
EventHelper.precomputeEventListeners(EngineSoftCrashEvent.class);
EventHelper.precomputeEventListeners(LogEvent.class);
EventHelper.precomputeEventListeners(ThrowableCatchEvent.class);
}
/**

View file

@ -28,12 +28,13 @@ import de.staropensource.sosengine.base.types.CodePart;
import de.staropensource.sosengine.base.types.DependencyVector;
import de.staropensource.sosengine.base.types.logging.LogIssuer;
import de.staropensource.sosengine.base.utility.Miscellaneous;
import de.staropensource.sosengine.graphics.glfw.events.GraphicsErrorEvent;
import de.staropensource.sosengine.graphics.events.GraphicsErrorEvent;
import de.staropensource.sosengine.graphics.glfw.exceptions.GlfwInitializationException;
import de.staropensource.sosengine.graphics.glfw.exceptions.NotOnMainThreadException;
import lombok.Getter;
import org.jetbrains.annotations.NotNull;
import org.lwjgl.glfw.GLFWErrorCallback;
import org.lwjgl.glfw.GLFWErrorCallbackI;
import java.util.Arrays;
@ -76,7 +77,13 @@ public class GlfwSubsystem implements SubsystemMainClass {
*
* @since v1-alpha2
*/
private final GLFWErrorCallback errorCallback = GLFWErrorCallback.create(new GraphicsErrorEvent());
private final GLFWErrorCallback errorCallback = GLFWErrorCallback.create(new GLFWErrorCallbackI() {
/** {@inheritDoc} */
@Override
public void invoke(int error, long description) {
new GraphicsErrorEvent().callEvent(description + " (" + error + ")");
}
});
/**
* Constructs this subsystem.
@ -87,9 +94,8 @@ public class GlfwSubsystem implements SubsystemMainClass {
// Check if subsystem has already initialized
if (instance == null)
instance = this;
else {
else
instance.logger.crash("The subsystem tried to initialize twice");
}
}
/** {@inheritDoc} */

View file

@ -1,42 +0,0 @@
/*
* STAROPENSOURCE ENGINE SOURCE FILE
* Copyright (c) 2024 The StarOpenSource Engine Contributors
* Licensed under the GNU Affero General Public License v3
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.staropensource.sosengine.graphics.glfw.events;
import org.lwjgl.glfw.GLFWErrorCallbackI;
/**
* Called when a graphics error occurs.
*
* @since v1-alpha0
*/
@SuppressWarnings({ "unused" })
public class GraphicsErrorEvent extends de.staropensource.sosengine.graphics.events.GraphicsErrorEvent implements GLFWErrorCallbackI {
/**
* Constructs this class.
*/
public GraphicsErrorEvent() {}
/** {@inheritDoc} */
@Override
public void invoke(int error, long description) {
callEvent(description + " (" + error + ")");
}
}

View file

@ -33,8 +33,10 @@ import de.staropensource.sosengine.base.types.logging.LogIssuer;
import de.staropensource.sosengine.base.utility.ListFormatter;
import de.staropensource.sosengine.base.utility.Miscellaneous;
import de.staropensource.sosengine.graphics.classes.ApiMainClass;
import de.staropensource.sosengine.graphics.events.GraphicsApiErrorEvent;
import de.staropensource.sosengine.graphics.events.GraphicsApiShutdownEvent;
import de.staropensource.sosengine.graphics.events.GraphicsErrorEvent;
import de.staropensource.sosengine.graphics.events.InputEvent;
import lombok.Getter;
import org.jetbrains.annotations.NotNull;
@ -136,6 +138,9 @@ public final class GraphicsSubsystem implements SubsystemMainClass {
// Precompute event listeners
precomputeEventListeners();
// Warn about subsystem and API instability
logger.warn("The graphics subsystem is experimental. Subsystem, API and Graphics API compatibility is not guaranteed.");
}
/**
@ -144,8 +149,10 @@ public final class GraphicsSubsystem implements SubsystemMainClass {
* @since v1-alpha0
*/
public static void precomputeEventListeners() {
EventHelper.precomputeEventListeners(GraphicsApiErrorEvent.class);
EventHelper.precomputeEventListeners(GraphicsApiShutdownEvent.class);
EventHelper.precomputeEventListeners(GraphicsErrorEvent.class);
EventHelper.precomputeEventListeners(InputEvent.class);
}
/** {@inheritDoc} */
@ -205,10 +212,10 @@ public final class GraphicsSubsystem implements SubsystemMainClass {
// Check if registered apis are compatible
for (String apiName : registeredApis.keySet())
if (registeredApis.get(apiName).isCompatible()) {
logger.diag(apiName + " is compatible");
logger.diag(apiName + " is compatible with the system");
compatibleApis.add(apiName);
} else
logger.diag(apiName + " is not compatible");
logger.diag(apiName + " is incompatible with the system");
logger.diag("Compatible are " + ListFormatter.formatList(compatibleApis));
@ -218,16 +225,16 @@ public final class GraphicsSubsystem implements SubsystemMainClass {
api = registeredApis.get(compatibleApis.getLast());
try {
logger.diag("Initializing Graphics API " + api.getApiName());
logger.diag("Initialized Graphics API " + api.getApiName() + " in " + Miscellaneous.measureExecutionTime(() -> api.initializeApi()) + "ms");
logger.diag("Initializing Graphics API");
logger.diag("Initialized Graphics API in " + Miscellaneous.measureExecutionTime(() -> api.initializeApi()) + "ms");
} catch (Throwable throwable) {
logger.crash("Graphics API " + api.getApiName() + " failed to initialize", throwable, true);
logger.crash("Graphics API failed to initialize", throwable, true);
throw throwable;
}
return true;
} else {
logger.error("Choosing a compatible Graphics API: No compatible Graphics API was found");
logger.error("No compatible Graphics API was found");
return false;
}
}