Android PreferredBackBufferWidth not working correctly

hey guys,

I found using:-
PreferredBackBufferWidth = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width;
PreferredBackBufferHeight = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Heigh;
in windows and linux, returns the correct numbers but compiling for android just returns zero?
i’m obviously missing something, how do you get the current android screen size?

Thanks in advance
Paul

1 Like

Try to ApplyChanges() again after LoadContent…

Why would i ApplyChanges when a Graphic device manager hasn’t even been created yet?
Heres the Code

graphicsDM = new GraphicsDeviceManager(this)
{
IsFullScreen = bFullScreen,
PreferredBackBufferWidth = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width,
PreferredBackBufferHeight = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height,
SupportedOrientations = DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight,
SynchronizeWithVerticalRetrace = true
};
the above works fine with windows and Linux but PreferredBackBufferWidth and Height return a ZERO value when this routine is called on an ANDROID build, what i’m asking is how to get the default values from ANDROID so i can use the current default as the screen size?
This is the very FIRST piece of code in Game1 class, because of the null return values, it won’t matter how many calls to applychanges() i do, an exception will always occur because there can’t be any RenderTargets made with null values?
Hope this helps with what i’m trying to explain?
Paul

In Ver. 3.5 I GDM.ApplyChanges() to the device inside Initialize() running fine… but has some strange effect on Ver. 3.6/7, I reapply GDM_ApplyChanges() inside LoadContent and it works perfectly.

GDM.PreferredBackBufferHeight =_GDM.GraphicsDevice.Adapter.CurrentDisplayMode.Height;
GDM.PreferredBackBufferWidth =_GDM.GraphicsDevice.Adapter.CurrentDisplayMode.Width;

I don’t have DefaultAdpter… only .Adapter… Works on Windows and Android on my side ^ _ ^ y

There is no “Adapter” part of GraphicsDeviceManager, there is Adapter**“S”** which is a read only “Collections” of graphic Devices so the code would read

int width=GraphicsAdapter.Adapters[0].CurrentDisplayMode.Width;
Which on and ANDROID build still = “0”
yet on Windows returns the correct width, once again your talking about Applying changes to a graphic device that hasn’t even been defined yet, Here is Game1 Constructor in full

#region GAME1 (Main Entry Constructor)
    /*********************************************************************************************
     * Function Name : Game1
     * Description : Constructor
     * ******************************************************************************************/
    public Game1()
    {
      // put an #if DEBUG here if you want full screen
#if (DEBUG)
      bFullScreen = false;
#else
      bFullScreen = true;
#endif
/* Below RETURNS ZERO IF BUILDING ANDROID!!!!!!!!*/
      int width = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width;
      int height = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height;

      graphicsDM = new GraphicsDeviceManager(this)
            {
              IsFullScreen = bFullScreen,
              PreferredBackBufferWidth = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width,
              PreferredBackBufferHeight = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height,
              SupportedOrientations = DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight,
              SynchronizeWithVerticalRetrace = true
            };
      sContent = Content;
      sContent.RootDirectory = "Content";
#if (WINDOWS)
      this.Window.IsBorderless = true;                                          // remove all window constraints
#endif
    }
    #endregion

As you can see there is NOWHERE to ApplyChanges, and NO POINT in ApplyChanges() to a graphicsdevicemanager that has its PREFFEREDBACKBUFFERWIDTH & PREFFEREDBACKBUFFERHEIGHT set to 0
Show me you entire Game1 Class that sets the PrefferedBackBufferWidth/Height from a default ANDROID screen

Thanks

Here’s my trim down code on device initialization for Windows & Android this works on my end. let’s hope other gets in if this does not work on your end… cheers :slight_smile: Y

internal class GameFrameWork : XF.Game
{

    ZD.DeviceSetting _DeviceSetting = null;

    XF.GraphicsDeviceManager _GDM = null;
    XFG.GraphicsDevice       _3GD = null;
    XFG.SpriteBatch          _2GD = null;
    

    internal GameFrameWork(ZD.DeviceSetting pDeviceSetting )
    {
        _DeviceSetting = pDeviceSetting;
        _GDM           = new XF.GraphicsDeviceManager(this);
    }


    protected override void Initialize()
    {
        _M_ApplyDeviceChanges();

        base.Initialize();
    }


    protected override void LoadContent()
    {
        _M_ApplyDeviceChanges();

        base.LoadContent();
    }


    private void _M_ApplyDeviceChanges()
    {
       
        if (_DeviceSetting.devicePlatform == ZD.DevicePlatform.WindowsDeskTop)
        {

            //--> Preferred device settings
            //                
            this.IsFixedTimeStep = _DeviceSetting.isFixedTimeStep;
            this.IsMouseVisible  = _DeviceSetting.IsMouseVisible;
            //
            _GDM.GraphicsProfile           = _DeviceSetting.graphicsProfile;
            _GDM.IsFullScreen              = _DeviceSetting.isFullScreen;
            _GDM.PreferMultiSampling       = _DeviceSetting.isMultiSampling;
            _GDM.PreferredBackBufferFormat = _DeviceSetting.preferredBackBufferFormat;
            //
            _GDM.PreferredBackBufferHeight = _GDM.GraphicsDevice.Adapter.CurrentDisplayMode.Height;             
            _GDM.PreferredBackBufferWidth  = _GDM.GraphicsDevice.Adapter.CurrentDisplayMode.Width;             
            //
            _GDM.PreferredDepthStencilFormat    = _DeviceSetting.preferredDepthStencilFormat;
            _GDM.SupportedOrientations          = _DeviceSetting.screenOrientations;
            _GDM.SynchronizeWithVerticalRetrace = _DeviceSetting.isVerticalSynch;

            //--> Presentation settings
            //
            _GDM.GraphicsDevice.PresentationParameters.PresentationInterval = XFG.PresentInterval.Default;

            //--> Apply changes
            //
            _GDM.ApplyChanges();

        }


        if ( _DeviceSetting.devicePlatform == ZD.DevicePlatform.Android )
        {

            //--> Preferred android device compatible settings
            //
            this.IsFixedTimeStep             = _DeviceSetting.isFixedTimeStep;
            //
            _GDM.GraphicsProfile             = _DeviceSetting.graphicsProfile;
            _GDM.IsFullScreen                = _DeviceSetting.isFullScreen;
            _GDM.PreferMultiSampling         = _DeviceSetting.isMultiSampling;
            _GDM.PreferredBackBufferFormat   = _DeviceSetting.preferredBackBufferFormat;
            _GDM.PreferredDepthStencilFormat = _DeviceSetting.preferredDepthStencilFormat;
            _GDM.SupportedOrientations       = _DeviceSetting.screenOrientations;
            //
            _GDM.PreferredBackBufferHeight  = _GDM.GraphicsDevice.Adapter.CurrentDisplayMode.Height;
            _GDM.PreferredBackBufferWidth   = _GDM.GraphicsDevice.Adapter.CurrentDisplayMode.Width;


            //--> Presentation settings;    
            //
            _GDM.GraphicsDevice.PresentationParameters.PresentationInterval = XFG.PresentInterval.Default;


            //--> Apply changes
            //
            _GDM.ApplyChanges();
        }


        //--> Save handles
        //
        _3GD = this.GraphicsDevice;
        _2GD = new XFG.SpriteBatch(this.GraphicsDevice);            
        
    }


}

EDIT : I’m using Ver. 3.6

Without knowing what’s in XF or XFG or ZD, the code is pretty much meaningless I forgot to mention that I’m using an openGL build but that shouldn’t make a difference.
Basically back to my original question
how do you get the default android screen size?

Sometimes things kick in later in the call sequence or after the first update. If you query the screen size at later points like in Initialize(), LoadContent() and Update(GameTime) do you get zero the whole time?

yep that did it, took loading 11 screens, 15 sound effect and two songs but yeah
GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width/Height
worked after that, i’ll mess around with it some see if i can figure out exactly when it will work

Many thx