This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| void nrzi_encode(uint8_t* buf, uint8_t* output, uint16_t len) | |
| { | |
| uint8_t prev_nrzi_bit = 0; | |
| uint8_t nrz_bit; | |
| uint8_t nrzi_bit; | |
| uint8_t i, j; | |
| for (i=0; i<len; i++) | |
| { | |
| for (j=0; j<8; j++) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| init() | |
| { | |
| guard let device = MTLCreateSystemDefaultDevice() else { fatalError("Metal device is not available.") } | |
| self.device = device | |
| guard let commandQueue = device.makeCommandQueue() else { fatalError("Failed creating Metal command queue.") } | |
| self.commandQueue = commandQueue | |
| guard let library = device.makeDefaultLibrary() else { fatalError("Failed creating Metal library.") } | |
| guard let function = library.makeFunction(name: "processData") else { fatalError("Failed creating Metal function.") } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func process(data: ContiguousArray<Float>) -> ContiguousArray<Float> | |
| { | |
| let dataBuffer = data.withUnsafeBytes { (bufferPointer) -> MTLBuffer? in | |
| guard let baseAddress = bufferPointer.baseAddress else { return nil } | |
| return device.makeBuffer(bytes: baseAddress, length: bufferPointer.count, options: .storageModeShared) | |
| } | |
| guard let inputBuffer = dataBuffer else { return [] } | |
| guard let outputBuffer = device.makeBuffer(length: inputBuffer.length, options: .storageModeShared) else { return [] } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extension SKTexture | |
| { | |
| convenience init(radialGradientWithColors colors: [UIColor], locations: [CGFloat], size: CGSize) | |
| { | |
| let renderer = UIGraphicsImageRenderer(size: size) | |
| let image = renderer.image { (context) in | |
| let colorSpace = context.cgContext.colorSpace ?? CGColorSpaceCreateDeviceRGB() | |
| let cgColors = colors.map({ $0.cgColor }) as CFArray | |
| guard let gradient = CGGradient(colorsSpace: colorSpace, colors: cgColors, locations: UnsafePointer<CGFloat>(locations)) else { | |
| fatalError("Failed creating gradient.") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| final class GameScene: SKScene | |
| { | |
| override func didMove(to view: SKView) | |
| { | |
| let linearGradientSize = size | |
| let linearGradientColors = [UIColor(red: 53.0 / 255.0, green: 92.0 / 255.0, blue: 125.0 / 255.0, alpha: 1.0), | |
| UIColor(red: 108.0 / 255.0, green: 91.0 / 255.0, blue: 123.0 / 255.0, alpha: 1.0), | |
| UIColor(red: 192.0 / 255.0, green: 108.0 / 255.0, blue: 132.0 / 255.0, alpha: 1.0)] | |
| let linearGradientLocations: [CGFloat] = [0, 0.5, 1] | |
| let textureCount = 8 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private var initialSize: CGSize = .zero | |
| private var presentedSize: CGSize { return scene?.view?.bounds.size ?? size } | |
| private var presentedScaleFactor: CGFloat { return initialSize.width / presentedSize.width } | |
| override func sceneDidLoad() | |
| { | |
| super.sceneDidLoad() | |
| initialSize = size | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let angleToCenter: CGFloat = .pi / 5 | |
| let angleToGridPoint: CGFloat = .pi / 3 | |
| // 1.0471975511966 | |
| let allowedRange = (angleToCenter - .pi / 8)...(angleToCenter + .pi / 8) | |
| let angle = angleToGridPoint.clamped(to: allowedRange) | |
| // 1.02101761241668 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extension FloatingPoint { | |
| func clamped(to range: ClosedRange<Self>) -> Self { | |
| return max(min(self, range.upperBound), range.lowerBound) | |
| } | |
| } | |
| let clamped = 5.4.clamped(to: 5.6...6.1) | |
| let clamped = 10.5.clamped(to: 5...7) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extension BinaryInteger { | |
| func clamped(to range: ClosedRange<Self>) -> Self { | |
| return max(min(self, range.upperBound), range.lowerBound) | |
| } | |
| } | |
| let clamped = 10.clamped(to: 5...7) |
OlderNewer