Skip to content

Instantly share code, notes, and snippets.

@romanov
Last active August 29, 2015 14:06
Show Gist options
  • Select an option

  • Save romanov/d8c8a1d67a3ccaa21933 to your computer and use it in GitHub Desktop.

Select an option

Save romanov/d8c8a1d67a3ccaa21933 to your computer and use it in GitHub Desktop.
MonoGame's responsive screen
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace im.romanov.subgame.Utility
{
public static class UIPosition
{
private const int MinResolutionWidth = 1366;
private const int MinResolutionHeight = 768;
public static Point GetCentralPixel()
{
var curRes = Game1.CurrentResolution;
var x = (curRes.X / 2);
var y = curRes.Y / 2;
return new Point(x, y);
}
public static Matrix getResolutionMatrix()
{
var centerX = (Game1.CurrentResolution.X - MinResolutionWidth) / 2;
var centerY = (Game1.CurrentResolution.Y - MinResolutionHeight) / 2;
return Matrix.CreateTranslation(centerX, centerY, 0);
}
public static Matrix InvertResolutionMatrix()
{
return Matrix.Invert(getResolutionMatrix());
}
public static Vector2 PlaceTextureCenter(Texture2D texture)
{
var width = texture.Width;
var height = texture.Height;
var centerX = (Game1.CurrentResolution.X - width) / 2;
var centerY = (Game1.CurrentResolution.Y - height) / 2;
return new Vector2(centerX, centerY);
}
public static Point PlaceResponsive(int x, int y)
{
if (Game1.CurrentResolution.X > 1366)
{
var centerX = (Game1.CurrentResolution.X - MinResolutionWidth) / 2;
var centerY = (Game1.CurrentResolution.Y - MinResolutionHeight) / 2;
return new Point(centerX + x, centerY + y);
}
return new Point(x, y);
}
public static Vector2 PlaceResponsiceVector(int x, int y)
{
var point = PlaceResponsive(x, y);
return new Vector2(point.X, point.Y);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment