Skip to content

Commit

Permalink
Refactors rendering code.
Browse files Browse the repository at this point in the history
This created an ITexture interface, which represents textures with the width and height bind to them, so we don't need to store that everywhere.
  • Loading branch information
tyra314 committed Jan 17, 2021
1 parent deaa9e4 commit 025421b
Show file tree
Hide file tree
Showing 27 changed files with 355 additions and 282 deletions.
17 changes: 14 additions & 3 deletions src/main/java/hunternif/mc/impl/atlas/api/oldimpl/TileApiImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import hunternif.mc.impl.atlas.client.BiomeTextureMap;
import hunternif.mc.impl.atlas.client.TextureSet;
import hunternif.mc.impl.atlas.client.TextureSetMap;
import hunternif.mc.impl.atlas.client.texture.TileTexture;
import hunternif.mc.impl.atlas.core.AtlasData;
import hunternif.mc.impl.atlas.ext.ExtTileTextureMap;
import hunternif.mc.impl.atlas.ext.TileDataStorage;
Expand Down Expand Up @@ -53,10 +54,20 @@ public TileApiImpl() {
}


private static TileTexture[] convertToTileTexture(Identifier... textures) {
TileTexture[] iTextures = new TileTexture[textures.length];

for (int i = 0; i < textures.length; i++) {
iTextures[i] = new TileTexture(textures[i]);
}

return iTextures;
}

@Override
@Environment(EnvType.CLIENT)
public TextureSet registerTextureSet(Identifier name, Identifier... textures) {
TextureSet textureSet = new TextureSet(name, textures);
TextureSet textureSet = new TextureSet(name, convertToTileTexture(textures));
TextureSetMap.instance().register(textureSet);
return textureSet;
}
Expand All @@ -67,7 +78,7 @@ public TextureSet registerTextureSet(Identifier name, Identifier... textures) {
@Override
@Environment(EnvType.CLIENT)
public void setBiomeTexture(Biome biome, Identifier textureSetName, Identifier... textures) {
TextureSet set = new TextureSet(textureSetName, textures);
TextureSet set = new TextureSet(textureSetName, convertToTileTexture(textures));
TextureSetMap.instance().register(set);
BiomeTextureMap.instance().setTexture(biome, set);
}
Expand All @@ -84,7 +95,7 @@ public void setBiomeTexture(Biome biome, TextureSet textureSet) {
@Override
@Environment(EnvType.CLIENT)
public void setCustomTileTexture(Identifier uniqueTileName, Identifier... textures) {
TextureSet set = new TextureSet(uniqueTileName, textures);
TextureSet set = new TextureSet(uniqueTileName, convertToTileTexture(textures));
TextureSetMap.instance().register(set);
setCustomTileTexture(uniqueTileName, set);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package hunternif.mc.impl.atlas.client;

import hunternif.mc.impl.atlas.client.texture.ITexture;
import hunternif.mc.impl.atlas.util.Log;
import hunternif.mc.impl.atlas.util.SaveData;
import net.fabricmc.api.EnvType;
Expand All @@ -13,6 +14,7 @@
import java.util.*;
import java.util.Map.Entry;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import static hunternif.mc.impl.atlas.client.TextureSet.*;

Expand Down Expand Up @@ -185,16 +187,15 @@ public TextureSet getTextureSet(Identifier tile) {
return textureMap.get(tile);
}

public Identifier getTexture(int variationNumber, Identifier tile) {
TextureSet set = getTextureSet(tile);
return set.textures[variationNumber % set.textures.length];
public ITexture getTexture(SubTile subTile) {
return getTextureSet(subTile.tile).getTexture(subTile.variationNumber);
}

public List<Identifier> getAllTextures() {
List<Identifier> list = new ArrayList<>();

for (Entry<Identifier, TextureSet> entry : textureMap.entrySet()) {
list.addAll(Arrays.asList(entry.getValue().textures));
Arrays.stream(entry.getValue().textures).map(iTexture -> list.add(iTexture.getTexture()));
}

return list;
Expand Down
20 changes: 11 additions & 9 deletions src/main/java/hunternif/mc/impl/atlas/client/TextureSet.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package hunternif.mc.impl.atlas.client;

import hunternif.mc.impl.atlas.client.texture.ITexture;
import net.minecraft.util.Identifier;

import java.util.Collections;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;
import java.util.*;

import static hunternif.mc.impl.atlas.client.Textures.*;

Expand Down Expand Up @@ -247,7 +245,7 @@ public class TextureSet implements Comparable<TextureSet> {
/**
* The actual textures in this set.
*/
public final Identifier[] textures;
public final ITexture[] textures;
/**
* Whether the texture set is part of the standard pack. Only true for
* static constants in this class.
Expand All @@ -263,7 +261,7 @@ public class TextureSet implements Comparable<TextureSet> {
private boolean stitchesToNull = false;
private boolean anisotropicStitching = false;

private TextureSet(boolean isStandard, Identifier name, Identifier... textures) {
private TextureSet(boolean isStandard, Identifier name, ITexture... textures) {
this.isStandard = isStandard;
this.name = name;
this.textures = textures;
Expand All @@ -272,11 +270,11 @@ private TextureSet(boolean isStandard, Identifier name, Identifier... textures)
/**
* Name has to be unique, it is used for equals() tests.
*/
public TextureSet(Identifier name, Identifier... textures) {
public TextureSet(Identifier name, ITexture... textures) {
this(false, name, textures);
}

private static TextureSet standard(String name, Identifier... textures) {
private static TextureSet standard(String name, ITexture... textures) {
return new TextureSet(true, new Identifier("antiqueatlas", name.toLowerCase(Locale.ROOT)), textures);
}

Expand Down Expand Up @@ -383,13 +381,17 @@ public int compareTo(TextureSet textureSet) {
return name.toString().compareTo(textureSet.name.toString());
}

public ITexture getTexture(int variationNumber) {
return textures[variationNumber % textures.length];
}

/**
* A special texture set that is stitched to everything except water.
*/
private static class TextureSetShore extends TextureSet {
private final TextureSet water;

TextureSetShore(String name, TextureSet water, Identifier... textures) {
TextureSetShore(String name, TextureSet water, ITexture... textures) {
super(true, new Identifier("antiqueatlas", name.toLowerCase(Locale.ROOT)), textures);
this.water = water;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.google.gson.*;
import hunternif.mc.impl.atlas.AntiqueAtlasMod;
import hunternif.mc.impl.atlas.client.texture.ITexture;
import hunternif.mc.impl.atlas.client.texture.TileTexture;
import hunternif.mc.impl.atlas.util.Log;

import java.io.InputStream;
Expand Down Expand Up @@ -51,10 +53,10 @@ public CompletableFuture<Collection<TextureSet>> load(ResourceManager manager, P
for (Entry<String, JsonElement> entry : obj.get("data").getAsJsonObject().entrySet()) {
String name = entry.getKey();
JsonArray array = entry.getValue().getAsJsonArray();
Identifier[] textures = new Identifier[array.size()];
ITexture[] textures = new ITexture[array.size()];
for (int i = 0; i < array.size(); i++) {
String path = array.get(i).getAsString();
textures[i] = new Identifier(path);
textures[i] = new TileTexture(new Identifier(path));
}
sets.add(new TextureSet(AntiqueAtlasMod.id(name), textures));
}
Expand Down
85 changes: 34 additions & 51 deletions src/main/java/hunternif/mc/impl/atlas/client/Textures.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package hunternif.mc.impl.atlas.client;

import hunternif.mc.impl.atlas.AntiqueAtlasMod;
import hunternif.mc.impl.atlas.client.texture.ITexture;
import hunternif.mc.impl.atlas.client.texture.IconTexture;
import hunternif.mc.impl.atlas.client.texture.Texture;
import hunternif.mc.impl.atlas.client.texture.TileTexture;
import net.minecraft.util.Identifier;

public class Textures {
Expand All @@ -11,21 +15,20 @@ public class Textures {
private static final String GUI_MARKERS = GUI + "markers/";
private static final String GUI_SCALEBAR = GUI + "scalebar/";

public static final Identifier
BOOK = gui("book.png"),
EXPORTED_BG = gui("exported_bg.png"),
BOOK_FRAME = gui("book_frame.png"),
BTN_ARROWS = gui("navigate_arrows.png"),
BTN_POSITION = gui("position.png"),
BOOKMARKS = gui("bookmarks.png"),
PLAYER = gui("player.png"),
SCROLLBAR_HOR = gui("scrollbar_hor.png"),
SCROLLBAR_VER = gui("scrollbar_ver.png"),
MARKER_FRAME_ON = gui("marker_frame_on.png"),
MARKER_FRAME_OFF = gui("marker_frame_off.png"),
ERASER = gui("eraser.png"),
public static final ITexture
BOOK = gui("book.png", 310, 218),
BOOK_FRAME = gui("book_frame.png", 310, 218),
BTN_ARROWS = gui("navigate_arrows.png", 24, 24),
BTN_POSITION = gui("position.png", 24, 24),
BOOKMARKS = gui("bookmarks.png", 84, 36),
PLAYER = gui("player.png", 7, 8),
SCROLLBAR_HOR = gui("scrollbar_hor.png", 8, 7),
SCROLLBAR_VER = gui("scrollbar_ver.png", 7,8),
MARKER_FRAME_ON = gui("marker_frame_on.png", 34, 34),
MARKER_FRAME_OFF = gui("marker_frame_off.png", 34, 34),
ERASER = gui("eraser.png", 24, 24),

SCALEBAR_4 = scaleBar("scalebar_4.png"),
SCALEBAR_4 = scaleBar("scalebar_4.png"),
SCALEBAR_8 = scaleBar("scalebar_8.png"),
SCALEBAR_16 = scaleBar("scalebar_16.png"),
SCALEBAR_32 = scaleBar("scalebar_32.png"),
Expand All @@ -34,31 +37,13 @@ public class Textures {
SCALEBAR_256 = scaleBar("scalebar_256.png"),
SCALEBAR_512 = scaleBar("scalebar_512.png"),

ICON_ADD_MARKER = icon("add_marker.png"),
ICON_ADD_MARKER = icon("add_marker.png"),
ICON_DELETE_MARKER = icon("del_marker.png"),
ICON_SHOW_MARKERS = icon("show_markers.png"),
ICON_HIDE_MARKERS = icon("hide_markers.png"),
ICON_EXPORT = icon("export.png"),

MARKER_GOOGLE_MARKER = marker("google_marker.png"),
MARKER_RED_X_LARGE = marker("red_x_large.png"),
MARKER_RED_X_SMALL = marker("red_x_small.png"),
MARKER_VILLAGE = marker("village.png"),
MARKER_END_CITY_FAR = marker("end_city_far.png"),
MARKER_END_CITY = marker("end_city.png"),
MARKER_END_CITY_MIP_32 = marker("end_city_mipped_32.png"),
MARKER_END_CITY_MIP_16 = marker("end_city_mipped_16.png"),
MARKER_DIAMOND = marker("diamond.png"),
MARKER_BED = marker("bed.png"),
MARKER_PICKAXE = marker("pickaxe.png"),
MARKER_SWORD = marker("sword.png"),
MARKER_NETHER_PORTAL = marker("nether_portal.png"),
MARKER_SKULL = marker("skull.png"),
MARKER_TOWER = marker("tower.png"),
MARKER_SCROLL = marker("scroll.png"),
MARKER_TOMB = marker("tomb.png"),

TILE_TEST = tile("test.png"),
TILE_TEST = tile("test.png"),
TILE_MOUNTAINS = tile("mountains.png"),
TILE_MOUNTAINS2 = tile("mountains2.png"),
TILE_MOUNTAINS3 = tile("mountains3.png"),
Expand Down Expand Up @@ -212,14 +197,14 @@ public class Textures {
TILE_LAVA_SHORE = tile("lava_shore.png"),
TILE_LAVA_SHORE2 = tile("lava_shore2.png"),

TILE_END_VOID = tile("end_void.png"),
TILE_END_VOID = tile("end_void.png"),
TILE_END_ISLAND = tile("end_island.png"),
TILE_END_ISLAND2 = tile("end_island2.png"),
TILE_END_ISLAND_PLANTS = tile("end_island_plants.png"),
TILE_END_ISLAND_PLANTS2 = tile("end_island_plants2.png"),

// Village:
TILE_HOUSE = tile("house.png"),
// Village:
TILE_HOUSE = tile("house.png"),
TILE_FENCE = tile("fence.png"),
TILE_LIBRARY = tile("library.png"),
TILE_SMITHY = tile("smithy.png"),
Expand All @@ -235,8 +220,8 @@ public class Textures {
TILE_BUTCHERS_SHOP = tile("butchers_shop.png"),
TILE_CHURCH = tile("church.png"),

// Nether Fortress:
TILE_NETHER_BRIDGE = tile("nether_bridge.png"),
// Nether Fortress:
TILE_NETHER_BRIDGE = tile("nether_bridge.png"),
TILE_NETHER_BRIDGE_X = tile("nether_bridge_x.png"),
TILE_NETHER_BRIDGE_Z = tile("nether_bridge_z.png"),
TILE_NETHER_BRIDGE_END_X = tile("nether_bridge_end_x.png"),
Expand All @@ -248,24 +233,22 @@ public class Textures {
TILE_NETHER_FORT_STAIRS = tile("nether_fort_stairs.png"),
TILE_NETHER_THRONE = tile("nether_throne.png");

// Constructor helpers:
private static Identifier gui(String fileName) {
return new Identifier(GUI + fileName);
}
public static final Identifier EXPORTED_BG = new Identifier(GUI + "exported_bg.png");

private static Identifier scaleBar(String fileName) {
return new Identifier(GUI_SCALEBAR + fileName);
// Constructor helpers:
private static ITexture gui(String fileName, int width, int height) {
return new Texture(new Identifier(GUI + fileName), width, height);
}

private static Identifier marker(String fileName) {
return new Identifier(GUI_MARKERS + fileName);
private static ITexture scaleBar(String fileName) {
return new Texture(new Identifier(GUI_SCALEBAR + fileName), 20, 8);
}

private static Identifier tile(String fileName) {
return new Identifier(GUI_TILES + fileName);
private static ITexture tile(String fileName) {
return new TileTexture(new Identifier(GUI_TILES + fileName));
}

private static Identifier icon(String fileName) {
return new Identifier(GUI_ICONS + fileName);
private static ITexture icon(String fileName) {
return new IconTexture(new Identifier(GUI_ICONS + fileName));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@
import com.mojang.blaze3d.systems.RenderSystem;
import hunternif.mc.impl.atlas.client.Textures;
import hunternif.mc.impl.atlas.client.gui.core.GuiComponentButton;
import hunternif.mc.impl.atlas.util.AtlasRenderHelper;
import net.minecraft.client.util.math.MatrixStack;
import org.lwjgl.opengl.GL11;

public class GuiArrowButton extends GuiComponentButton {
private static final int WIDTH = 12;
private static final int HEIGHT = 12;
private static final int IMAGE_WIDTH = 24;
private static final int IMAGE_HEIGHT = 24;

public enum ArrowDirection {
UP("Up"), DOWN("Down"), LEFT("Left"), RIGHT("Right");
Expand Down Expand Up @@ -82,7 +79,7 @@ public void render(MatrixStack matrices, int mouseX, int mouseY, float partialTi
v = 12;
break;
}
AtlasRenderHelper.drawTexturedRect(matrices, Textures.BTN_ARROWS, x, y, u, v, WIDTH, HEIGHT, IMAGE_WIDTH, IMAGE_HEIGHT);
Textures.BTN_ARROWS.draw(matrices, x, y, u, v, WIDTH, HEIGHT);

RenderSystem.disableBlend();
}
Expand Down
Loading

0 comments on commit 025421b

Please sign in to comment.