diff --git a/src/main/java/com/github/halotroop/litecraft/Litecraft.java b/src/main/java/com/github/halotroop/litecraft/Litecraft.java index bdf900b..c467086 100644 --- a/src/main/java/com/github/halotroop/litecraft/Litecraft.java +++ b/src/main/java/com/github/halotroop/litecraft/Litecraft.java @@ -18,9 +18,10 @@ import com.github.hydos.ginger.engine.common.info.RenderAPI; import com.github.hydos.ginger.engine.common.io.Window; import com.github.hydos.ginger.engine.common.obj.ModelLoader; import com.github.hydos.ginger.engine.opengl.api.*; +import com.github.hydos.ginger.engine.opengl.postprocessing.PostProcessing; import com.github.hydos.ginger.engine.opengl.render.MasterRenderer; import com.github.hydos.ginger.engine.opengl.render.models.TexturedModel; -import com.github.hydos.ginger.engine.opengl.utils.GlLoader; +import com.github.hydos.ginger.engine.opengl.utils.GLLoader; import tk.valoeghese.gateways.client.io.*; @@ -30,14 +31,10 @@ public class Litecraft extends Game private World world; private LitecraftSave save; private GingerGL engine; - public PlayerEntity playerEntity; - private Camera camera; public int fps, ups, tps; public Vector4i dbgStats = new Vector4i(); private long frameTimer; - public int threadWaitlist = 0; - public Litecraft() { Litecraft.INSTANCE = this; @@ -61,7 +58,7 @@ public class Litecraft extends Game System.out.println("Saving chunks..."); long time = System.currentTimeMillis(); this.world.unloadAllChunks(); - this.getSave().saveGlobalData(this.world.getSeed(), this.playerEntity); + this.getSave().saveGlobalData(this.world.getSeed(), ((PlayerEntity) this.player)); System.out.println("Saved world in " + (System.currentTimeMillis() - time) + " milliseconds"); } engine.cleanup(); @@ -80,7 +77,7 @@ public class Litecraft extends Game // Render shadows GingerRegister.getInstance().masterRenderer.renderShadowMap(data.entities, data.lights.get(0)); // If there's a world, render it! - if (this.world != null) this.engine.renderWorld(this); + if (this.world != null) renderWorld(this); // Render any overlays (GUIs, HUDs) this.engine.renderOverlays(this); // Put what's stored in the inactive framebuffer on the screen @@ -90,16 +87,27 @@ public class Litecraft extends Game // Updates the debug stats once per real-time second, regardless of how many frames have been rendered private void updateDebugStats() { - this.dbgStats.set(fps, ups, tps, threadWaitlist); + this.dbgStats.set(fps, ups, tps, 0); this.fps=0; this.ups=0; this.tps=0; this.frameTimer += 1000; } + public void renderWorld(Game game) + { + GameData data = game.data; + GingerUtils.preRenderScene(engine.getRegistry().masterRenderer); + engine.contrastFbo.bindFBO(); + engine.getRegistry().masterRenderer.renderScene(data.entities, data.normalMapEntities, data.lights, data.camera, data.clippingPlane); + engine.contrastFbo.unbindFBO(); + PostProcessing.doPostProcessing(engine.contrastFbo.colorTexture); + if (data.handleGuis) engine.renderOverlays(game); + } + public void update() { - Input.invokeAllListeners(); + ups += 1; } private void setupConstants() @@ -124,17 +132,17 @@ public class Litecraft extends Game //Set the player model TexturedModel playerModel = ModelLoader.loadGenericCube("block/cubes/stone/brick/stonebrick.png"); Light sun = new Light(new Vector3f(0, 105, 0), new Vector3f(0.9765625f, 0.98828125f, 0.05859375f), new Vector3f(0.002f, 0.002f, 0.002f)); - FontType font = new FontType(GlLoader.loadFontAtlas("candara.png"), "candara.fnt"); + FontType font = new FontType(GLLoader.loadFontAtlas("candara.png"), "candara.fnt"); this.engine = new GingerGL(); - this.playerEntity = new PlayerEntity(playerModel, new Vector3f(0, 0, -3), 0, 180f, 0, new Vector3f(0.2f, 0.2f, 0.2f)); - this.camera = new FirstPersonCamera(playerEntity); - this.playerEntity.setVisible(false); - this.data = new GameData(this.playerEntity, this.camera, 20); + this.player = new PlayerEntity(playerModel, new Vector3f(0, 0, -3), 0, 180f, 0, new Vector3f(0.2f, 0.2f, 0.2f)); + this.camera = new FirstPersonCamera(player); + this.player.setVisible(false); + this.data = new GameData(this.player, this.camera, 20); this.data.handleGuis = false; this.engine.setup(new MasterRenderer(this.camera), INSTANCE); this.engine.setGlobalFont(font); this.data.lights.add(sun); - this.data.entities.add(this.playerEntity); + this.data.entities.add(this.player); } } @@ -143,12 +151,12 @@ public class Litecraft extends Game Input.addPressCallback(Keybind.EXIT, this::exit); Input.addInitialPressCallback(Keybind.FULLSCREEN, Window::fullscreen); Input.addInitialPressCallback(Keybind.WIREFRAME, GingerRegister.getInstance()::toggleWireframe); - Input.addPressCallback(Keybind.MOVE_FORWARD, () -> this.playerEntity.move(RelativeDirection.FORWARD)); - Input.addPressCallback(Keybind.MOVE_BACKWARD, () -> this.playerEntity.move(RelativeDirection.BACKWARD)); - Input.addPressCallback(Keybind.STRAFE_LEFT, () -> this.playerEntity.move(RelativeDirection.LEFT)); - Input.addPressCallback(Keybind.STRAFE_RIGHT, () -> this.playerEntity.move(RelativeDirection.RIGHT)); - Input.addPressCallback(Keybind.FLY_UP, () -> this.playerEntity.move(RelativeDirection.UP)); - Input.addPressCallback(Keybind.FLY_DOWN, () -> this.playerEntity.move(RelativeDirection.DOWN)); + Input.addPressCallback(Keybind.MOVE_FORWARD, () -> ((PlayerEntity) this.player).move(RelativeDirection.FORWARD)); + Input.addPressCallback(Keybind.MOVE_BACKWARD, () -> ((PlayerEntity) this.player).move(RelativeDirection.BACKWARD)); + Input.addPressCallback(Keybind.STRAFE_LEFT, () -> ((PlayerEntity) this.player).move(RelativeDirection.LEFT)); + Input.addPressCallback(Keybind.STRAFE_RIGHT, () -> ((PlayerEntity) this.player).move(RelativeDirection.RIGHT)); + Input.addPressCallback(Keybind.FLY_UP, () -> ((PlayerEntity) this.player).move(RelativeDirection.UP)); + Input.addPressCallback(Keybind.FLY_DOWN, () -> ((PlayerEntity) this.player).move(RelativeDirection.DOWN)); } /** @@ -158,13 +166,14 @@ public class Litecraft extends Game @Override public void tick() { + tps += 1; // Open the title screen if it's not already open. - if (GingerRegister.getInstance().currentScreen == null && world == null) - engine.openScreen(new TitleScreen()); + if (GingerRegister.getInstance().currentScreen == null && world == null) engine.openScreen(new TitleScreen()); - if (data.playerEntity != null && data.camera != null) + if (this.player instanceof PlayerEntity && camera != null) { - data.playerEntity.updateMovement(); + Input.invokeAllListeners(); + ((PlayerEntity) this.player).updateMovement(); data.camera.updateMovement(); } } @@ -187,4 +196,10 @@ public class Litecraft extends Game public void setSave(LitecraftSave save) { this.save = save; } + + @Override + public void renderScene() + { + world.render(GingerRegister.getInstance().masterRenderer.blockRenderer); + } } \ No newline at end of file diff --git a/src/main/java/com/github/hydos/ginger/Starter.java b/src/main/java/com/github/halotroop/litecraft/Starter.java similarity index 81% rename from src/main/java/com/github/hydos/ginger/Starter.java rename to src/main/java/com/github/halotroop/litecraft/Starter.java index ad5694c..6d76696 100644 --- a/src/main/java/com/github/hydos/ginger/Starter.java +++ b/src/main/java/com/github/halotroop/litecraft/Starter.java @@ -1,10 +1,8 @@ -package com.github.hydos.ginger; +package com.github.halotroop.litecraft; import org.lwjgl.Version; import org.lwjgl.glfw.GLFW; -import com.github.halotroop.litecraft.Litecraft; - public class Starter { // private static final boolean usingEclipse = false; diff --git a/src/main/java/com/github/halotroop/litecraft/world/block/BlockRenderer.java b/src/main/java/com/github/halotroop/litecraft/render/BlockRenderer.java similarity index 94% rename from src/main/java/com/github/halotroop/litecraft/world/block/BlockRenderer.java rename to src/main/java/com/github/halotroop/litecraft/render/BlockRenderer.java index ed9f30e..4146f7c 100644 --- a/src/main/java/com/github/halotroop/litecraft/world/block/BlockRenderer.java +++ b/src/main/java/com/github/halotroop/litecraft/render/BlockRenderer.java @@ -1,4 +1,4 @@ -package com.github.halotroop.litecraft.world.block; +package com.github.halotroop.litecraft.render; import org.joml.Matrix4f; import org.lwjgl.opengl.*; @@ -12,7 +12,6 @@ import com.github.hydos.ginger.engine.common.math.Maths; import com.github.hydos.ginger.engine.opengl.render.Renderer; import com.github.hydos.ginger.engine.opengl.render.models.TexturedModel; import com.github.hydos.ginger.engine.opengl.render.shaders.StaticShader; -import com.github.hydos.ginger.engine.opengl.utils.GlLoader; public class BlockRenderer extends Renderer implements WorldGenConstants { @@ -25,7 +24,7 @@ public class BlockRenderer extends Renderer implements WorldGenConstants shader.start(); shader.loadProjectionMatrix(projectionMatrix); shader.stop(); - this.atlasID = GlLoader.createBlockAtlas(); + this.atlasID = VoxelLoader.createBlockAtlas(); } private void prepBlockInstance(RenderObject entity) diff --git a/src/main/java/com/github/halotroop/litecraft/render/VoxelLoader.java b/src/main/java/com/github/halotroop/litecraft/render/VoxelLoader.java new file mode 100644 index 0000000..0a814f7 --- /dev/null +++ b/src/main/java/com/github/halotroop/litecraft/render/VoxelLoader.java @@ -0,0 +1,51 @@ +package com.github.halotroop.litecraft.render; + +import java.nio.ByteBuffer; + +import org.lwjgl.opengl.*; + +import com.github.halotroop.litecraft.types.block.*; +import com.github.hydos.ginger.engine.opengl.utils.GLLoader; + +public class VoxelLoader extends GLLoader +{ + public static int createBlockAtlas() + { + int width = 16; + int height = 16; + //Prepare the atlas texture and gen it + int atlasId = GL40.glGenTextures(); + //Bind it to openGL + GL40.glBindTexture(GL40.GL_TEXTURE_2D, atlasId); + //Apply the settings for the texture + GL40.glTexParameteri(GL40.GL_TEXTURE_2D, GL40.GL_TEXTURE_MIN_FILTER, GL40.GL_NEAREST); + GL40.glTexParameteri(GL40.GL_TEXTURE_2D, GL40.GL_TEXTURE_MAG_FILTER, GL40.GL_NEAREST); + //Fill the image with blank image data + GL40.glTexImage2D(GL40.GL_TEXTURE_2D, 0, GL11.GL_RGBA, width*2, height*2, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, (ByteBuffer) null); + + long maxX = Math.round(Math.sqrt(Blocks.blocks.size())); + int currentX = 0; + int currentY = 0; + for(Block block: Blocks.blocks) { + //just in case + + if(!block.texture.equals("DONTLOAD")) { + System.out.println(block.texture); + block.updateBlockModelData(); + if(currentX > maxX) { + currentX = 0; + currentY--; + } + GL11.glTexSubImage2D(GL11.GL_TEXTURE_2D, 0, + currentX*width, currentY*height, + width, height, + GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, + block.model.getTexture().getTexture().getImage() + ); + currentX++; + } + + } + return atlasId; + } +} diff --git a/src/main/java/com/github/halotroop/litecraft/screens/IngameHUD.java b/src/main/java/com/github/halotroop/litecraft/screens/IngameHUD.java index 7353188..19aa89b 100644 --- a/src/main/java/com/github/halotroop/litecraft/screens/IngameHUD.java +++ b/src/main/java/com/github/halotroop/litecraft/screens/IngameHUD.java @@ -37,7 +37,7 @@ public class IngameHUD extends Screen long freeMemory = Runtime.getRuntime().freeMemory(); long usedMemory = (totalMemory - freeMemory) / 1024 / 1024; Vector4i dbg = litecraft.dbgStats; - Vector3f position = GingerRegister.getInstance().game.data.playerEntity.getPosition(); + Vector3f position = GingerRegister.getInstance().game.data.playerObject.getPosition(); debugText.setText("FPS: " + dbg.x() + " UPS: " + dbg.y() + " TPS: " + dbg.z() + " TWL: " + dbg.w() + " Mem: " + usedMemory + "MB"); positionText.setText("Position: " + (int) position.x() + ", " + (int) position.y() + ", "+ (int) position.z() ); } diff --git a/src/main/java/com/github/halotroop/litecraft/screens/TitleScreen.java b/src/main/java/com/github/halotroop/litecraft/screens/TitleScreen.java index b92c66b..ee746c6 100644 --- a/src/main/java/com/github/halotroop/litecraft/screens/TitleScreen.java +++ b/src/main/java/com/github/halotroop/litecraft/screens/TitleScreen.java @@ -20,16 +20,16 @@ import com.github.hydos.ginger.engine.opengl.api.GingerGL; public class TitleScreen extends Screen { private GUIText debugText; - private GingerGL ginger3D = GingerGL.getInstance(); + private GingerGL engine = GingerGL.getInstance(); private TextureButton playButton; private Litecraft litecraft = Litecraft.getInstance(); public TitleScreen() { elements = new ArrayList(); - playButton = ginger3D.registerButton("/textures/guis/playbutton.png", new Vector2f(0, 0), new Vector2f(0.25f, 0.1f)); + playButton = engine.registerButton("/textures/guis/playbutton.png", new Vector2f(0, 0), new Vector2f(0.25f, 0.1f)); playButton.show(Litecraft.getInstance().data.guis); - debugText = ginger3D.registerText("Loading...", 2, new Vector2f(0, 0), 1f, true, "debugInfo"); + debugText = engine.registerText("Loading...", 2, new Vector2f(0, 0), 1f, true, "debugInfo"); debugText.setBorderWidth(0.5f); } @@ -51,11 +51,11 @@ public class TitleScreen extends Screen { Litecraft.getInstance().setSave(new LitecraftSave("SegregatedOrdinalData", false)); Litecraft.getInstance().changeWorld(Litecraft.getInstance().getSave().getWorldOrCreate(Dimensions.OVERWORLD)); - ginger3D.setGingerPlayer(Litecraft.getInstance().getWorld().playerEntity); + engine.setGingerPlayer(Litecraft.getInstance().getWorld().playerEntity); } if (Litecraft.getInstance().getWorld() != null) { - ginger3D.openScreen(new IngameHUD()); + engine.openScreen(new IngameHUD()); this.cleanup(); } //TODO: add world creation gui so it takes u to world creation place diff --git a/src/main/java/com/github/halotroop/litecraft/types/entity/PlayerEntity.java b/src/main/java/com/github/halotroop/litecraft/types/entity/PlayerEntity.java index 39f41ff..c9bbbf8 100644 --- a/src/main/java/com/github/halotroop/litecraft/types/entity/PlayerEntity.java +++ b/src/main/java/com/github/halotroop/litecraft/types/entity/PlayerEntity.java @@ -30,8 +30,8 @@ public class PlayerEntity extends Entity implements WorldGenConstants float ry = (float) Math.toRadians(GingerRegister.getInstance().game.data.camera.getYaw()); switch (direction) { - case FORWARD: default: + case FORWARD: position.z -= Math.cos(ry) * Constants.movementSpeed; position.x += Math.sin(ry) * Constants.movementSpeed; break; diff --git a/src/main/java/com/github/halotroop/litecraft/world/Chunk.java b/src/main/java/com/github/halotroop/litecraft/world/Chunk.java index 3f72d9b..56e55fe 100644 --- a/src/main/java/com/github/halotroop/litecraft/world/Chunk.java +++ b/src/main/java/com/github/halotroop/litecraft/world/Chunk.java @@ -6,8 +6,8 @@ import java.util.function.ToIntFunction; import org.joml.Vector3f; import com.github.halotroop.litecraft.logic.SODSerializable; +import com.github.halotroop.litecraft.render.BlockRenderer; import com.github.halotroop.litecraft.types.block.*; -import com.github.halotroop.litecraft.world.block.BlockRenderer; import com.github.halotroop.litecraft.world.gen.WorldGenConstants; import it.unimi.dsi.fastutil.ints.*; diff --git a/src/main/java/com/github/halotroop/litecraft/world/World.java b/src/main/java/com/github/halotroop/litecraft/world/World.java index d79843c..66be8b3 100644 --- a/src/main/java/com/github/halotroop/litecraft/world/World.java +++ b/src/main/java/com/github/halotroop/litecraft/world/World.java @@ -10,10 +10,10 @@ import java.util.function.LongConsumer; import org.joml.Vector3f; +import com.github.halotroop.litecraft.render.BlockRenderer; import com.github.halotroop.litecraft.save.LitecraftSave; import com.github.halotroop.litecraft.types.block.*; import com.github.halotroop.litecraft.types.entity.PlayerEntity; -import com.github.halotroop.litecraft.world.block.BlockRenderer; import com.github.halotroop.litecraft.world.dimension.Dimension; import com.github.halotroop.litecraft.world.gen.*; import com.github.halotroop.litecraft.world.gen.modifier.WorldModifier; diff --git a/src/main/java/com/github/hydos/ginger/engine/common/api/game/Game.java b/src/main/java/com/github/hydos/ginger/engine/common/api/game/Game.java index 61b5e8f..32da21a 100644 --- a/src/main/java/com/github/hydos/ginger/engine/common/api/game/Game.java +++ b/src/main/java/com/github/hydos/ginger/engine/common/api/game/Game.java @@ -1,8 +1,13 @@ package com.github.hydos.ginger.engine.common.api.game; +import com.github.hydos.ginger.engine.common.cameras.Camera; +import com.github.hydos.ginger.engine.common.elements.objects.RenderObject; + public abstract class Game { public GameData data; + public RenderObject player; // FIXME: @hYdos you know these probably don't need to be here, but the game stops working when they're not. + public Camera camera; public Game() {} @@ -11,6 +16,8 @@ public abstract class Game public abstract void render(); + public abstract void renderScene(); + public abstract void tick(); public abstract void update(); diff --git a/src/main/java/com/github/hydos/ginger/engine/common/api/game/GameData.java b/src/main/java/com/github/hydos/ginger/engine/common/api/game/GameData.java index 57fba5f..de22f1a 100644 --- a/src/main/java/com/github/hydos/ginger/engine/common/api/game/GameData.java +++ b/src/main/java/com/github/hydos/ginger/engine/common/api/game/GameData.java @@ -4,7 +4,6 @@ import java.util.*; import org.joml.Vector4f; -import com.github.halotroop.litecraft.types.entity.PlayerEntity; import com.github.hydos.ginger.engine.common.cameras.Camera; import com.github.hydos.ginger.engine.common.elements.GuiTexture; import com.github.hydos.ginger.engine.common.elements.objects.*; @@ -19,20 +18,20 @@ public class GameData public List entities; public List lights; public List normalMapEntities; - public PlayerEntity playerEntity; + public RenderObject playerObject; public Camera camera; public Vector4f clippingPlane; public boolean handleGuis = true; public int tickSpeed = 20; - public GameData(PlayerEntity playerEntity, Camera camera, int tickSpeed) + public GameData(RenderObject playerEntity, Camera camera, int tickSpeed) { clippingPlane = new Vector4f(0, -1, 0, 100000); guis = new ArrayList(); entities = new ArrayList(); lights = new ArrayList(); normalMapEntities = new ArrayList(); - this.playerEntity = playerEntity; + this.playerObject = playerEntity; this.camera = camera; this.tickSpeed = tickSpeed; } diff --git a/src/main/java/com/github/hydos/ginger/engine/common/cameras/Camera.java b/src/main/java/com/github/hydos/ginger/engine/common/cameras/Camera.java index 6a21737..4f7074e 100644 --- a/src/main/java/com/github/hydos/ginger/engine/common/cameras/Camera.java +++ b/src/main/java/com/github/hydos/ginger/engine/common/cameras/Camera.java @@ -3,7 +3,7 @@ package com.github.hydos.ginger.engine.common.cameras; import org.joml.Vector3f; import org.lwjgl.glfw.*; -import com.github.halotroop.litecraft.types.entity.PlayerEntity; +import com.github.hydos.ginger.engine.common.elements.objects.RenderObject; import com.github.hydos.ginger.engine.common.io.Window; public class Camera @@ -13,15 +13,15 @@ public class Camera private Vector3f position = new Vector3f(0, 0, 0); private float pitch, yaw; private float roll; - public PlayerEntity playerEntity; + public RenderObject player; - public Camera(PlayerEntity playerEntity) - { this.playerEntity = playerEntity; } + public Camera(RenderObject playerEntity) + { this.player = playerEntity; } - public Camera(Vector3f vector3f, PlayerEntity playerEntity) + public Camera(Vector3f vector3f, RenderObject player) { this.position = vector3f; - this.playerEntity = playerEntity; + this.player = player; } private void calculateAngleAroundPlayer() @@ -35,12 +35,12 @@ public class Camera private void calculateCameraPosition(float horizDistance, float verticDistance) { - float theta = playerEntity.getRotY() + angleAroundPlayer; + float theta = player.getRotY() + angleAroundPlayer; float offsetX = (float) (horizDistance * Math.sin(Math.toRadians(theta))); float offsetZ = (float) (horizDistance * Math.cos(Math.toRadians(theta))); - position.x = playerEntity.getPosition().x - offsetX; - position.z = playerEntity.getPosition().z - offsetZ; - position.y = playerEntity.getPosition().y + verticDistance; + position.x = player.getPosition().x - offsetX; + position.z = player.getPosition().z - offsetZ; + position.y = player.getPosition().y + verticDistance; } private float calculateHorizontalDistance() @@ -105,7 +105,7 @@ public class Camera float horizontalDistance = calculateHorizontalDistance(); float verticalDistance = calculateVerticalDistance(); calculateCameraPosition(horizontalDistance, verticalDistance); - this.yaw = 180 - (playerEntity.getRotY() + angleAroundPlayer); + this.yaw = 180 - (player.getRotY() + angleAroundPlayer); } public void setPitch(float pitch) diff --git a/src/main/java/com/github/hydos/ginger/engine/common/cameras/FirstPersonCamera.java b/src/main/java/com/github/hydos/ginger/engine/common/cameras/FirstPersonCamera.java index 645e2d4..90127e2 100644 --- a/src/main/java/com/github/hydos/ginger/engine/common/cameras/FirstPersonCamera.java +++ b/src/main/java/com/github/hydos/ginger/engine/common/cameras/FirstPersonCamera.java @@ -2,7 +2,7 @@ package com.github.hydos.ginger.engine.common.cameras; import org.joml.Vector3f; -import com.github.halotroop.litecraft.types.entity.PlayerEntity; +import com.github.hydos.ginger.engine.common.elements.objects.RenderObject; import com.github.hydos.ginger.engine.common.io.Window; public class FirstPersonCamera extends Camera @@ -11,7 +11,7 @@ public class FirstPersonCamera extends Camera private float pitch, yaw; private float roll; - public FirstPersonCamera(PlayerEntity playerEntity) + public FirstPersonCamera(RenderObject playerEntity) { super(playerEntity); playerEntity.setVisible(false); @@ -36,11 +36,11 @@ public class FirstPersonCamera extends Camera @Override public void updateMovement() { - position.x = playerEntity.getPosition().x; - position.z = playerEntity.getPosition().z; - position.y = playerEntity.getPosition().y; - roll = playerEntity.getRotX(); - yaw = -playerEntity.getRotY() + 180 + Window.getNormalizedMouseCoordinates().x() * 70; - pitch = playerEntity.getRotZ() + -Window.getNormalizedMouseCoordinates().y() * 70; + position.x = player.getPosition().x; + position.z = player.getPosition().z; + position.y = player.getPosition().y; + roll = player.getRotX(); + yaw = -player.getRotY() + 180 + Window.getNormalizedMouseCoordinates().x() * 70; + pitch = player.getRotZ() + -Window.getNormalizedMouseCoordinates().y() * 70; } } diff --git a/src/main/java/com/github/hydos/ginger/engine/common/elements/buttons/TextureButton.java b/src/main/java/com/github/hydos/ginger/engine/common/elements/buttons/TextureButton.java index ea9adb8..fce388a 100644 --- a/src/main/java/com/github/hydos/ginger/engine/common/elements/buttons/TextureButton.java +++ b/src/main/java/com/github/hydos/ginger/engine/common/elements/buttons/TextureButton.java @@ -7,7 +7,7 @@ import org.lwjgl.glfw.GLFW; import com.github.hydos.ginger.engine.common.elements.GuiTexture; import com.github.hydos.ginger.engine.common.io.Window; -import com.github.hydos.ginger.engine.opengl.utils.GlLoader; +import com.github.hydos.ginger.engine.opengl.utils.GLLoader; public class TextureButton { @@ -20,7 +20,7 @@ public class TextureButton public TextureButton(String texture, Vector2f position, Vector2f scale) { resourceLocation = texture; - guiTexture = new GuiTexture(GlLoader.loadTextureDirectly(texture), position, scale); + guiTexture = new GuiTexture(GLLoader.loadTextureDirectly(texture), position, scale); } public void hide(List guiTextureList) diff --git a/src/main/java/com/github/hydos/ginger/engine/common/font/TextMaster.java b/src/main/java/com/github/hydos/ginger/engine/common/font/TextMaster.java index 0aad404..16d2a73 100644 --- a/src/main/java/com/github/hydos/ginger/engine/common/font/TextMaster.java +++ b/src/main/java/com/github/hydos/ginger/engine/common/font/TextMaster.java @@ -4,7 +4,7 @@ import java.util.*; import com.github.hydos.ginger.engine.opengl.api.GingerGL; import com.github.hydos.ginger.engine.opengl.render.renderers.FontRenderer; -import com.github.hydos.ginger.engine.opengl.utils.GlLoader; +import com.github.hydos.ginger.engine.opengl.utils.GLLoader; public class TextMaster { @@ -21,7 +21,7 @@ public class TextMaster { FontType font = text.getFont(); TextMeshData data = font.loadText(text); - int vao = GlLoader.loadToVAO(data.getVertexPositions(), data.getTextureCoords()); + int vao = GLLoader.loadToVAO(data.getVertexPositions(), data.getTextureCoords()); text.setMeshInfo(vao, data.getVertexCount()); List textBatch = texts.get(font); if (textBatch == null) diff --git a/src/main/java/com/github/hydos/ginger/engine/common/obj/ModelLoader.java b/src/main/java/com/github/hydos/ginger/engine/common/obj/ModelLoader.java index d642bd2..7209793 100644 --- a/src/main/java/com/github/hydos/ginger/engine/common/obj/ModelLoader.java +++ b/src/main/java/com/github/hydos/ginger/engine/common/obj/ModelLoader.java @@ -3,20 +3,20 @@ package com.github.hydos.ginger.engine.common.obj; import com.github.hydos.ginger.engine.common.obj.shapes.StaticCube; import com.github.hydos.ginger.engine.opengl.render.models.TexturedModel; import com.github.hydos.ginger.engine.opengl.render.texture.ModelTexture; -import com.github.hydos.ginger.engine.opengl.utils.GlLoader; +import com.github.hydos.ginger.engine.opengl.utils.GLLoader; public class ModelLoader { public static TexturedModel loadGenericCube(String cubeTexture) { Mesh data = StaticCube.getCube(); - TexturedModel tm = new TexturedModel(GlLoader.loadToVAO(data.getVertices(), data.getIndices(), data.getNormals(), data.getTextureCoords()), new ModelTexture(cubeTexture)); + TexturedModel tm = new TexturedModel(GLLoader.loadToVAO(data.getVertices(), data.getIndices(), data.getNormals(), data.getTextureCoords()), new ModelTexture(cubeTexture)); return tm; } public static TexturedModel loadModel(String objPath, String texturePath) { Mesh data = OBJFileLoader.loadModel(objPath); - return new TexturedModel(GlLoader.loadToVAO(data.getVertices(), data.getIndices(), data.getNormals(), data.getTextureCoords()), new ModelTexture(texturePath)); + return new TexturedModel(GLLoader.loadToVAO(data.getVertices(), data.getIndices(), data.getNormals(), data.getTextureCoords()), new ModelTexture(texturePath)); } } diff --git a/src/main/java/com/github/hydos/ginger/engine/common/obj/normals/NormalMappedObjLoader.java b/src/main/java/com/github/hydos/ginger/engine/common/obj/normals/NormalMappedObjLoader.java index 10f5df1..5be4a0e 100644 --- a/src/main/java/com/github/hydos/ginger/engine/common/obj/normals/NormalMappedObjLoader.java +++ b/src/main/java/com/github/hydos/ginger/engine/common/obj/normals/NormalMappedObjLoader.java @@ -6,7 +6,7 @@ import java.util.*; import org.joml.*; import com.github.hydos.ginger.engine.opengl.render.models.RawModel; -import com.github.hydos.ginger.engine.opengl.utils.GlLoader; +import com.github.hydos.ginger.engine.opengl.utils.GLLoader; public class NormalMappedObjLoader { @@ -133,7 +133,7 @@ public class NormalMappedObjLoader float[] normalsArray = new float[vertices.size() * 3]; float[] tangentsArray = new float[vertices.size() * 3]; int[] indicesArray = convertIndicesListToArray(indices); - return GlLoader.loadToVAO(verticesArray, indicesArray, normalsArray, tangentsArray, texturesArray); + return GLLoader.loadToVAO(verticesArray, indicesArray, normalsArray, tangentsArray, texturesArray); } private static VertexNM processVertex(String[] vertex, List vertices, diff --git a/src/main/java/com/github/halotroop/litecraft/logic/Timer.java b/src/main/java/com/github/hydos/ginger/engine/common/util/Timer.java similarity index 80% rename from src/main/java/com/github/halotroop/litecraft/logic/Timer.java rename to src/main/java/com/github/hydos/ginger/engine/common/util/Timer.java index 74847c9..1c16fe1 100644 --- a/src/main/java/com/github/halotroop/litecraft/logic/Timer.java +++ b/src/main/java/com/github/hydos/ginger/engine/common/util/Timer.java @@ -1,26 +1,22 @@ -package com.github.halotroop.litecraft.logic; +package com.github.hydos.ginger.engine.common.util; import java.util.*; -import com.github.hydos.multithreading.GingerThread; - -/** - * @author Jack Wilsdon (Stack Exchange) - * https://codereview.stackexchange.com/questions/111855/ticker-for-game-timing - */ -public class Timer extends GingerThread +/** @author Jack Wilsdon (Stack Exchange) + * https://codereview.stackexchange.com/questions/111855/ticker-for-game-timing */ +public class Timer { - @Override - public void run() { + public void run() + { for (TickListener listener : tickListeners) { listener.onTick(deltaTime); } } - + public interface TickListener { void onTick(float deltaTime); } - + float deltaTime; private double lastTick; private double nextTick; diff --git a/src/main/java/com/github/hydos/ginger/engine/opengl/api/GingerGL.java b/src/main/java/com/github/hydos/ginger/engine/opengl/api/GingerGL.java index fd3b395..04028df 100644 --- a/src/main/java/com/github/hydos/ginger/engine/opengl/api/GingerGL.java +++ b/src/main/java/com/github/hydos/ginger/engine/opengl/api/GingerGL.java @@ -2,20 +2,19 @@ package com.github.hydos.ginger.engine.opengl.api; import org.joml.Vector2f; -import com.github.halotroop.litecraft.Litecraft; -import com.github.halotroop.litecraft.logic.Timer; -import com.github.halotroop.litecraft.logic.Timer.TickListener; -import com.github.halotroop.litecraft.types.entity.PlayerEntity; import com.github.hydos.ginger.engine.common.api.GingerRegister; import com.github.hydos.ginger.engine.common.api.game.*; import com.github.hydos.ginger.engine.common.elements.buttons.TextureButton; +import com.github.hydos.ginger.engine.common.elements.objects.RenderObject; import com.github.hydos.ginger.engine.common.font.*; import com.github.hydos.ginger.engine.common.io.Window; import com.github.hydos.ginger.engine.common.screen.Screen; import com.github.hydos.ginger.engine.common.tools.MousePicker; +import com.github.hydos.ginger.engine.common.util.Timer; +import com.github.hydos.ginger.engine.common.util.Timer.TickListener; import com.github.hydos.ginger.engine.opengl.postprocessing.*; import com.github.hydos.ginger.engine.opengl.render.MasterRenderer; -import com.github.hydos.ginger.engine.opengl.utils.GlLoader; +import com.github.hydos.ginger.engine.opengl.utils.GLLoader; public class GingerGL { @@ -31,8 +30,8 @@ public class GingerGL @Override public void onTick(float deltaTime) { - if (registry.game != null) registry.game.tick(); - if (registry.currentScreen != null) registry.currentScreen.tick(); + if (getRegistry().game != null) getRegistry().game.tick(); + if (getRegistry().currentScreen != null) getRegistry().currentScreen.tick(); }; }; @@ -40,30 +39,30 @@ public class GingerGL { Window.stop(); PostProcessing.cleanUp(); - registry.masterRenderer.cleanUp(); + getRegistry().masterRenderer.cleanUp(); TextMaster.cleanUp(); - GlLoader.cleanUp(); + GLLoader.cleanUp(); } public void openScreen(Screen screen) { - if (registry.currentScreen != null) registry.currentScreen.cleanup(); - registry.currentScreen = screen; + if (getRegistry().currentScreen != null) getRegistry().currentScreen.cleanup(); + getRegistry().currentScreen = screen; } - public void setGingerPlayer(PlayerEntity playerEntity) + public void setGingerPlayer(RenderObject player) { - registry.game.data.entities.remove(Litecraft.getInstance().playerEntity); // remove the old player - registry.game.data.playerEntity = playerEntity; // set all the player variables - Litecraft.getInstance().playerEntity = playerEntity; - Litecraft.getInstance().getCamera().playerEntity = playerEntity; - registry.game.data.entities.add(playerEntity); // add the new player + registry.game.data.entities.remove(registry.game.player); // remove the old player + registry.game.data.playerObject = player; // set all the player variables + registry.game.player = player; + registry.game.camera.player = player; + registry.game.data.entities.add(player); // add the new player } public TextureButton registerButton(String resourceLocation, Vector2f position, Vector2f scale) { TextureButton button = new TextureButton(resourceLocation, position, scale); - registry.registerButton(button); + getRegistry().registerButton(button); return button; } @@ -71,28 +70,17 @@ public class GingerGL { GUIText text = new GUIText(string, textSize, globalFont, position, maxLineLength, false); text.textID = id; - registry.registerText(text); + getRegistry().registerText(text); return text; } public void renderOverlays(Game game) { - registry.masterRenderer.renderGuis(game.data.guis); - if (registry.currentScreen != null) registry.masterRenderer.renderGuis(registry.currentScreen.elements); + getRegistry().masterRenderer.renderGuis(game.data.guis); + if (getRegistry().currentScreen != null) getRegistry().masterRenderer.renderGuis(getRegistry().currentScreen.elements); TextMaster.render(); } - public void renderWorld(Litecraft game) - { - GameData data = game.data; - GingerUtils.preRenderScene(registry.masterRenderer); - contrastFbo.bindFBO(); - registry.masterRenderer.renderScene(data.entities, data.normalMapEntities, data.lights, data.camera, data.clippingPlane, game.getWorld()); - contrastFbo.unbindFBO(); - PostProcessing.doPostProcessing(contrastFbo.colorTexture); - if (data.handleGuis) renderOverlays(game); - } - public void setGlobalFont(FontType font) { this.globalFont = font; } @@ -100,11 +88,11 @@ public class GingerGL { INSTANCE = this; registry = new GingerRegister(); - registry.registerGame(game); + getRegistry().registerGame(game); timer = new Timer(game.data.tickSpeed); timer.addTickListener(gameTickListener); contrastFbo = new Fbo(new ContrastChanger()); - registry.masterRenderer = masterRenderer; + getRegistry().masterRenderer = masterRenderer; picker = new MousePicker(game.data.camera, masterRenderer.getProjectionMatrix()); PostProcessing.init(); } @@ -113,23 +101,27 @@ public class GingerGL { while (!Window.closed()) { - update(Litecraft.getInstance().data); // Run this regardless, (so as fast as possible) - if (timer.tick()) Litecraft.getInstance().tps += 1; // Run this only [ticklimit] times per second (This invokes gameTickListener.onTick!) - if (Window.shouldRender()) registry.game.render(); // Run this only [framelimit] times per second + update(); // Run this regardless, (so as fast as possible) + timer.tick(); // Run this only [ticklimit] times per second (This invokes gameTickListener.onTick!) + if (Window.shouldRender()) getRegistry().game.render(); // Run this only [framelimit] times per second } - registry.game.exit(); + getRegistry().game.exit(); } // Things that should be run as often as possible, without limits - public void update(GameData data) + public void update() { - registry.game.update(); + getRegistry().game.update(); picker.update(); GingerUtils.update(); Window.update(); - Litecraft.getInstance().ups += 1; } public static GingerGL getInstance() { return INSTANCE; } + + public GingerRegister getRegistry() + { + return registry; + } } \ No newline at end of file diff --git a/src/main/java/com/github/hydos/ginger/engine/opengl/postprocessing/PostProcessing.java b/src/main/java/com/github/hydos/ginger/engine/opengl/postprocessing/PostProcessing.java index d4eaa23..35a38a0 100644 --- a/src/main/java/com/github/hydos/ginger/engine/opengl/postprocessing/PostProcessing.java +++ b/src/main/java/com/github/hydos/ginger/engine/opengl/postprocessing/PostProcessing.java @@ -3,7 +3,7 @@ package com.github.hydos.ginger.engine.opengl.postprocessing; import org.lwjgl.opengl.*; import com.github.hydos.ginger.engine.opengl.render.models.RawModel; -import com.github.hydos.ginger.engine.opengl.utils.GlLoader; +import com.github.hydos.ginger.engine.opengl.utils.GLLoader; public class PostProcessing { @@ -33,7 +33,7 @@ public class PostProcessing public static void init() { - quad = GlLoader.loadToVAO(POSITIONS, 2); + quad = GLLoader.loadToVAO(POSITIONS, 2); contrastChanger = new ContrastChanger(); } diff --git a/src/main/java/com/github/hydos/ginger/engine/opengl/render/MasterRenderer.java b/src/main/java/com/github/hydos/ginger/engine/opengl/render/MasterRenderer.java index ebd0801..d23bf99 100644 --- a/src/main/java/com/github/hydos/ginger/engine/opengl/render/MasterRenderer.java +++ b/src/main/java/com/github/hydos/ginger/engine/opengl/render/MasterRenderer.java @@ -6,8 +6,8 @@ import java.util.*; import org.joml.*; import org.lwjgl.opengl.*; -import com.github.halotroop.litecraft.world.World; -import com.github.halotroop.litecraft.world.block.BlockRenderer; +import com.github.halotroop.litecraft.render.BlockRenderer; +import com.github.hydos.ginger.engine.common.api.GingerRegister; import com.github.hydos.ginger.engine.common.cameras.Camera; import com.github.hydos.ginger.engine.common.elements.GuiTexture; import com.github.hydos.ginger.engine.common.elements.objects.*; @@ -156,12 +156,12 @@ public class MasterRenderer normalRenderer.render(normalMapEntities, clipPlane, lights, camera); } - public void renderScene(List entities, List normalEntities, List lights, Camera camera, Vector4f clipPlane, World world) + public void renderScene(List entities, List normalEntities, List lights, Camera camera, Vector4f clipPlane) { prepare(); renderEntities(entities, camera, lights); - world.render(blockRenderer); renderNormalEntities(normalEntities, lights, camera, clipPlane); + GingerRegister.getInstance().game.renderScene(); // skyboxRenderer.render(camera); } diff --git a/src/main/java/com/github/hydos/ginger/engine/opengl/render/renderers/GuiRenderer.java b/src/main/java/com/github/hydos/ginger/engine/opengl/render/renderers/GuiRenderer.java index 4e76b79..1ed0dbd 100644 --- a/src/main/java/com/github/hydos/ginger/engine/opengl/render/renderers/GuiRenderer.java +++ b/src/main/java/com/github/hydos/ginger/engine/opengl/render/renderers/GuiRenderer.java @@ -10,7 +10,7 @@ import com.github.hydos.ginger.engine.common.math.Maths; import com.github.hydos.ginger.engine.opengl.render.Renderer; import com.github.hydos.ginger.engine.opengl.render.models.RawModel; import com.github.hydos.ginger.engine.opengl.render.shaders.GuiShader; -import com.github.hydos.ginger.engine.opengl.utils.GlLoader; +import com.github.hydos.ginger.engine.opengl.utils.GLLoader; public class GuiRenderer extends Renderer { @@ -24,7 +24,7 @@ public class GuiRenderer extends Renderer { -1, 1, -1, -1, 1, 1, 1, -1 }; - quad = GlLoader.loadToVAO(positions, 2); + quad = GLLoader.loadToVAO(positions, 2); } public void cleanUp() diff --git a/src/main/java/com/github/hydos/ginger/engine/opengl/render/renderers/SkyboxRenderer.java b/src/main/java/com/github/hydos/ginger/engine/opengl/render/renderers/SkyboxRenderer.java index e884f33..72e8518 100644 --- a/src/main/java/com/github/hydos/ginger/engine/opengl/render/renderers/SkyboxRenderer.java +++ b/src/main/java/com/github/hydos/ginger/engine/opengl/render/renderers/SkyboxRenderer.java @@ -7,7 +7,7 @@ import com.github.hydos.ginger.engine.common.cameras.Camera; import com.github.hydos.ginger.engine.opengl.render.Renderer; import com.github.hydos.ginger.engine.opengl.render.models.RawModel; import com.github.hydos.ginger.engine.opengl.render.shaders.SkyboxShader; -import com.github.hydos.ginger.engine.opengl.utils.GlLoader; +import com.github.hydos.ginger.engine.opengl.utils.GLLoader; public class SkyboxRenderer extends Renderer { @@ -62,8 +62,8 @@ public class SkyboxRenderer extends Renderer public SkyboxRenderer(Matrix4f projectionMatrix) { - cube = GlLoader.loadToVAO(VERTICES, 3); - texture = GlLoader.loadCubeMap(TEXTURE_FILES); + cube = GLLoader.loadToVAO(VERTICES, 3); + texture = GLLoader.loadCubeMap(TEXTURE_FILES); shader = new SkyboxShader(); shader.start(); shader.loadProjectionMatrix(projectionMatrix); diff --git a/src/main/java/com/github/hydos/ginger/engine/opengl/utils/GlLoader.java b/src/main/java/com/github/hydos/ginger/engine/opengl/utils/GLLoader.java similarity index 84% rename from src/main/java/com/github/hydos/ginger/engine/opengl/utils/GlLoader.java rename to src/main/java/com/github/hydos/ginger/engine/opengl/utils/GLLoader.java index ed35c8a..895cc77 100644 --- a/src/main/java/com/github/hydos/ginger/engine/opengl/utils/GlLoader.java +++ b/src/main/java/com/github/hydos/ginger/engine/opengl/utils/GLLoader.java @@ -6,12 +6,11 @@ import java.util.*; import org.lwjgl.BufferUtils; import org.lwjgl.opengl.*; -import com.github.halotroop.litecraft.types.block.*; import com.github.hydos.ginger.engine.common.io.Window; import com.github.hydos.ginger.engine.opengl.render.models.RawModel; import com.github.hydos.ginger.engine.opengl.render.texture.*; -public class GlLoader +public class GLLoader { private static List vaos = new ArrayList(); private static List vbos = new ArrayList(); @@ -102,46 +101,6 @@ public class GlLoader public static int loadTexture(String path) { return loadTextureDirectly("/textures/" + path); } - public static int createBlockAtlas() - { - int width = 16; - int height = 16; - //Prepare the atlas texture and gen it - int atlasId = GL40.glGenTextures(); - //Bind it to openGL - GL40.glBindTexture(GL40.GL_TEXTURE_2D, atlasId); - //Apply the settings for the texture - GL40.glTexParameteri(GL40.GL_TEXTURE_2D, GL40.GL_TEXTURE_MIN_FILTER, GL40.GL_NEAREST); - GL40.glTexParameteri(GL40.GL_TEXTURE_2D, GL40.GL_TEXTURE_MAG_FILTER, GL40.GL_NEAREST); - //Fill the image with blank image data - GL40.glTexImage2D(GL40.GL_TEXTURE_2D, 0, GL11.GL_RGBA, width*2, height*2, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, (ByteBuffer) null); - - long maxX = Math.round(Math.sqrt(Blocks.blocks.size())); - int currentX = 0; - int currentY = 0; - for(Block block: Blocks.blocks) { - //just in case - - if(!block.texture.equals("DONTLOAD")) { - System.out.println(block.texture); - block.updateBlockModelData(); - if(currentX > maxX) { - currentX = 0; - currentY--; - } - GL11.glTexSubImage2D(GL11.GL_TEXTURE_2D, 0, - currentX*width, currentY*height, - width, height, - GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, - block.model.getTexture().getTexture().getImage() - ); - currentX++; - } - - } - return atlasId; - } - public static int loadTextureDirectly(String path) { int textureID = GL11.glGenTextures(); diff --git a/src/main/java/com/github/hydos/multithreading/GingerThread.java b/src/main/java/com/github/hydos/multithreading/GingerThread.java deleted file mode 100644 index ca74c04..0000000 --- a/src/main/java/com/github/hydos/multithreading/GingerThread.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.github.hydos.multithreading; - -public abstract class GingerThread extends Thread -{ - public boolean finished = false; - public String threadName; - public boolean started = false; -} diff --git a/src/main/java/com/github/hydos/multithreading/GingerThreading.java b/src/main/java/com/github/hydos/multithreading/GingerThreading.java deleted file mode 100644 index b68054d..0000000 --- a/src/main/java/com/github/hydos/multithreading/GingerThreading.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.github.hydos.multithreading; - -import java.util.*; - -import com.github.halotroop.litecraft.Litecraft; -import com.github.hydos.ginger.engine.common.io.Window; - -public class GingerThreading extends Thread -{ - public List worldChunkThreadWaitlist; - - public GingerThreading() - { worldChunkThreadWaitlist = new ArrayList<>(); } - - public void registerChunkThreadToWaitlist(GingerThread thread) - { worldChunkThreadWaitlist.add(thread); } - - @Override - public void run() { - while(!Window.closed()) { - if(worldChunkThreadWaitlist.size() != 0) { - Litecraft.getInstance().threadWaitlist = worldChunkThreadWaitlist.size(); - GingerThread yes = worldChunkThreadWaitlist.get(0); - if(yes.finished) { - worldChunkThreadWaitlist.remove(0); - if(worldChunkThreadWaitlist.size() != 0) { - worldChunkThreadWaitlist.get(0).start(); - } - }else { - if(!yes.isAlive() && !yes.started) { - yes.start(); - } - } - } - } - } -}