Skip to content

Commit

Permalink
Fixed string building race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
Hilligans committed Jan 1, 2024
1 parent b134b62 commit 936738b
Show file tree
Hide file tree
Showing 47 changed files with 362 additions and 252 deletions.
2 changes: 1 addition & 1 deletion src/main/java/dev/hilligans/ourcraft/ClientMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ public static void main(String[] args) throws IOException {
}
//client1.setupClient();
client.startClient();
gameInstance.THREAD_PROVIDER.EXECUTOR.shutdownNow();
Ourcraft.EXECUTOR.shutdownNow();
TextureAtlas.EXECUTOR.shutdownNow();
if(argumentContainer.getBoolean("--integratedServer", false)) {
ServerMain.getServer().stop();
}
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/dev/hilligans/ourcraft/GameInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public class GameInstance {
//public final
public Side side;

public int gameInstanceUniversalID;
public final int gameInstanceUniversalID;

public final ToolLevelList MATERIAL_LIST = new ToolLevelList();

Expand Down Expand Up @@ -387,11 +387,15 @@ public void registerDefaultContent() {
Entity.register();
}

public int getUniqueID() {
return gameInstanceUniversalID;
}

public void handleArgs(String[] args) {
ARGUMENTS.handle(args);
}

private static int staticGameInstanceUniversalID = 0;
private static volatile int staticGameInstanceUniversalID = 0;

public static synchronized int getNewGameInstanceUniversalID() {
return staticGameInstanceUniversalID++;
Expand Down
12 changes: 0 additions & 12 deletions src/main/java/dev/hilligans/ourcraft/client/Camera.java

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public ContainerScreen() {
@Override
public void render(RenderWindow window, MatrixStack matrixStack, GraphicsContext graphicsContext) {
super.render(window, matrixStack, graphicsContext);
container.render(window, matrixStack);
container.render(window,graphicsContext, matrixStack);

}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package dev.hilligans.ourcraft.client.rendering;

import dev.hilligans.ourcraft.GameInstance;
import dev.hilligans.ourcraft.client.MatrixStack;
import dev.hilligans.ourcraft.client.rendering.graphics.api.GraphicsContext;
import dev.hilligans.ourcraft.client.rendering.graphics.api.IDefaultEngineImpl;
import dev.hilligans.ourcraft.client.rendering.graphics.RenderWindow;
import dev.hilligans.ourcraft.client.rendering.newrenderer.PrimitiveBuilder;
import org.jetbrains.annotations.NotNull;

public class ResizingTexture extends Texture {

Expand Down Expand Up @@ -37,24 +40,24 @@ public ResizingTexture endSegment(int x, int y, int width, int height) {
return this;
}

public PrimitiveBuilder get(int width, int height,int x, int y) {
public PrimitiveBuilder get(GameInstance gameInstance, int width, int height, int x, int y) {
PrimitiveBuilder primitiveBuilder = new PrimitiveBuilder(shaderSource.vertexFormat);
startSegment.put(primitiveBuilder,x,y,height * startSegment.getRatio(),height,this);
startSegment.put(primitiveBuilder,x,y,height * startSegment.getRatio(),height,this.getWidth(gameInstance), this.getHeight(gameInstance));
float middleWidth = width - height * startSegment.getRatio() - height * endSegment.getRatio();
middleSegment.put(primitiveBuilder,height * startSegment.getRatio() + x,y,middleWidth,height,this);
endSegment.put(primitiveBuilder,height * startSegment.getRatio() + middleWidth + x,y,height * endSegment.getRatio(),height,this);
middleSegment.put(primitiveBuilder,height * startSegment.getRatio() + x,y,middleWidth,height,this.getWidth(gameInstance), this.getHeight(gameInstance));
endSegment.put(primitiveBuilder,height * startSegment.getRatio() + middleWidth + x,y,height * endSegment.getRatio(),height,this.getWidth(gameInstance), this.getHeight(gameInstance));
return primitiveBuilder;
}


@Override
public void drawTexture(RenderWindow window, MatrixStack matrixStack, int x, int y, int width, int height) {
PrimitiveBuilder primitiveBuilder = get(width,height,x,y);
public void drawTexture(RenderWindow window, @NotNull GraphicsContext graphicsContext, MatrixStack matrixStack, int x, int y, int width, int height) {
PrimitiveBuilder primitiveBuilder = get(window.getGameInstance(), width,height,x,y);
IDefaultEngineImpl<?,?> defaultEngineImpl = window.getEngineImpl();
defaultEngineImpl.bindTexture(null, textureId);
defaultEngineImpl.bindPipeline(null, shaderSource.program);
defaultEngineImpl.uploadMatrix(null, matrixStack, shaderSource);
defaultEngineImpl.drawAndDestroyMesh(null,matrixStack,primitiveBuilder.toVertexMesh());
defaultEngineImpl.bindTexture(graphicsContext, getTextureId(window.getGameInstance()));
defaultEngineImpl.bindPipeline(graphicsContext, shaderSource.program);
defaultEngineImpl.uploadMatrix(graphicsContext, matrixStack, shaderSource);
defaultEngineImpl.drawAndDestroyMesh(graphicsContext,matrixStack,primitiveBuilder.toVertexMesh());
}

static class Segment {
Expand All @@ -71,8 +74,8 @@ public Segment(int x, int y, int width, int height) {
this.height = height;
}

public void put(PrimitiveBuilder builder, float x, float y, float width, float height, ResizingTexture resizingTexture) {
builder.buildQuad(x,y,0,1f/resizingTexture.width * this.x,1f/resizingTexture.height * this.y,x + width, y + height, 1f/ resizingTexture.width * (this.x + this.width),1f/resizingTexture.height * (this.y + this.height));
public void put(PrimitiveBuilder builder, float x, float y, float width, float height, int textureWidth, int textureHeight) {
builder.buildQuad(x,y,0,1f/textureWidth * this.x,1f/textureHeight * this.y,x + width, y + height, 1f/ textureWidth * (this.x + this.width),1f/textureHeight * (this.y + this.height));
}

public float getRatio() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void drawScreen(RenderWindow window, MatrixStack matrixStack, GraphicsCon
public void render(RenderWindow window, MatrixStack matrixStack, GraphicsContext graphicsContext) {
drawScreen(window, matrixStack, graphicsContext);
for(Widget widget : widgets) {
widget.render(window, matrixStack,0,0);
widget.render(window, graphicsContext, matrixStack, 0, 0);
}
}

Expand Down
91 changes: 50 additions & 41 deletions src/main/java/dev/hilligans/ourcraft/client/rendering/Texture.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,32 @@
import dev.hilligans.ourcraft.GameInstance;
import dev.hilligans.ourcraft.mod.handler.content.ModContent;
import dev.hilligans.ourcraft.resource.ResourceLocation;
import dev.hilligans.ourcraft.util.UniqueResource;
import dev.hilligans.ourcraft.util.registry.IRegistryElement;
import dev.hilligans.ourcraft.util.Settings;
import org.jetbrains.annotations.NotNull;

public class Texture implements IRegistryElement, IGraphicsElement {

public String path;
public ModContent source;

public int width;
public int height;

public long textureId = -1;

public Image texture;
public ShaderSource shaderSource;

public int program;
UniqueResource<TextureData> data = new UniqueResource<>();

public Texture(String path) {
this.path = path;
}


public void drawTexture(RenderWindow window, MatrixStack matrixStack, int x, int y, int width, int height, int startX, int startY, int endX, int endY) {
public void drawTexture(@NotNull RenderWindow window, @NotNull GraphicsContext graphicsContext, MatrixStack matrixStack, int x, int y, int width, int height, int startX, int startY, int endX, int endY) {
IDefaultEngineImpl<?,?> defaultEngineImpl = window.getEngineImpl();

float minX = (float)startX / this.width;
float minY = (float)startY / this.height;
float maxX = (float)endX / this.width;
float maxY = (float)endY / this.height;
GameInstance gameInstance = window.getGameInstance();
float minX = (float)startX / this.getWidth(gameInstance);
float minY = (float)startY / this.getHeight(gameInstance);
float maxX = (float)endX / this.getWidth(gameInstance);
float maxY = (float)endY / this.getHeight(gameInstance);
float[] vertices = new float[] {x,y,0,minX,minY,x,y + height,0,minX,maxY,x + width,y,0,maxX,minY,x + width,y + height,0,maxX,maxY};
int[] indices = new int[] {0,1,2,2,1,3};

Expand All @@ -49,63 +45,75 @@ public void drawTexture(RenderWindow window, MatrixStack matrixStack, int x, int
mesh.addData(indices, vertices);

//GL11.glDisable(GL11.GL_DEPTH_TEST);
defaultEngineImpl.bindPipeline(null, shaderSource.program);
defaultEngineImpl.bindTexture( null, textureId);
defaultEngineImpl.drawAndDestroyMesh(null,matrixStack,mesh);
defaultEngineImpl.bindPipeline(graphicsContext, shaderSource.program);
defaultEngineImpl.bindTexture( graphicsContext, getTextureId(gameInstance));
defaultEngineImpl.drawAndDestroyMesh(graphicsContext,matrixStack,mesh);
}

public void drawTexture(RenderWindow window, MatrixStack matrixStack, int x, int y, int width, int height) {
drawTexture(window, matrixStack,x,y,width,height,0,0,this.width,this.height);
public void drawTexture(@NotNull RenderWindow window, @NotNull GraphicsContext graphicsContext, MatrixStack matrixStack, int x, int y, int width, int height) {
drawTexture(window, graphicsContext, matrixStack,x,y,width,height,0,0,this.getWidth(window.getGameInstance()), this.getHeight(window.getGameInstance()));
}

public void drawTexture(RenderWindow window, MatrixStack matrixStack, int x, int y, int startX, int startY, int endX, int endY) {
drawTexture(window, matrixStack,x,y,(int)((endX - startX) * Settings.guiSize),(int) ((endY - startY) * Settings.guiSize),startX,startY,endX,endY);
public void drawTexture(@NotNull RenderWindow window, @NotNull GraphicsContext graphicsContext, MatrixStack matrixStack, int x, int y, int startX, int startY, int endX, int endY) {
drawTexture(window, graphicsContext, matrixStack,x,y,(int)((endX - startX) * Settings.guiSize),(int) ((endY - startY) * Settings.guiSize),startX,startY,endX,endY);
}

public void drawCenteredTexture(RenderWindow window, MatrixStack matrixStack, float size) {
drawTexture(window, matrixStack, (int)(window.getWindowWidth() / 2 - width / 2 * size), (int)(window.getWindowHeight() / 2 - height / 2 * size),(int)(width * size), (int)(height * size));
public void drawCenteredTexture(@NotNull RenderWindow window, @NotNull GraphicsContext graphicsContext, MatrixStack matrixStack, float size) {
drawTexture(window, graphicsContext, matrixStack, (int)(window.getWindowWidth() / 2 - getWidth(window.getGameInstance()) / 2 * size), (int)(window.getWindowHeight() / 2 - getHeight(window.getGameInstance()) / 2 * size),(int)(getWidth(window.getGameInstance()) * size), (int)(getHeight(window.getGameInstance()) * size));
}

public void drawCenteredTexture(RenderWindow window, MatrixStack matrixStack, int startX, int startY, int endX, int endY, float size) {
public void drawCenteredTexture(RenderWindow window, @NotNull GraphicsContext graphicsContext, MatrixStack matrixStack, int startX, int startY, int endX, int endY, float size) {
int width = (int) ((endX - startX) * size);
int height = (int) ((endY - startY) * size);
drawTexture(window, matrixStack, (int) (window.getWindowWidth() / 2 - width / 2), (int) (window.getWindowHeight() / 2 - height / 2), width, height, startX,startY,endX,endY);
drawTexture(window, graphicsContext, matrixStack, (int) (window.getWindowWidth() / 2 - width / 2), (int) (window.getWindowHeight() / 2 - height / 2), width, height, startX,startY,endX,endY);
}

public int drawCenteredXTexture(RenderWindow window, MatrixStack matrixStack, int y, float size) {
int x = (int)(window.getWindowWidth() / 2 - width / 2 * size);
drawTexture(window, matrixStack, x, y,(int)(width * size), (int)(height * size));
public int drawCenteredXTexture(@NotNull RenderWindow window, @NotNull GraphicsContext graphicsContext, MatrixStack matrixStack, int y, float size) {
int x = (int)(window.getWindowWidth() / 2 - getWidth(window.getGameInstance()) / 2 * size);
drawTexture(window, graphicsContext, matrixStack, x, y,(int)(getWidth(window.getGameInstance()) * size), (int)(getHeight(window.getGameInstance()) * size));
return x;
}

public int drawCenteredXTexture(RenderWindow window, MatrixStack matrixStack, int y, float width, float height) {
public int drawCenteredXTexture(@NotNull RenderWindow window, @NotNull GraphicsContext graphicsContext, MatrixStack matrixStack, int y, float width, float height) {
int x = (int) (window.getWindowWidth() / 2 - width / 2);
drawTexture(window, matrixStack, x, y, (int)width, (int)height);
drawTexture(window, graphicsContext, matrixStack, x, y, (int)width, (int)height);
return x;
}

public int drawCenteredXTexture(RenderWindow window, MatrixStack matrixStack, int y, int startX, int startY, int endX, int endY, float size) {
public int drawCenteredXTexture(@NotNull RenderWindow window, @NotNull GraphicsContext graphicsContext, MatrixStack matrixStack, int y, int startX, int startY, int endX, int endY, float size) {
int width = (int) ((endX - startX) * size);
int height = (int) ((endY - startY) * size);
int x = (int) (window.getWindowWidth() / 2 - width / 2);
drawTexture(window, matrixStack, x, y, width, height, startX,startY,endX,endY);
drawTexture(window, graphicsContext, matrixStack, x, y, width, height, startX,startY,endX,endY);
return x;
}

public int drawCenteredYTexture(RenderWindow window, MatrixStack matrixStack, Texture texture, int x, float size) {
int y = (int)(window.getWindowHeight() / 2 - texture.height / 2 * size);
drawTexture(window, matrixStack, x, y,(int)(texture.width * size), (int)(texture.height * size));
public int drawCenteredYTexture(@NotNull RenderWindow window, @NotNull GraphicsContext graphicsContext, MatrixStack matrixStack, Texture texture, int x, float size) {
int y = (int)(window.getWindowHeight() / 2 - texture.getWidth(window.getGameInstance()) / 2 * size);
drawTexture(window, graphicsContext, matrixStack, x, y,(int)(texture.getWidth(window.getGameInstance()) * size), (int)(texture.getHeight(window.getGameInstance()) * size));
return y;
}

public int drawCenteredYTexture(RenderWindow window, MatrixStack matrixStack, int x, int startX, int startY, int endX, int endY, float size) {
public int drawCenteredYTexture(RenderWindow window, @NotNull GraphicsContext graphicsContext, MatrixStack matrixStack, int x, int startX, int startY, int endX, int endY, float size) {
int width = (int) ((endX - startX) * size);
int height = (int) ((endY - startY) * size);
int y = (int) (window.getWindowHeight() / 2 - height / 2);
drawTexture(window, matrixStack, x, y, width, height, startX,startY,endX,endY);
drawTexture(window, graphicsContext, matrixStack, x, y, width, height, startX,startY,endX,endY);
return y;
}

public int getWidth(GameInstance gameInstance) {
return data.get(gameInstance).image().getWidth();
}

public int getHeight(GameInstance gameInstance) {
return data.get(gameInstance).image().getHeight();
}

public long getTextureId(GameInstance gameInstance) {
return data.get(gameInstance).textureID();
}

@Override
public String getResourceName() {
return path;
Expand All @@ -132,19 +140,20 @@ public void load(GameInstance gameInstance) {

@Override
public void load(GameInstance gameInstance, IGraphicsEngine<?, ?, ?> graphicsEngine, GraphicsContext graphicsContext) {
texture = (Image) gameInstance.RESOURCE_LOADER.getResource(new ResourceLocation(path,source.getModID()));
Image texture = (Image) gameInstance.RESOURCE_LOADER.getResource(new ResourceLocation(path,source.getModID()));
if(texture == null) {
System.err.println(path);
return;
}
width = texture.getWidth();
height = texture.getHeight();

textureId = graphicsEngine.getDefaultImpl().createTexture(graphicsContext, texture);
long textureId = graphicsEngine.getDefaultImpl().createTexture(graphicsContext, texture);
data.add(gameInstance, new TextureData(texture, textureId, shaderSource));
}

@Override
public void cleanup(GameInstance gameInstance, IGraphicsEngine<?, ?, ?> graphicsEngine, GraphicsContext graphicsContext) {
graphicsEngine.getDefaultImpl().destroyTexture(graphicsContext, textureId);
graphicsEngine.getDefaultImpl().destroyTexture(graphicsContext, getTextureId(gameInstance));
}

record TextureData(Image image, long textureID, ShaderSource shaderSource) {}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.hilligans.ourcraft.client.rendering.graphics;

import dev.hilligans.ourcraft.GameInstance;
import dev.hilligans.ourcraft.client.Client;
import dev.hilligans.ourcraft.client.input.InputHandler;
import dev.hilligans.ourcraft.client.input.InputHandlerProvider;
Expand Down Expand Up @@ -190,6 +191,10 @@ public Vector2f getScissor() {
return new Vector2f(0,0);
}

public GameInstance getGameInstance() {
return graphicsEngine.getGameInstance();
}

public void setupInputs() {
inputHandler = new InputHandler(graphicsEngine.getGameInstance(), this);
for(InputHandlerProvider provider : graphicsEngine.getGameInstance().INPUT_HANDLER_PROVIDERS.ELEMENTS) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@ default void setRotation(Vector2f rotation) {

void addRotation(float pitch, float yaw);

float getSensitivity();
int getSensitivity();

void setSensitivity(int val);

int getFOV();

void setFOV(int fov);

Vector2f getRotation();

Expand Down
Loading

0 comments on commit 936738b

Please sign in to comment.