How to make my Gideros game adaptable to all screen sizes? - lua

I am quite new to Gideros and game development as well.
I am trying to build a game, it looked fine with the Gideros player, but when I tried with an Android phone, the background was way too small. I changed its properties to autoscaling so as to fit in with the width. The background now fits in the width, but other objects seem to go to absurd locations.
Though I was using W = application:getDeviceWidth(), H = application:getDeviceHeight(), and while setting up the location, used W/2 and H/2 instead of hardcoding it.
However, this object that seems to shift to rightmost bottom end(in landscape left mode) was right at its center if I do not apply fit width property.
What can I do to fix it?

You should use W = application:getContentWidth() , H = application:getContentHeight() which would return logical dimensions that are used when in scaling mode.
Basically it all brings down to these points:
1) Choose the scaling mode that is proper for your game (Letterbox being most popular)
2) Choose logical dimensions for your game and create all the graphics for logical dimensions you set in the project properties (recommended 480x800 or 640x960)
3) Create backgrounds a little more bigger than logical dimensions to cover whitespaces on devices with different ratios
4) Use absolute positioning (http://appcodingeasy.com/Gideros-Mobile/Ignore-Automatic-Screen-Scaling-when-positioning-objects) for objects that need to stick to sides of the screen as on screen buttons for example
5) (Optionally) prepare bigger graphics in in some fixed ratio coefficient and use Automatic Image Resoltuion feature to automatically load them for bigger devices
More information available here:
http://members.giderosmobile.com/knowledgebase.php?action=displayarticle&id=79
Addition: (Difference between device and logical dimensions)
Device dimensions is exactly what device has. Meaning on an iPhone 3GS it will return width as 320
But logical dimensions are exactly what you set in your project properties. No matter what resolution you have, the logical dimensions will always be the same. They basically will be scaled based on the scale mode you choose.
Here are more specifics on that topic: http://appcodingeasy.com/Gideros-Mobile/Difference-between-content-logical-and-device-dimensions-in-Gideros-Mobile
So if you are developing only for one specific resolution, you can use Device dimensions, otherwise it is suggested to use Logical dimensions with the scale mode you find suitable.

Related

How can I create a UIView that is one inch square independent of the device it is on?

I want to create a UIView that takes up a one inch square on every iOS device. So far, I have looked up the pixel size of my current test-device and translated a CGFloat to an inch using my test-device's pixel size times its UIScreen's scale factor. Is there any clever way to use built-in information about the UIDevice to get a CGFloat that amounts to one inch across the screen when applied to a UIView's width and height? Instead of creating cases where I manually account for every possible device pixel size, then have to update it every time a new pixel size comes out, I would like to somehow figure out the pixel size using the device's characteristics.
The extension at https://github.com/marchv/UIScreenExtension works to do what I want to do by compiling data about all possible screen PPIs and comparing the UIDevice Model against them to get the CGFloat representing a centimeter of physical distance. Thanks to NobodyNada - Reinstate Monica's comment leading me to this extension by linking How to find PPI programmatically with precision.
I am accepting for now that there may not be a way to get physical distance just from the UIDevice's intrinsic properties, as another answerer to that question suggests.

supplying the right image size when not knowing what the size will be at runtime

I am displaying a grid of images (3rows x 3 columns) in collection view. Each image is a square and its width is determined to be 1/3 of collectionView's width. Collection view is pinned to left and right margin of the mainView.
I do not know what the image height and width will be at runtime, because of different screen sizes of various iPhones. For example each image will be 100x100 display pixels on 5S, but 130x130 on 6+. I was advised to supply images that exactly matches the size on screen. Bigger images often tend to become pixelate and too sharp when downsized. How does one tackle such problem?
The usual solution is to supply three versions, for single-, double-, and triple-resolution screens, and downsize in real time by redrawing with drawInRect into a graphics context when the image is first needed.
I do not know what the image height and width will be at runtime, because of different screen sizes of various iPhones. For example each image will be 100x100 display pixels on 5S, but 130x130 on 6+
Okay, so your first sentence is a lie. The second sentence proves that you do know what the size is to be on the different screen sizes. Clearly, if I tell you the name of a device, you can tell me what you think the image size should be. So, if you don't want to downscale a larger image at runtime because you don't like the resulting quality, simply supply actual images at the correct size and resolution for every device, and use the correct image on the actual device type you find yourself running on.
If your images are photos or raster type images created using a raster drawing tool, then somewhere you will have to scale the original to the sizes you want. You can either do this while running in iOS, or create sets up front using a tool which can give you better scaling results. Unfortunately, the only perfect image will be the original with everything else being a distortion of the truth.
For icons, the only accurate rendering solution is to use vector graphics. Tools like Adobe Illustrator will let you create images which you can scale to different sizes without losing clarity. Unfortunately this still leaves you generating images up front. You can script this generation using most tools and given you said your images were all square, then the total number needed is not huge. At most you need 3 for iPhone (4/5 are same width, 6 and 6+) and 2 for iPad (#1 for mini/ipad1 and #2 for retina).
Although iOS has no direct support I know of for vector image rendering, there are some 3rd party tools. http://www.paintcodeapp.com/ is an example which seems to let you import vector images or draw vector images and then generate image code to run in your app. This kind of tool would give you what you want as the images are now vector drawings drawn at the scale you choose at run time. $99 though.
There is also the SVGKit (https://github.com/SVGKit/SVGKit), but not sure how good/bad this is. It seems to let you simply load and render direct from SVG files. Might be worth trying.
So in summary, I think you either generate the relatively small subset up front using a tool you can control the output from, take the hit in iOS and let it scale the images or use a 3rd party vector to image rendering kit which would give you what you want.

Universal 2D Game Assets and Absolute Node Positioning

I have a question regarding universal game assets and absolute positioning of a SKNodes in Sprite Kit (iOS 8+).
I will try to present my problem through an example as follows:
Imagine a 2D top down game with a SKSpriteNode which represents a house. A house has multiple child SKSpriteNodes which represent chairs, desk, sofa, etc.
I have 3 versions of house asset:
1x - 200 x 200px (Non-retina iPads),
2x - 400 x 400px (Retina iPhones and iPads),
3x - 600 x 600px (iPhone 6 Plus).
Important:
Child nodes (chairs, desk, etc.) positions are defined in a .plist file. Something like this (JSON representation):
children: [
{
position = {20,20};
},
...
]
Since the position is defined in points and not in pixels, everything gets positioned like expected according to device screen scale. For 1x devices the position stays {20,20}, for 2x position is {40,40} and for 3x the position is {60,60}.
Problem:
The problem is that 200x200px and 400x400px assets are way to small for iPad devices in order to achieve similar look and feel on all devices.
Question:
How to successfully present/import assets in a way that would enable
me to achieve similar (if not the same) look and feel on all
devices/screen sizes without breaking child nodes positioning?
My takes:
Take 1:
I could simply use the existing 400x400px assets on Non-retina iPad devices and 600x600px assets on Retina iPad devices for the house node but the positioning of a child nodes would become broken. This is because the child position value wouldn't change and would still be {20,20} and {40,40} for iPad devices respectively, while the assets would be bigger. This would yield inaccurate child positions relative to the house node.
Take 2:
I could also scale the SKScene size (zoom effect) while using the normal 200x200px and 400x400px sized assets for iPad devices respectively. This works and it keeps the child nodes positioning working but the rendered quality of the scene/assets is not good as it should be. Also, this feels like a hack and we don't want that.
Take 3:
I could also use twice as big assets for iPad devices and double the child nodes position at the runtime. In this case I would use a 400x400px asset for non-retina iPad devices and a new 800x800px asset for retina iPad devices. While this looks great and keeps the child nodes positioning working, it seems like a really big hack fixing child node position during runtime with this:
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
position.x *= 2.0f;
position.y *= 2.0f;
}
Thank you for taking the time to read the question!
I could simply use the existing 400x400px assets on Non-retina iPad
devices and 600x600px assets on Retina iPad devices for the house node
but the positioning of a child nodes would become broken. This is
because the child position value wouldn't change and would still be
{20,20} and {40,40} for iPad devices respectively, while the assets
would be bigger. This would yield inaccurate child positions relative
to the house node.
You can simply scale your house node (not the scene) to a larger size. All you need to do is set the scale on your house to a value that looks good on larger devices. And in fact, instead of checking for iPad we can come up with a formula that sets the scale depending on the size of the screen. Something like the code below should work. Note that it assumes your house is positioned perfectly on a iPhone 4 and it will consistently scale to all larger screens. Note that you really could pick any arbitrary size as your base case, but choosing the smallest screen and scaling up is easiest. Just be sure to provide larger textures so that the textures don't become blurry when scaled.
[house setScale:self.scene.size.width/320.0];
OR
You could use two nodes. A root node for holding the "actual" position, and then an image node child for displaying the image. This will allow you to separate your positional data from what's being displayed. You could resize and position your child image node however you want without messing with the actual position of the root node. You could even include this extra image node data in your JSON.
I could also scale the SKScene size (zoom effect) while using the
normal 200x200px and 400x400px sized assets for iPad devices
respectively. This works and it keeps the child nodes positioning
working but the rendered quality of the scene/assets is not good as it
should be. Also, this feels like a hack and we don't want that.
This option can definitely work if your App can handle the different aspect ratios in someway. For example you could allow scrolling the scene if the scene is scaled larger than the device screen. The loss in quality occurs because you are scaling the textures larger than their expected size. You need to provide larger textures to keep the quality high when zooming. In this case you could probably just use your 600x600 images (or maybe even larger) and let it scale with zoom. For example, in my RTS Sprite-Kit game for OS X I scale the entire scene so I get the same look across all devices. And I don't lose any quality because I make sure to provide very large textures so there is no loss in quality while scaling.
I could also use twice as big assets for iPad devices and double the
child nodes position at the runtime. In this case I would use a
400x400px asset for non-retina iPad devices and a new 800x800px asset
for retina iPad devices. While this looks great and keeps the child
nodes positioning working, it seems like a really big hack fixing
child node position during runtime with this:
This could also work, especially if your iPad requires custom layout. However, if possible avoid checking specifically for iPad and instead use the screen size to create layout rules so your nodes dynamically adjust on all screen sizes consistently (See the line of code above). Sometimes this is not possible if your iPad layout is very different from the iPhone, in which case you will have no choice but to check for iPad.
All three of these solution are good. I wouldn't consider any one of them "hacky." They all work for different purposes. You need to find the solution that works best for your game.
I would also recommend you see my two answers below. Not sure but they may help you with understanding universal positioning and scaling in Sprite Kit.
https://stackoverflow.com/a/25256339/2158465
https://stackoverflow.com/a/29171224/2158465
Good luck with your game, let me know if you have any questions.
There's no easy way to do what you want. One approach would be to use a fixed screen size on your devices. The iPhone 5 all the way up to iPhone 6+ all use a 16:9 aspect ratio for their screens. Whereas the iPad and iPhones 4s and earlier, all use a 4:3 screen aspect ratio.
Before presenting your GameScene, you can determine the screen's aspect ratio and then set a fixed view size like this for 16:9:
GameScene *startGame = [[GameScene alloc] initWithSize:CGSizeMake(736, 414)];
startGame.scaleMode = SKSceneScaleModeAspectFit;
or this for 4:3
GameScene *startGame = [[GameScene alloc] initWithSize:CGSizeMake(1024, 768)];
startGame.scaleMode = SKSceneScaleModeAspectFit;
The exact values really do not matter, only the ratios.
Knowing the exact screen size will allow you to place objects in more precise manner regardless of iPhone 5 screen or 6+ screen.
Using image assets, you can also specify iPad versions of an image.
However there's not really a way around adding some extra logic to your app, branching depending on whether or not your running on the iPad, and adjusting the position manually.
We could discuss how to best incorporate that though: I'm not a fan of this "if I'm not this device" checks all throughout the code. Create an abstract superclass and two subclasses, each handling layout (or whatever you may want to call it) for one interface idiom. You will the only need to check once (when instantiating these) and polymorphism will take care of the rest.
You can use a software as PaintCode to dynamically generate texture perfectly sized to your need.
All you have to do is to define the frame' dimensions for each of your devices.

Auto adjust alignment to screen size

I am working on a BlackBerry App that has a lot of ImageButtons, LabelFields and MessageBoxes. What appears to be perfect on one screen size, seems a mess on the other. For instance, Vertical Field Managers that are neatly aligned center with LabelFields, are left/right aligned on bigger screens. Images that cover the width of the screen appear too small on larger screens. Is there some mechanism to auto-align and dynamically change images with respect to the screen size. Any ideas and documents that can help in this regard?
Here are some tips for making screens that look good on almost all devices:
Use less images. If you have to use images, use atleast 3-4 for different screen sizes. for example if you need to have an image as the screen header, use images with widths 320px, 480px and 640px. Load image depending on the width of the screen.
Do not use pixel measurements. Use point measurements instead. Most of the devices are similar in terms of physical size, whereas they have huge difference in pixel density. Using this you can have a screen which will look exactly identical on curve (320x240), bold2 (480x360) and bold 4 (640x480). If you notice, they have the same aspect ratio and similar physical size.
Do not hardcode positions. Instead use FIELD_HCENTER and DRAW_HCENTER etc for fields.
Do not use fonts with fixed pixel height. Use fixed point height instead.
If using custom fields, make sure that they can automatically expand according to device and pixel density.

Printers' points vs. iOS points

When programming for the iPad, font (and other) sizes are specified in "points." I have seen reference to a point as a pixel that is independent of screen resolution. But I am having trouble finding definite confirmation of how big a point is in real terms (that is, in terms of inches). Is a point equal to one pixel on the standard iPad screen, so 1pt = 1/132in? And then, to confirm, this means that an "iOS point" is a different unit than the printer's point = 1/72in?
Thanks.
See here and here (scroll down to points vs. pixels) for the official word. Basically, a point is one pixel on a non-retina device (so the size varies between the iPad and the iPhone - it isn't related to a printer's point) and 2 pixels on a retina device (which has twice the number of pixels in each direction).
Drawing and positioning is done in points to allow the same code to run on both types of device - the frameworks will fill in the gaps to make drawing smoother on retina devices.
An iPad point is different to an iPhone point, which is different to a printers point, to answer your question.

Resources