2
\$\begingroup\$

I come here, because i'm trying to add some "menu" interface in my small XNA Game . I found on internet a nice library adviced by many devellopers. Till now everythink is ok, the problem is my C# code logic. I try to allow the player (user) to change the screen resolution . The resolution and screen are both of them change . The problem is about the "cursor" of my computer mouse . It's really weird, i don't why but the cursor of my mouse does not select the right elements .It's look like a gap persisted between the old window.

Here is the code i use, as you can see , very simple. And should be normally work. I hope you have some idea why . I'm lost :'(

void btnApply_Click(object sender, Controls.EventArgs e)
{
  Manager.Graphics.IsFullScreen = chkResFull.Checked;

  //Resolution set by default
  int w = 1024;
  int h = 768;

  if (rdbRes1024.Checked)
  {
    w = 1024;
    h = 768;
  }
  else if (rdbRes1280.Checked)
  {
    w = 1280;
    h = 1024;
  }
  else if (rdbRes1680.Checked)
  {
    w = 1680;
    h = 1050;
  }

  Manager.Graphics.PreferredBackBufferWidth = w;
  Manager.Graphics.PreferredBackBufferHeight = h;      

  Manager.Graphics.ApplyChanges();      
}

Here is the screen shot of my original screen ( 800/480 ) enter image description here Here is the screen shot after change to the screen resolution ( 1280/860 ) enter image description here

\$\endgroup\$
3
\$\begingroup\$

When you resize your game resolution you need to update every single rectangle in your game (every button, etc) and multiply it by a proper scale, so that the "hitboxes" would work, which would be a lot of work. So, a smarter way of achieving the same result is simply by multiplying your mouse position by a Vector2 scale = new Vector2(originalWidth / width, originalHeight / height);.

So, for example, no matter the game resolution, if your mouse is in the middle of the screen, you will see the mousePosition as (400,240);

int GameWidth = 800; // the original game's width
int Gameheight = 480; // the original game's height

Matrix ScaleMatrix = Matrix.CreateScale(
    (float)graphics.PreferredBackBufferWidth / GameWidth,
    (float)graphics.PreferredBackBufferHeight / GameHeight, 
    1f
    );
Vector2 Scale = new Vector2(ScaleMatrix.M11, ScaleMatrix.M22);

Vector2 mousePosition = ...; // calculate mousePosition as you normally do
mousePosition = mousePosition / Scale;

//Update the input in Game_Reset event
private void Graphics_DeviceReset(object sender, System.EventArgs e)
    {
        if (arcadia.manager.RenderTarget != null)
        {
            //These steps are already done by the manager on Graphics Device Reset.
            //Manager.RenderTarget.Dispose();
            //Manager.RenderTarget = CreateRenderTarget(); 
            arcadia.manager.Input.InputOffset = new InputOffset(0, 0, arcadia.manager.ScreenWidth / (float)arcadia.manager.TargetWidth, arcadia.manager.ScreenHeight / (float)arcadia.manager.TargetHeight);
        }
        Window mainWindow = arcadia.manager.Controls.First() as Window;
        if (mainWindow != null)
        {
            mainWindow.Height = arcadia.manager.TargetHeight;
            mainWindow.Width = arcadia.manager.TargetWidth;

        }
    }

ScaleMatrix should be the same matrix you use when using you call: spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, ScaleMatrix); to scale your sprites.

\$\endgroup\$
  • \$\begingroup\$ Thank's a looot ! I find the problem about the GUI , need add some code in Game_Reset event for update the input mouse position. But about calcul the scale for the spriteBatch you help me nice ;-) \$\endgroup\$ – Mehdi Bugnard May 18 '15 at 21:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.