Skip to content

Commit

Permalink
Implement new rendering paradigm
Browse files Browse the repository at this point in the history
  • Loading branch information
SmylerMC committed Jul 7, 2024
1 parent 23b58cc commit 652289d
Show file tree
Hide file tree
Showing 6 changed files with 240 additions and 79 deletions.
28 changes: 27 additions & 1 deletion fabric/src/main/java/net/smyler/terramap/TerramapFabricMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,28 @@
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;
import net.smyler.smylib.json.TextJsonAdapter;
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())
Expand All @@ -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;
Expand All @@ -53,4 +74,9 @@ public Gson gsonPretty() {
return this.gsonPretty;
}

@Override
public RasterTileSetManager rasterTileSetManager() {
return this.rasterTileSetManager;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,7 +35,7 @@ public Scissor scissor() {
}

@Override
public GlState glState() {
public GlContext gl() {
return this.glState;
}

Expand All @@ -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
Expand All @@ -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]
);
Expand Down Expand Up @@ -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<points.length; i+=2) {
builder.vertex(points[i], points[i+1], z).endVertex();
}
Expand Down
Loading

0 comments on commit 652289d

Please sign in to comment.