SimpleJavaEngine/src/main/java/speiger/src/coreengine/application/Application.java

207 lines
5.8 KiB
Java

package speiger.src.coreengine.application;
import java.io.File;
import java.nio.ByteBuffer;
import java.util.function.IntConsumer;
import java.util.function.ObjLongConsumer;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.glfw.GLFWErrorCallback;
import speiger.src.coreengine.assets.AssetManager;
import speiger.src.coreengine.assets.reloader.ResourceReloader;
import speiger.src.coreengine.rendering.gui.GuiManager;
import speiger.src.coreengine.rendering.gui.base.DebugOverlay;
import speiger.src.coreengine.rendering.gui.renderer.provider.FontManager;
import speiger.src.coreengine.rendering.input.Keyboard;
import speiger.src.coreengine.rendering.input.Mouse;
import speiger.src.coreengine.rendering.input.camera.Camera;
import speiger.src.coreengine.rendering.input.window.Window;
import speiger.src.coreengine.rendering.input.window.WindowProvider;
import speiger.src.coreengine.rendering.shader.ProjectionBuffer;
import speiger.src.coreengine.rendering.shader.ShaderTracker;
import speiger.src.coreengine.rendering.textures.base.NativeMemoryParser;
import speiger.src.coreengine.rendering.textures.base.TextureManager;
import speiger.src.coreengine.rendering.utils.Cursor;
import speiger.src.coreengine.utils.counters.timers.FPSTimer;
import speiger.src.coreengine.utils.eventbus.EventBus;
import speiger.src.coreengine.utils.helpers.FileUtils;
import speiger.src.coreengine.utils.profiler.GPUProfiler;
import speiger.src.coreengine.utils.profiler.IProfiler;
import speiger.src.coreengine.utils.profiler.Profiler;
public abstract class Application
{
protected FPSTimer timer;
protected ApplicationExecutor executor = new ApplicationExecutor(this);
protected IProfiler clientProfiler = new Profiler("Client Thread");
protected IProfiler gpuProfiler = new GPUProfiler("GPU", clientProfiler);
protected boolean showProfilingInfo = true;
protected WindowProvider provider = new WindowProvider();
protected Window mainWindow;
protected Camera camera;
protected EventBus eventBus = new EventBus();
protected ResourceReloader reloader = new ResourceReloader();
protected AssetManager assetManager;
protected FontManager fonts = new FontManager();
protected ProjectionBuffer projectionBuffer;
protected GuiManager uiManager;
public void run()
{
GLFWErrorCallback.createPrint(System.err).set();
if(!GLFW.glfwInit()) throw new IllegalStateException("OpenGL can't be loaded");
provider.init();
boolean initEarly = earlyUILoad();
try
{
mainWindow = createWindow(provider);
if(initEarly) mainWindow.finishWindow();
}
catch(Exception e)
{
System.err.println("Could not create a Window!");
e.printStackTrace();
if(mainWindow != null) mainWindow.destroy();
provider.destroy();
System.exit(0);
return;
}
Thread.currentThread().setName("Client Thread");
File file = FileUtils.getBase();
file = file.getName().endsWith(".jar") ? file : new File("bin/main");
internalInit(file);
init(file);
if(!initEarly) mainWindow.finishWindow();
executor.start(mainWindow);
mainWindow.destroy();
reloader.deleteResources();
destroy();
System.exit(0);
}
protected void internalInit(File file)
{
assetManager = reloader.addReloadableResource(new AssetManager(file), true);
assetManager.registerAssetParser(ByteBuffer.class, new NativeMemoryParser());
reloader.addReloadableResource(fonts);
fonts.setAssetManager(assetManager);
ShaderTracker.INSTANCE.init(assetManager);
TextureManager.INSTANCE.init(assetManager);
preinit();
reloader.addReloadableResource(ShaderTracker.INSTANCE);
reloader.addReloadableResource(TextureManager.INSTANCE);
camera = new Camera(mainWindow);
if(initUI()) uiManager = BaseUIManager.create(this);
Keyboard.INSTANCE.init(eventBus, mainWindow);
Mouse.INSTANCE.init(eventBus, mainWindow, camera);
reloader.addReloadableResource(Cursor.INSTANCE, true);
mainWindow.addListener(camera.getFrustrum(), true);
projectionBuffer = new ProjectionBuffer(camera, mainWindow);
}
public abstract String getMainWindowName();
public void addExtraTickRates(IntConsumer ticks) {};
public void addExtraTimers(ObjLongConsumer<String> profiler) {};
public boolean initUI() { return true; }
public boolean earlyUILoad() { return true; }
public DebugOverlay createCustomDebug() { return null; }
public abstract Window createWindow(WindowProvider provider) throws Exception;
public void preinit() {}
public abstract void init(File file);
public void preUpdate() {}
public abstract void update();
public abstract void render(float particalTicks);
public abstract void destroy();
protected final void updateInternal()
{
preUpdate();
if(uiManager != null)
{
clientProfiler.start("UI");
uiManager.onFixedUpdate();
clientProfiler.stop();
}
clientProfiler.start("Input");
camera.onInput();
Mouse.update();
clientProfiler.stop();
update();
}
protected final void renderInternal(float particalTicks)
{
render(particalTicks);
if(uiManager != null)
{
gpuProfiler.start("UI");
uiManager.render(particalTicks);
gpuProfiler.stop();
}
gpuProfiler.start("camera");
camera.update(particalTicks);
gpuProfiler.stop();
}
public AssetManager getAssetManager()
{
return assetManager;
}
public ResourceReloader getReloader()
{
return reloader;
}
public Window getMainWindow()
{
return mainWindow;
}
public Camera getCamera()
{
return camera;
}
public EventBus getEventBus()
{
return eventBus;
}
public ProjectionBuffer getProjectionBuffer()
{
return projectionBuffer;
}
public FPSTimer getTimer()
{
return timer;
}
public FontManager getFonts()
{
return fonts;
}
public GuiManager getUiManager()
{
return uiManager;
}
public IProfiler getClientProfiler()
{
return clientProfiler;
}
public IProfiler getGPUProfiler()
{
return gpuProfiler;
}
public long getFrame()
{
return executor.frame;
}
}