Skip to content

Commit db4bfdd

Browse files
committed
Fix rendering issue with fonts.
1 parent 95cca89 commit db4bfdd

File tree

4 files changed

+127
-6
lines changed

4 files changed

+127
-6
lines changed

src/main/java/alexiil/mc/mod/load/Tips.java

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
package alexiil.mc.mod.load;
22

3+
import java.io.BufferedReader;
4+
import java.io.IOException;
35
import java.util.ArrayList;
46
import java.util.Collections;
57
import java.util.List;
68

7-
/** Basic tips manager. Provides access to a single list of */
9+
import javax.annotation.Nullable;
10+
11+
/** Basic tips manager. Provides access to a single list of tips. */
812
public class Tips {
913

1014
private static final List<String> tips = new ArrayList<>();
15+
private static boolean anyTips = false;
1116

1217
static {
1318
// Just ensure that nothing can crash by having an empty list
@@ -23,17 +28,52 @@ public static void load(List<String> from) {
2328
Collections.shuffle(tips);
2429
if (tips.isEmpty()) {
2530
tips.add("Tips file was empty!");
31+
anyTips = false;
32+
} else {
33+
anyTips = true;
34+
}
35+
}
36+
37+
public static void parseTips(BufferedReader from, List<String> to) throws IOException {
38+
String line;
39+
while ((line = from.readLine()) != null) {
40+
if (line.isEmpty() || line.startsWith("#")) {
41+
// Comment
42+
} else {
43+
to.add(line);
44+
}
2645
}
2746
}
2847

48+
public static List<String> parseTips(BufferedReader from) throws IOException {
49+
List<String> list = new ArrayList<>();
50+
parseTips(from, list);
51+
return list;
52+
}
53+
2954
public static String getFirstTip() {
3055
return tips.get(0);
3156
}
3257

58+
/** Checks to see if any valid tips have been loaded ({@link #getFirstTip()} will return the default tip if this
59+
* returns false). */
60+
public static boolean hasAnyTips() {
61+
return anyTips;
62+
}
63+
3364
public static int getTipCount() {
3465
return tips.size();
3566
}
3667

68+
/** @return The tip at the given index, or null if the index is out of bounds. */
69+
@Nullable
70+
public static String getTipAt(int index) {
71+
if (index < 0 || index >= tips.size()) {
72+
return null;
73+
}
74+
return tips.get(index);
75+
}
76+
3777
/** @return The tip at the given index. Wraps around if the index was outside of bounds */
3878
public static String getTip(int index) {
3979
int count = tips.size();
@@ -50,4 +90,8 @@ public static String getTip(int index) {
5090
public static String getTip(long index) {
5191
return getTip((int) index);
5292
}
93+
94+
public static class TipsInstance {
95+
96+
}
5397
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package alexiil.mc.mod.load.render;
2+
3+
import java.awt.image.BufferedImage;
4+
import java.io.FileNotFoundException;
5+
import java.io.IOException;
6+
import java.util.HashMap;
7+
import java.util.Map;
8+
9+
import org.lwjgl.opengl.GL11;
10+
11+
import net.minecraft.client.Minecraft;
12+
import net.minecraft.client.gui.FontRenderer;
13+
import net.minecraft.client.renderer.texture.TextureManager;
14+
import net.minecraft.client.renderer.texture.TextureUtil;
15+
import net.minecraft.client.resources.IResource;
16+
import net.minecraft.client.settings.GameSettings;
17+
import net.minecraft.util.ResourceLocation;
18+
19+
public class FontRendererSeparate extends FontRenderer {
20+
21+
private final Map<ResourceLocation, BufferedImage> textureData = new HashMap<>();
22+
private final Map<ResourceLocation, Integer> textureLocations = new HashMap<>();
23+
24+
public FontRendererSeparate(GameSettings settings, ResourceLocation location, TextureManager textureManagerIn,
25+
boolean unicode) {
26+
super(settings, location, textureManagerIn, unicode);
27+
28+
loadTex(location);
29+
// for (int i = 0; i < 256; i++) {
30+
31+
// }
32+
}
33+
34+
private void loadTex(ResourceLocation location) {
35+
try (IResource resource = Minecraft.getMinecraft().getResourceManager().getResource(location)) {
36+
textureData.put(location, TextureUtil.readBufferedImage(resource.getInputStream()));
37+
} catch (FileNotFoundException e) {
38+
throw new Error(e);
39+
} catch (IOException e) {
40+
throw new Error(e);
41+
}
42+
}
43+
44+
@Override
45+
protected void bindTexture(ResourceLocation location) {
46+
if (textureLocations == null) {
47+
// During init, so we don't care
48+
return;
49+
}
50+
Integer value = textureLocations.get(location);
51+
if (value == null) {
52+
BufferedImage img = textureData.get(location);
53+
if (img == null) {
54+
return;
55+
}
56+
int next = GL11.glGenTextures();
57+
TextureUtil.uploadTextureImage(next, img);
58+
textureLocations.put(location, next);
59+
value = next;
60+
}
61+
GL11.glBindTexture(GL11.GL_TEXTURE_2D, value.intValue());
62+
}
63+
64+
public void destroy() {
65+
for (Integer value : textureLocations.values()) {
66+
GL11.glDeleteTextures(value.intValue());
67+
}
68+
textureLocations.clear();
69+
}
70+
}

src/main/java/alexiil/mc/mod/load/render/MainSplashRenderer.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040

4141
import org.lwjgl.LWJGLException;
4242
import org.lwjgl.opengl.Display;
43+
import org.lwjgl.opengl.GL11;
4344

4445
import net.minecraft.client.Minecraft;
4546
import net.minecraft.client.gui.FontRenderer;
@@ -143,17 +144,18 @@ public static void run() {
143144
mutex.acquireUninterruptibly();
144145
Display.update();
145146
mutex.release();
147+
GL11.glFlush();
146148

147149
if (finishedLoading && !reachedConstruct) {
148150
// We crashed
149151
break;
150152
}
151153

154+
Display.sync(100);
152155
boolean grabUngrab = pause;// & !finishedLoading;
153156
if (grabUngrab) {
154157
clearGL();
155158
}
156-
Display.sync(100);
157159
if (grabUngrab) {
158160
setGL();
159161
}

src/main/java/alexiil/mc/mod/load/render/MinecraftDisplayerRenderer.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package alexiil.mc.mod.load.render;
22

3+
import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D;
4+
import static org.lwjgl.opengl.GL11.glEnable;
5+
36
import java.util.Map;
47

58
import com.google.common.collect.Maps;
@@ -22,6 +25,8 @@
2225
import alexiil.mc.mod.load.baked.BakedVariable;
2326

2427
public class MinecraftDisplayerRenderer {
28+
private static final ResourceLocation FONT_LOCATION = new ResourceLocation("textures/font/ascii.png");
29+
2530
public final TextureAnimator animator;
2631
private final BakedVariable[] variables;
2732
private final BakedRenderingPart[] renderingParts;
@@ -30,7 +35,7 @@ public class MinecraftDisplayerRenderer {
3035
private long lastTime;
3136
private Minecraft mc;
3237
private final Map<String, FontRenderer> fontRenderers = Maps.newHashMap();
33-
private final FontRenderer _font_render_instance;
38+
private final FontRendererSeparate _font_render_instance;
3439
public TextureManager textureManager;
3540
private boolean first = true;
3641
private SharedDrawable drawable;
@@ -40,7 +45,7 @@ public MinecraftDisplayerRenderer(BakedConfig config, TextureAnimator animator)
4045
mc = Minecraft.getMinecraft();
4146

4247
textureManager = new TextureManager(mc.getResourceManager());
43-
_font_render_instance = new FontRenderer(mc.gameSettings, new ResourceLocation("textures/font/ascii.png"), textureManager, false);
48+
_font_render_instance = new FontRendererSeparate(mc.gameSettings, FONT_LOCATION, textureManager, false);
4449
mc.refreshResources();
4550
textureManager.onResourceManagerReload(mc.getResourceManager());
4651
_font_render_instance.onResourceManagerReload(mc.getResourceManager());
@@ -65,6 +70,7 @@ public void render() {
6570
GlStateManager.disableLighting();
6671
GlStateManager.disableFog();
6772
GlStateManager.disableDepth();
73+
glEnable(GL_TEXTURE_2D);
6874
GlStateManager.enableTexture2D();
6975

7076
GlStateManager.clearColor(1, 1, 1, 1);
@@ -102,7 +108,6 @@ public void render() {
102108
GlStateManager.enableAlpha();
103109
GlStateManager.alphaFunc(GL11.GL_GREATER, 0.1F);
104110
GlStateManager.color(1, 1, 1, 1);
105-
mc.updateDisplay();
106111
}
107112

108113
public FontRenderer fontRenderer(String fontTexture) {
@@ -116,7 +121,6 @@ public FontRenderer fontRenderer(String fontTexture) {
116121
// font.onResourceManagerReload(mc.getResourceManager());
117122
// fontRenderers.put(fontTexture, font);
118123
// return font;
119-
120124
return _font_render_instance;
121125
}
122126

@@ -127,6 +131,7 @@ public void close() {
127131
GlStateManager.alphaFunc(GL11.GL_GREATER, 0.1F);
128132
GlStateManager.clearColor(1, 1, 1, 0);
129133
GlStateManager.clear(GL11.GL_COLOR_BUFFER_BIT);
134+
_font_render_instance.destroy();
130135
drawable.destroy();
131136
}
132137
}

0 commit comments

Comments
 (0)