CGRectGet doesn't work well - swift

I'm working in Swift with XCode 6 and Sprite Kit and I want to have the biggest pixel in y, so i'm using this function :
CGRectGetMaxY(frame)
and I tested my code with the iPhone 6 simulator, so the value should be 1334.0 but the console shows me only 667.0 ...
What is wrong ?

It appears you're working in "point-space" and not "pixel-space". Depending on your settings, your image (I'm assuming that's where 1334 comes from) is likely at a #2x resolution.
Check your assets folder to make sure the image is defined how it should be.
Check your scale settings
UIKit (and most of iOS) runs on point-space, so try to work in these coordinates - they abstract device resolution (important now that we have #1x, #2x, and #3x devices).
Point-space:
The original iPhone was 320x480 pixels. In order to have your code be device-agnostic, when they went to Retina, the screen and all the logic stayed at a resolution of 320x480. These didn't always correspond to pixels, so the terminology changes to "points". That's why when you grab the screen size for an iPhone 4 it still reports 320x480.
Apple's Documentation on this.

Related

Cocos2d/Cocos2d-x Retina Issues

I have a multiplatform iOS/Android game that I programmed in Cocos2d-x. I have only one set of images to use (I'm only after high res devices) that get scaled according to the device's resolution. For example all images are for the iPad but if you are using a Galaxy S then the image locations and scaling are affected by GalaxyS_screenWidth/iPadscreenWidth.
This works fine for all Android devices and on iPad, iPad2 and old iPhones, but with Retina iPhone there is a problem. On the iPhone 4 all my images look extremely pixelated, a result from the images being scaled to iPhone non-retina resolution then scaled back up to fill the screen.
I tried enabling Retina Mode and the images are half of their intended size (maybe due to the usage of get winsize() which uses points) and scaling manually causes other sorts of problems. I tried playing with a lot of options and attributes but to no avail, so what should I do now?
EDIT:
This is not only a graphics issue, as text gets automatically scaled down then up and appears pixelated.
EDIT 2:
Fonts are bitmaps so my bad. But I don't want to use retina as all images are already retina by default. All of my images are set up for the iPad so for iPhone 4 I just scale them down a bit. This works with Android phones.
For example I have an image, depending on screen resolution obtained through getwinsize():
If current resolution width is 1024 then image stays the same.
If current resolution width is 900 then image gets scaled by 900/1024, no problem.
If device is iPhone 4 resolution width is 480, so image gets scaled by 480/1024, then cocos2d-x automatically scales the resulting image by 2 thus the pixelation. I tried using getwinsizeinpixels, I tried multiplying the screen size, I tried many things but nothing worked out of the box unless I am to redo many of my code.
So the question is, how can I just let the damn engine treat the iPhone 4 as an Android phone with resolution of 960x640?
If you want Retina resolution images to look like Retina resolution images, then those images need to be in Retina resolution (960x480).
If you first scale down the image to 480x320 and then upscale it on the device, it will of course look blurred. You can't magically make the Retina pixels appear from a lower resolution image by scaling it up.
SI couldn't get to the bottom of it so I employed a hack. I enabled Retina Display then I scaled everything x2 through code except for the text. Sounds stupid, but it worked, and pixelation is gone. Thanks everyone who spent time trying to help me.
I tried enabling Retina Mode and the images are half of their intended size
when you enable retina support, cocos2d gets images by appending #"-hd" to their provided filenames. Such images are meant to be double the "visual" size (in iOS terms, pixels vs. points), so that they can be sort of scaled down to make full use of device resolution.
If you have a look at the CCDirectorIOS class, you will find there a couple of methods dealing with this, especially those dealing with the scalingFactor. I don't know what kind of changes you should do to make it work, but if you step into those methods and look at the value of various objects, you might find a way to modify cocos2D default behavior for your specific case.
If this seems to complicated, one thing you could try is changing the CC_RETINA_DISPLAY_FILENAME_SUFFIX so that cocos2d will not look for specially-named files for the retina iPhone, but just use the normal ones.
For example all images are for the iPad but if you are using a Galaxy S then the image locations and scaling are affected by GalaxyS_screenWidth/iPadscreenWidth.
Another thing you might try is not using winSize, but winSizeInPixels, so that when you scale down, you down do it down to the point resolution, but just to the pixel resolution (which is double the point resolution).
Hope this helps.

App running on iPhone 4 has a 320x480 resolution instead of 640x960

I'm creating an iPhone app, which will run on iPhone 3 and iPhone 4.
To that point, I was trying to make the images adjust to the resolution, but it turns out that even when running on the iPhone 4, the size of my window is 320x480.
On the XIB I specified "fullscreen at launch" and for the ViewController I even specified "Wants fullscreen", but I can never get it to recognise the actual resolution.
Am I missing something?
Is it running full screen on your iPhone 4? (I don't mean what resolution is being reported, I mean are there black bars around the app - I bet there aren't).
iOS is 'magical' in that you just deal with iPhone 3 size things i.e. xibs etc and it scales it correctly for you on the iPhone 4. You will always see the smaller resolution when you query the display.
To get images to load the correct resolution just create two versions i.e.
myImage.png
myImage#2x.png
Make the second one exactly twice the size of the first and the iPhone will do the rest.
PS There's a property that's been added to UIImageView called scale - it's how the iPhone knows how large to render an image - have a look at the docs here.

Upgrading a cocos2d iPhone game to work at iPhone4 resolution using #2x

I am trying to add art to my game for the iPhone4 resolution. The images i am using are twice the original and i have added the #2x suffix to the image names.
When i load in a sprite like this:
[CCSprite spritewithFile:#"example.png"];
where example.png is the original image and example#2x.png is the scaled up version. When running on the original iPhone it loads the low res image and when running on the iPHone4 it loads the high res image, but it doubles the size of the image. So the high res image appears the same on the iPhone4 as on the original iPhone. Each pixel on the image takes up 4 pixels on the screen.
Any idea why this might be happening? Is there something I am supposed to do to tell the app not to double the size of the art?
Are you using the latest version of cocos2d?
The entire cocos2d API was converted
to Points. Previous versions were
using Pixels.
If your using v0.99.4:
cocos2d v0.99.4 has RetinaDisplay
support, however it required you to
use two different sets of positions
depending on the device , since the
API was in Pixels. (more)
If your using >= v0.99.5-rc0
But in v0.99.5-rc0 (and newer) the only thing that you have to do is[...] (more)
Have you read the "Retina Display in cocos2d" section of the cocos2d for iPhone wiki? (It pretty much tells you all you need to know.)

Retina display compatibility

For our iPhone app, we are using 50 x 50 user image (from facebook). It works fine for iPhone 3G and 3GS but not for iPhone 4 due to its high resolution display. For iPhone 4 we tried to send 100 x 100 (variable height) image but it is not working for obvious reasons. Any thought?
You don't need to do anything different for the Retina Display. Using exactly the same image as you have on the older iPhone will work exactly the same.
If you have a higher resolution image that you'd like to display instead, then if you save the file with a #2x suffix then, if the device has a Retina display, it will pick that instead. In most cases it should not require any extra code.

High Resolution iPhone Background

According to Apple's website, the iPhone 4 is 960-by-640-pixel resolution at 326 ppi. I made an image with these specs, however when I put it onto my iPhone as a background for one of my applications, it is way too big and you can only see the upper left corner of the image. How do I make it fit and how do I make a high resolution iPhone 4 background?
You need to give it the name "someImage#2x.png" with the important part being #2x so they can apply the scale correctly.
Realize though that you also need "someImage.png" which is half that size for devices without the retina display.
I always use the "someImage.png" file as the initial image and then the OS will automagically switch to the #2x image during runtime so don't worry about that. Just set it to the standard 320x480 or 480x320 and let the OS do the rest.
Are you using an UIImageView? You should have two versions of your images
image.png - normal 320x480 image for older phones
image#2x.png - for iphone4 with the 640x960 resolution
When you set the file location (be it programically or in Interface Builder) just tell it about the 'image.png'. The iPhone4 will automatically look for the '#2x' version.