Switched to a scene2d stage. Removed all mentions of SpriteBatch

This commit is contained in:
2020-04-12 13:03:32 -06:00
parent 4c9e73b005
commit 515d781390
7 changed files with 41 additions and 57 deletions

BIN
assets/tree.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

View File

@@ -7,7 +7,6 @@ buildscript {
jcenter() jcenter()
} }
dependencies { dependencies {
} }
} }

View File

@@ -1,6 +1,7 @@
import org.gradle.internal.os.OperatingSystem import org.gradle.internal.os.OperatingSystem
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8' [compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
eclipse.project.name = appName + '-core' eclipse.project.name = appName + '-core'
project.ext.lwjglVersion = "3.2.3"
dependencies { dependencies {
api "com.badlogicgames.gdx:gdx:$gdxVersion" api "com.badlogicgames.gdx:gdx:$gdxVersion"
@@ -23,32 +24,4 @@ dependencies {
api "com.github.ykrasik:jaci-libgdx-cli-java:$jaciVersion" api "com.github.ykrasik:jaci-libgdx-cli-java:$jaciVersion"
} }
dependencies { dependencies {
switch (OperatingSystem.current()) {
case OperatingSystem.WINDOWS:
ext.lwjglNatives = "natives-windows"
break
case OperatingSystem.LINUX:
ext.lwjglNatives = "natives-linux"
break
case OperatingSystem.MAC_OS:
ext.lwjglNatives = "natives-macos"
break
}
// Look up which modules and versions of LWJGL are required and add setup the approriate natives.
configurations.compile.resolvedConfiguration.getResolvedArtifacts().forEach {
if (it.moduleVersion.id.group == "org.lwjgl") {
runtime "org.lwjgl:${it.moduleVersion.id.name}:${it.moduleVersion.id.version}:${lwjglNatives}"
}
}
implementation 'com.github.kotlin-graphics.imgui:imgui:v1.75'
implementation 'com.github.kotlin-graphics.imgui:imgui-bgfx:v1.75'
implementation 'com.github.kotlin-graphics.imgui:imgui-core:v1.75'
implementation 'com.github.kotlin-graphics.imgui:imgui-gl:v1.75'
implementation 'com.github.kotlin-graphics.imgui:imgui-glfw:v1.75'
implementation 'com.github.kotlin-graphics.imgui:imgui-jogl:v1.75'
implementation 'com.github.kotlin-graphics.imgui:imgui-openjfx:v1.75'
implementation 'com.github.kotlin-graphics.imgui:imgui-vk:v1.75'
} }

View File

@@ -6,6 +6,7 @@ import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.Batch; import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector3; import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.github.czyzby.kiwi.util.tuple.immutable.Triple; import com.github.czyzby.kiwi.util.tuple.immutable.Triple;
import Collector.Character.InputController; import Collector.Character.InputController;
import Collector.Character.Mouse; import Collector.Character.Mouse;
@@ -22,11 +23,11 @@ public class WorldRenderer implements Restrictions {
this.mouse = mouse; this.mouse = mouse;
} }
public void drawWorld(Batch batch, int layer) { public void drawWorld(Stage stage, int layer) {
for (Iterator<Triple<Integer, Integer, Integer>> iterator = Chunks.loadedChunks.keySet().iterator(); iterator.hasNext(); ) { for (Iterator<Triple<Integer, Integer, Integer>> iterator = Chunks.loadedChunks.keySet().iterator(); iterator.hasNext(); ) {
Triple<Integer, Integer, Integer> chunkpair = iterator.next(); Triple<Integer, Integer, Integer> chunkpair = iterator.next();
if (chunkpair.getThird() == layer) { if (chunkpair.getThird() == layer) {
batch.draw( stage.getBatch().draw(
BlockMaterials.textures.get(Chunks.loadedChunks.get(chunkpair).getName()), BlockMaterials.textures.get(Chunks.loadedChunks.get(chunkpair).getName()),
chunkpair.getFirst() << TILE_SHIFT, chunkpair.getFirst() << TILE_SHIFT,
chunkpair.getSecond() << TILE_SHIFT chunkpair.getSecond() << TILE_SHIFT
@@ -35,21 +36,21 @@ public class WorldRenderer implements Restrictions {
} }
} }
private void mouseCrosshair(SpriteBatch batch) { private void mouseCrosshair(Stage stage) {
Vector3 mousePos = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0); Vector3 mousePos = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
Main.cam.unproject(mousePos); Main.cam.unproject(mousePos);
int x = mouse.getSelectedX(mousePos); int x = mouse.getSelectedX(mousePos);
int y = mouse.getSelectedY(mousePos); int y = mouse.getSelectedY(mousePos);
batch.draw(mouse.getCrosshair(), x, y); stage.getBatch().draw(mouse.getCrosshair(), x, y);
} }
public void render(SpriteBatch batch, float timeSinceLastUpdate) { public void render(Stage stage, float timeSinceLastUpdate) {
//Higher means draws in a lower layer //Higher means draws in a lower layer
drawWorld(batch,0); drawWorld(stage,0);
batch.draw(Player.getAnimation().getKeyFrame(timeSinceLastUpdate, true), Player.x, Player.y,TILE_SIZE,TILE_SIZE); stage.getBatch().draw(Player.getAnimation().getKeyFrame(timeSinceLastUpdate, true), Player.x, Player.y,TILE_SIZE,TILE_SIZE);
drawWorld(batch,1); drawWorld(stage,1);
mouseCrosshair(batch); mouseCrosshair(stage);
inputController.handleInput(); inputController.handleInput();
batch.setProjectionMatrix(Main.cam.combined); stage.getBatch().setProjectionMatrix(stage.getCamera().combined);
} }
} }

View File

@@ -6,23 +6,23 @@ import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL30; import com.badlogic.gdx.graphics.GL30;
import com.badlogic.gdx.graphics.OrthographicCamera; import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.utils.viewport.FitViewport; import com.badlogic.gdx.utils.viewport.FitViewport;
import Collector.Character.InputController; import Collector.Character.InputController;
import Collector.Character.Mouse; import Collector.Character.Mouse;
import Collector.Character.Player; import Collector.Character.Player;
import Collector.UI.GUI; import Collector.UI.GUI;
import imgui.ImGui;
import static Collector.Restrictions.*; import static Collector.Restrictions.*;
public class Main extends ApplicationAdapter { public class Main extends ApplicationAdapter {
private Stage stage;
private SpriteBatch batch; private SpriteBatch batch;
private WorldRenderer worldRenderer; private WorldRenderer worldRenderer;
private GUI gui; private GUI gui;
private FitViewport fitViewport; private FitViewport fitViewport;
public static float delta = 0; public static float chunkLoadingTime = 0;
float timeSinceLastUpdate = 0f; //accumulator float playerAnimationTime = 0f; //accumulator
public static OrthographicCamera cam; public static OrthographicCamera cam;
private InputController inputController; private InputController inputController;
private Mouse mouse; private Mouse mouse;
@@ -40,6 +40,7 @@ public class Main extends ApplicationAdapter {
inputController = new InputController(player,mouse); inputController = new InputController(player,mouse);
worldRenderer = new WorldRenderer(inputController,mouse); worldRenderer = new WorldRenderer(inputController,mouse);
batch = new SpriteBatch(); batch = new SpriteBatch();
stage = new Stage(fitViewport,batch);
BlockMaterials.create(); BlockMaterials.create();
Chunks.create(); Chunks.create();
@@ -48,30 +49,41 @@ public class Main extends ApplicationAdapter {
cam.zoom = 25f; cam.zoom = 25f;
} }
@Override
public void resize(int width, int height) {
stage.getViewport().update(width, height, true);
}
@Override @Override
public void render () { public void render () {
float deltaTime = Gdx.graphics.getDeltaTime();
Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT); Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
timeSinceLastUpdate += Gdx.graphics.getDeltaTime(); //Accumulate delta time playerAnimationTime += deltaTime;
batch.begin(); chunkLoadingTime += deltaTime;
worldRenderer.render(batch,timeSinceLastUpdate);
delta += Gdx.graphics.getDeltaTime(); stage.act(deltaTime);
if (delta > RENDER_TIME) { stage.getBatch().begin();
delta -= RENDER_TIME; worldRenderer.render(stage,playerAnimationTime);
if (chunkLoadingTime > RENDER_TIME) {
chunkLoadingTime -= RENDER_TIME;
World.loadChunks(); World.loadChunks();
World.unloadChunks(); World.unloadChunks();
} }
cam.update();
stage.getCamera().update();
gui.render(); gui.render();
batch.end(); stage.getBatch().end();
stage.draw();
} }
@Override @Override
public void dispose () { public void dispose () {
gui.dispose(); gui.dispose();
batch.dispose();
player.textureAtlas.dispose(); player.textureAtlas.dispose();
stage.dispose();
} }
} }

View File

@@ -7,16 +7,16 @@ public interface Restrictions {
float VIEWPORT_WIDTH = 16/2f; float VIEWPORT_WIDTH = 16/2f;
int MOVEMENT_SPEED = 16; int MOVEMENT_SPEED = 16;
int FREE_SPEED = 2;
int KEY_DELAY = 0; int KEY_DELAY = 0;
int TILE_SIZE = 16; int TILE_SIZE = 16;
int CHUNK_SIZE = 8; int CHUNK_SIZE = 8;
int SUPER_CHUNK_SIZE = 1; int SUPER_CHUNK_SIZE = 1;
int TILE_SHIFT = 4; int TILE_SHIFT = (int)(Math.log(TILE_SIZE)/Math.log(2));
int CHUNK_SHIFT = 3; int CHUNK_SHIFT = (int)(Math.log(CHUNK_SIZE)/Math.log(2));
//int CHUNK_HEIGHT_SHIFT = 2; //int SUPER_CHUNK_SHIFT = (int)(Math.log(SUPER_CHUNK_SIZE)/Math.log(2));
//int SUPER_CHUNK_SHIFT = 0;
int RENDER_DISTANCE = 3; int RENDER_DISTANCE = 3;
float RENDER_TIME = 1/60f; float RENDER_TIME = 1/60f;

View File

@@ -24,7 +24,6 @@ public class GUI implements Restrictions {
Gdx.input.setInputProcessor(stage); Gdx.input.setInputProcessor(stage);
stage.addActor(label); stage.addActor(label);
} }
public void render() { public void render() {