diff --git a/fabric/src/main/java/net/smyler/terramap/TerramapFabricMod.java b/fabric/src/main/java/net/smyler/terramap/TerramapFabricMod.java index a3502155..8e5573c6 100644 --- a/fabric/src/main/java/net/smyler/terramap/TerramapFabricMod.java +++ b/fabric/src/main/java/net/smyler/terramap/TerramapFabricMod.java @@ -3,6 +3,11 @@ import com.google.gson.Gson; import com.google.gson.GsonBuilder; import net.fabricmc.api.ModInitializer; +import net.fabricmc.loader.api.FabricLoader; +import net.fabricmc.loader.api.ModContainer; +import net.fabricmc.loader.api.Version; +import net.fabricmc.loader.api.metadata.ModMetadata; +import net.fabricmc.loader.impl.util.version.StringVersion; import net.minecraft.client.Minecraft; import net.smyler.smylib.SmyLib; import net.smyler.smylib.game.WrappedMinecraft; @@ -10,12 +15,16 @@ import net.smyler.smylib.text.Text; import net.smyler.terramap.http.HttpClient; import net.smyler.terramap.http.TerramapHttpClient; +import net.smyler.terramap.tilesets.raster.RasterTileSetManager; import org.apache.logging.log4j.Logger; import static org.apache.logging.log4j.LogManager.getLogger; + public class TerramapFabricMod implements ModInitializer, Terramap { + private Version version; + private final Logger logger = getLogger("terramap"); private final Gson gson = new GsonBuilder() .registerTypeAdapter(Text.class, new TextJsonAdapter()) @@ -25,14 +34,26 @@ public class TerramapFabricMod implements ModInitializer, Terramap { .setPrettyPrinting() .create(); private final HttpClient httpClient = new TerramapHttpClient(this.logger); + private RasterTileSetManager rasterTileSetManager; @Override public void onInitialize() { - this.logger.info("Initializing Terramap"); + FabricLoader fabric = FabricLoader.getInstance(); + this.version = fabric.getModContainer(Terramap.MOD_ID) + .map(ModContainer::getMetadata) + .map(ModMetadata::getVersion) + .orElse(new StringVersion("Unknown")); + this.logger.info("Initializing Terramap version {}", this.version()); + this.rasterTileSetManager = new RasterTileSetManager(fabric.getConfigDir().toFile()); Terramap.InstanceHolder.setInstance(this); SmyLib.initializeGameClient(new WrappedMinecraft(Minecraft.getInstance()), this.logger); } + @Override + public String version() { + return this.version.toString(); + } + @Override public Logger logger() { return this.logger; @@ -53,4 +74,9 @@ public Gson gsonPretty() { return this.gsonPretty; } + @Override + public RasterTileSetManager rasterTileSetManager() { + return this.rasterTileSetManager; + } + } diff --git a/fabric/src/main/java/net/smyler/terramap/mixins/ShowTestScreenMixin.java b/fabric/src/main/java/net/smyler/terramap/mixins/ShowTestScreenMixin.java index 350d62bb..f4f4e583 100644 --- a/fabric/src/main/java/net/smyler/terramap/mixins/ShowTestScreenMixin.java +++ b/fabric/src/main/java/net/smyler/terramap/mixins/ShowTestScreenMixin.java @@ -3,6 +3,7 @@ import net.minecraft.client.Minecraft; import net.minecraft.client.gui.screens.Screen; import net.minecraft.client.gui.screens.TitleScreen; +import net.smyler.smylib.game.GameClient; import net.smyler.smylib.gui.screen.TestScreen; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; @@ -17,7 +18,8 @@ public class ShowTestScreenMixin { @Inject(method = "setScreen(Lnet/minecraft/client/gui/screens/Screen;)V", at = @At("HEAD"), cancellable = true) private void onSetScreen(Screen screen, CallbackInfo ci) { if (screen instanceof TitleScreen) { - getGameClient().displayScreen(new TestScreen(null)); + GameClient gameClient = getGameClient(); + gameClient.displayScreen(new TestScreen(gameClient.getCurrentScreen())); ci.cancel(); } } diff --git a/smylib/fabric/src/main/java/net/smyler/smylib/gui/Lwjgl3GlState.java b/smylib/fabric/src/main/java/net/smyler/smylib/gui/Lwjgl3GlState.java deleted file mode 100644 index be66c964..00000000 --- a/smylib/fabric/src/main/java/net/smyler/smylib/gui/Lwjgl3GlState.java +++ /dev/null @@ -1,63 +0,0 @@ -package net.smyler.smylib.gui; - -import com.mojang.blaze3d.systems.RenderSystem; -import net.smyler.smylib.Color; - - -public class Lwjgl3GlState implements GlState { - @Override - public void enableAlpha() { - //TODO enable alpha - } - - @Override - public void disableAlpha() { - //TODO disable alpha - } - - @Override - public void setColor(Color color) { - RenderSystem.setShaderColor(color.redf(), color.greenf(), color.bluef(), color.alphaf()); - } - - @Override - public Color getColor() { - return null; //TODO get color - } - - @Override - public void enableColorLogic(ColorLogic colorLogic) { - RenderSystem.enableColorLogicOp(); - } - - @Override - public void disableColorLogic() { - RenderSystem.disableColorLogicOp(); - } - - @Override - public void pushViewMatrix() { - RenderSystem.getModelViewStack().pushPose(); - } - - @Override - public void rotate(double angle) { - RenderSystem.getModelViewMatrix().rotate((float)(angle), 0f, 0f, 1f); - } - - @Override - public void translate(double x, double y) { - RenderSystem.getModelViewMatrix().translate((float)x, (float)y, 0); - } - - @Override - public void scale(double x, double y) { - RenderSystem.getModelViewMatrix().scale((float)x, (float)y, 1); - } - - @Override - public void popViewMatrix() { - RenderSystem.getModelViewStack().popPose(); - } - -} diff --git a/smylib/fabric/src/main/java/net/smyler/smylib/gui/WrappedGuiGraphics.java b/smylib/fabric/src/main/java/net/smyler/smylib/gui/WrappedGuiGraphics.java index abee49e3..e3e1dea5 100644 --- a/smylib/fabric/src/main/java/net/smyler/smylib/gui/WrappedGuiGraphics.java +++ b/smylib/fabric/src/main/java/net/smyler/smylib/gui/WrappedGuiGraphics.java @@ -2,20 +2,28 @@ import com.mojang.blaze3d.systems.RenderSystem; import com.mojang.blaze3d.vertex.*; +import net.minecraft.client.Minecraft; import net.minecraft.client.gui.GuiGraphics; import net.minecraft.client.renderer.GameRenderer; import net.minecraft.resources.ResourceLocation; import net.smyler.smylib.Color; +import net.smyler.smylib.Identifier; +import net.smyler.smylib.gui.gl.Blaze3dGlContext; +import net.smyler.smylib.gui.gl.GlContext; +import net.smyler.smylib.gui.gl.Scissor; +import net.smyler.smylib.gui.gl.WrappedGuiGraphicsScissor; import net.smyler.smylib.gui.sprites.Sprite; import org.joml.Matrix4f; +import java.awt.image.BufferedImage; + import static net.smyler.smylib.Preconditions.checkArgument; -public class WrappedGuiGraphics implements DrawContext { +public class WrappedGuiGraphics implements UiDrawContext { public final GuiGraphics vanillaGraphics; private final Scissor scissor = new WrappedGuiGraphicsScissor(); - private final GlState glState = new Lwjgl3GlState(); + private final GlContext glState = new Blaze3dGlContext(Minecraft.getInstance()); public WrappedGuiGraphics(GuiGraphics vanillaGraphics) { this.vanillaGraphics = vanillaGraphics; @@ -27,7 +35,7 @@ public Scissor scissor() { } @Override - public GlState glState() { + public GlContext gl() { return this.glState; } @@ -49,15 +57,10 @@ public void drawGradientRectangle(double z, double xLeft, double yTop, double xR RenderSystem.disableBlend(); } - @Override - public void drawPolygon(double z, Color color, double... points) { - //TODO implement drawPolygon - } - @Override public void drawStrokeLine(double z, Color color, float size, double... points) { RenderSystem.lineWidth(size); - this.drawMultiPointsGeometry(VertexFormat.Mode.LINE_STRIP, z, color, points); + this.drawMultiPointsGeometry(z, color, points); } @Override @@ -66,8 +69,8 @@ public void drawClosedStrokeLine(double z, Color color, float size, double... po return; } RenderSystem.lineWidth(size); - this.drawMultiPointsGeometry(VertexFormat.Mode.LINE_STRIP, z, color, points); - this.drawMultiPointsGeometry(VertexFormat.Mode.LINE_STRIP, z, color, + this.drawMultiPointsGeometry(z, color, points); + this.drawMultiPointsGeometry(z, color, points[points.length - 2], points[points.length - 1], points[0], points[1] ); @@ -108,14 +111,24 @@ public void drawTooltip(String text, double x, double y) { //TODO implement drawTooltip } - private void drawMultiPointsGeometry(VertexFormat.Mode mode, double z, Color color, double... points) { + @Override + public Identifier loadDynamicTexture(BufferedImage image) { + return null; + } + + @Override + public void unloadDynamicTexture(Identifier texture) { + + } + + private void drawMultiPointsGeometry(double z, Color color, double... points) { checkArgument(points.length % 2 == 0, "An even number of coordinates is required"); RenderSystem.enableBlend(); RenderSystem.setShader(GameRenderer::getPositionColorShader); RenderSystem.setShaderColor(color.redf(), color.greenf(), color.bluef(), color.alphaf()); BufferBuilder builder = Tesselator.getInstance().getBuilder(); - builder.begin(mode, DefaultVertexFormat.POSITION); + builder.begin(VertexFormat.Mode.LINE_STRIP, DefaultVertexFormat.POSITION); for(int i=0; i com.mojang.blaze3d.vertex.VertexFormat.Mode.LINES; + case LINE_STRIP -> com.mojang.blaze3d.vertex.VertexFormat.Mode.LINE_STRIP; + case TRIANGLES -> com.mojang.blaze3d.vertex.VertexFormat.Mode.TRIANGLES; + case TRIANGLE_STRIP -> com.mojang.blaze3d.vertex.VertexFormat.Mode.TRIANGLE_STRIP; + case TRIANGLE_FAN -> com.mojang.blaze3d.vertex.VertexFormat.Mode.TRIANGLE_FAN; + case QUADS -> com.mojang.blaze3d.vertex.VertexFormat.Mode.QUADS; + }; + com.mojang.blaze3d.vertex.VertexFormat blazeFormat = switch (format) { + case POSITION -> DefaultVertexFormat.POSITION; + case POSITION_TEXTURE -> DefaultVertexFormat.POSITION_TEX; + case POSITION_COLOR -> DefaultVertexFormat.POSITION_COLOR; + case POSITION_TEXTURE_COLOR -> DefaultVertexFormat.POSITION_TEX_COLOR; + }; + this.bufferBuilder.begin(blazeMode, blazeFormat); + } + + @Override + public VertexBuilder vertex() { + return new VertexBuilderImplementation(); + } + + @Override + public void draw() { + checkState(this.currentFormat != null, "Not drawing!"); + this.tesselator.end(); + } + + private class VertexBuilderImplementation implements VertexBuilder { + double x, y, z; + float u, v; + float r, g, b, a; + + @Override + public VertexBuilder position(double x, double y, double z) { + this.x = x; + this.y = y; + this.z = z; + return this; + } + + @Override + public VertexBuilder color(float r, float g, float b, float a) { + this.r = r; + this.g = g; + this.b = b; + this.a = a; + return this; + } + + @Override + public VertexBuilder color(Color color) { + this.color(color.redf(), color.greenf(), color.bluef(), color.alphaf()); + return this; + } + + @Override + public VertexBuilder texture(double u, double v) { + this.u = (float)u; + this.v = (float)v; + return this; + } + + @Override + public void end() { + VertexFormat format = Blaze3dGlContext.this.currentFormat; + VertexConsumer consumer = Blaze3dGlContext.this.bufferBuilder.vertex(this.x, this.y, this.z); + if (format.texture) { + consumer = consumer.uv(this.u, this.v); + } + if (format.color) { + consumer.color(this.r, this.g, this.b, this.a); + } + consumer.endVertex(); + } + + } + +} diff --git a/smylib/fabric/src/main/java/net/smyler/smylib/gui/WrappedGuiGraphicsScissor.java b/smylib/fabric/src/main/java/net/smyler/smylib/gui/gl/WrappedGuiGraphicsScissor.java similarity index 93% rename from smylib/fabric/src/main/java/net/smyler/smylib/gui/WrappedGuiGraphicsScissor.java rename to smylib/fabric/src/main/java/net/smyler/smylib/gui/gl/WrappedGuiGraphicsScissor.java index 22e28c52..c5cc729f 100644 --- a/smylib/fabric/src/main/java/net/smyler/smylib/gui/WrappedGuiGraphicsScissor.java +++ b/smylib/fabric/src/main/java/net/smyler/smylib/gui/gl/WrappedGuiGraphicsScissor.java @@ -1,4 +1,4 @@ -package net.smyler.smylib.gui; +package net.smyler.smylib.gui.gl; //FIXME implement scissors public class WrappedGuiGraphicsScissor implements Scissor {