Skip to content

Commit

Permalink
Crosshair
Browse files Browse the repository at this point in the history
  • Loading branch information
minecraft8997 committed Nov 29, 2023
1 parent 7858a61 commit bae7f70
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 7 deletions.
2 changes: 1 addition & 1 deletion core/src/ru/mclord/classic/Chunk.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void initGraphics() {

int realX = getX(this);
int realZ = getZ(this);
modelCache = new ModelCache();
modelCache = new ModelCache(new ModelCache.Sorter(), new ModelCache.TightMeshPool());
modelCache.begin();
for (int x = realX; x < realX + CHUNK_SIZE; x++) {
for (int y = 0; y < level.sizeY; y++) {
Expand Down
21 changes: 21 additions & 0 deletions core/src/ru/mclord/classic/Helper.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ru.mclord.classic;

import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.VertexAttributes;
import com.badlogic.gdx.graphics.g3d.Material;
Expand Down Expand Up @@ -155,6 +156,26 @@ public static float distanceSquared(Chunk chunk, float pointX, float pointZ) {
return dx * dx + dz * dz;
}

public static Texture generateCrosshairTexture(int screenWidth, int screenHeight) {
int size = Math.max(screenWidth / 40, screenHeight / 40);
if (size % 2 != 0) size++;

int half = size / 2;
Pixmap pixmap = new Pixmap(size, size, Pixmap.Format.RGBA8888);
for (int i = 0; i < size; i++) {
pixmap.drawPixel(half, i, 0xFFFFFFFF);
pixmap.drawPixel(half + 1, i, 0xFFFFFFFF);
}
for (int i = 0; i < size; i++) {
pixmap.drawPixel(i, half, 0xFFFFFFFF);
pixmap.drawPixel(i, half + 1, 0xFFFFFFFF);
}
Texture result = new Texture(pixmap);
pixmap.dispose();

return result;
}

public static String getStacktrace(Throwable t) {
StringWriter writer0 = new StringWriter();
PrintWriter writer = new PrintWriter(writer0);
Expand Down
18 changes: 14 additions & 4 deletions core/src/ru/mclord/classic/InGameScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.PerspectiveCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g3d.Environment;
Expand All @@ -14,6 +15,7 @@ public class InGameScreen implements Screen {

private BitmapFont font;
private SpriteBatch spriteBatch;
private Texture crosshairTexture;
private ModelBatch modelBatch;
private McLordFirstPersonCameraController cameraController;
private Level level;
Expand Down Expand Up @@ -57,12 +59,12 @@ public void show() {
environment = null;
}

Player player = McLordClassic.getPlayer();
camera = new PerspectiveCamera(
fov, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
// camera.position.set(player.spawnLocation.x, player.spawnLocation.y, player.spawnLocation.z);
// camera.position.set(player.spawnLocation.x,
// player.spawnLocation.y, player.spawnLocation.z);
camera.position.set(-1.0f, -1.0f, -1.0f);
camera.near = 0.35f; // todo might be not the best value
camera.near = 0.35f;
camera.far = 1000000000.0f;
camera.update();

Expand Down Expand Up @@ -90,11 +92,18 @@ public void render(float delta) {
}
level.render(modelBatch, environment);
modelBatch.end();

spriteBatch.begin();
int size = crosshairTexture.getWidth();
spriteBatch.draw(crosshairTexture, Gdx.graphics.getWidth() / 2.0f -
size / 2.0f, Gdx.graphics.getHeight() / 2.0f - size / 2.0f);
spriteBatch.end();
}

@Override
public void resize(int width, int height) {

Helper.dispose(crosshairTexture);
crosshairTexture = Helper.generateCrosshairTexture(width, height);
}

@Override
Expand All @@ -116,6 +125,7 @@ public void hide() {
public void dispose() {
Helper.dispose(font); font = null;
Helper.dispose(spriteBatch); spriteBatch = null;
Helper.dispose(crosshairTexture); crosshairTexture = null;
Helper.dispose(modelBatch); modelBatch = null;
Helper.dispose(level); level = null;
cameraController = null;
Expand Down
4 changes: 2 additions & 2 deletions core/src/ru/mclord/classic/McLordClassic.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ public enum GameStage {

public static final boolean DEBUG = true;
public static final String APP_NAME = "McLordClassic";
public static final String VERSION = "0.1.2";
public static final int VERSION_CODE = 2;
public static final String VERSION = "0.1.4";
public static final int VERSION_CODE = 4;

private static final McLordClassic INSTANCE = new McLordClassic();

Expand Down
24 changes: 24 additions & 0 deletions core/src/ru/mclord/classic/Pair.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ru.mclord.classic;

import java.util.Objects;

public class Pair<A, B> {
private final A first;
private final B second;
Expand All @@ -20,4 +22,26 @@ public A getFirst() {
public B getSecond() {
return second;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Pair<?, ?> pair = (Pair<?, ?>) o;

return Objects.equals(first, pair.first) && Objects.equals(second, pair.second);
}

@Override
public int hashCode() {
return Objects.hash(first, second);
}

@Override
public String toString() {
return "Pair{" +
"first=" + first +
", second=" + second +
'}';
}
}

0 comments on commit bae7f70

Please sign in to comment.