Skip to content

Commit

Permalink
Merge pull request #5 from XenFork/gui
Browse files Browse the repository at this point in the history
  • Loading branch information
squid233 authored Jul 6, 2024
2 parents ec4978f + c504f51 commit 8957ff6
Show file tree
Hide file tree
Showing 15 changed files with 376 additions and 123 deletions.
146 changes: 95 additions & 51 deletions modules/freeworld.client/src/main/java/freeworld/client/Freeworld.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
import freeworld.client.render.RenderSystem;
import freeworld.client.render.gl.GLStateMgr;
import freeworld.client.render.model.block.BlockModelManager;
import freeworld.client.render.screen.ingame.CreativeTabScreen;
import freeworld.client.render.screen.ingame.PauseScreen;
import freeworld.client.render.screen.Screen;
import freeworld.client.render.world.HitResult;
import freeworld.core.registry.BuiltinRegistries;
import freeworld.math.Vector2d;
Expand All @@ -30,6 +33,7 @@
import freeworld.world.entity.EntityComponents;
import freeworld.world.entity.EntityTypes;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import overrun.marshal.Unmarshal;
import overrungl.glfw.GLFW;
Expand Down Expand Up @@ -72,7 +76,9 @@ public final class Freeworld implements AutoCloseable {
private BlockModelManager blockModelManager;
private World world;
private Entity player;
private int gameTicks = 0;
@Nullable
private Screen screen = null;
private final float guiScale = 2;
private int blockDestroyTimer = 0;
private int blockPlaceTimer = 0;
private int hotBarSelection = 0;
Expand Down Expand Up @@ -172,6 +178,26 @@ private void onKey(int key, int scancode, int action, int mods) {
case GLFW.KEY_8 -> hotBarSelection = 7;
case GLFW.KEY_9 -> hotBarSelection = 8;
case GLFW.KEY_0 -> hotBarSelection = 9;
case GLFW.KEY_ESCAPE -> {
if (screen != null) {
if (screen.escapeCanClose()) {
screen.close();
}
} else if (world != null) {
openScreen(new PauseScreen(this, null));
}
}
default -> {
if (screen == null) {
if (world != null) {
switch (key) {
case GLFW.KEY_E -> openScreen(new CreativeTabScreen(this, null));
}
}
} else {
screen.onKeyPressed(key);
}
}
}
}
}
Expand All @@ -181,6 +207,10 @@ private void onResize(int width, int height) {
framebufferWidth = width;
framebufferHeight = height;
gl.viewport(0, 0, width, height);

if (screen != null) {
screen.onResize(framebufferWidth / guiScale, framebufferHeight / guiScale);
}
}

private void onCursorPos(double x, double y) {
Expand Down Expand Up @@ -219,56 +249,56 @@ private void onScroll(double x, double y) {
}

private void tick() {
camera.preUpdate();

final boolean onGround = player.hasComponent(EntityComponents.ON_GROUND);
double speed = onGround ? 0.1 : 0.02;
if (glfw.getKey(window, GLFW.KEY_LEFT_CONTROL) == GLFW.PRESS) speed *= 2.0;
double xo = 0.0;
double zo = 0.0;
if (glfw.getKey(window, GLFW.KEY_W) == GLFW.PRESS) zo -= 1.0;
if (glfw.getKey(window, GLFW.KEY_S) == GLFW.PRESS) zo += 1.0;
if (glfw.getKey(window, GLFW.KEY_A) == GLFW.PRESS) xo -= 1.0;
if (glfw.getKey(window, GLFW.KEY_D) == GLFW.PRESS) xo += 1.0;
if (onGround && glfw.getKey(window, GLFW.KEY_SPACE) == GLFW.PRESS) {
final Vector3d value = player.getComponent(EntityComponents.VELOCITY);
player.setComponent(EntityComponents.VELOCITY, new Vector3d(value.x(), 0.5, value.z()));
}
player.setComponent(EntityComponents.ACCELERATION,
MathUtil.moveRelative(xo, 0.0, zo, player.getComponent(EntityComponents.ROTATION).y(), speed));
world.tick();

if (blockDestroyTimer >= 2) {
final HitResult hitResult = gameRenderer.hitResult();
if (!hitResult.missed() &&
glfw.getMouseButton(window, GLFW.MOUSE_BUTTON_LEFT) == GLFW.PRESS) {
world.setBlockType(hitResult.x(), hitResult.y(), hitResult.z(), BlockTypes.AIR);
blockDestroyTimer = 0;
}
}
if (blockPlaceTimer >= 2) {
final HitResult hitResult = gameRenderer.hitResult();
if (!hitResult.missed() &&
glfw.getMouseButton(window, GLFW.MOUSE_BUTTON_RIGHT) == GLFW.PRESS) {
final Direction face = hitResult.face();
final BlockType type = hotBar[hotBarSelection];
if (!type.air()) {
world.setBlockType(
hitResult.x() + face.axisX(),
hitResult.y() + face.axisY(),
hitResult.z() + face.axisZ(),
type
);
if (world != null) {
camera.preUpdate();
if (screen == null) {
final boolean onGround = player.hasComponent(EntityComponents.ON_GROUND);
double speed = onGround ? 0.1 : 0.02;
if (glfw.getKey(window, GLFW.KEY_LEFT_CONTROL) == GLFW.PRESS) speed *= 2.0;
double xo = 0.0;
double zo = 0.0;
if (glfw.getKey(window, GLFW.KEY_W) == GLFW.PRESS) zo -= 1.0;
if (glfw.getKey(window, GLFW.KEY_S) == GLFW.PRESS) zo += 1.0;
if (glfw.getKey(window, GLFW.KEY_A) == GLFW.PRESS) xo -= 1.0;
if (glfw.getKey(window, GLFW.KEY_D) == GLFW.PRESS) xo += 1.0;
if (onGround && glfw.getKey(window, GLFW.KEY_SPACE) == GLFW.PRESS) {
final Vector3d value = player.getComponent(EntityComponents.VELOCITY);
player.setComponent(EntityComponents.VELOCITY, new Vector3d(value.x(), 0.5, value.z()));
}
player.setComponent(EntityComponents.ACCELERATION,
MathUtil.moveRelative(xo, 0.0, zo, player.getComponent(EntityComponents.ROTATION).y(), speed));

if (blockDestroyTimer >= 2) {
final HitResult hitResult = gameRenderer.hitResult();
if (!hitResult.missed() &&
glfw.getMouseButton(window, GLFW.MOUSE_BUTTON_LEFT) == GLFW.PRESS) {
world.setBlockType(hitResult.x(), hitResult.y(), hitResult.z(), BlockTypes.AIR);
blockDestroyTimer = 0;
}
}
blockPlaceTimer = 0;
if (blockPlaceTimer >= 2) {
final HitResult hitResult = gameRenderer.hitResult();
if (!hitResult.missed() &&
glfw.getMouseButton(window, GLFW.MOUSE_BUTTON_RIGHT) == GLFW.PRESS) {
final Direction face = hitResult.face();
final BlockType type = hotBar[hotBarSelection];
if (!type.air()) {
world.setBlockType(
hitResult.x() + face.axisX(),
hitResult.y() + face.axisY(),
hitResult.z() + face.axisZ(),
type
);
}
blockPlaceTimer = 0;
}
}
blockDestroyTimer++;
blockPlaceTimer++;
}
world.tick();
}
blockDestroyTimer++;
blockPlaceTimer++;

gameRenderer.tick();

gameTicks++;
}

private void initGL() {
Expand Down Expand Up @@ -306,6 +336,20 @@ public void close() {
glfw.setErrorCallback(null);
}

public void openScreen(@Nullable Screen screen) {
if (this.screen != null) {
this.screen.dispose();
}
this.screen = screen;
if (screen != null) {
screen.init(framebufferWidth / guiScale, framebufferHeight / guiScale);
}
}

public @Nullable Screen screen() {
return screen;
}

public GLFW glfw() {
return glfw;
}
Expand Down Expand Up @@ -355,10 +399,6 @@ public Entity player() {
return player;
}

public int gameTicks() {
return gameTicks;
}

public int hotBarSelection() {
return hotBarSelection;
}
Expand All @@ -367,6 +407,10 @@ public BlockType[] hotBar() {
return hotBar;
}

public float guiScale() {
return guiScale;
}

public static Freeworld getInstance() {
return INSTANCE;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,22 @@ public final class Camera {
private Vector3d position = Vector3d.ZERO;
private Vector3d lerpPosition = Vector3d.ZERO;
private Vector2d rotation = Vector2d.ZERO;
private Vector3d eyePosition = Vector3d.ZERO;

public void moveToEntity(Entity entity) {
if (EntitySystem.hasAllComponents(entity, EntityComponents.POSITION, EntityComponents.ROTATION)) {
final Vector3d ePos = entity.getComponent(EntityComponents.POSITION);
if (entity.hasComponent(EntityComponents.EYE_HEIGHT)) {
if (entity.hasComponent(EntityComponents.EYE_POSITION)) {
final Vector3d vector3d = entity.getComponent(EntityComponents.EYE_POSITION);
position = new Vector3d(
ePos.x(),
ePos.y() + entity.getComponent(EntityComponents.EYE_HEIGHT),
ePos.y() + vector3d.y(),
ePos.z()
);
eyePosition = vector3d;
} else {
position = ePos;
eyePosition = Vector3d.ZERO;
}
rotation = entity.getComponent(EntityComponents.ROTATION);
}
Expand All @@ -52,7 +56,8 @@ public void updateLerp(double partialTick) {
}

public Matrix4f updateViewMatrix() {
return Matrix4f.rotationX((float) -Math.toRadians(rotation.x()))
return Matrix4f.translation((float) -eyePosition.x(), 0.0f, (float) -eyePosition.z())
.rotateX((float) -Math.toRadians(rotation.x()))
.rotateY((float) -Math.toRadians(rotation.y()))
.translate((float) -lerpPosition.x(), (float) -lerpPosition.y(), (float) -lerpPosition.z());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import freeworld.client.render.model.block.BlockModelPart;
import freeworld.client.render.model.vertex.VertexLayout;
import freeworld.client.render.model.vertex.VertexLayouts;
import freeworld.client.render.texture.Texture;
import freeworld.client.render.screen.Screen;
import freeworld.client.render.texture.TextureAtlas;
import freeworld.client.render.texture.TextureManager;
import freeworld.client.render.world.BlockRenderer;
Expand Down Expand Up @@ -53,9 +53,7 @@ public final class GameRenderer implements GLResource {
private final Freeworld client;
private GLProgram positionColorProgram;
private GLProgram positionColorTexProgram;
private final float guiScale = 2;
private TextureManager textureManager;
private TextureAtlas blockAtlas;
private GuiGraphics guiGraphics;
private HudRenderer hudRenderer;
private BlockRenderer blockRenderer;
Expand All @@ -74,7 +72,6 @@ public void init(GLStateMgr gl) {
gl.clearColor(0.4f, 0.6f, 0.9f, 1.0f);

textureManager = new TextureManager();
textureManager.addTexture(Texture.MISSING, Texture.load(gl, Texture.MISSING));

initBlockAtlas(gl);

Expand Down Expand Up @@ -111,8 +108,7 @@ private void initBlockAtlas(GLStateMgr gl) {
}
}

blockAtlas = TextureAtlas.load(gl, list, 4);

final TextureAtlas blockAtlas = TextureAtlas.load(gl, list, 4);
textureManager.addTexture(TextureManager.BLOCK_ATLAS, blockAtlas);
logAtlas(blockAtlas, TextureManager.BLOCK_ATLAS);
}
Expand Down Expand Up @@ -144,17 +140,16 @@ public void render(GLStateMgr gl, double partialTick) {
gl.setDepthFunc(GL10C.LEQUAL);

try (var _ = RenderSystem.matricesScope()) {
RenderSystem.setProjectionMatrix(_ -> Matrix4f.setPerspective(
(float) Math.toRadians(70.0),
(float) client.framebufferWidth() / client.framebufferHeight(),
0.01f,
1000.0f
));
final Camera camera = client.camera();
final Entity player = client.player();
camera.moveToEntity(player);
camera.updateLerp(partialTick);
RenderSystem.setViewMatrix(_ -> camera.updateViewMatrix());
RenderSystem.setProjectionViewMatrix(_ -> Matrix4f.setPerspective(
(float) Math.toRadians(70.0),
(float) client.framebufferWidth() / client.framebufferHeight(),
0.01f,
1000.0f
), _ -> camera.updateViewMatrix());
RenderSystem.setModelMatrix(_ -> Matrix4f.IDENTITY);

RenderSystem.useProgram(positionColorTexProgram);
Expand All @@ -163,7 +158,7 @@ public void render(GLStateMgr gl, double partialTick) {
final List<ClientChunk> chunks = worldRenderer.renderingChunks(player);
worldRenderer.compileChunks(chunks);

RenderSystem.bindTexture2D(blockAtlas);
RenderSystem.bindTexture2D(textureManager.getTexture(TextureManager.BLOCK_ATLAS));
worldRenderer.renderChunks(gl, chunks);

hitResult = worldRenderer.selectBlock(player);
Expand Down Expand Up @@ -203,15 +198,30 @@ public void render(GLStateMgr gl, double partialTick) {
}

gl.clear(GL10C.DEPTH_BUFFER_BIT);

gl.disableCullFace();
gl.disableDepthTest();
gl.enableBlend();
gl.setBlendFunc(GL10C.SRC_ALPHA, GL10C.ONE_MINUS_SRC_ALPHA);

try (var _ = RenderSystem.matricesScope()) {
hudRenderer.update(client.framebufferWidth(), client.framebufferHeight());
hudRenderer.render(guiGraphics, gl, partialTick);
}

gl.disableCullFace();
gl.disableDepthTest();
gl.enableBlend();
gl.setBlendFunc(GL10C.SRC_ALPHA, GL10C.ONE_MINUS_SRC_ALPHA);
try (var _ = RenderSystem.matricesScope()) {
renderScreen(guiGraphics, gl, partialTick);
}
}

private void renderScreen(GuiGraphics graphics, GLStateMgr gl, double partialTick) {
final Screen screen = client.screen();
if (screen != null) {
screen.render(graphics, gl, partialTick);
}
}

public void tick() {
Expand Down Expand Up @@ -254,8 +264,4 @@ public BlockRenderer blockRenderer() {
public HitResult hitResult() {
return hitResult;
}

public float guiScale() {
return guiScale;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ public void flush(GLStateMgr gl) {
gl.bufferSubData(GL15C.ELEMENT_ARRAY_BUFFER, 0L, indexData);
}
gl.drawElements(drawMode.value(), indexCount, GL10C.UNSIGNED_INT, MemorySegment.NULL);

vertexBuilder.reset();
}

public void begin(GLDrawMode drawMode) {
Expand Down
Loading

0 comments on commit 8957ff6

Please sign in to comment.