Open History to see the last commit
1import AppKit
2import SpriteKit
3import ExpeditionCore
4
5/// Renders the real character rig so the icon stays faithful to Jerry's artwork.
6@main enum JerryAppIconRenderer {
7 @MainActor static func main() throws {
8 _ = NSApplication.shared
9 let root = URL(fileURLWithPath: CommandLine.arguments[1], isDirectory: true)
10 let resources = root.appendingPathComponent("Jerry/Resources", isDirectory: true)
11 let iconset = root.appendingPathComponent("build/AppIcon.iconset", isDirectory: true)
12 try FileManager.default.createDirectory(at: resources, withIntermediateDirectories: true)
13 try FileManager.default.createDirectory(at: iconset, withIntermediateDirectories: true)
14 let scene = SKScene(size: .init(width: 1024, height: 1024)); scene.backgroundColor = .clear
15 let tilePath = CGPath(roundedRect: .init(x: 100, y: 100, width: 824, height: 824), cornerWidth: 185, cornerHeight: 185, transform: nil)
16 let tile = SKShapeNode(path: tilePath)
17 tile.fillColor = NSColor(calibratedRed: 0.98, green: 0.95, blue: 0.86, alpha: 1)
18 tile.strokeColor = NSColor(calibratedRed: 0.56, green: 0.47, blue: 0.33, alpha: 0.28); tile.lineWidth = 3
19 scene.addChild(tile)
20 let crop = SKCropNode(); let mask = SKShapeNode(path: tilePath); mask.fillColor = .white; mask.strokeColor = .clear; crop.maskNode = mask; scene.addChild(crop)
21 // A quiet paper vignette provides depth without competing with the face.
22 let warmBase = SKShapeNode(ellipseIn: .init(x: 30, y: -260, width: 1060, height: 700))
23 warmBase.fillColor = NSColor(calibratedRed: 0.73, green: 0.56, blue: 0.32, alpha: 0.07); warmBase.strokeColor = .clear; crop.addChild(warmBase)
24 PaintedPart.rasterScale = 32
25 let rig = ExplorerRig()
26 // The icon has its own tile; omit the desktop-only silhouette shadow.
27 for child in rig.children where child is SKCropNode { child.isHidden = true }
28 rig.render(delta: 0)
29 // Bake the magnification into geometry, including crop masks. Scaling a
30 // rig node magnifies SpriteKit's low-resolution stroke/mask caches.
31 func enlarge(_ node: SKNode, by scale: CGFloat) {
32 node.position = .init(x: node.position.x * scale, y: node.position.y * scale)
33 if let sprite = node as? SKSpriteNode { sprite.size = .init(width: sprite.size.width * scale, height: sprite.size.height * scale) }
34 if let shape = node as? SKShapeNode {
35 var transform = CGAffineTransform(scaleX: scale, y: scale)
36 shape.path = shape.path?.copy(using: &transform); shape.lineWidth *= scale
37 }
38 if let mask = (node as? SKCropNode)?.maskNode { enlarge(mask, by: scale) }
39 for child in node.children { enlarge(child, by: scale) }
40 }
41 enlarge(rig, by: 14); rig.position = .init(x: 512, y: -240); crop.addChild(rig)
42 let view = SKView(frame: .init(origin: .zero, size: scene.size)); view.allowsTransparency = true; view.presentScene(scene)
43 guard let rendered = view.texture(from: scene)?.cgImage() else { throw NSError(domain: "IconRenderer", code: 1) }
44 func export(_ pixels: Int, to url: URL) throws {
45 let context = CGContext(data: nil, width: pixels, height: pixels, bitsPerComponent: 8, bytesPerRow: pixels * 4, space: CGColorSpace(name: CGColorSpace.sRGB)!, bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue)!
46 context.interpolationQuality = .high; context.draw(rendered, in: .init(x: 0, y: 0, width: pixels, height: pixels))
47 let image = NSBitmapImageRep(cgImage: context.makeImage()!)
48 try image.representation(using: .png, properties: [:])!.write(to: url)
49 }
50 try export(1024, to: resources.appendingPathComponent("AppIcon.png"))
51 for size in [16, 32, 128, 256, 512] {
52 try export(size, to: iconset.appendingPathComponent("icon_\(size)x\(size).png"))
53 try export(size * 2, to: iconset.appendingPathComponent("icon_\(size)x\(size)@2x.png"))
54 }
55 }
56}