diff --git a/pom.xml b/pom.xml index 194503a0..04b1af60 100644 --- a/pom.xml +++ b/pom.xml @@ -72,6 +72,10 @@ org.lwjgl lwjgl-vma + + org.lwjgl + lwjgl-nuklear + org.lwjgl lwjgl @@ -107,6 +111,11 @@ lwjgl-vma ${lwjgl.natives} + + org.lwjgl + lwjgl-nuklear + ${lwjgl.natives} + org.joml diff --git a/src/main/java/dev/hilligans/ourcraft/GameInstance.java b/src/main/java/dev/hilligans/ourcraft/GameInstance.java index a3d4a441..18321b8f 100644 --- a/src/main/java/dev/hilligans/ourcraft/GameInstance.java +++ b/src/main/java/dev/hilligans/ourcraft/GameInstance.java @@ -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; @@ -99,6 +101,7 @@ public GameInstance() { REGISTRIES.put(INPUT_HANDLER_PROVIDERS); REGISTRIES.put(TEXTURES); REGISTRIES.put(SHADERS); + REGISTRIES.put(LAYOUT_ENGINES); } public void loadContent() { @@ -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); } } } @@ -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); } } } @@ -165,6 +168,7 @@ public void cleanupGraphics(IGraphicsEngine graphicsEngine, GraphicsConte public final Registry INPUT_HANDLER_PROVIDERS = new Registry<>(this, InputHandlerProvider.class, "input"); public final Registry TEXTURES = new Registry<>(this, Texture.class, "texture"); public final Registry SHADERS = new Registry<>(this, ShaderSource.class, "shader"); + public final Registry> LAYOUT_ENGINES = new Registry<>(this, ILayoutEngine.class, "layout_engine"); public ArrayList BLOCK_STATES; public void buildBlockStates() { @@ -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) { diff --git a/src/main/java/dev/hilligans/ourcraft/Ourcraft.java b/src/main/java/dev/hilligans/ourcraft/Ourcraft.java index b404f62b..c6ad9507 100644 --- a/src/main/java/dev/hilligans/ourcraft/Ourcraft.java +++ b/src/main/java/dev/hilligans/ourcraft/Ourcraft.java @@ -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; @@ -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()); } } @@ -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")); @@ -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) diff --git a/src/main/java/dev/hilligans/ourcraft/client/rendering/ContainerScreen.java b/src/main/java/dev/hilligans/ourcraft/client/rendering/ContainerScreen.java index d4432cab..05769ddd 100644 --- a/src/main/java/dev/hilligans/ourcraft/client/rendering/ContainerScreen.java +++ b/src/main/java/dev/hilligans/ourcraft/client/rendering/ContainerScreen.java @@ -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; @@ -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); } diff --git a/src/main/java/dev/hilligans/ourcraft/client/rendering/Screen.java b/src/main/java/dev/hilligans/ourcraft/client/rendering/Screen.java index f04a9bd8..c45a0881 100644 --- a/src/main/java/dev/hilligans/ourcraft/client/rendering/Screen.java +++ b/src/main/java/dev/hilligans/ourcraft/client/rendering/Screen.java @@ -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 { @@ -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) {} diff --git a/src/main/java/dev/hilligans/ourcraft/client/rendering/ScreenBase.java b/src/main/java/dev/hilligans/ourcraft/client/rendering/ScreenBase.java index 523d9bb7..dae172eb 100644 --- a/src/main/java/dev/hilligans/ourcraft/client/rendering/ScreenBase.java +++ b/src/main/java/dev/hilligans/ourcraft/client/rendering/ScreenBase.java @@ -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; @@ -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); } diff --git a/src/main/java/dev/hilligans/ourcraft/client/rendering/ScreenBuilder.java b/src/main/java/dev/hilligans/ourcraft/client/rendering/ScreenBuilder.java index 5e313ae0..29f12164 100644 --- a/src/main/java/dev/hilligans/ourcraft/client/rendering/ScreenBuilder.java +++ b/src/main/java/dev/hilligans/ourcraft/client/rendering/ScreenBuilder.java @@ -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; @@ -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); @@ -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 diff --git a/src/main/java/dev/hilligans/ourcraft/client/rendering/Texture.java b/src/main/java/dev/hilligans/ourcraft/client/rendering/Texture.java index 33e20872..3039913d 100644 --- a/src/main/java/dev/hilligans/ourcraft/client/rendering/Texture.java +++ b/src/main/java/dev/hilligans/ourcraft/client/rendering/Texture.java @@ -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; @@ -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; @@ -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(); @@ -133,6 +128,10 @@ 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); @@ -140,17 +139,12 @@ public void load(GameInstance gameInstance) { } 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); } } diff --git a/src/main/java/dev/hilligans/ourcraft/client/rendering/VertexMesh.java b/src/main/java/dev/hilligans/ourcraft/client/rendering/VertexMesh.java index 23e7651b..cf89e860 100644 --- a/src/main/java/dev/hilligans/ourcraft/client/rendering/VertexMesh.java +++ b/src/main/java/dev/hilligans/ourcraft/client/rendering/VertexMesh.java @@ -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 allocator) { this.allocator = allocator; return this; diff --git a/src/main/java/dev/hilligans/ourcraft/client/rendering/graphics/ShaderSource.java b/src/main/java/dev/hilligans/ourcraft/client/rendering/graphics/ShaderSource.java index 03422344..5ebbaa04 100644 --- a/src/main/java/dev/hilligans/ourcraft/client/rendering/graphics/ShaderSource.java +++ b/src/main/java/dev/hilligans/ourcraft/client/rendering/graphics/ShaderSource.java @@ -1,6 +1,7 @@ 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; @@ -8,7 +9,7 @@ import java.util.ArrayList; -public class ShaderSource implements IRegistryElement { +public class ShaderSource implements IRegistryElement, IGraphicsElement { public String format; public String name; @@ -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) { @@ -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{" + diff --git a/src/main/java/dev/hilligans/ourcraft/client/rendering/graphics/api/IDefaultEngineImpl.java b/src/main/java/dev/hilligans/ourcraft/client/rendering/graphics/api/IDefaultEngineImpl.java index af1e456d..d184b94d 100644 --- a/src/main/java/dev/hilligans/ourcraft/client/rendering/graphics/api/IDefaultEngineImpl.java +++ b/src/main/java/dev/hilligans/ourcraft/client/rendering/graphics/api/IDefaultEngineImpl.java @@ -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); @@ -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); } diff --git a/src/main/java/dev/hilligans/ourcraft/client/rendering/graphics/api/IGraphicsElement.java b/src/main/java/dev/hilligans/ourcraft/client/rendering/graphics/api/IGraphicsElement.java new file mode 100644 index 00000000..0900e121 --- /dev/null +++ b/src/main/java/dev/hilligans/ourcraft/client/rendering/graphics/api/IGraphicsElement.java @@ -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); +} diff --git a/src/main/java/dev/hilligans/ourcraft/client/rendering/graphics/api/ILayout.java b/src/main/java/dev/hilligans/ourcraft/client/rendering/graphics/api/ILayout.java new file mode 100644 index 00000000..c966d53e --- /dev/null +++ b/src/main/java/dev/hilligans/ourcraft/client/rendering/graphics/api/ILayout.java @@ -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); + +} diff --git a/src/main/java/dev/hilligans/ourcraft/client/rendering/graphics/api/ILayoutEngine.java b/src/main/java/dev/hilligans/ourcraft/client/rendering/graphics/api/ILayoutEngine.java new file mode 100644 index 00000000..6a5f3493 --- /dev/null +++ b/src/main/java/dev/hilligans/ourcraft/client/rendering/graphics/api/ILayoutEngine.java @@ -0,0 +1,13 @@ +package dev.hilligans.ourcraft.client.rendering.graphics.api; + +import dev.hilligans.ourcraft.util.registry.IRegistryElement; + +public interface ILayoutEngine extends IRegistryElement, IGraphicsElement { + + T parseLayout(String layout); + + @Override + default String getResourceType() { + return "layout_engine"; + } +} diff --git a/src/main/java/dev/hilligans/ourcraft/client/rendering/graphics/fixedfunctiongl/FixedFunctionGLDefaultImpl.java b/src/main/java/dev/hilligans/ourcraft/client/rendering/graphics/fixedfunctiongl/FixedFunctionGLDefaultImpl.java index 9f082630..fc6d85af 100644 --- a/src/main/java/dev/hilligans/ourcraft/client/rendering/graphics/fixedfunctiongl/FixedFunctionGLDefaultImpl.java +++ b/src/main/java/dev/hilligans/ourcraft/client/rendering/graphics/fixedfunctiongl/FixedFunctionGLDefaultImpl.java @@ -156,6 +156,11 @@ public long createProgram(GraphicsContext graphicsContext, ShaderSource shaderSo return 0; } + @Override + public void destroyProgram(GraphicsContext graphicsContext, long program) { + + } + @Override public void uploadData(GraphicsContext graphicsContext, FloatBuffer data, long index, String type, long program, ShaderSource shaderSource) { diff --git a/src/main/java/dev/hilligans/ourcraft/client/rendering/graphics/nuklear/NuklearLayout.java b/src/main/java/dev/hilligans/ourcraft/client/rendering/graphics/nuklear/NuklearLayout.java new file mode 100644 index 00000000..f88e4f2d --- /dev/null +++ b/src/main/java/dev/hilligans/ourcraft/client/rendering/graphics/nuklear/NuklearLayout.java @@ -0,0 +1,217 @@ +package dev.hilligans.ourcraft.client.rendering.graphics.nuklear; + +import dev.hilligans.ourcraft.client.Client; +import dev.hilligans.ourcraft.client.MatrixStack; +import dev.hilligans.ourcraft.client.rendering.VertexMesh; +import dev.hilligans.ourcraft.client.rendering.graphics.RenderWindow; +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.IGraphicsEngine; +import dev.hilligans.ourcraft.client.rendering.graphics.api.ILayout; +import org.lwjgl.BufferUtils; +import org.lwjgl.nuklear.*; +import org.lwjgl.system.MemoryStack; + +import java.nio.ByteBuffer; +import java.nio.FloatBuffer; +import java.nio.IntBuffer; + +import static org.lwjgl.nuklear.Nuklear.*; +import static org.lwjgl.system.MemoryStack.stackPush; +import static org.lwjgl.system.MemoryUtil.*; + +public class NuklearLayout implements ILayout { + + private NkContext ctx; + + public NuklearLayoutEngine engine; + private NkBuffer cmds; + + public int width; + public int height; + + public NuklearLayout(NuklearLayoutEngine engine) { + this.engine = engine; + width = 500; + height = 500; + } + + protected NuklearLayout setup() { + ctx = NkContext.create(); + cmds = NkBuffer.create(); + nk_init(ctx, ALLOCATOR, null); + nk_buffer_init(cmds, ALLOCATOR, 1024); + nk_style_set_font(ctx, engine.default_font); + + layout(ctx, width, height); + return this; + } + + @Override + public void drawLayout(RenderWindow renderWindow, GraphicsContext graphicsContext, IGraphicsEngine engine, MatrixStack matrixStack, Client client) { + int AA = NK_ANTI_ALIASING_ON; + + IDefaultEngineImpl impl = engine.getDefaultImpl(); + + ByteBuffer vertices; + ByteBuffer elements; + try (MemoryStack stack = stackPush()) { + // fill convert configuration + NkConvertConfig config = NkConvertConfig.calloc(stack) + .vertex_layout(VERTEX_LAYOUT) + .vertex_size(20) + .vertex_alignment(4) + .null_texture(this.engine.null_texture) + .circle_segment_count(22) + .curve_segment_count(22) + .arc_segment_count(22) + .global_alpha(1.0f) + .shape_AA(AA) + .line_AA(AA); + + // setup buffers to load vertices and elements + NkBuffer vbuf = NkBuffer.malloc(stack); + NkBuffer ebuf = NkBuffer.malloc(stack); + + nk_buffer_init(vbuf, ALLOCATOR, 1024); + nk_buffer_init(ebuf, ALLOCATOR, 1024); + vertices = vbuf.memory().ptr(); + elements = ebuf.memory().ptr(); + //nk_buffer_init_fixed(vbuf, vertices/*, max_vertex_buffer*/); + //nk_buffer_init_fixed(ebuf, elements/*, max_element_buffer*/); + nk_convert(ctx, cmds, vbuf, ebuf, config); + + + FloatBuffer f = stack.floats( + 2.0f / width, 0.0f, 0.0f, 0.0f, + 0.0f, -2.0f / height, 0.0f, 0.0f, + 0.0f, 0.0f, -1.0f, 0.0f, + -1.0f, 1.0f, 0.0f, 1.0f + ); + impl.uploadData(graphicsContext, f, this.engine.nkProgram.uniformIndexes[0], "4fv", this.engine.nkProgram.program, this.engine.nkProgram); + + } catch (Exception e) { + throw new RuntimeException("Failed to render nk layout", e); + } + if(vertices == null) { + throw new NullPointerException("vertex buffer is null"); + } + if(elements == null) { + throw new NullPointerException("index buffer is null"); + } + //glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER); + //glUnmapBuffer(GL_ARRAY_BUFFER); + + VertexMesh vertexMesh = new VertexMesh(this.engine.vertexFormat).addData(elements, vertices); + long mesh = impl.createMesh(graphicsContext, vertexMesh); + + // iterate over and execute each draw command + //float fb_scale_x = (float) display_width / (float) width; + //float fb_scale_y = (float) display_height / (float) height; + long offset = NULL; + for (NkDrawCommand cmd = nk__draw_begin(ctx, cmds); cmd != null; cmd = nk__draw_next(cmd, cmds, ctx)) { + if (cmd.elem_count() == 0) { + continue; + } + impl.bindTexture(graphicsContext, cmd.texture().id()); + impl.drawMesh(graphicsContext, matrixStack, mesh, offset, cmd.elem_count()); + + //glBindTexture(GL_TEXTURE_2D, cmd.texture().id()); + //glScissor( + // (int) (cmd.clip_rect().x() * fb_scale_x), + // (int) ((height - (int) (cmd.clip_rect().y() + cmd.clip_rect().h())) * fb_scale_y), + // (int) (cmd.clip_rect().w() * fb_scale_x), + // (int) (cmd.clip_rect().h() * fb_scale_y) + //); + //glDrawElements(GL_TRIANGLES, cmd.elem_count(), GL_UNSIGNED_SHORT, offset); + offset += cmd.elem_count() * 2; + } + nk_clear(ctx); + nk_buffer_clear(cmds); + } + + void layout(NkContext ctx, int x, int y) { + try (MemoryStack stack = stackPush()) { + NkRect rect = NkRect.malloc(stack); + + if (nk_begin( + ctx, + "Demo", + nk_rect(x, y, 230, 250, rect), + NK_WINDOW_BORDER | NK_WINDOW_MOVABLE | NK_WINDOW_SCALABLE | NK_WINDOW_MINIMIZABLE | NK_WINDOW_TITLE)) { + nk_label(ctx, "some rando text", NK_LEFT); + } + + /*{ + nk_layout_row_static(ctx, 30, 80, 1); + if (nk_button_label(ctx, "button")) { + System.out.println("button pressed"); + } + + nk_layout_row_dynamic(ctx, 30, 2); + if (nk_option_label(ctx, "easy", op == EASY)) { + op = EASY; + } + if (nk_option_label(ctx, "hard", op == HARD)) { + op = HARD; + } + + nk_layout_row_dynamic(ctx, 25, 1); + nk_property_int(ctx, "Compression:", 0, compression, 100, 10, 1); + + nk_layout_row_dynamic(ctx, 20, 1); + nk_label(ctx, "background:", NK_TEXT_LEFT); + nk_layout_row_dynamic(ctx, 25, 1); + if (nk_combo_begin_color(ctx, nk_rgb_cf(background, NkColor.malloc(stack)), NkVec2.malloc(stack).set(nk_widget_width(ctx), 400))) { + nk_layout_row_dynamic(ctx, 120, 1); + nk_color_picker(ctx, background, NK_RGBA); + nk_layout_row_dynamic(ctx, 25, 1); + background + .r(nk_propertyf(ctx, "#R:", 0, background.r(), 1.0f, 0.01f, 0.005f)) + .g(nk_propertyf(ctx, "#G:", 0, background.g(), 1.0f, 0.01f, 0.005f)) + .b(nk_propertyf(ctx, "#B:", 0, background.b(), 1.0f, 0.01f, 0.005f)) + .a(nk_propertyf(ctx, "#A:", 0, background.a(), 1.0f, 0.01f, 0.005f)); + nk_combo_end(ctx); + } + } + + */ + nk_end(ctx); + } + } + + private static final int EASY = 0; + private static final int HARD = 1; + + NkColorf background = NkColorf.create() + .r(0.10f) + .g(0.18f) + .b(0.24f) + .a(1.0f); + + private int op = EASY; + + private IntBuffer compression = BufferUtils.createIntBuffer(1).put(0, 20); + + @Override + public void setField(String fieldName, String data) { + + } + + @Override + public void setField(String fieldName, int data) { + + } + + public static NkAllocator ALLOCATOR = NkAllocator.create() + .alloc((handle, old, size) -> nmemAllocChecked(size)) + .mfree((handle, ptr) -> nmemFree(ptr)); + + public static NkDrawVertexLayoutElement.Buffer VERTEX_LAYOUT = NkDrawVertexLayoutElement.create(4) + .position(0).attribute(NK_VERTEX_POSITION).format(NK_FORMAT_FLOAT).offset(0) + .position(1).attribute(NK_VERTEX_TEXCOORD).format(NK_FORMAT_FLOAT).offset(8) + .position(2).attribute(NK_VERTEX_COLOR).format(NK_FORMAT_R8G8B8A8).offset(16) + .position(3).attribute(NK_VERTEX_ATTRIBUTE_COUNT).format(NK_FORMAT_COUNT).offset(0) + .flip(); +} + diff --git a/src/main/java/dev/hilligans/ourcraft/client/rendering/graphics/nuklear/NuklearLayoutEngine.java b/src/main/java/dev/hilligans/ourcraft/client/rendering/graphics/nuklear/NuklearLayoutEngine.java new file mode 100644 index 00000000..943757d9 --- /dev/null +++ b/src/main/java/dev/hilligans/ourcraft/client/rendering/graphics/nuklear/NuklearLayoutEngine.java @@ -0,0 +1,187 @@ +package dev.hilligans.ourcraft.client.rendering.graphics.nuklear; + +import dev.hilligans.ourcraft.GameInstance; +import dev.hilligans.ourcraft.client.rendering.graphics.ShaderSource; +import dev.hilligans.ourcraft.client.rendering.graphics.VertexFormat; +import dev.hilligans.ourcraft.client.rendering.graphics.api.GraphicsContext; +import dev.hilligans.ourcraft.client.rendering.graphics.api.IGraphicsEngine; +import dev.hilligans.ourcraft.client.rendering.graphics.api.ILayoutEngine; +import dev.hilligans.ourcraft.resource.ResourceLocation; +import org.lwjgl.nuklear.NkDrawNullTexture; +import org.lwjgl.nuklear.NkUserFont; +import org.lwjgl.nuklear.NkUserFontGlyph; +import org.lwjgl.stb.STBTTAlignedQuad; +import org.lwjgl.stb.STBTTFontinfo; +import org.lwjgl.stb.STBTTPackContext; +import org.lwjgl.stb.STBTTPackedchar; +import org.lwjgl.system.MemoryStack; + +import java.nio.ByteBuffer; +import java.nio.FloatBuffer; +import java.nio.IntBuffer; +import java.util.ArrayList; + +import static org.lwjgl.nuklear.Nuklear.*; +import static org.lwjgl.stb.STBTruetype.*; +import static org.lwjgl.stb.STBTruetype.stbtt_GetCodepointHMetrics; +import static org.lwjgl.system.MemoryStack.stackPush; +import static org.lwjgl.system.MemoryUtil.*; +import static org.lwjgl.system.MemoryUtil.memAddress; + +public class NuklearLayoutEngine implements ILayoutEngine { + + public ArrayList layouts = new ArrayList<>(); + public ShaderSource nkProgram; + public NkUserFont default_font; + public NkDrawNullTexture null_texture; + public VertexFormat vertexFormat; + + public GameInstance gameInstance; + + @Override + public NuklearLayout parseLayout(String layout) { + return new NuklearLayout(this).setup(); + } + + public void load(IGraphicsEngine graphicsEngine, GraphicsContext graphicsContext, ByteBuffer ttf) { + default_font = NkUserFont.create(); + null_texture = NkDrawNullTexture.create(); + + int BITMAP_W = 1024; + int BITMAP_H = 1024; + + int FONT_HEIGHT = 18; + int fontTexID; + + STBTTFontinfo fontInfo = STBTTFontinfo.create(); + STBTTPackedchar.Buffer cdata = STBTTPackedchar.create(95); + + float scale; + float descent; + + try (MemoryStack stack = stackPush()) { + stbtt_InitFont(fontInfo, ttf); + scale = stbtt_ScaleForPixelHeight(fontInfo, FONT_HEIGHT); + + IntBuffer d = stack.mallocInt(1); + stbtt_GetFontVMetrics(fontInfo, null, d, null); + descent = d.get(0) * scale; + + ByteBuffer bitmap = memAlloc(BITMAP_W * BITMAP_H); + + STBTTPackContext pc = STBTTPackContext.malloc(stack); + stbtt_PackBegin(pc, bitmap, BITMAP_W, BITMAP_H, 0, 1, NULL); + stbtt_PackSetOversampling(pc, 4, 4); + stbtt_PackFontRange(pc, ttf, 0, FONT_HEIGHT, 32, cdata); + stbtt_PackEnd(pc); + + // Convert R8 to RGBA8 + ByteBuffer texture = memAlloc(BITMAP_W * BITMAP_H * 4); + for (int i = 0; i < bitmap.capacity(); i++) { + texture.putInt((bitmap.get(i) << 24) | 0x00FFFFFF); + } + texture.flip(); + + fontTexID = (int) graphicsEngine.getDefaultImpl().createTexture(graphicsContext, texture, BITMAP_W, BITMAP_H, 4); + + // glBindTexture(GL_TEXTURE_2D, fontTexID); + // glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, BITMAP_W, BITMAP_H, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, texture); + //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + + memFree(texture); + memFree(bitmap); + } + + default_font + .width((handle, h, text, len) -> { + float text_width = 0; + try (MemoryStack stack = stackPush()) { + IntBuffer unicode = stack.mallocInt(1); + + int glyph_len = nnk_utf_decode(text, memAddress(unicode), len); + int text_len = glyph_len; + + if (glyph_len == 0) { + return 0; + } + + IntBuffer advance = stack.mallocInt(1); + while (text_len <= len && glyph_len != 0) { + if (unicode.get(0) == NK_UTF_INVALID) { + break; + } + + /* query currently drawn glyph information */ + stbtt_GetCodepointHMetrics(fontInfo, unicode.get(0), advance, null); + text_width += advance.get(0) * scale; + + /* offset next glyph */ + glyph_len = nnk_utf_decode(text + text_len, memAddress(unicode), len - text_len); + text_len += glyph_len; + } + } + return text_width; + }) + .height(FONT_HEIGHT) + .query((handle, font_height, glyph, codepoint, next_codepoint) -> { + try (MemoryStack stack = stackPush()) { + FloatBuffer x = stack.floats(0.0f); + FloatBuffer y = stack.floats(0.0f); + + STBTTAlignedQuad q = STBTTAlignedQuad.malloc(stack); + IntBuffer advance = stack.mallocInt(1); + + stbtt_GetPackedQuad(cdata, BITMAP_W, BITMAP_H, codepoint - 32, x, y, q, false); + stbtt_GetCodepointHMetrics(fontInfo, codepoint, advance, null); + + NkUserFontGlyph ufg = NkUserFontGlyph.create(glyph); + + ufg.width(q.x1() - q.x0()); + ufg.height(q.y1() - q.y0()); + ufg.offset().set(q.x0(), q.y0() + (FONT_HEIGHT + descent)); + ufg.xadvance(advance.get(0) * scale); + ufg.uv(0).set(q.s0(), q.t0()); + ufg.uv(1).set(q.s1(), q.t1()); + } + }) + .texture(it -> it + .id(fontTexID)); + + try (MemoryStack stack = stackPush()) { + int texture = (int) graphicsEngine.getDefaultImpl().createTexture(graphicsContext, stack.bytes((byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF), 1, 1, 4); + null_texture.texture().id(texture); + null_texture.uv().set(0.5f, 0.5f); + } + } + + @Override + public String getResourceName() { + return "nk_layout_engine"; + } + + @Override + public String getResourceOwner() { + return "ourcraft"; + } + + @Override + public void load(GameInstance gameInstance) { + this.gameInstance = gameInstance; + vertexFormat = gameInstance.VERTEX_FORMATS.get("ourcraft:position2_texture_color"); + } + + @Override + public void load(GameInstance gameInstance, IGraphicsEngine graphicsEngine, GraphicsContext graphicsContext) { + nkProgram = gameInstance.SHADERS.get("ourcraft:nk_shader"); + load(graphicsEngine, graphicsContext, gameInstance.getResourceDirect(new ResourceLocation("Roboto-Medium.ttf", "ourcraft"))); + } + + @Override + public void cleanup(GameInstance gameInstance, IGraphicsEngine graphicsEngine, GraphicsContext graphicsContext) { + graphicsEngine.getDefaultImpl().destroyTexture(graphicsContext, null_texture.texture().id()); + graphicsEngine.getDefaultImpl().destroyTexture(graphicsContext, default_font.texture().id()); + default_font.free(); + null_texture.free(); + } +} diff --git a/src/main/java/dev/hilligans/ourcraft/client/rendering/graphics/opengl/OpenglDefaultImpl.java b/src/main/java/dev/hilligans/ourcraft/client/rendering/graphics/opengl/OpenglDefaultImpl.java index a8ea1d9d..46576166 100644 --- a/src/main/java/dev/hilligans/ourcraft/client/rendering/graphics/opengl/OpenglDefaultImpl.java +++ b/src/main/java/dev/hilligans/ourcraft/client/rendering/graphics/opengl/OpenglDefaultImpl.java @@ -226,6 +226,11 @@ public long createProgram(GraphicsContext graphicsContext, ShaderSource shaderSo return shader; } + @Override + public void destroyProgram(GraphicsContext graphicsContext, long program) { + glDeleteShader((int)program); + } + @Override public void uploadData(GraphicsContext graphicsContext, FloatBuffer data, long index, String type, long program, ShaderSource shaderSource) { //if(program != boundProgram) { diff --git a/src/main/java/dev/hilligans/ourcraft/client/rendering/graphics/tasks/GUIRenderTask.java b/src/main/java/dev/hilligans/ourcraft/client/rendering/graphics/tasks/GUIRenderTask.java index 66230f8d..2ec4ea8c 100644 --- a/src/main/java/dev/hilligans/ourcraft/client/rendering/graphics/tasks/GUIRenderTask.java +++ b/src/main/java/dev/hilligans/ourcraft/client/rendering/graphics/tasks/GUIRenderTask.java @@ -83,7 +83,7 @@ public void draw(RenderWindow window, GraphicsContext graphicsContext, IGraphics if (client.screen != null) { - client.screen.render(window, screenStack); + client.screen.render(window, screenStack, graphicsContext); // client.playerData.heldStack.renderStack(screenStack, (int) (Camera.newX - Settings.guiSize * 8), (int) (Camera.newY - Settings.guiSize * 8)); } //else { // Textures.CURSOR.drawCenteredTexture(window, screenStack,1.0f); diff --git a/src/main/java/dev/hilligans/ourcraft/client/rendering/graphics/tasks/WorldRenderTask.java b/src/main/java/dev/hilligans/ourcraft/client/rendering/graphics/tasks/WorldRenderTask.java index 077fe697..9572c478 100644 --- a/src/main/java/dev/hilligans/ourcraft/client/rendering/graphics/tasks/WorldRenderTask.java +++ b/src/main/java/dev/hilligans/ourcraft/client/rendering/graphics/tasks/WorldRenderTask.java @@ -303,9 +303,4 @@ public void load(GameInstance gameInstance) { super.load(gameInstance); shaderSource = gameInstance.SHADERS.get("ourcraft:world_shader"); } - - @Override - public void loadGraphics(IGraphicsEngine graphicsEngine, GraphicsContext graphicsContext) { - super.loadGraphics(graphicsEngine, graphicsContext); - } } \ No newline at end of file diff --git a/src/main/java/dev/hilligans/ourcraft/client/rendering/graphics/tasks/WorldTransparentRenderTask.java b/src/main/java/dev/hilligans/ourcraft/client/rendering/graphics/tasks/WorldTransparentRenderTask.java index aba01c43..8d68ed69 100644 --- a/src/main/java/dev/hilligans/ourcraft/client/rendering/graphics/tasks/WorldTransparentRenderTask.java +++ b/src/main/java/dev/hilligans/ourcraft/client/rendering/graphics/tasks/WorldTransparentRenderTask.java @@ -201,11 +201,6 @@ public void load(GameInstance gameInstance) { super.load(gameInstance); shaderSource = gameInstance.SHADERS.get("ourcraft:world_shader"); } - - @Override - public void loadGraphics(IGraphicsEngine graphicsEngine, GraphicsContext graphicsContext) { - super.loadGraphics(graphicsEngine, graphicsContext); - } } diff --git a/src/main/java/dev/hilligans/ourcraft/client/rendering/graphics/vulkan/VulkanDefaultImpl.java b/src/main/java/dev/hilligans/ourcraft/client/rendering/graphics/vulkan/VulkanDefaultImpl.java index b5ee97a2..71d2fbec 100644 --- a/src/main/java/dev/hilligans/ourcraft/client/rendering/graphics/vulkan/VulkanDefaultImpl.java +++ b/src/main/java/dev/hilligans/ourcraft/client/rendering/graphics/vulkan/VulkanDefaultImpl.java @@ -196,6 +196,11 @@ public long createProgram(VulkanBaseGraphicsContext graphicsContext, ShaderSourc return graphicsPipeline.pipeline; } + @Override + public void destroyProgram(VulkanBaseGraphicsContext graphicsContext, long program) { + + } + @Override public void uploadData(VulkanBaseGraphicsContext gc, FloatBuffer data, long index, String type, long program, ShaderSource shaderSource) { VulkanGraphicsContext graphicsContext = (VulkanGraphicsContext)gc; diff --git a/src/main/java/dev/hilligans/ourcraft/client/rendering/screens/AccountCreationScreen.java b/src/main/java/dev/hilligans/ourcraft/client/rendering/screens/AccountCreationScreen.java index c006eddd..3836d70a 100644 --- a/src/main/java/dev/hilligans/ourcraft/client/rendering/screens/AccountCreationScreen.java +++ b/src/main/java/dev/hilligans/ourcraft/client/rendering/screens/AccountCreationScreen.java @@ -4,6 +4,7 @@ import dev.hilligans.ourcraft.client.MatrixStack; import dev.hilligans.ourcraft.client.rendering.graphics.RenderWindow; import dev.hilligans.ourcraft.client.rendering.ScreenBase; +import dev.hilligans.ourcraft.client.rendering.graphics.api.GraphicsContext; import dev.hilligans.ourcraft.client.rendering.widgets.Button; import dev.hilligans.ourcraft.client.rendering.widgets.InputField; import dev.hilligans.ourcraft.network.packet.auth.CCreateAccount; @@ -35,8 +36,8 @@ public AccountCreationScreen() { } @Override - public void drawScreen(RenderWindow window, MatrixStack matrixStack) { - super.drawScreen(window, matrixStack); + public void drawScreen(RenderWindow window, MatrixStack matrixStack, GraphicsContext graphicsContext) { + super.drawScreen(window, matrixStack, graphicsContext); window.getStringRenderer().drawStringInternal(window, matrixStack,debug,100,700,0.5f); } } diff --git a/src/main/java/dev/hilligans/ourcraft/client/rendering/screens/DisconnectScreen.java b/src/main/java/dev/hilligans/ourcraft/client/rendering/screens/DisconnectScreen.java index a48f885b..2ed8a75c 100644 --- a/src/main/java/dev/hilligans/ourcraft/client/rendering/screens/DisconnectScreen.java +++ b/src/main/java/dev/hilligans/ourcraft/client/rendering/screens/DisconnectScreen.java @@ -4,6 +4,7 @@ import dev.hilligans.ourcraft.client.MatrixStack; import dev.hilligans.ourcraft.client.rendering.graphics.RenderWindow; import dev.hilligans.ourcraft.client.rendering.ScreenBase; +import dev.hilligans.ourcraft.client.rendering.graphics.api.GraphicsContext; import dev.hilligans.ourcraft.client.rendering.widgets.Button; import dev.hilligans.ourcraft.ClientMain; import dev.hilligans.ourcraft.util.Settings; @@ -24,8 +25,8 @@ public void buildContentForWindow(RenderWindow window) { } @Override - public void render(RenderWindow window, MatrixStack matrixStack) { + public void render(RenderWindow window, MatrixStack matrixStack, GraphicsContext graphicsContext) { window.getStringRenderer().drawCenteredStringTranslated(window, matrixStack,message == null ? "" : message, (int) ((window.getWindowHeight() + window.getStringRenderer().stringHeight) / 2),1.0f); - super.render(window, matrixStack); + super.render(window, matrixStack, graphicsContext); } } diff --git a/src/main/java/dev/hilligans/ourcraft/client/rendering/screens/JoinScreen.java b/src/main/java/dev/hilligans/ourcraft/client/rendering/screens/JoinScreen.java index c7486572..c08451c4 100644 --- a/src/main/java/dev/hilligans/ourcraft/client/rendering/screens/JoinScreen.java +++ b/src/main/java/dev/hilligans/ourcraft/client/rendering/screens/JoinScreen.java @@ -5,6 +5,7 @@ import dev.hilligans.ourcraft.client.MatrixStack; import dev.hilligans.ourcraft.client.rendering.graphics.RenderWindow; import dev.hilligans.ourcraft.client.rendering.Textures; +import dev.hilligans.ourcraft.client.rendering.graphics.api.GraphicsContext; import dev.hilligans.ourcraft.client.rendering.widgets.ServerSelectorWidget; import dev.hilligans.ourcraft.client.rendering.ScreenBase; import dev.hilligans.ourcraft.client.rendering.widgets.Button; @@ -86,8 +87,8 @@ public void setActive(ServerSelectorWidget selected) { } @Override - public void drawScreen(RenderWindow window, MatrixStack matrixStack) { - super.drawScreen(window, matrixStack); + public void drawScreen(RenderWindow window, MatrixStack matrixStack, GraphicsContext graphicsContext) { + super.drawScreen(window, matrixStack, graphicsContext); if(getClient().playerData.valid_account) { window.getStringRenderer().drawStringInternal(window, matrixStack,getClient().playerData.userName, (int) (Settings.guiSize * 8), (int) (1 * Settings.guiSize),0.5f); Textures.CHECK_MARK.drawTexture(window, matrixStack,0,0,(int)(8 * Settings.guiSize), (int)(8 * Settings.guiSize)); diff --git a/src/main/java/dev/hilligans/ourcraft/client/rendering/screens/LoadingScreen.java b/src/main/java/dev/hilligans/ourcraft/client/rendering/screens/LoadingScreen.java index daf91502..324c230c 100644 --- a/src/main/java/dev/hilligans/ourcraft/client/rendering/screens/LoadingScreen.java +++ b/src/main/java/dev/hilligans/ourcraft/client/rendering/screens/LoadingScreen.java @@ -4,6 +4,7 @@ import dev.hilligans.ourcraft.client.MatrixStack; import dev.hilligans.ourcraft.client.rendering.graphics.RenderWindow; import dev.hilligans.ourcraft.client.rendering.ScreenBase; +import dev.hilligans.ourcraft.client.rendering.graphics.api.GraphicsContext; public class LoadingScreen extends ScreenBase { @@ -11,8 +12,8 @@ public LoadingScreen() { } @Override - public void drawScreen(RenderWindow window, MatrixStack matrixStack) { - super.drawScreen(window, matrixStack); + public void drawScreen(RenderWindow window, MatrixStack matrixStack, GraphicsContext graphicsContext) { + super.drawScreen(window, matrixStack, graphicsContext); //if(ClientNetworkInit.networkInit != null && ClientNetworkInit.networkInit.packetDecoder != null) { // StringRenderer.drawString(matrixStack, "Content downloading:" + ClientNetworkInit.networkInit.packetDecoder.getPercentage(), 100, 100, 0.5f); // } diff --git a/src/main/java/dev/hilligans/ourcraft/client/rendering/screens/MiniMapScreen.java b/src/main/java/dev/hilligans/ourcraft/client/rendering/screens/MiniMapScreen.java index 4af9c58f..15f04607 100644 --- a/src/main/java/dev/hilligans/ourcraft/client/rendering/screens/MiniMapScreen.java +++ b/src/main/java/dev/hilligans/ourcraft/client/rendering/screens/MiniMapScreen.java @@ -4,6 +4,7 @@ import dev.hilligans.ourcraft.client.MatrixStack; import dev.hilligans.ourcraft.client.input.key.MouseHandler; import dev.hilligans.ourcraft.client.rendering.graphics.RenderWindow; +import dev.hilligans.ourcraft.client.rendering.graphics.api.GraphicsContext; import dev.hilligans.ourcraft.client.rendering.minimap.MiniMap; import dev.hilligans.ourcraft.client.rendering.ScreenBase; import dev.hilligans.ourcraft.ClientMain; @@ -22,8 +23,8 @@ public MiniMapScreen(Client client, MiniMap miniMap) { int mouseLastY = 0; @Override - public void drawScreen(RenderWindow window, MatrixStack matrixStack) { - super.drawScreen(window, matrixStack); + public void drawScreen(RenderWindow window, MatrixStack matrixStack, GraphicsContext graphicsContext) { + super.drawScreen(window, matrixStack, graphicsContext); if(MouseHandler.instance.mousePressed) { DoubleBuffer mousePos = window.getClient().getMousePos(); int x = (int) (mousePos.get(0) - mouseLastX); diff --git a/src/main/java/dev/hilligans/ourcraft/client/rendering/screens/ModListScreen.java b/src/main/java/dev/hilligans/ourcraft/client/rendering/screens/ModListScreen.java index bf084f65..50eae50a 100644 --- a/src/main/java/dev/hilligans/ourcraft/client/rendering/screens/ModListScreen.java +++ b/src/main/java/dev/hilligans/ourcraft/client/rendering/screens/ModListScreen.java @@ -4,6 +4,7 @@ import dev.hilligans.ourcraft.client.MatrixStack; import dev.hilligans.ourcraft.client.rendering.graphics.RenderWindow; import dev.hilligans.ourcraft.client.rendering.ScreenBase; +import dev.hilligans.ourcraft.client.rendering.graphics.api.GraphicsContext; import dev.hilligans.ourcraft.mod.handler.content.ModContent; import dev.hilligans.ourcraft.client.rendering.widgets.InvisibleFolder; import dev.hilligans.ourcraft.client.rendering.widgets.SelectorScreen; @@ -35,8 +36,8 @@ public void setActive(SelectorWidget selectorWidget) { } @Override - public void drawScreen(RenderWindow window, MatrixStack matrixStack) { - super.drawScreen(window, matrixStack); + public void drawScreen(RenderWindow window, MatrixStack matrixStack, GraphicsContext graphicsContext) { + super.drawScreen(window, matrixStack, graphicsContext); if(selectorWidget != null) { ModContent modContent = getClient().gameInstance.CONTENT_PACK.mods.get(selectorWidget.name); window.getStringRenderer().drawStringInternal(window, matrixStack,new String[]{modContent.getModID() + "Version: " + modContent.version, "Dependencies: " + Util.toString(modContent.getDependencies()), " ", modContent.description},350,100,0.5f); diff --git a/src/main/java/dev/hilligans/ourcraft/client/rendering/screens/TestScreen.java b/src/main/java/dev/hilligans/ourcraft/client/rendering/screens/TestScreen.java new file mode 100644 index 00000000..7e4a1641 --- /dev/null +++ b/src/main/java/dev/hilligans/ourcraft/client/rendering/screens/TestScreen.java @@ -0,0 +1,28 @@ +package dev.hilligans.ourcraft.client.rendering.screens; + +import dev.hilligans.ourcraft.client.MatrixStack; +import dev.hilligans.ourcraft.client.rendering.ScreenBase; +import dev.hilligans.ourcraft.client.rendering.graphics.RenderWindow; +import dev.hilligans.ourcraft.client.rendering.graphics.api.GraphicsContext; +import dev.hilligans.ourcraft.client.rendering.graphics.api.ILayout; +import dev.hilligans.ourcraft.client.rendering.graphics.api.ILayoutEngine; + +public class TestScreen extends ScreenBase { + + ILayoutEngine layoutEngine; + ILayout layout; + + @Override + public void setWindow(RenderWindow renderWindow) { + super.setWindow(renderWindow); + System.out.println(renderWindow.getGraphicsEngine().getGameInstance().LAYOUT_ENGINES.ELEMENTS); + layoutEngine = renderWindow.getGraphicsEngine().getGameInstance().LAYOUT_ENGINES.get("ourcraft:nk_layout_engine"); + layout = layoutEngine.parseLayout(""); + } + + @Override + public void drawScreen(RenderWindow window, MatrixStack matrixStack, GraphicsContext graphicsContext) { + super.drawScreen(window, matrixStack, graphicsContext); + layout.drawLayout(window, graphicsContext, window.getGraphicsEngine(), matrixStack, window.getClient()); + } +} diff --git a/src/main/java/dev/hilligans/ourcraft/client/rendering/screens/container/screens/ChestScreen.java b/src/main/java/dev/hilligans/ourcraft/client/rendering/screens/container/screens/ChestScreen.java index bada067e..f0a395e4 100644 --- a/src/main/java/dev/hilligans/ourcraft/client/rendering/screens/container/screens/ChestScreen.java +++ b/src/main/java/dev/hilligans/ourcraft/client/rendering/screens/container/screens/ChestScreen.java @@ -4,6 +4,7 @@ import dev.hilligans.ourcraft.client.MatrixStack; import dev.hilligans.ourcraft.client.rendering.ContainerScreen; import dev.hilligans.ourcraft.client.rendering.graphics.RenderWindow; +import dev.hilligans.ourcraft.client.rendering.graphics.api.GraphicsContext; import dev.hilligans.ourcraft.container.containers.ChestContainer; import dev.hilligans.ourcraft.client.rendering.Textures; import dev.hilligans.ourcraft.util.Settings; @@ -19,7 +20,7 @@ public ChestContainer getContainer() { } @Override - public void drawScreen(RenderWindow window, MatrixStack matrixStack) { + public void drawScreen(RenderWindow window, MatrixStack matrixStack, GraphicsContext graphicsContext) { Textures.CHEST.drawCenteredTexture(window, matrixStack,0,0,158,162, Settings.guiSize); } } diff --git a/src/main/java/dev/hilligans/ourcraft/client/rendering/screens/container/screens/CreativeInventoryScreen.java b/src/main/java/dev/hilligans/ourcraft/client/rendering/screens/container/screens/CreativeInventoryScreen.java index e92ab7a3..8b82dad8 100644 --- a/src/main/java/dev/hilligans/ourcraft/client/rendering/screens/container/screens/CreativeInventoryScreen.java +++ b/src/main/java/dev/hilligans/ourcraft/client/rendering/screens/container/screens/CreativeInventoryScreen.java @@ -5,6 +5,7 @@ import dev.hilligans.ourcraft.client.rendering.ContainerScreen; import dev.hilligans.ourcraft.client.rendering.graphics.RenderWindow; import dev.hilligans.ourcraft.client.rendering.Textures; +import dev.hilligans.ourcraft.client.rendering.graphics.api.GraphicsContext; import dev.hilligans.ourcraft.container.containers.CreativeContainer; import dev.hilligans.ourcraft.util.Settings; @@ -16,7 +17,7 @@ public CreativeContainer getContainer() { } @Override - public void drawScreen(RenderWindow window, MatrixStack matrixStack) { + public void drawScreen(RenderWindow window, MatrixStack matrixStack, GraphicsContext graphicsContext) { Textures.CREATIVE_INVENTORY.drawCenteredTexture(window, matrixStack,0,0,158,210, Settings.guiSize); } } diff --git a/src/main/java/dev/hilligans/ourcraft/client/rendering/screens/container/screens/CustomContainerScreen.java b/src/main/java/dev/hilligans/ourcraft/client/rendering/screens/container/screens/CustomContainerScreen.java index 5c47d828..efa9c9fa 100644 --- a/src/main/java/dev/hilligans/ourcraft/client/rendering/screens/container/screens/CustomContainerScreen.java +++ b/src/main/java/dev/hilligans/ourcraft/client/rendering/screens/container/screens/CustomContainerScreen.java @@ -4,12 +4,13 @@ import dev.hilligans.ourcraft.client.MatrixStack; import dev.hilligans.ourcraft.client.rendering.ContainerScreen; import dev.hilligans.ourcraft.client.rendering.graphics.RenderWindow; +import dev.hilligans.ourcraft.client.rendering.graphics.api.GraphicsContext; import dev.hilligans.ourcraft.container.Container; public abstract class CustomContainerScreen extends ContainerScreen { @Override - public abstract void drawScreen(RenderWindow window, MatrixStack matrixStack); + public abstract void drawScreen(RenderWindow window, MatrixStack matrixStack, GraphicsContext graphicsContext); @Override public Container getContainer() { diff --git a/src/main/java/dev/hilligans/ourcraft/client/rendering/screens/container/screens/InventoryScreen.java b/src/main/java/dev/hilligans/ourcraft/client/rendering/screens/container/screens/InventoryScreen.java index 70f881d4..dd6d65be 100644 --- a/src/main/java/dev/hilligans/ourcraft/client/rendering/screens/container/screens/InventoryScreen.java +++ b/src/main/java/dev/hilligans/ourcraft/client/rendering/screens/container/screens/InventoryScreen.java @@ -5,6 +5,7 @@ import dev.hilligans.ourcraft.client.rendering.ContainerScreen; import dev.hilligans.ourcraft.client.rendering.graphics.RenderWindow; import dev.hilligans.ourcraft.ClientMain; +import dev.hilligans.ourcraft.client.rendering.graphics.api.GraphicsContext; import dev.hilligans.ourcraft.container.containers.InventoryContainer; import dev.hilligans.ourcraft.client.rendering.Textures; import dev.hilligans.ourcraft.item.ItemStack; @@ -18,7 +19,7 @@ public InventoryContainer getContainer() { } @Override - public void drawScreen(RenderWindow window, MatrixStack matrixStack) { + public void drawScreen(RenderWindow window, MatrixStack matrixStack, GraphicsContext graphicsContext) { Textures.INVENTORY.drawCenteredTexture(window, matrixStack,0,0,158,99, Settings.guiSize); } diff --git a/src/main/java/dev/hilligans/ourcraft/container/containers/ContainerBuilder.java b/src/main/java/dev/hilligans/ourcraft/container/containers/ContainerBuilder.java index 00f217bf..fec0cd94 100644 --- a/src/main/java/dev/hilligans/ourcraft/container/containers/ContainerBuilder.java +++ b/src/main/java/dev/hilligans/ourcraft/container/containers/ContainerBuilder.java @@ -4,6 +4,7 @@ import dev.hilligans.ourcraft.client.MatrixStack; import dev.hilligans.ourcraft.client.rendering.ContainerScreen; import dev.hilligans.ourcraft.client.rendering.graphics.RenderWindow; +import dev.hilligans.ourcraft.client.rendering.graphics.api.GraphicsContext; import dev.hilligans.ourcraft.client.rendering.screens.container.screens.CustomContainerScreen; import dev.hilligans.ourcraft.client.rendering.widgets.Widget; import dev.hilligans.ourcraft.client.rendering.widgets.WidgetFetcher; @@ -39,7 +40,7 @@ public Container getContainer() { public ContainerScreen getContainerScreen() { return new CustomContainerScreen() { @Override - public void drawScreen(RenderWindow window, MatrixStack matrixStack) { + public void drawScreen(RenderWindow window, MatrixStack matrixStack, GraphicsContext graphicsContext) { // Texture texture1 = Textures.getTexture(textureName); // if(texture1 != null) { // texture1.drawCenteredTexture(matrixStack, 0, 0, width, height, Settings.guiSize); diff --git a/src/main/java/dev/hilligans/ourcraft/mod/handler/content/ContentPack.java b/src/main/java/dev/hilligans/ourcraft/mod/handler/content/ContentPack.java index 0a276b68..42e6975f 100644 --- a/src/main/java/dev/hilligans/ourcraft/mod/handler/content/ContentPack.java +++ b/src/main/java/dev/hilligans/ourcraft/mod/handler/content/ContentPack.java @@ -5,6 +5,7 @@ import dev.hilligans.ourcraft.client.input.Input; import dev.hilligans.ourcraft.client.input.InputHandlerProvider; import dev.hilligans.ourcraft.client.rendering.graphics.api.IGraphicsEngine; +import dev.hilligans.ourcraft.client.rendering.graphics.api.ILayoutEngine; import dev.hilligans.ourcraft.client.rendering.newrenderer.IModel; import dev.hilligans.ourcraft.client.rendering.ScreenBuilder; import dev.hilligans.ourcraft.client.rendering.Texture; @@ -168,6 +169,9 @@ private void rebuild() { for(ShaderSource shaderSource : mod.shaders) { gameInstance.registerShader(shaderSource); } + for(ILayoutEngine layoutEngine : mod.layoutEngines) { + gameInstance.registerLayoutEngine(layoutEngine); + } } } for(Registry registry : gameInstance.REGISTRIES.ELEMENTS) { diff --git a/src/main/java/dev/hilligans/ourcraft/mod/handler/content/CoreExtensionView.java b/src/main/java/dev/hilligans/ourcraft/mod/handler/content/CoreExtensionView.java index 7d5babeb..8eb8aa96 100644 --- a/src/main/java/dev/hilligans/ourcraft/mod/handler/content/CoreExtensionView.java +++ b/src/main/java/dev/hilligans/ourcraft/mod/handler/content/CoreExtensionView.java @@ -3,6 +3,7 @@ import dev.hilligans.ourcraft.client.input.InputHandlerProvider; import dev.hilligans.ourcraft.client.rendering.graphics.api.IGraphicsEngine; import dev.hilligans.ourcraft.GameInstance; +import dev.hilligans.ourcraft.client.rendering.graphics.api.ILayoutEngine; import dev.hilligans.ourcraft.resource.registry.loaders.RegistryLoader; import dev.hilligans.ourcraft.client.rendering.graphics.*; @@ -46,6 +47,10 @@ public void registerInputHandlerProviders(InputHandlerProvider... providers) { modContent.registerInputHandlerProviders(providers); } + public void registerLayoutEngine(ILayoutEngine... layoutEngines) { + modContent.registerLayoutEngine(layoutEngines); + } + @Override public ModContent getModContent() { return modContent; diff --git a/src/main/java/dev/hilligans/ourcraft/mod/handler/content/ModContent.java b/src/main/java/dev/hilligans/ourcraft/mod/handler/content/ModContent.java index f61a7987..948ee34f 100644 --- a/src/main/java/dev/hilligans/ourcraft/mod/handler/content/ModContent.java +++ b/src/main/java/dev/hilligans/ourcraft/mod/handler/content/ModContent.java @@ -6,6 +6,8 @@ import dev.hilligans.ourcraft.client.input.Input; import dev.hilligans.ourcraft.client.input.InputHandlerProvider; import dev.hilligans.ourcraft.client.rendering.graphics.api.IGraphicsEngine; +import dev.hilligans.ourcraft.client.rendering.graphics.api.ILayout; +import dev.hilligans.ourcraft.client.rendering.graphics.api.ILayoutEngine; import dev.hilligans.ourcraft.client.rendering.newrenderer.IModel; import dev.hilligans.ourcraft.client.rendering.ScreenBuilder; import dev.hilligans.ourcraft.client.rendering.Texture; @@ -74,6 +76,7 @@ public class ModContent { public ArrayList inputHandlerProviders = new ArrayList<>(); public ArrayList keybinds = new ArrayList<>(); public ArrayList shaders = new ArrayList<>(); + public ArrayList> layoutEngines = new ArrayList<>(); public HashMap protocols = new HashMap<>(); @@ -305,6 +308,13 @@ public void registerShader(ShaderSource... shaderSources) { this.shaders.addAll(Arrays.asList(shaderSources)); } + public void registerLayoutEngine(ILayoutEngine... layoutEngines) { + for(ILayoutEngine layoutEngine : layoutEngines) { + layoutEngine.assignModContent(this); + } + this.layoutEngines.addAll(Arrays.asList(layoutEngines)); + } + public void putData(IByteArray byteArray) { byteArray.writeInt(version); byteArray.writeUTF16(getModID()); diff --git a/src/main/java/dev/hilligans/ourcraft/util/registry/IRegistryElement.java b/src/main/java/dev/hilligans/ourcraft/util/registry/IRegistryElement.java index 1f20850a..92bf16da 100644 --- a/src/main/java/dev/hilligans/ourcraft/util/registry/IRegistryElement.java +++ b/src/main/java/dev/hilligans/ourcraft/util/registry/IRegistryElement.java @@ -39,7 +39,5 @@ default TagCollection getTagCollection() { return new TagCollection(); } - default void loadGraphics(IGraphicsEngine graphicsEngine, GraphicsContext graphicsContext) {} - default void cleanupGraphics(IGraphicsEngine graphicsEngine, GraphicsContext graphicsContext) {} } diff --git a/src/main/java/dev/hilligans/ourcraft/util/registry/Registry.java b/src/main/java/dev/hilligans/ourcraft/util/registry/Registry.java index b2d32722..db28cf58 100644 --- a/src/main/java/dev/hilligans/ourcraft/util/registry/Registry.java +++ b/src/main/java/dev/hilligans/ourcraft/util/registry/Registry.java @@ -1,6 +1,7 @@ package dev.hilligans.ourcraft.util.registry; import dev.hilligans.ourcraft.GameInstance; +import dev.hilligans.ourcraft.mod.handler.content.UnknownResourceException; import dev.hilligans.ourcraft.mod.handler.events.common.RegisterEvent; import dev.hilligans.ourcraft.mod.handler.Identifier; @@ -35,6 +36,14 @@ public T get(String name) { return MAPPED_ELEMENTS.get(name); } + public T getExcept(String name) { + T val = get(name); + if(val == null) { + throw new RuntimeException(STR."Unknown resource type \{name}, are you missing the modID?"); + } + return val; + } + public void put(String name, T element) { if(!registryType.equals(element.getResourceType())) { throw new RegistryException("Failed to add elements to registry, " + element.getResourceType() + " does not match type of this registry of " + registryType, this); diff --git a/src/main/resources/Shaders/NkFragmentShader.glsl b/src/main/resources/Shaders/NkFragmentShader.glsl new file mode 100644 index 00000000..482d4fa0 --- /dev/null +++ b/src/main/resources/Shaders/NkFragmentShader.glsl @@ -0,0 +1,11 @@ +#version 150 +precision mediump float; +uniform sampler2D Texture; +in vec2 Frag_UV; +in vec4 Frag_Color; + +out vec4 Out_Color; + +void main() { + Out_Color = Frag_Color * texture(Texture, Frag_UV.st); +} \ No newline at end of file diff --git a/src/main/resources/Shaders/NkVertexShader.glsl b/src/main/resources/Shaders/NkVertexShader.glsl new file mode 100644 index 00000000..acf8d292 --- /dev/null +++ b/src/main/resources/Shaders/NkVertexShader.glsl @@ -0,0 +1,14 @@ +#version 150 +uniform mat4 transform; +in vec2 Position; +in vec2 TexCoord; +in vec4 Color; + +out vec2 Frag_UV; +out vec4 Frag_Color; + +void main() { + Frag_UV = TexCoord; + Frag_Color = Color; + gl_Position = transform * vec4(Position.xy, 0, 1); +} \ No newline at end of file