Skip to content

Commit

Permalink
Code cleanup and refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Hilligans committed Nov 27, 2023
1 parent 7d37f6c commit cd1652e
Show file tree
Hide file tree
Showing 12 changed files with 42 additions and 66 deletions.
8 changes: 0 additions & 8 deletions src/main/java/dev/hilligans/ourcraft/ClientMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,4 @@ public static void main(String[] args) throws IOException {
}
client.startClient();
}

public static int getWindowX() {
return client == null ? 0 : client.windowX;
}

public static int getWindowY() {
return client == null ? 0 : client.windowY;
}
}
7 changes: 3 additions & 4 deletions src/main/java/dev/hilligans/ourcraft/client/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ public class Client implements IClientPacketHandler {

public int playerId;

public int windowX = 1600;
public int windowY = 800;
public boolean joinServer = true;
public boolean valid = false;
public boolean screenShot = false;
Expand Down Expand Up @@ -180,7 +178,8 @@ public void closeScreen() {
}

public void openScreen(Screen screen1) {
screen1.setWindow(graphicsEngine.getWindows().get(0));
RenderWindow renderWindow = graphicsEngine.getWindows().get(0);
screen1.setWindow(renderWindow);
gameInstance.EVENT_BUS.postEvent(new OpenScreenEvent(screen1,screen));
//glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
if(screen != null) {
Expand All @@ -199,7 +198,7 @@ public void openScreen(Screen screen1) {
}
playerData.openContainer = container;
}
screen1.resize(windowX,windowY);
screen1.resize(renderWindow.getWindowWidth(),renderWindow.getWindowHeight());
sendPacket(new COpenScreen(screen1));
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/dev/hilligans/ourcraft/client/ScreenShot.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public static void largeScreenshot(int width, int height, Client client) {

glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0,0,1920,1080);
client.windowX = 1920;
client.windowY = 1080;
// client.windowX = 1920;
// client.windowY = 1080;

writeImage(bufferedImage);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public void resize(int x, int y) {
public void addWidget(Widget widget) {
widgets.add(widget);
widget.screenBase = this;
widget.onScreenResize(client.windowX,client.windowY);
widget.onScreenResize(window.getWindowWidth(),window.getWindowHeight());
}

@Override
Expand All @@ -107,5 +107,9 @@ public void setWindow(RenderWindow renderWindow) {
buildContentForWindow(renderWindow);
}

public RenderWindow getWindow() {
return window;
}

public void buildContentForWindow(RenderWindow window) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ public abstract class RenderWindow {

public String queuedPipeline;

public double mouseX;
public double mouseY;

public RenderWindow(IGraphicsEngine<?,?,?> graphicsEngine) {
this.graphicsEngine = graphicsEngine;
if(graphicsEngine != null) {
Expand Down Expand Up @@ -89,9 +86,9 @@ public InputHandler getInputProvider() {
return inputHandler;
}

public abstract float getWindowWidth();
public abstract int getWindowWidth();

public abstract float getWindowHeight();
public abstract int getWindowHeight();

public abstract boolean isWindowFocused();

Expand All @@ -102,7 +99,7 @@ public Logger getLogger() {
}

public float getAspectRatio() {
return getWindowWidth() / getWindowHeight();
return (float) getWindowWidth() / getWindowHeight();
}

public void setMouseCursor(Image image) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ public class FixedFunctionGLWindow extends RenderWindow {
public boolean shouldClose = false;
public boolean mouseLocked = false;
public boolean windowFocused = true;
public float width;
public float height;
public int width;
public int height;

public FixedFunctionGLWindow(Client client, FixedFunctionGLEngine engine) {
super(engine);
this.camera = new FreeCamera(this);
window = glfwCreateWindow(client.windowX,client.windowY,"Ourcraft",NULL,NULL);
window = glfwCreateWindow(1920,1080,"Ourcraft",NULL,NULL);
if(window == NULL) {

glfwTerminate();
Expand Down Expand Up @@ -59,12 +59,12 @@ public Client getClient() {
}

@Override
public float getWindowWidth() {
public int getWindowWidth() {
return width;
}

@Override
public float getWindowHeight() {
public int getWindowHeight() {
return height;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ public Client getClient() {
}

@Override
public float getWindowWidth() {
public int getWindowWidth() {
return 0;
}

@Override
public float getWindowHeight() {
public int getWindowHeight() {
return 0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ public Client getClient() {
}

@Override
public float getWindowWidth() {
public int getWindowWidth() {
return 0;
}

@Override
public float getWindowHeight() {
public int getWindowHeight() {
return 0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public class OpenGLWindow extends RenderWindow {
public boolean shouldClose = false;
public boolean mouseLocked = false;
public boolean windowFocused = true;
public float width;
public float height;
public int width;
public int height;
public Vector4f clearColor = new Vector4f();

public OpenGLWindow(Client client, OpenGLEngine engine, String name, int width, int height) {
Expand Down Expand Up @@ -78,12 +78,12 @@ public Client getClient() {
}

@Override
public float getWindowWidth() {
public int getWindowWidth() {
return width;
}

@Override
public float getWindowHeight() {
public int getWindowHeight() {
return height;
}

Expand All @@ -103,27 +103,9 @@ public void setClearColor(float r, float g, float b, float a) {
}

public void registerCallbacks() {
glfwSetCursorPosCallback(window, (window, xpos, ypos) -> {
mouseX = xpos;
mouseY = ypos;
if(mouseLocked) {
double halfWindowX = (double) getWindowWidth() / 2;
double halfWindowY = (double) getWindowHeight() / 2;

double deltaX = xpos - halfWindowX;
double deltaY = ypos - halfWindowY;

if(camera != null) {
// camera.addRotation((float) (deltaY / camera.getSensitivity()), (float) (deltaX / camera.getSensitivity()));
}
glfwSetCursorPos(window, halfWindowX, halfWindowY);
}
});
glfwSetWindowSizeCallback(window, (window, w, h) -> {
width = w;
height = h;
client.windowX = w;
client.windowY = h;
GL30.glViewport(0,0,w,h);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ public void draw(RenderWindow window, GraphicsContext graphicsContext, IGraphics

if (client.playerData.f3) {
// stringRenderer.drawStringInternal(window,screenStack,"1",0,0,0.5f);
stringRenderer.drawStringInternal(window, screenStack, "FPS:" + window.frameTracker.getFPS(), client.windowX / 2, 29, 0.5f);
stringRenderer.drawStringInternal(window, screenStack, "FPS:" + window.frameTracker.getFPS(), window.getWindowWidth() / 2, 29, 0.5f);
/* stringRenderer.drawStringInternal(window, screenStack, "Biome:" + client.clientWorld.biomeMap.getBiome((int) Camera.pos.x, (int) Camera.pos.z).name, client.windowX / 2, 58, 0.5f);
stringRenderer.drawStringInternal(window, screenStack, "VelY:" + Camera.velY, client.windowX / 2, 87, 0.5f);
Runtime runtime = Runtime.getRuntime();
long usedMB = (runtime.totalMemory() - runtime.freeMemory()) / 1024 / 1024;
stringRenderer.drawStringInternal(window, screenStack, "Memory:" + usedMB + "MB", client.windowX / 2, 126, 0.5f);
*/
stringRenderer.drawStringInternal(window, screenStack, "Pitch:" + Math.toDegrees(window.camera.getPitch()), client.windowX / 2, 155, 0.5f);
stringRenderer.drawStringInternal(window, screenStack, "Yaw:" + Math.toDegrees(window.camera.getYaw()), client.windowX / 2, 184, 0.5f);
stringRenderer.drawStringInternal(window, screenStack, "Pitch:" + Math.toDegrees(window.camera.getPitch()), window.getWindowWidth() / 2, 155, 0.5f);
stringRenderer.drawStringInternal(window, screenStack, "Yaw:" + Math.toDegrees(window.camera.getYaw()), window.getWindowWidth() / 2, 184, 0.5f);
/*
stringRenderer.drawStringInternal(window, screenStack, "Sounds:" + client.soundEngine.sounds.size(), client.windowX / 2, 213, 0.5f);
stringRenderer.drawStringInternal(window, screenStack, "Render Calls:" + GLRenderer.drawCalls, client.windowX / 2, 242, 0.5f);
Expand All @@ -48,9 +48,9 @@ public void draw(RenderWindow window, GraphicsContext graphicsContext, IGraphics
stringRenderer.drawStringInternal(window, screenStack, "Chunks:" + client.chunks, client.windowX / 2, 329, 0.5f);
*/
stringRenderer.drawStringInternal(window,screenStack, "X:" + client.rWindow.camera.getCameraPos().x, client.windowX / 2, 358,0.5f);
stringRenderer.drawStringInternal(window,screenStack, "Y:" + client.rWindow.camera.getCameraPos().y, client.windowX / 2, 387,0.5f);
stringRenderer.drawStringInternal(window,screenStack, "Z:" + client.rWindow.camera.getCameraPos().z, client.windowX / 2, 416,0.5f);
stringRenderer.drawStringInternal(window,screenStack, "X:" + client.rWindow.camera.getCameraPos().x, window.getWindowWidth() / 2, 358,0.5f);
stringRenderer.drawStringInternal(window,screenStack, "Y:" + client.rWindow.camera.getCameraPos().y, window.getWindowWidth() / 2, 387,0.5f);
stringRenderer.drawStringInternal(window,screenStack, "Z:" + client.rWindow.camera.getCameraPos().z, window.getWindowWidth() / 2, 416,0.5f);
}
// ItemStack stack = client.playerData.inventory.getItem(client.playerData.handSlot);
// if (stack != null && stack.item != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,12 +217,12 @@ public InputHandler getInputProvider() {
}

@Override
public float getWindowWidth() {
public int getWindowWidth() {
return glfwWidth;
}

@Override
public float getWindowHeight() {
public int getWindowHeight() {
return glfwHeight;
}

Expand Down
14 changes: 8 additions & 6 deletions src/main/java/dev/hilligans/ourcraft/container/Container.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,14 @@ public void setTextureSize(int x, int y) {
}

public void resize() {
int newX = (int)(ClientMain.getWindowX() / 2 - textureX * Settings.guiSize / 2);
int newY = (int)(ClientMain.getWindowY() / 2 - textureY * Settings.guiSize / 2);
for(Slot slot : slots) {
slot.x = (int)(newX + slot.startX * Settings.guiSize);
slot.y = (int)(newY + slot.startY * Settings.guiSize);
}
//TODO implement
//throw new RuntimeException("implement");
//int newX = (int)(ClientMain.getWindowX() / 2 - textureX * Settings.guiSize / 2);
//int newY = (int)(ClientMain.getWindowY() / 2 - textureY * Settings.guiSize / 2);
//for(Slot slot : slots) {
// slot.x = (int)(newX + slot.startX * Settings.guiSize);
// slot.y = (int)(newY + slot.startY * Settings.guiSize);
//}
}

public void addPlayerInventorySlots(int startX, int startY, IInventory inventory, int startIndex) {
Expand Down

0 comments on commit cd1652e

Please sign in to comment.