无法绘制文字

kabuki_


版本信息
Forge版本: 2838
Minecraft版本: 1.12
Mapping 文件版本: stable_39

问题
监听RenderGameOverlayEvent时,绘制文字却没有任何反馈,但是经过测试是正常运行的。

相关代码
HudCompass用于在屏幕上绘制图形和文字

@Mod.EventBusSubscriber(modid = MidgardReborn.MODID)
public class HudCompass {
	private static Minecraft mc = Minecraft.getMinecraft();
	private static ScaledResolution scaled = new ScaledResolution(mc);
	private static FontRenderer fontrender = mc.fontRenderer;
	
	private final static int WIDTH = 200;
	private final static int HEIGHT = 50;
	private final static int OFFSETX = (scaled.getScaledWidth() - WIDTH) / 2;
	private final static int OFFSETY = 10;
	
	private static boolean S = true;
	private static boolean W = true;
	private static boolean N = true;
	private static boolean E = true;
	/** agree must be 20 - 70*/
	private static float perspective = 0.5463024898F;
	
	public HudCompass() {
		MinecraftForge.EVENT_BUS.register(this);
	}
	
	@SideOnly(Side.CLIENT)
	@SubscribeEvent
	public static void onHudCompassPrint(RenderGameOverlayEvent.Text event)
	{
		if(event.getType() == RenderGameOverlayEvent.ElementType.ALL)
		{
			if(hasCompass())
			{
				GlStateManager.color(1.0F, 1.0F, 1.0F, 1.F);
				EntityPlayer player =  Minecraft.getMinecraft().player;
				float yaw = player.rotationYaw;
				drawFont(yaw);
				drawBackGround();
			}
		}
		return;
	}
	
	private static void drawBackGround()
	{
		//TODO
	}
	
	private static void drawFont(float yaw)
	{
		reset();
		float min = yaw - perspective;
		float max = yaw + perspective;
		
		if(min < -3.141592653589793F)
		{
			float temp = min;
			min = max;
			max = temp + perspective * 2;
		}
		
		if(max > -3.141592653589793F)
		{
			float temp = max;
			max = min;
			min = temp - perspective * 2;
		}
		
		EnumCompassFacing minCompassFacing = EnumCompassFacing.NORTH;
		
		for(int agree = -180; agree < 180; agree += 90)
		{
			if(EnumCompassFacing.getFacingForAgree(agree).radian < min)
			{
				setFacingFlag(EnumCompassFacing.getFacingForAgree(agree), false);
			}
			else
			{
				minCompassFacing = EnumCompassFacing.getFacingForAgree(agree);
				break;
			}
		}
		
		for(int agree = 180; agree > -180; agree -= 90)
		{
			if(EnumCompassFacing.getFacingForAgree(agree).radian > max)
			{
				setFacingFlag(EnumCompassFacing.getFacingForAgree(agree), false);
			}
			else
			{
				break;
			}
		}
		
		int y = OFFSETY + HEIGHT / 2;
		if(Math.toDegrees(perspective * 2) < 90)
		{
			int x = (int) (OFFSETX + 200 * (minCompassFacing.radian - min / perspective * 2));
			fontrender.drawString(minCompassFacing.getInitials(), x, y, 14737632);
		}
	}
	
	private static void reset()
	{
		W = true;
		E = true;
		S = true;
		W = true;
	}

	private static void setFacingFlag(EnumCompassFacing facing, boolean active)
	{
		if(facing == EnumCompassFacing.NORTH)
		{
			N = active;
		}
		else if(facing == EnumCompassFacing.EAST)
		{
			E = active;
		}
		else if(facing == EnumCompassFacing.SOUTH)
		{
			S = active;
		}
		else
		{
			W = active;
		}
	}
	
	private static boolean hasCompass()
	{
		//TODO
		return true;
	}
	
	public static enum EnumCompassFacing
	{
		NORTH(3.141592653589793F),
		EAST(1.570796326794897F),
		SOUTH(0.0F),
		WEST(-1.570796326794897F);
		
		public float radian;
		private EnumCompassFacing(float radian) {
			this.radian = radian;
		}
		
		/**
		 * @param agree agree value <b>-180</b> between <b>180</b>
		 * <br> enter the wrong parameter <b>WILL RETURN NULL</b>
		 * @return 
		 * -180 - {@link EnumCompassFacing#NORTH} <br>
		 * -90  - {@link EnumCompassFacing#EAST}  <br>
		 * 0    - {@link EnumCompassFacing#SORTH} <br>
		 * 90   - {@link EnumCompassFacing#EAST}  <br>
		 * 180  - {@link EnumCompassFacing#NORTH} <br>
		 */
		@Nullable
		public static EnumCompassFacing getFacingForAgree(int agree)
		{
			switch(agree)
			{
				case 0:
					return SOUTH;
				case 90:
					return EAST;
				case -90:
					return WEST;
				case 180:
				case -180:
					return NORTH;
				default:
					return null;
			}
		}
		
		public String getInitials()
		{
			switch(this)
			{
				case NORTH:
					return "N";
				case EAST:
					return "E";
				case SOUTH:
					return "S";
				case WEST:
					return "W";
				default:
					return "";
			}
		}
		
		public float getMiniMumCritical()
		{
			return this.radian - perspective; 
		}
		
		public float getMaxMumCritical()
		{
			return this.radian + perspective; 
		}
	}
	
}

查到的资料
去论坛和查阅教程时都是使用FontRenderer#drawString来绘制文字,甚至也尝试将Post事件改成Text,但是感觉没有任何变化


FledgeXu


是不是你的文字选的颜色有问题……改成0x000000试试。
如果还是有问题,把

	private static FontRenderer fontrender = mc.fontRenderer;

移动到onHudCompassPrint方法里,每次渲染的时候都获取一遍试试。


kabuki_


我第一次尝试的时候就写了0


FledgeXu


那试试在onHudCompassPrint方法里调用Minecraft.getMinecraft().fontRenderer
还有我不是很清楚什么叫做「正常运行」,是指fontrender.drawString被调用到了吗?


kabuki_


正常运行是指执行到 onHudCompassPrint这个方法中if(hasCompass)里了

噢,请原谅我的疏忽,我忘记检查这步了,我是用计算器转化得到的值,我以为它仍然小于90

if(Math.toDegrees(perspective * 2) < 90)
		{
			int x = (int) (OFFSETX + 200 * (minCompassFacing.radian - min / perspective * 2));
			fontrender.drawString(minCompassFacing.getInitials(), x, y, 14737632);
		}

FledgeXu


你先在fontrender.drawString打个断点看看是不是调用到了。
然后检查minCompassFacing.getInitials()是不是在调用的时候返回的是""。我注意到minCompassFacing#getInitials是有可能返回空值的。
你可试试把minCompassFacing.getInitials()换成一个确定的字符串,比如"Test"看看是否被渲染出来了。


FledgeXu


你的问题解决了吗?解决了请选择一个楼层作为答案,或者自己补充信息作为答案。


kabuki_


很抱歉我这么晚回复您的消息,这本该是昨天就做的事,但是我的pc端无法登陆V2McDev

问题已经得到了解决,原因是
int x = (int) (OFFSETX + 200 * (minCompassFacing.radian - min / perspective * 2));
这个算法得到的值超出了屏幕的范围,我正在修改算法使它变得更好


system


该主题在最后一个回复创建后7天后自动关闭。不再允许新的回复。