XNA: get screen's width and height - xna

how do i get the width and height of the entire screen in XNA?

Empirically I've found that in XNA 4.0 I need to use
GraphicsDevice.Viewport.Width
GraphicsDevice.Viewport.Height
when running windowed mode, as I find
GraphicsDevice.DisplayMode.Width
GraphicsDevice.DisplayMode.Height
gives me the resolution of the entire screen.
Hopefully this helps someone else out.

This seems to be it (just googled for "xna screen width height" myself):
GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height
GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width

GraphicsDevice.Viewport.Bounds - this returns Rectangle2D and it has parameters Width and Height.

Related

XCode12: How to set up constraints in an image to make it responsive to an screen?

I'm having issues with constraints in XCode 12: Even if I describe accurately all measures involved, there is no way apparently to resize a 850x600 image to make it fit proportionally in the screen of an apple device. As you can see in the image, the constraints are defined to make the original image fit WITH BORDERS on the Iphone's screen, but nevertheless is turns out to be a disaster afterwards...
As you can see in the screen capture, the image overflows the screen when the model device is changed. So...we have a problem. And the answer or clue that I'm seeking night and day is...
How to make this image to become RESPONSIVE to screen size, and keep it's proportions. No way how the hell something so basic is so difficult in XCode...
must edit... look how the alignment constrainst are shown in grey...no idea why...this avoids me to try the solution provided by the good fellow above...
It seems you've defined constraints to center the imageView and having it a size of 850x600 pixels. This will keep the image of that exact same size, regardless the device it is shown on.
If you want the image to shrink, keeping the aspect ration you need to define constraint for the top, bottom, leading and trailing edge.
And you need to set the imageView's contentMode to "Aspect Fit", which is the default, afaik.
This is an image shown on an iPhone 11:
And this the same image shown on an iPhone 4s:
Edit:
One way to add those constraints is to select (highlight) the imageView and click on the button with the little square in the middle surrounded by those "T"s. :-)

maintaining aspect ratio on launch screen

I expect there's a simple answer here. If so, sorry and thanks.
I'm trying to make my launch screen for all the different iPad and iPhone sizes. On the launch storyboard I have two UIViews, one full size for the background (backView) and one in the shape of a square (squareView). I'm trying to keep the square from stretching (i.e. I'm trying to maintain a 1:1 aspect ratio) and make it 0.75 the width of whatever device the app runs on. I know that there will be varying amounts of space below, but how can I keep the square from distorting when I switch devices?
I've been researching the constraints and diddling with them for hours. I expect someone will tell me it's easy, just do ... what?
First, I've centered my rect horizontally and vertically
Then I've set the rect's width to x0.75 of his superview
For the final step I'm setting his ratio to 1:1
That's it! Final result:

How can I use UIImageView with aspectFill and alignment to the top?

How could I use UIImageView with aspectFill and alignment to the top? Auto-layout or programmatically don't mind.
It can be done, but you are talking about quite a bit of coding. Let's start with a detailed description of what you are asking for.
We'll start with an iPhone6s in portrait mode with a screen size of 667x375 points, or a regular height by compressed width in size classes.
(Keep in mind, on any iPad you'll also need to work with slide-over and split screen too.)
But for now we'll go with 667x375. Into that we'll add our imageView as a square size of 375x375. Along the top of the screen when in portrait mode.
(Keep in mind that with AutoLayout you'll also have it pinned, and without putting some code in to determine if it's in landscape or portrait, otherwise, that 375x375 will shrink in landscape if you have other views underneath it.)
So now, stripped out to the most minimum (for now), we are talking about how to pin "an image" to the top of a UIImageView that is 375x375 in size and with a contentMode of aspectFill.
We need to find the size of the image next, based on both the largest dimension (height or width) AND how that dimension compares to the size of the imageView, calculate how the imageView will normally render it. The key figure you want is the height it will render it as
For instance, how will a 480x1020 (HxW) image fit into it? The 1020 width will be taken down to 375, so the height will be 480/(1020375) or 176.4705 points.
Always do the math for both dimensions - if your imageView isn't square or if the height isn't the "bounded" dimension, you'll be off.
Once you've calculated what the "Aspect Filled" height will be, check it against the dimensions of the imageView, and adjust the height of it as needed.
Here's where the real work comes in. If you are using AutoLayout, you can do a heightAnchor adjustment, but depending on how you pinned those other views things may be laid out really bad after you do. If you are using frames and CGRects be aware that you have to take into account iPad slide-out and split screen, etc.
Finally, if you want your app to load a different image in that imageView after it was loaded, you will have to "reset" the height back to what it originally was, at least in your calculations.
My recommendation is to find an alternative to what you are asking. For instance, put a small border around the image view to let the user know the true dimensions of that view.

Will auto layout scaling always produce the same result no matter the resolution?

Is there a way to have each preview/simulator match the look of the storyboard, look for look, regarding the same location and aspect ratio no matter the screen size? In other words, in my storyboard I have the look I am looking for. What properties/details would I have to add to have the storyboard be replicated the same despite the different iPhone resolutions? The iPhone 5.5" looks incredibly close to my desired results while the 4" cuts some of my buttons off? What am I missing or what should I add?
This is what Auto Layout is for. Your Storyboard is a fixed size whereas there are many different device screen sizes. That's why you use Auto Layout to position view relative to one another so that no matter the device, the layout looks right. In your case, your content will probably need to scroll to fit in all device screen sizes.
Yes. The reason behind this is vector image generation during stretch in or out. If you convert it into raster images. Expected result is not guaranteed.

IOS Auto Layout scale down

I'm trying to create this view where images stick together and get resized when the device get smaller. Every thing works, but the only thing that I don't seem to get to work is the scale down.
As you can see from the screenshot there are two boxes that need to be centered. The space between the boxes always remains the same as do the width and height (e.g. 100x100 or 50x50 or 25x25 etc.). But it's this simple resize where the boxes get smaller that doesn't work. Do you guys have any clue what kind of auto layout property I should use? Or the combination of properties? Help is much appreciated.
Regards
You can do this all in the storyboard. Give the views whatever spacing you want to the left and right edges, as well as a space between them. Select the 2 boxes, and choose "Equal Widths" and "Equal Heights". Choose one of the boxes, and give it a aspect ratio constraint of 1:1. Don't give the boxes any fixed width or height constraints.
I am absolutely agree with #rdelmar answer ... +1 for the answer...
Actually for a maintain ratio of any object you need to use aspect ratio like 100x100 in bigger screen and 50x50 in smaller screen...same for other view sizes...
and as we want both object to be same in terms of height and width, we have to give equal width and height of one object to another...
For a better understanding how it done ...here is the visual format..
And Here is the output as you want...

Resources