3

I am creating a space exploration game using SpriteKit and Swift. I am generating a space environment using Core Graphics. Since the player can control a spaceship which can move in every direction, I am creating 9 different backgrounds and positioning them around the users ship like this.

7 8 9
4 5 6
1 2 3

Player start in layer 5. My game uses 2048x1536 screen with aspectFill so I can support all resolutions.

I generate background with this code.

UIGraphicsBeginImageContext(CGSizeMake(width, height))
let ctx = UIGraphicsGetCurrentContext();

let max = width * height / CGFloat(starCountModifier)
for i in 0...Int(max)
{
    let x = arc4random_uniform(UInt32(width))
    let y = arc4random_uniform(UInt32(height))
    let alpha = CGFloat(Float(arc4random()) / Float(UINT32_MAX))

    CGContextSetFillColorWithColor(ctx, SKColor(red: 1.0, green: 1.0, blue: 1.0, alpha: alpha * alpha * alpha).CGColor)
    CGContextFillRect(ctx, CGRect(x: CGFloat(x), y: CGFloat(y), width: 2, height: 2))
}


let textureImage: UIImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext()
let texture = SKTexture(CGImage: textureImage.CGImage)
let sprite = SKSpriteNode(texture: texture)
addChild(sprite)

It works fine with one problem. Every background I create costs about 20-25mb of memory!

Even if I dont draw stars, it alone allocates this much memory by just creating SKSpriteNode out of that context.

Is there any way to optimize this texture data?

Thanks.

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
Aderae
  • 41
  • 1
  • You could create an SKEmitterNode with a particlePositionRange a bit larger than the screen size. Set the emissionAngle to the direction of movement-180 and set the particleSpeed to the speed your player is moving at. –  Feb 03 '15 at 16:06
  • Another option would be to make nine container nodes like you did and fill the with random SKSpriteNodes. You could then use these like your backgrounds but with a much smaller memory impact (you would also be able to modify them at any time without having to generate a new texture). If you need and example for any of my suggestions just ask. –  Feb 03 '15 at 16:08
  • just for the texture data alone, assuming 2048x2048 texture size with 32bit (4 bytes) per color you'll end up with a 16 meg texture. To optimize you can reduce the dimensions or color bit depth - however i don't know how to do the latter with what you are doing. What I also don't get is why you need to generate the pixel stars in the first place? Just use one star image in rhe background and repeat it, no one is going to notice they'll be repeating. Then add some other individual images on top: planets, large stars, nebula, etc – CodeSmile Feb 04 '15 at 09:09
  • Thanks for the answers. Actually I can optimize the code to run faster / better. However I want to know if there is any way to actually lower the texture memory allocated after using the context eg rasterizing it or compressing it somehow. Thanks! – Aderae Feb 04 '15 at 14:20
  • Did you ever find a way to free up memory from unused textures? – Kudit Aug 03 '16 at 22:06
  • Newer versions of Xcode include tile support, this is likely your best bet to address this issue. You might also interested in a texture compression approach for SpriteKit described in this answer http://stackoverflow.com/a/38679128/763355 – MoDJ Aug 05 '16 at 18:29

0 Answers0