Skip to content

Commit 49c6ea0

Browse files
committed
Hotbar
1 parent 56c8e76 commit 49c6ea0

File tree

10 files changed

+263
-47
lines changed

10 files changed

+263
-47
lines changed

modules/freeworld.client/src/main/java/freeworld/client/Freeworld.java

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
package freeworld.client;
1212

1313
import freeworld.client.render.GameRenderer;
14+
import freeworld.client.render.RenderSystem;
1415
import freeworld.client.render.gl.GLStateMgr;
1516
import freeworld.client.render.world.HitResult;
1617
import freeworld.client.render.Camera;
@@ -70,6 +71,18 @@ public final class Freeworld implements AutoCloseable {
7071
private int blockDestroyTimer = 0;
7172
private int blockPlaceTimer = 0;
7273
private int hotBarSelection = 0;
74+
private final BlockType[] hotBar = {
75+
BlockTypes.STONE,
76+
BlockTypes.DIRT,
77+
BlockTypes.GRASS_BLOCK,
78+
BlockTypes.AIR,
79+
BlockTypes.AIR,
80+
BlockTypes.AIR,
81+
BlockTypes.AIR,
82+
BlockTypes.AIR,
83+
BlockTypes.AIR,
84+
BlockTypes.AIR
85+
};
7386

7487
private Freeworld() {
7588
this.glfw = GLFW.INSTANCE;
@@ -104,7 +117,8 @@ public void start() {
104117

105118
glfw.setKeyCallback(window, (_, key, scancode, action, mods) -> onKey(key, scancode, action, mods));
106119
glfw.setFramebufferSizeCallback(window, (_, width, height) -> onResize(width, height));
107-
glfw.setCursorPosCallback(window, (_, xpos, ypos) -> onCursorPos(xpos, ypos));
120+
glfw.setCursorPosCallback(window, (_, posX, posY) -> onCursorPos(posX, posY));
121+
glfw.setScrollCallback(window, (_, scrollX, scrollY) -> onScroll(scrollX, scrollY));
108122

109123
final Pair.OfInt framebufferSize = glfw.getFramebufferSize(window);
110124
framebufferWidth = framebufferSize.x();
@@ -143,6 +157,13 @@ private void onKey(int key, int scancode, int action, int mods) {
143157
case GLFW.KEY_1 -> hotBarSelection = 0;
144158
case GLFW.KEY_2 -> hotBarSelection = 1;
145159
case GLFW.KEY_3 -> hotBarSelection = 2;
160+
case GLFW.KEY_4 -> hotBarSelection = 3;
161+
case GLFW.KEY_5 -> hotBarSelection = 4;
162+
case GLFW.KEY_6 -> hotBarSelection = 5;
163+
case GLFW.KEY_7 -> hotBarSelection = 6;
164+
case GLFW.KEY_8 -> hotBarSelection = 7;
165+
case GLFW.KEY_9 -> hotBarSelection = 8;
166+
case GLFW.KEY_0 -> hotBarSelection = 9;
146167
}
147168
}
148169
}
@@ -176,6 +197,19 @@ private void onCursorPos(double x, double y) {
176197
cursorY = y;
177198
}
178199

200+
private void onScroll(double x, double y) {
201+
if (y < 0.0) {
202+
hotBarSelection++;
203+
} else if (y > 0.0) {
204+
hotBarSelection--;
205+
}
206+
if (hotBarSelection > 9) {
207+
hotBarSelection = 0;
208+
} else if (hotBarSelection < 0) {
209+
hotBarSelection = 9;
210+
}
211+
}
212+
179213
private void tick() {
180214
camera.preUpdate();
181215

@@ -207,12 +241,7 @@ private void tick() {
207241
if (!hitResult.missed() &&
208242
glfw.getMouseButton(window, GLFW.MOUSE_BUTTON_RIGHT) == GLFW.PRESS) {
209243
final Direction face = hitResult.face();
210-
final BlockType type = switch (hotBarSelection) {
211-
case 0 -> BlockTypes.STONE;
212-
case 1 -> BlockTypes.DIRT;
213-
case 2 -> BlockTypes.GRASS_BLOCK;
214-
default -> BlockTypes.AIR;
215-
};
244+
final BlockType type = hotBar[hotBarSelection];
216245
if (!type.air()) {
217246
world.setBlockType(
218247
hitResult.x() + face.axisX(),
@@ -233,6 +262,8 @@ private void initGL() {
233262
glFlags = GLLoader.loadFlags(glfw::getProcAddress);
234263
gl = GLLoader.loadContext(MethodHandles.lookup(), glFlags, GLStateMgr.class);
235264

265+
RenderSystem.initialize(gl);
266+
236267
gameRenderer = new GameRenderer(this);
237268
gameRenderer.init(gl);
238269
}
@@ -305,6 +336,10 @@ public int hotBarSelection() {
305336
return hotBarSelection;
306337
}
307338

339+
public BlockType[] hotBar() {
340+
return hotBar;
341+
}
342+
308343
public static Freeworld getInstance() {
309344
return INSTANCE;
310345
}

modules/freeworld.client/src/main/java/freeworld/client/render/GameRenderer.java

Lines changed: 55 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,25 @@
1010

1111
package freeworld.client.render;
1212

13-
import freeworld.client.render.gl.GLDrawMode;
14-
import freeworld.client.render.gl.GLStateMgr;
15-
import freeworld.client.render.texture.TextureRegion;
16-
import freeworld.client.render.world.HitResult;
17-
import freeworld.client.render.world.WorldRenderer;
1813
import freeworld.client.Freeworld;
14+
import freeworld.client.render.gl.GLDrawMode;
1915
import freeworld.client.render.gl.GLProgram;
2016
import freeworld.client.render.gl.GLResource;
17+
import freeworld.client.render.gl.GLStateMgr;
2118
import freeworld.client.render.model.VertexLayout;
2219
import freeworld.client.render.model.VertexLayouts;
2320
import freeworld.client.render.texture.TextureAtlas;
2421
import freeworld.client.render.texture.TextureManager;
22+
import freeworld.client.render.texture.TextureRegion;
2523
import freeworld.client.render.world.BlockRenderer;
24+
import freeworld.client.render.world.HitResult;
25+
import freeworld.client.render.world.WorldRenderer;
2626
import freeworld.client.world.chunk.ClientChunk;
2727
import freeworld.core.Identifier;
2828
import freeworld.core.math.AABBox;
2929
import freeworld.util.Direction;
3030
import freeworld.util.Logging;
31+
import freeworld.world.block.BlockType;
3132
import freeworld.world.entity.Entity;
3233
import org.joml.Matrix4f;
3334
import org.slf4j.Logger;
@@ -46,7 +47,7 @@ public final class GameRenderer implements GLResource {
4647
private final Freeworld client;
4748
private GLProgram positionColorProgram;
4849
private GLProgram positionColorTexProgram;
49-
private final Matrix4f projectionViewMatrix = new Matrix4f();
50+
@Deprecated
5051
private final Matrix4f modelMatrix = new Matrix4f();
5152
public static final Identifier TEX_DIRT = Identifier.ofBuiltin("texture/block/dirt.png");
5253
public static final Identifier TEX_GRASS_BLOCK = Identifier.ofBuiltin("texture/block/grass_block.png");
@@ -112,23 +113,24 @@ public void render(GLStateMgr gl, double partialTick) {
112113
gl.enableDepthTest();
113114
gl.setDepthFunc(GL10C.LEQUAL);
114115
blockAtlas.bind(gl);
115-
projectionViewMatrix.setPerspective(
116+
117+
RenderSystem.pushMatrices();
118+
RenderSystem.setProjectionMatrix(RenderSystem.projectionMatrix().setPerspective(
116119
(float) Math.toRadians(70.0),
117120
(float) client.framebufferWidth() / client.framebufferHeight(),
118121
0.01f,
119122
1000.0f
120-
);
123+
));
121124
final Camera camera = client.camera();
122125
final Entity player = client.player();
123126
camera.moveToEntity(player);
124127
camera.updateLerp(partialTick);
125128
camera.updateViewMatrix();
126-
projectionViewMatrix.mul(camera.viewMatrix());
127-
modelMatrix.identity();
128-
positionColorTexProgram.use(gl);
129-
positionColorTexProgram.getUniform(GLProgram.UNIFORM_PROJECTION_VIEW_MATRIX).set(projectionViewMatrix);
130-
positionColorTexProgram.getUniform(GLProgram.UNIFORM_MODEL_MATRIX).set(modelMatrix);
131-
positionColorTexProgram.uploadUniforms(gl);
129+
RenderSystem.setViewMatrix(camera.viewMatrix());
130+
RenderSystem.setModelMatrix(RenderSystem.modelMatrix().identity());
131+
132+
RenderSystem.bindProgram(positionColorTexProgram);
133+
RenderSystem.updateMatrices();
132134

133135
final List<ClientChunk> chunks = worldRenderer.renderingChunks(player);
134136
worldRenderer.compileChunks(chunks);
@@ -145,10 +147,8 @@ public void render(GLStateMgr gl, double partialTick) {
145147
final float maxZ = (float) box.maxZ();
146148
final float offset = 0.005f;
147149
gl.setTextureBinding2D(0);
148-
positionColorProgram.use(gl);
149-
positionColorProgram.getUniform(GLProgram.UNIFORM_PROJECTION_VIEW_MATRIX).set(projectionViewMatrix);
150-
positionColorProgram.getUniform(GLProgram.UNIFORM_MODEL_MATRIX).set(modelMatrix);
151-
positionColorProgram.uploadUniforms(gl);
150+
RenderSystem.bindProgram(positionColorProgram);
151+
RenderSystem.updateMatrices();
152152
tessellator.begin(GLDrawMode.LINES);
153153
tessellator.color(0, 0, 0);
154154
// -x
@@ -170,6 +170,8 @@ public void render(GLStateMgr gl, double partialTick) {
170170
tessellator.end(gl);
171171
}
172172

173+
RenderSystem.popMatrices();
174+
173175
renderGui(gl, partialTick);
174176
}
175177

@@ -178,8 +180,13 @@ private void renderGui(GLStateMgr gl, double partialTick) {
178180
final int height = client.framebufferHeight();
179181
final float screenWidth = width / guiScale;
180182
final float screenHeight = height / guiScale;
181-
projectionViewMatrix.setOrtho(0.0f, screenWidth, 0.0f, screenHeight, -100.0f, 100.0f);
182-
modelMatrix.translation(screenWidth * 0.5f, screenHeight * 0.5f, 0.0f);
183+
184+
RenderSystem.pushMatrices();
185+
186+
RenderSystem.setProjectionMatrix(RenderSystem.projectionMatrix()
187+
.setOrtho(0.0f, screenWidth, 0.0f, screenHeight, -300.0f, 300.0f));
188+
RenderSystem.setModelMatrix(RenderSystem.modelMatrix()
189+
.translation(screenWidth * 0.5f, screenHeight * 0.5f, 0.0f));
183190

184191
gl.clear(GL10C.DEPTH_BUFFER_BIT);
185192

@@ -188,10 +195,8 @@ private void renderGui(GLStateMgr gl, double partialTick) {
188195
gl.disableCullFace();
189196
gl.disableDepthTest();
190197
guiAtlas.bind(gl);
191-
positionColorTexProgram.use(gl);
192-
positionColorTexProgram.getUniform(GLProgram.UNIFORM_PROJECTION_VIEW_MATRIX).set(projectionViewMatrix);
193-
positionColorTexProgram.getUniform(GLProgram.UNIFORM_MODEL_MATRIX).set(modelMatrix);
194-
positionColorTexProgram.uploadUniforms(gl);
198+
RenderSystem.bindProgram(positionColorTexProgram);
199+
RenderSystem.updateMatrices();
195200
tessellator.begin(GLDrawMode.TRIANGLES);
196201
renderCrossing();
197202
tessellator.end(gl);
@@ -201,6 +206,12 @@ private void renderGui(GLStateMgr gl, double partialTick) {
201206
renderHotBar(screenHeight);
202207
renderHotBarSelected(screenHeight);
203208
tessellator.end(gl);
209+
210+
gl.enableDepthTest();
211+
blockAtlas.bind(gl);
212+
renderHotBarItems(gl, screenHeight);
213+
214+
RenderSystem.popMatrices();
204215
}
205216

206217
private void renderGuiSprite(Identifier identifier, float x, float y, float anchorX, float anchorY) {
@@ -228,11 +239,29 @@ private void renderCrossing() {
228239
}
229240

230241
private void renderHotBar(float screenHeight) {
231-
renderGuiSprite(TEX_HOT_BAR, 0.0f, -screenHeight * 0.5f, 0.5f, 0.0f);
242+
renderGuiSprite(TEX_HOT_BAR, 0.0f, -screenHeight * 0.5f + 1, 0.5f, 0.0f);
232243
}
233244

234245
private void renderHotBarSelected(float screenHeight) {
235-
renderGuiSprite(TEX_HOT_BAR_SELECTED, (client.hotBarSelection() - 5) * 22.0f - 2.0f, -screenHeight * 0.5f, 0.0f, 0.0f);
246+
renderGuiSprite(TEX_HOT_BAR_SELECTED, (client.hotBarSelection() - 5) * 20.0f - 2.0f, -screenHeight * 0.5f, 0.0f, 0.0f);
247+
}
248+
249+
private void renderHotBarItems(GLStateMgr gl, float screenHeight) {
250+
int i = 0;
251+
for (BlockType blockType : client.hotBar()) {
252+
RenderSystem.modelMatrix().pushMatrix();
253+
RenderSystem.setModelMatrix(RenderSystem.modelMatrix()
254+
.translate((i - 5) * 20 + 3, 0, 0)
255+
.translate(0, -screenHeight * 0.5f + 8, 100)
256+
.rotateX((float) Math.toRadians(30.0))
257+
.rotateY((float) Math.toRadians(45.0))
258+
.scale(10));
259+
tessellator.begin(GLDrawMode.TRIANGLES);
260+
blockRenderer.renderBlock(tessellator, blockType, 0, 0, 0);
261+
tessellator.end(gl);
262+
i++;
263+
RenderSystem.modelMatrix().popMatrix();
264+
}
236265
}
237266

238267
@Override
@@ -268,14 +297,6 @@ public BlockRenderer blockRenderer() {
268297
return blockRenderer;
269298
}
270299

271-
public Matrix4f projectionViewMatrix() {
272-
return projectionViewMatrix;
273-
}
274-
275-
public Matrix4f modelMatrix() {
276-
return modelMatrix;
277-
}
278-
279300
public HitResult hitResult() {
280301
return hitResult;
281302
}

0 commit comments

Comments
 (0)