Skip to content

Commit

Permalink
Fixed game not closing on window exit, added it so each window has th…
Browse files Browse the repository at this point in the history
…eir own unique pipeline instance for individual render tasks
  • Loading branch information
Hilligans committed Dec 29, 2023
1 parent 5a381b4 commit 8e867f0
Show file tree
Hide file tree
Showing 13 changed files with 92 additions and 24 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<lwjgl.version>3.3.1</lwjgl.version>
<lwjgl.version>3.3.3</lwjgl.version>
<lwjgl.natives>natives-linux</lwjgl.natives>

<maven.compiler.source>21</maven.compiler.source>
Expand Down
12 changes: 9 additions & 3 deletions src/main/java/dev/hilligans/ourcraft/ClientMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import dev.hilligans.ourcraft.client.Client;
import dev.hilligans.ourcraft.client.rendering.graphics.api.IGraphicsEngine;
import dev.hilligans.ourcraft.client.rendering.newrenderer.TextureAtlas;
import dev.hilligans.ourcraft.data.other.BoundingBox;
import dev.hilligans.ourcraft.util.ArgumentContainer;
import dev.hilligans.ourcraft.util.Side;
Expand Down Expand Up @@ -44,14 +45,19 @@ public static void main(String[] args) throws IOException {
}

Client client = new Client(gameInstance, argumentContainer);
Client client1 = new Client(gameInstance, argumentContainer);
// Client client1 = new Client(gameInstance, argumentContainer);
String graphicsEngine = argumentContainer.getString("--graphicsEngine", null);
if(graphicsEngine != null) {
System.out.println(graphicsEngine);
client.setGraphicsEngine(gameInstance.GRAPHICS_ENGINES.get(graphicsEngine));
client1.setGraphicsEngine(gameInstance.GRAPHICS_ENGINES.get(graphicsEngine));
// client1.setGraphicsEngine(gameInstance.GRAPHICS_ENGINES.get(graphicsEngine));
}
client1.setupClient();
//client1.setupClient();
client.startClient();
Ourcraft.EXECUTOR.shutdownNow();
TextureAtlas.EXECUTOR.shutdownNow();
if(argumentContainer.getBoolean("--integratedServer", false)) {
ServerMain.getServer().stop();
}
}
}
3 changes: 0 additions & 3 deletions src/main/java/dev/hilligans/ourcraft/GameInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,6 @@ public void loadContent() {
}

public void build(IGraphicsEngine<?,?,?> graphicsEngine, GraphicsContext graphicsContext) {
for(RenderPipeline renderPipeline : RENDER_PIPELINES.ELEMENTS) {
renderPipeline.buildTargets(graphicsEngine);
}
for(Registry<?> registry : REGISTRIES.ELEMENTS) {
for(Object o : registry.ELEMENTS) {
if(o instanceof IGraphicsElement graphicsElement) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package dev.hilligans.ourcraft.client.rendering.graphics;

import dev.hilligans.ourcraft.GameInstance;
import dev.hilligans.ourcraft.client.rendering.graphics.api.GraphicsContext;
import dev.hilligans.ourcraft.client.rendering.graphics.api.IGraphicsElement;
import dev.hilligans.ourcraft.client.rendering.graphics.api.IGraphicsEngine;

import java.util.ArrayList;

public class PipelineInstance implements IGraphicsElement {


public RenderPipeline renderPipeline;
public ArrayList<RenderTask> tasks;

public PipelineInstance(RenderPipeline renderPipeline, ArrayList<RenderTask> renderTasks) {
this.renderPipeline = renderPipeline;
this.tasks = renderTasks;
}

@Override
public void load(GameInstance gameInstance, IGraphicsEngine<?, ?, ?> graphicsEngine, GraphicsContext graphicsContext) {
for(RenderTask renderTask : tasks) {
renderTask.load(gameInstance, graphicsEngine, graphicsContext);
}
}

@Override
public void cleanup(GameInstance gameInstance, IGraphicsEngine<?, ?, ?> graphicsEngine, GraphicsContext graphicsContext) {
for(RenderTask renderTask : tasks) {
renderTask.cleanup(gameInstance, graphicsEngine, graphicsContext);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public class RenderPipeline implements IRegistryElement, IGraphicsElement {
// public RenderWindow window;

public ArrayList<RenderTarget> renderTargets = new ArrayList<>();
public ArrayList<RenderTask> renderTasks = new ArrayList<>();

public RenderPipeline(String name) {
this.name = name;
Expand Down Expand Up @@ -60,14 +59,16 @@ public void build(RenderWindow window) {
//this.window = window;
}

public void buildTargets(IGraphicsEngine<?, ?, ?> graphicsEngine) {
public PipelineInstance buildTargets(IGraphicsEngine<?, ?, ?> graphicsEngine) {
ArrayList<RenderTask> tasks = new ArrayList<>();
for(RenderTarget renderTarget : renderTargets) {
for (RenderTaskSource renderTaskSource : modContent.getGameInstance().RENDER_TASK.ELEMENTS) {
if(renderTaskSource.renderTargetName.equals(renderTarget.getIdentifierName())) {
renderTasks.add(renderTaskSource.getTask(graphicsEngine.getIdentifierName()));
tasks.add(renderTaskSource.getTask(graphicsEngine.getIdentifierName()));
}
}
}
return new PipelineInstance(this, tasks);
}

@Override
Expand Down Expand Up @@ -105,21 +106,14 @@ public String toString() {
"name='" + name + '\'' +
// ", window=" + window +
", renderTargets=" + renderTargets +
", renderTasks=" + renderTasks +
'}';
}

@Override
public void load(GameInstance gameInstance, IGraphicsEngine<?, ?, ?> graphicsEngine, GraphicsContext graphicsContext) {
for(RenderTask renderTask : renderTasks) {
renderTask.load(gameInstance, graphicsEngine, graphicsContext);
}
}

@Override
public void cleanup(GameInstance gameInstance, IGraphicsEngine<?, ?, ?> graphicsEngine, GraphicsContext graphicsContext) {
for(RenderTask renderTask : renderTasks) {
renderTask.cleanup(gameInstance, graphicsEngine, graphicsContext);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public abstract class RenderWindow {

public FrameTracker frameTracker = new FrameTracker();
public RenderPipeline renderPipeline;
public PipelineInstance pipelineInstance;

public ICamera camera;
public IGraphicsEngine<?,?,?> graphicsEngine;
Expand Down Expand Up @@ -54,7 +55,7 @@ public void renderPipeline(Client client, MatrixStack worldStack, MatrixStack sc

public void render(GraphicsContext graphicsContext, Client client, MatrixStack worldStack, MatrixStack screenStack) {
ISection section = graphicsContext.getSection();
for(RenderTask renderTask : renderPipeline.renderTasks) {
for(RenderTask renderTask : pipelineInstance.tasks) {
try(var $ = section.startSection(renderTask.getIdentifierName())) {
PipelineState pipelineState = renderTask.getPipelineState();
graphicsContext.setPipelineState(false);
Expand All @@ -72,7 +73,8 @@ public void setRenderPipeline(RenderPipeline renderPipeline) {
throw new NullPointerException();
}
this.renderPipeline = renderPipeline;
this.renderPipeline.build(this);
this.pipelineInstance = this.renderPipeline.buildTargets(graphicsEngine);
this.pipelineInstance.load(graphicsEngine.getGameInstance(), graphicsEngine, graphicsEngine.getGraphicsContext());
}

public void setRenderPipeline(String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void drawLayout(RenderWindow renderWindow, GraphicsContext graphicsContex
.vertex_layout(VERTEX_LAYOUT)
.vertex_size(20)
.vertex_alignment(4)
.null_texture(this.engine.null_texture)
.tex_null(this.engine.null_texture)
.circle_segment_count(22)
.curve_segment_count(22)
.arc_segment_count(22)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,13 @@ public void invoke(int source, int type, int id, int severity, int length, long
@Override
public void close() {
gameInstance.cleanupGraphics(this, getGraphicsContext());
for(RenderWindow renderWindow : windows) {
if(renderWindow.pipelineInstance != null) {
renderWindow.pipelineInstance.cleanup(getGameInstance(), this, getGraphicsContext());
}
}
stringRenderer.close(engineImpl, getGraphicsContext());

engineImpl.close();
glfwTerminate();
}
Expand Down
16 changes: 14 additions & 2 deletions src/main/java/dev/hilligans/ourcraft/network/ServerNetwork.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ public class ServerNetwork extends Network {
public GameInstance gameInstance;
public IServer server;

EventLoopGroup bossGroup;
EventLoopGroup workerGroup;

public ServerNetwork(Protocol protocol, IServer server) {
super(protocol);
this.server = server;
Expand All @@ -30,8 +33,8 @@ public void startServer(String port) throws Exception {

SelfSignedCertificate ssc = new SelfSignedCertificate();
sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();
bossGroup = new NioEventLoopGroup(1);
workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
Expand All @@ -51,4 +54,13 @@ public ServerNetwork debug(boolean debug) {
super.debug(debug);
return this;
}

public void close() {
if(bossGroup != null) {
bossGroup.shutdownGracefully();
}
if(workerGroup != null) {
workerGroup.shutdownGracefully();
}
}
}
7 changes: 5 additions & 2 deletions src/main/java/dev/hilligans/ourcraft/save/FileLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ public class FileLoader {
public static void openFile(String acceptedFiles, String defaultPath, HandleFile handleFile) {
PointerBuffer outPath = memAllocPointer(1);
try {
/*
checkResult(
NFD_OpenDialog(acceptedFiles, defaultPath, outPath),
outPath, handleFile
);
*/
//);
} finally {
memFree(outPath);
}
Expand All @@ -24,7 +27,7 @@ private static void checkResult(int result, PointerBuffer path, HandleFile handl
switch (result) {
case NFD_OKAY:
handleFile.success(path);
nNFD_Free(path.get(0));
//nNFD_Free(path.get(0));
break;
case NFD_CANCEL:
handleFile.cancel();
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/dev/hilligans/ourcraft/server/IServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public interface IServer {

GameInstance getGameInstance();

void stop();

class Server implements Runnable {
public IServer server;
//public IGameProcessor gameProcessor = new TE2GameProcessor(new TickEngineSettings());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,9 @@ public void sendPacket(PacketBase packetBase, PlayerEntity playerEntity) {
public GameInstance getGameInstance() {
return null;
}

@Override
public void stop() {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ public GameInstance getGameInstance() {
return gameInstance;
}

@Override
public void stop() {
if(serverNetwork != null) {
serverNetwork.close();
}
}

static class PlayerHandler implements Runnable {

MultiPlayerServer server;
Expand Down

0 comments on commit 8e867f0

Please sign in to comment.