Skip to content

Commit

Permalink
Added a stepping section to allow for stopping and waiting at certain…
Browse files Browse the repository at this point in the history
… sections, more legacy graphics code cleanup and added scissoring to the default impl
  • Loading branch information
Hilligans committed Dec 25, 2023
1 parent 7c067f4 commit bc78ea6
Show file tree
Hide file tree
Showing 34 changed files with 160 additions and 90 deletions.
1 change: 1 addition & 0 deletions src/main/java/dev/hilligans/ourcraft/ClientMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public static Client getClient() {
public static void main(String[] args) throws IOException {
startTime = System.currentTimeMillis();
argumentContainer = new ArgumentContainer(args);
System.out.println(STR."Starting client with PID \{ProcessHandle.current().pid()}");
gameInstance.handleArgs(args);
gameInstance.side = Side.CLIENT;
gameInstance.loadContent();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ public PlantBlock(String name, BlockProperties blockProperties) {

@Override
public void renderItem(MatrixStack matrixStack, int x, int y, int size, ItemStack itemStack) {
Renderer.renderItem(matrixStack,x,y,size,blockProperties.blockTextureManager);
Renderer.renderItem(matrixStack,x,y,size);
}
}
1 change: 1 addition & 0 deletions src/main/java/dev/hilligans/ourcraft/client/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ public Client(GameInstance gameInstance, ArgumentContainer argumentContainer) {

public Client setGraphicsEngine(IGraphicsEngine<?,?,?> graphicsEngine) {
if(graphicsEngine != null) {

this.graphicsEngine = graphicsEngine;
if(graphicsEngine instanceof VulkanEngine engine) {
engine.client = this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import dev.hilligans.ourcraft.client.rendering.graphics.RenderWindow;
import dev.hilligans.ourcraft.client.rendering.newrenderer.TextAtlas;
import dev.hilligans.ourcraft.item.ItemStack;
import dev.hilligans.ourcraft.client.rendering.world.managers.TextureManager;
import dev.hilligans.ourcraft.client.rendering.world.managers.VAOManager;
import dev.hilligans.ourcraft.Ourcraft;
import dev.hilligans.ourcraft.util.Settings;
Expand Down Expand Up @@ -46,13 +45,14 @@ public static void renderBlockItem(MatrixStack matrixStack, int x, int y, int si
*/
}

public static void renderItem(MatrixStack matrixStack, int x, int y, int size, TextureManager textureManager) {
public static void renderItem(MatrixStack matrixStack, int x, int y, int size) {
size *= 2;
size -= Settings.guiSize * 2;
x += Settings.guiSize;
y += Settings.guiSize;
//matrixStack.applyTransformation(ClientMain.getClient().shaderManager.shaderProgram);
//glUseProgram(ClientMain.getClient().shaderManager.shaderProgram);
/*
glDisable(GL_DEPTH_TEST);
int id = textureManager.getTextureId();
float minX = TextAtlas.getMinX(id);
Expand All @@ -67,9 +67,12 @@ public static void renderItem(MatrixStack matrixStack, int x, int y, int size, T
glDrawElements(GL_TRIANGLES,6,GL_UNSIGNED_INT,0);
VAOManager.destroyBuffer(vao);
glEnable(GL_DEPTH_TEST);
*/
}

public static void renderItem(RenderWindow window, MatrixStack matrixStack, int x, int y, int size, TextureManager textureManager) {
public static void renderItem(RenderWindow window, MatrixStack matrixStack, int x, int y, int size) {
/*
size *= 2;
size -= Settings.guiSize * 2;
x += Settings.guiSize;
Expand All @@ -85,5 +88,6 @@ public static void renderItem(RenderWindow window, MatrixStack matrixStack, int
VertexMesh mesh = new VertexMesh(Ourcraft.position_color_texture);
mesh.addData(indices, vertices);
//window.getGraphicsEngine().getDefaultImpl().drawAndDestroyMesh(window, null, matrixStack, mesh, textureManager.getTextureId(), 0);
*/
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class VertexMesh implements IAllocator<VertexMesh> {

public IntBuffer indices;
public FloatBuffer vertices;
public int elementSize = 4;
public IAllocator<VertexMesh> allocator;


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
import dev.hilligans.ourcraft.client.Client;
import dev.hilligans.ourcraft.client.MatrixStack;
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 RenderPipeline implements IRegistryElement {
public class RenderPipeline implements IRegistryElement, IGraphicsElement {

public String name;
public ModContent modContent;
Expand Down Expand Up @@ -107,4 +108,18 @@ public String toString() {
", renderTasks=" + renderTasks +
'}';
}

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

@Override
public void cleanup(GameInstance gameInstance, IGraphicsEngine<?, ?, ?> graphicsEngine, GraphicsContext graphicsContext) {
for(RenderTask renderTask : renderTasks) {
renderTask.cleanup(gameInstance, graphicsEngine, graphicsContext);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package dev.hilligans.ourcraft.client.rendering.graphics;

import dev.hilligans.ourcraft.GameInstance;
import dev.hilligans.ourcraft.client.Client;
import dev.hilligans.ourcraft.client.MatrixStack;
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;

public abstract class RenderTask {
public abstract class RenderTask implements IGraphicsElement {

protected String identifierName;

Expand All @@ -30,4 +32,10 @@ public RenderTask setNameIdentifierName(String name) {
public String getIdentifierName() {
return identifierName;
}

@Override
public void load(GameInstance gameInstance, IGraphicsEngine<?, ?, ?> graphicsEngine, GraphicsContext graphicsContext) {}

@Override
public void cleanup(GameInstance gameInstance, IGraphicsEngine<?, ?, ?> graphicsEngine, GraphicsContext graphicsContext) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public void queueRenderPipeline(String name) {

public abstract boolean shouldClose();

public void swapBuffers() {
public void swapBuffers(GraphicsContext graphicsContext) {
if(queuedPipeline != null) {
setRenderPipeline(queuedPipeline);
queuedPipeline = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ default long createTexture(Q graphicsContext, Image image) {

void clearFBO(Q graphicsContext, Vector4f clearColor);

void setScissor(Q graphicsContext, int x, int y, int width, int height);

default void drawMesh(Object graphicsContext, MatrixStack matrixStack, long meshID, long indicesIndex, int length) {
drawMesh((Q) graphicsContext, matrixStack, meshID, indicesIndex, length);
}
Expand Down Expand Up @@ -150,4 +152,12 @@ default long getBoundProgram(Object graphicsContext) {
default void clearFBO(Object graphicsContext, Vector4f clearColor) {
clearFBO((Q)graphicsContext, clearColor);
}

default void setScissor(Object graphicsContext, int x, int y, int width, int height) {
setScissor((Q)graphicsContext, x, y, width, height);
}

default void defaultScissor(Object graphicsContext, RenderWindow renderWindow) {
setScissor(graphicsContext, 0, 0, renderWindow.getWindowWidth(), renderWindow.getWindowHeight());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ default Runnable createRenderLoop(GameInstance gameInstance, RenderWindow window
render(window, graphicsContext);
}
try (var $2 = section.startSection("swapBuffers")) {
window.swapBuffers();
window.swapBuffers(graphicsContext);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,11 @@ public void clearFBO(GraphicsContext graphicsContext, Vector4f clearColor) {

}

@Override
public void setScissor(GraphicsContext graphicsContext, int x, int y, int width, int height) {

}

private int getGLPrimitive(int type) {
return type + 0x1400;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.hilligans.ourcraft.client.rendering.graphics.fixedfunctiongl;

import dev.hilligans.ourcraft.client.Client;
import dev.hilligans.ourcraft.client.rendering.graphics.api.GraphicsContext;
import dev.hilligans.ourcraft.client.rendering.graphics.implementations.FreeCamera;
import dev.hilligans.ourcraft.client.rendering.graphics.RenderWindow;

Expand Down Expand Up @@ -46,7 +47,7 @@ public boolean shouldClose() {
}

@Override
public void swapBuffers() {
public void swapBuffers(GraphicsContext graphicsContext) {
glfwSwapBuffers(window);
client.rendering = false;
glfwPollEvents();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.hilligans.ourcraft.client.rendering.graphics.implementations.splitwindows;

import dev.hilligans.ourcraft.client.Client;
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.RenderWindow;

Expand Down Expand Up @@ -35,7 +36,7 @@ public boolean shouldClose() {
}

@Override
public void swapBuffers() {
public void swapBuffers(GraphicsContext graphicsContext) {

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ public boolean shouldClose() {
}

@Override
public void swapBuffers() {
super.swapBuffers();
baseWindow.swapBuffers();
public void swapBuffers(GraphicsContext graphicsContext) {
super.swapBuffers(graphicsContext);
baseWindow.swapBuffers(graphicsContext);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public long getTexture() {
}

@Override
public void swapBuffers() {
super.swapBuffers();
public void swapBuffers(GraphicsContext graphicsContext) {
super.swapBuffers(graphicsContext);
long t = fbo1;
fbo1 = fbo2;
fbo2 = t;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.nio.IntBuffer;

import static org.lwjgl.nuklear.Nuklear.*;
import static org.lwjgl.opengl.GL11.glScissor;
import static org.lwjgl.system.MemoryStack.stackPush;
import static org.lwjgl.system.MemoryUtil.*;

Expand All @@ -42,16 +43,15 @@ protected NuklearLayout setup() {
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();
IDefaultEngineImpl<?, ?> impl = engine.getDefaultImpl();
layout(ctx, width, height);

ByteBuffer vertices;
ByteBuffer elements;
Expand All @@ -70,8 +70,8 @@ public void drawLayout(RenderWindow renderWindow, GraphicsContext graphicsContex
.line_AA(AA);

// setup buffers to load vertices and elements
NkBuffer vbuf = NkBuffer.malloc(stack);
NkBuffer ebuf = NkBuffer.malloc(stack);
NkBuffer vbuf = NkBuffer.calloc(stack);
NkBuffer ebuf = NkBuffer.calloc(stack);

nk_buffer_init(vbuf, ALLOCATOR, 1024);
nk_buffer_init(ebuf, ALLOCATOR, 1024);
Expand All @@ -93,39 +93,42 @@ public void drawLayout(RenderWindow renderWindow, GraphicsContext graphicsContex
} catch (Exception e) {
throw new RuntimeException("Failed to render nk layout", e);
}
if(vertices == null) {
if (vertices == null) {
throw new NullPointerException("vertex buffer is null");
}
if(elements == 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);
vertexMesh.elementSize = 2;
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;
float fb_scale_x = (float) renderWindow.getWindowWidth() / (float) width;
float fb_scale_y = (float) renderWindow.getWindowHeight() / (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;
}
//System.out.println(cmd.elem_count());
impl.bindTexture(graphicsContext, cmd.texture().id());
//glBindTexture(GL_TEXTURE_2D, cmd.texture().id());
NkRect rect = cmd.clip_rect();
int x = (int) (rect.x() * fb_scale_x);
int y = (int) ((height - (int) (rect.y() + rect.h())) * fb_scale_y);
int width = (int) (rect.w() * fb_scale_x);
int height = (int) (rect.h() * fb_scale_y);
impl.setScissor(graphicsContext, x, y, width, height);
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;
offset += cmd.elem_count() * 2L;
}
impl.defaultScissor(graphicsContext, renderWindow);
nk_clear(ctx);
nk_buffer_clear(cmds);
}
Expand All @@ -139,10 +142,9 @@ void layout(NkContext ctx, int x, int y) {
"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_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");
Expand Down Expand Up @@ -175,7 +177,6 @@ void layout(NkContext ctx, int x, int y) {
}
}

*/
nk_end(ctx);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import dev.hilligans.ourcraft.client.rendering.screens.JoinScreen;
import dev.hilligans.ourcraft.data.primitives.Tuple;
import dev.hilligans.ourcraft.util.Logger;
import dev.hilligans.ourcraft.util.sections.ISection;
import dev.hilligans.ourcraft.util.sections.ProfiledSection;
import dev.hilligans.ourcraft.util.sections.SteppingSection;
import org.lwjgl.opengl.GL;
import org.lwjgl.opengl.GL43;
import org.lwjgl.opengl.GLDebugMessageCallback;
Expand Down Expand Up @@ -142,7 +144,7 @@ public GraphicsContext getGraphicsContext() {
if(profiling) {
return new GraphicsContext().setSection(new ProfiledSection().setMonitorName("loop"));
}
return new GraphicsContext();
return new GraphicsContext().setSection(ISection.getSection(client.argumentContainer.getString("--clientSection", "empty")));
}

@Override
Expand Down
Loading

0 comments on commit bc78ea6

Please sign in to comment.