-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathAllocationRateHUD.java
More file actions
40 lines (34 loc) · 1.71 KB
/
Copy pathAllocationRateHUD.java
File metadata and controls
40 lines (34 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package com.mitchej123.hodgepodge.client;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.util.MathHelper;
import net.minecraftforge.client.event.RenderGameOverlayEvent;
import cpw.mods.fml.common.eventhandler.EventPriority;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
public final class AllocationRateHUD {
private long lastUpdateTime = System.nanoTime();
private long lastFreeMemory = Runtime.getRuntime().freeMemory();
private String cachedText = "";
@SubscribeEvent(priority = EventPriority.LOWEST)
public void onRenderGameOverlayTextEvent(RenderGameOverlayEvent.Text event) {
if (!Minecraft.getMinecraft().gameSettings.showDebugInfo) {
final String text = this.updateText();
final FontRenderer fr = Minecraft.getMinecraft().fontRenderer;
final int strWidth = fr.getStringWidth(text);
fr.drawStringWithShadow(text, event.resolution.getScaledWidth() - strWidth - 2, 2, 0xFFFFFFFF);
}
}
public String updateText() {
final long currentTime = System.nanoTime();
if (currentTime - lastUpdateTime >= 1_000_000_000L) {
final long currentFreeMemory = Runtime.getRuntime().freeMemory();
final long memDiff = lastFreeMemory - currentFreeMemory;
final double seconds = (currentTime - lastUpdateTime) / 1_000_000_000.0;
final int allocationRate = MathHelper.floor_double(memDiff / seconds / (1024.0 * 1024.0));
cachedText = String.format("Allocation rate: %s MB/s", allocationRate);
lastFreeMemory = currentFreeMemory;
lastUpdateTime = currentTime;
}
return cachedText;
}
}