Skip to content

Commit

Permalink
Started work on Layout engines and a Nuklear layout engine
Browse files Browse the repository at this point in the history
  • Loading branch information
Hilligans committed Dec 21, 2023
1 parent 9965111 commit 7c067f4
Show file tree
Hide file tree
Showing 41 changed files with 647 additions and 64 deletions.
9 changes: 9 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@
<groupId>org.lwjgl</groupId>
<artifactId>lwjgl-vma</artifactId>
</dependency>
<dependency>
<groupId>org.lwjgl</groupId>
<artifactId>lwjgl-nuklear</artifactId>
</dependency>
<dependency>
<groupId>org.lwjgl</groupId>
<artifactId>lwjgl</artifactId>
Expand Down Expand Up @@ -107,6 +111,11 @@
<artifactId>lwjgl-vma</artifactId>
<classifier>${lwjgl.natives}</classifier>
</dependency>
<dependency>
<groupId>org.lwjgl</groupId>
<artifactId>lwjgl-nuklear</artifactId>
<classifier>${lwjgl.natives}</classifier>
</dependency>

<dependency>
<groupId>org.joml</groupId>
Expand Down
16 changes: 12 additions & 4 deletions src/main/java/dev/hilligans/ourcraft/GameInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
import dev.hilligans.ourcraft.client.input.InputHandlerProvider;
import dev.hilligans.ourcraft.client.rendering.graphics.*;
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 dev.hilligans.ourcraft.client.rendering.ScreenBuilder;
import dev.hilligans.ourcraft.client.rendering.Texture;
import dev.hilligans.ourcraft.client.rendering.graphics.api.ILayoutEngine;
import dev.hilligans.ourcraft.command.CommandHandler;
import dev.hilligans.ourcraft.container.Container;
import dev.hilligans.ourcraft.data.descriptors.Tag;
Expand Down Expand Up @@ -99,6 +101,7 @@ public GameInstance() {
REGISTRIES.put(INPUT_HANDLER_PROVIDERS);
REGISTRIES.put(TEXTURES);
REGISTRIES.put(SHADERS);
REGISTRIES.put(LAYOUT_ENGINES);
}

public void loadContent() {
Expand All @@ -117,8 +120,8 @@ public void loadContent() {
public void build(IGraphicsEngine<?,?,?> graphicsEngine, GraphicsContext graphicsContext) {
for(Registry<?> registry : REGISTRIES.ELEMENTS) {
for(Object o : registry.ELEMENTS) {
if(o instanceof IRegistryElement registryElement) {
registryElement.loadGraphics(graphicsEngine, graphicsContext);
if(o instanceof IGraphicsElement graphicsElement) {
graphicsElement.load(this, graphicsEngine, graphicsContext);
}
}
}
Expand All @@ -130,8 +133,8 @@ public void build(IGraphicsEngine<?,?,?> graphicsEngine, GraphicsContext graphic
public void cleanupGraphics(IGraphicsEngine<?,?,?> graphicsEngine, GraphicsContext graphicsContext) {
for(Registry<?> registry : REGISTRIES.ELEMENTS) {
for(Object o : registry.ELEMENTS) {
if(o instanceof IRegistryElement registryElement) {
registryElement.cleanupGraphics(graphicsEngine, graphicsContext);
if(o instanceof IGraphicsElement graphicsElement) {
graphicsElement.cleanup(this, graphicsEngine, graphicsContext);
}
}
}
Expand Down Expand Up @@ -165,6 +168,7 @@ public void cleanupGraphics(IGraphicsEngine<?,?,?> graphicsEngine, GraphicsConte
public final Registry<InputHandlerProvider> INPUT_HANDLER_PROVIDERS = new Registry<>(this, InputHandlerProvider.class, "input");
public final Registry<Texture> TEXTURES = new Registry<>(this, Texture.class, "texture");
public final Registry<ShaderSource> SHADERS = new Registry<>(this, ShaderSource.class, "shader");
public final Registry<ILayoutEngine<?>> LAYOUT_ENGINES = new Registry<>(this, ILayoutEngine.class, "layout_engine");
public ArrayList<IBlockState> BLOCK_STATES;

public void buildBlockStates() {
Expand Down Expand Up @@ -327,6 +331,10 @@ public void registerShader(ShaderSource... shaderSources) {
SHADERS.putAll(shaderSources);
}

public void registerLayoutEngine(ILayoutEngine<?>... layoutEngines) {
LAYOUT_ENGINES.putAll(layoutEngines);
}

public void register(String name, Object o) {
boolean put = false;
for(Registry<?> registry : REGISTRIES.ELEMENTS) {
Expand Down
24 changes: 19 additions & 5 deletions src/main/java/dev/hilligans/ourcraft/Ourcraft.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@
import dev.hilligans.ourcraft.client.input.handlers.MouseHandler;
import dev.hilligans.ourcraft.client.rendering.graphics.fixedfunctiongl.FixedFunctionGLEngine;
import dev.hilligans.ourcraft.client.rendering.graphics.implementations.WorldCamera;
import dev.hilligans.ourcraft.client.rendering.graphics.nuklear.NuklearLayoutEngine;
import dev.hilligans.ourcraft.client.rendering.graphics.opengl.OpenGLEngine;
import dev.hilligans.ourcraft.client.rendering.graphics.tasks.*;
import dev.hilligans.ourcraft.client.rendering.graphics.vulkan.VulkanEngine;
import dev.hilligans.ourcraft.client.rendering.ScreenBuilder;
import dev.hilligans.ourcraft.client.rendering.screens.EscapeScreen;
import dev.hilligans.ourcraft.client.rendering.screens.FrameTimeScreen;
import dev.hilligans.ourcraft.client.rendering.screens.JoinScreen;
import dev.hilligans.ourcraft.client.rendering.screens.TagEditorScreen;
import dev.hilligans.ourcraft.client.rendering.screens.*;
import dev.hilligans.ourcraft.client.rendering.Textures;
import dev.hilligans.ourcraft.item.data.ToolLevel;
import dev.hilligans.ourcraft.mod.handler.content.CoreExtensionView;
Expand Down Expand Up @@ -171,14 +169,17 @@ public static void registerCoreExtensions(CoreExtensionView view) {


view.registerVertexFormat(position_texture_color, position_color_texture, position_texture_globalColor, position_texture, position_texture_animatedWrap_shortenedColor, position_color);

view.registerVertexFormat(position2_texture_color);

// view.registerShader(new ShaderSource("world_shader","ourcraft:position_texture_color", "Shaders/WorldVertexShader.glsl","Shaders/WorldFragmentShader.glsl"));
view.registerShader(new ShaderSource("world_shader", "ourcraft:position_color_texture", "Shaders/WorldVertexShader.glsl", "Shaders/WorldFragmentShader.glsl").withUniform("transform", "4fv").withUniform("color", "4f"));
view.registerShader(new ShaderSource("position_color_shader", "ourcraft:position_color", "Shaders/WorldVertexColorShader.glsl", "Shaders/WorldFragmentShader.glsl").withUniform("transform", "4fv").withUniform("color", "4f"));
view.registerShader(new ShaderSource("position_texture", "ourcraft:position_texture", "Shaders/PositionTexture.vsh", "Shaders/PositionTexture.fsh").withUniform("transform", "4fv").withUniform("color", "4f"));
view.registerShader(new ShaderSource("nk_shader", "position2_texture_color", "Shaders/NkVertexShader.glsl", "Shaders/NkFragmentShader.glsl").withUniform("transform", "4fv"));

view.registerInputHandlerProviders(new ControllerHandlerProvider(), new KeyPressHandlerProvider(), new MouseHandlerProvider());

view.registerLayoutEngine(new NuklearLayoutEngine());
}
}

Expand Down Expand Up @@ -254,6 +255,14 @@ public void press(RenderWindow window, float strength) {
}
}.onlyWithPipelines("ourcraft:new_world_pipeline"));

modContent.registerKeybinds(new Input("ourcraft:key_press_handler::" + GLFW_KEY_SEMICOLON) {
@Override
public void press(RenderWindow renderWindow, float strength) {
super.press(renderWindow, strength);
renderWindow.client.openScreen(new TestScreen());
}
});

modContent.registerKeybinds(new RepeatingInput("ourcraft:key_press_handler::" + GLFW_KEY_W,
(window, strength) -> window.getCamera().moveForward(5f * strength)).onlyWithPipelines("ourcraft:new_world_pipeline"));

Expand Down Expand Up @@ -337,6 +346,11 @@ public static DoubleBuffer getMousePos(long window) {
.addPart("color", VertexFormat.FLOAT, 4)
.addPart("texture", VertexFormat.FLOAT, 2);

public static final VertexFormat position2_texture_color = new VertexFormat("ourcraft", "position2_texture_color", VertexFormat.TRIANGLES)
.addPart("position2", VertexFormat.FLOAT, 2)
.addPart("texture", VertexFormat.FLOAT, 2)
.addPart("color", VertexFormat.FLOAT, 4);

public static final VertexFormat position_texture_globalColor = new VertexFormat("oucraft", "position_texture_globalColor", VertexFormat.TRIANGLES)
.addPart("position", VertexFormat.FLOAT, 3)
.addPart("texture", VertexFormat.FLOAT, 2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import dev.hilligans.ourcraft.client.Client;
import dev.hilligans.ourcraft.client.MatrixStack;
import dev.hilligans.ourcraft.client.rendering.graphics.RenderWindow;
import dev.hilligans.ourcraft.client.rendering.graphics.api.GraphicsContext;
import dev.hilligans.ourcraft.container.Container;
import dev.hilligans.ourcraft.container.Slot;
import dev.hilligans.ourcraft.item.ItemStack;
Expand All @@ -29,8 +30,8 @@ public ContainerScreen() {
public RecipeHelper recipeHelper = new RecipeHelper(Ourcraft.GAME_INSTANCE);

@Override
public void render(RenderWindow window, MatrixStack matrixStack) {
super.render(window, matrixStack);
public void render(RenderWindow window, MatrixStack matrixStack, GraphicsContext graphicsContext) {
super.render(window, matrixStack, graphicsContext);
container.render(window, matrixStack);

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import dev.hilligans.ourcraft.client.Client;
import dev.hilligans.ourcraft.client.MatrixStack;
import dev.hilligans.ourcraft.client.rendering.graphics.RenderWindow;
import dev.hilligans.ourcraft.client.rendering.graphics.api.GraphicsContext;

public interface Screen {

Expand All @@ -14,7 +15,7 @@ default Client getClient() {
return getWindow().getClient();
}

default void render(RenderWindow window, MatrixStack matrixStack) {}
default void render(RenderWindow window, MatrixStack matrixStack, GraphicsContext graphicsContext) {}

default void close(boolean replaced) {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import dev.hilligans.ourcraft.client.MatrixStack;
import dev.hilligans.ourcraft.client.rendering.graphics.RenderWindow;
import dev.hilligans.ourcraft.client.rendering.graphics.ShaderSource;
import dev.hilligans.ourcraft.client.rendering.graphics.api.GraphicsContext;
import dev.hilligans.ourcraft.client.rendering.widgets.Widget;
import dev.hilligans.ourcraft.network.packet.client.CCloseScreen;

Expand All @@ -31,10 +32,10 @@ public ScreenBase(Client client) {

public RenderWindow window;

public void drawScreen(RenderWindow window, MatrixStack matrixStack) {}
public void drawScreen(RenderWindow window, MatrixStack matrixStack, GraphicsContext graphicsContext) {}

public void render(RenderWindow window, MatrixStack matrixStack) {
drawScreen(window, matrixStack);
public void render(RenderWindow window, MatrixStack matrixStack, GraphicsContext graphicsContext) {
drawScreen(window, matrixStack, graphicsContext);
for(Widget widget : widgets) {
widget.render(window, matrixStack,0,0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import dev.hilligans.ourcraft.client.MatrixStack;
import dev.hilligans.ourcraft.client.rendering.graphics.RenderWindow;
import dev.hilligans.ourcraft.GameInstance;
import dev.hilligans.ourcraft.client.rendering.graphics.api.GraphicsContext;
import dev.hilligans.ourcraft.mod.handler.content.ModContent;
import dev.hilligans.ourcraft.util.registry.IRegistryElement;
import org.json.JSONArray;
Expand Down Expand Up @@ -43,7 +44,7 @@ public Screen get(Client client) {
int height;

@Override
public void drawScreen(RenderWindow window, MatrixStack matrixStack) {
public void drawScreen(RenderWindow window, MatrixStack matrixStack, GraphicsContext graphicsContext) {
for(TextureElement textureElement : textures) {
int x = (int) (textureElement.position[0] * width);
int y = (int) (textureElement.position[1] * height);
Expand All @@ -56,7 +57,7 @@ public void drawScreen(RenderWindow window, MatrixStack matrixStack) {
}
// textureElement.texture.drawTexture(matrixStack, x,y,widthX,heightY, textureElement.region[0], textureElement.region[1], textureElement.region[2], textureElement.region[3]);
}
super.drawScreen(window, matrixStack);
super.drawScreen(window, matrixStack, graphicsContext);
}

@Override
Expand Down
22 changes: 8 additions & 14 deletions src/main/java/dev/hilligans/ourcraft/client/rendering/Texture.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
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.api.IGraphicsElement;
import dev.hilligans.ourcraft.client.rendering.graphics.api.IGraphicsEngine;
import dev.hilligans.ourcraft.client.rendering.graphics.RenderWindow;
import dev.hilligans.ourcraft.client.rendering.graphics.ShaderSource;
Expand All @@ -13,7 +14,7 @@
import dev.hilligans.ourcraft.util.registry.IRegistryElement;
import dev.hilligans.ourcraft.util.Settings;

public class Texture implements IRegistryElement {
public class Texture implements IRegistryElement, IGraphicsElement {

public String path;
public ModContent source;
Expand All @@ -31,13 +32,7 @@ public class Texture implements IRegistryElement {
public Texture(String path) {
this.path = path;
}

public Texture(String path, Image texture) {
this.path = path;
width = texture.getWidth();
height = texture.getHeight();
this.texture = texture;
}


public void drawTexture(RenderWindow window, MatrixStack matrixStack, int x, int y, int width, int height, int startX, int startY, int endX, int endY) {
IDefaultEngineImpl<?,?> defaultEngineImpl = window.getEngineImpl();
Expand Down Expand Up @@ -133,24 +128,23 @@ public void assignModContent(ModContent modContent) {
@Override
public void load(GameInstance gameInstance) {
shaderSource = gameInstance.SHADERS.get("ourcraft:position_texture");
}

@Override
public void load(GameInstance gameInstance, IGraphicsEngine<?, ?, ?> graphicsEngine, GraphicsContext graphicsContext) {
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();
}

@Override
public void loadGraphics(IGraphicsEngine<?, ?, ?> graphicsEngine, GraphicsContext graphicsContext) {
IRegistryElement.super.loadGraphics(graphicsEngine, graphicsContext);
textureId = graphicsEngine.getDefaultImpl().createTexture(graphicsContext, texture);
}

@Override
public void cleanupGraphics(IGraphicsEngine<?, ?, ?> graphicsEngine, GraphicsContext graphicsContext) {
IRegistryElement.super.cleanupGraphics(graphicsEngine, graphicsContext);
public void cleanup(GameInstance gameInstance, IGraphicsEngine<?, ?, ?> graphicsEngine, GraphicsContext graphicsContext) {
graphicsEngine.getDefaultImpl().destroyTexture(graphicsContext, textureId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ public VertexMesh addData(int[] indices, float[] vertices) {
return this;
}

public VertexMesh addData(ByteBuffer indices, ByteBuffer vertices) {
this.vertices = vertices.asFloatBuffer();
this.indices = indices.asIntBuffer();
return this;
}

public VertexMesh setAllocator(IAllocator<VertexMesh> allocator) {
this.allocator = allocator;
return this;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package dev.hilligans.ourcraft.client.rendering.graphics;

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 dev.hilligans.ourcraft.GameInstance;
import dev.hilligans.ourcraft.mod.handler.content.ModContent;
import dev.hilligans.ourcraft.util.registry.IRegistryElement;

import java.util.ArrayList;

public class ShaderSource implements IRegistryElement {
public class ShaderSource implements IRegistryElement, IGraphicsElement {

public String format;
public String name;
Expand Down Expand Up @@ -73,8 +74,7 @@ public String getResourceType() {
}

@Override
public void loadGraphics(IGraphicsEngine<?, ?, ?> graphicsEngine, GraphicsContext graphicsContext) {
System.err.println("LOADING");
public void load(GameInstance gameInstance, IGraphicsEngine<?, ?, ?> graphicsEngine, GraphicsContext graphicsContext) {
program = (int) graphicsEngine.getDefaultImpl().createProgram(graphicsContext,this);
/*
if(uniformNames != null) {
Expand All @@ -88,6 +88,11 @@ public void loadGraphics(IGraphicsEngine<?, ?, ?> graphicsEngine, GraphicsContex
*/
}

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

@Override
public String toString() {
return "ShaderSource{" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ default long createTexture(Q graphicsContext, Image image) {

long createProgram(Q graphicsContext, ShaderSource shaderSource);

void destroyProgram(Q graphicsContext, long program);

void uploadData(Q graphicsContext, FloatBuffer data, long index, String type, long program, ShaderSource shaderSource);

long createFrameBuffer(Q graphicsContext, int width, int height);
Expand Down Expand Up @@ -100,6 +102,10 @@ default long createProgram(Object graphicsContext, ShaderSource shaderSource) {
return createProgram((Q) graphicsContext, shaderSource);
}

default void destroyProgram(Object graphicsContext, long program) {
destroyProgram((Q)graphicsContext, program);
}

default void uploadData(Object graphicsContext, FloatBuffer data, long index, String type, long program, ShaderSource shaderSource) {
uploadData((Q) graphicsContext, data, index, type, program, shaderSource);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package dev.hilligans.ourcraft.client.rendering.graphics.api;

import dev.hilligans.ourcraft.GameInstance;

public interface IGraphicsElement {

void load(GameInstance gameInstance, IGraphicsEngine<?, ?, ?> graphicsEngine, GraphicsContext graphicsContext);

void cleanup(GameInstance gameInstance, IGraphicsEngine<?,?,?> graphicsEngine, GraphicsContext graphicsContext);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package dev.hilligans.ourcraft.client.rendering.graphics.api;

import dev.hilligans.ourcraft.client.Client;
import dev.hilligans.ourcraft.client.MatrixStack;
import dev.hilligans.ourcraft.client.rendering.graphics.RenderWindow;

public interface ILayout {

void drawLayout(RenderWindow renderWindow, GraphicsContext graphicsContext, IGraphicsEngine<?,?,?> engine, MatrixStack matrixStack, Client client);

void setField(String fieldName, String data);

void setField(String fieldName, int data);

}
Loading

0 comments on commit 7c067f4

Please sign in to comment.