How can I correct TouchPanel offset bug with Windows Phone XNA game on WP8 720p devices?

允我心安 提交于 2020-01-07 05:52:37

问题


XNA apps (WP7 or WP7.5 apps) that run on a WP8 720p device will get automatically letterboxed so the 480x800 BackBuffer size stays the same (for compatibility I presume).

This would be fine, except there appears to be a bug in the XNA compatibility layer. The TouchPanel reports touch locations that are off by the size of the top letterbox blank area.

This has two problems:

  1. The user's touches will appear to be off making gameplay and menu navigation difficult.
  2. Because it is off in the negative direction, the user will be unable to touch things at the very bottom of the screen at all.

I tried working around the issue by just factoring in 53 / 2 pixel offset (53 is the total amount of extra space in scaled coordinate, divide by two because it is only off by one letterbox bar - the one on the top). This does correct the touch location, but because TouchPanel internally clamps negative values to 0, it means that there is still a dead zone at the top of the game (because -22 through -1 should be translated to 0 through 22, but if all negative input values are clamped to 0 then information is lost and everything in the negative range will translate to 22 always).

Has anyone run into this and found a way to work around it?

I'v even tried resetting the TouchPanel.DisplayHeight/Width to the actual 720p values of the device and somehow it gets reset to 480x800 by the next frame update.


回答1:


I just got this working, the TouchPanel.DisplayHeight needs to be set to 853 (if you detect you are on one of these 720p devices) very early. I do it at OnNavigatedTo from the main Silverlight page (this is SL/XNA actually).

Next, you have to offset every touch location and gesture location by + 53.0f / 2.0f.

I'm not sure why this didn't work before, as this is the solution I mentioned above that didn't seem to work because TouchPanel kept resetting to 800 height.

But, I got it working in both a reduced repro (new SL/XNA app) and my main game app.




回答2:


I was working on a game a couple days ago. It was packaged for 7.1, but worked fine on the 720p emulator.

I don't know much about the compatibility layer, if it gets effected by the size of images then here goes : I created separate images for WVGA, WXGA and 720p. Used them and found out about the letterboxing and decided to use 720p images for all.

Probably doesn't help but there you go anyway.




回答3:


This is great solutio what I found from here: http://developer.nokia.com It's not just fixing issue with touch but it also remove black blocks from side. Of course depending about your programn this can cause some more issues since resolution and screen ratio will change.

if (Environment.OSVersion.Version.Major == 8)
{
    int? scaleFactor = null;
    var content = System.Windows.Application.Current.Host.Content;
    var scaleFactorProperty = content.GetType().GetProperty("ScaleFactor");
    if (scaleFactorProperty != null)
    {
        scaleFactor = scaleFactorProperty.GetValue(content, null) as int?;
    }
    if (scaleFactor == null)
        scaleFactor = 100;

    if (scaleFactor == 150)
    {
        SharedGraphicsDeviceManager sdm = SharedGraphicsDeviceManager.Current;

        sdm.PreferredBackBufferHeight = 800;
        sdm.PreferredBackBufferWidth = 450;

        TouchPanel.DisplayHeight = 800;
        TouchPanel.DisplayWidth = 450;
    }
}


来源:https://stackoverflow.com/questions/14166687/how-can-i-correct-touchpanel-offset-bug-with-windows-phone-xna-game-on-wp8-720p

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!