From 9cda4a1cd9f35a56b2f6e094a815203b8c1d1e49 Mon Sep 17 00:00:00 2001 From: Alsey Coleman Miller Date: Sun, 18 Nov 2018 01:33:49 -0500 Subject: [PATCH] Updated for Swift 4 --- Cartfile | 1 - Package.pins | 60 --------------------- Package.resolved | 40 ++++---------- Package.swift | 49 +++++++++++------ Sources/Cacao/NSIndexPath+UITableView.swift | 2 +- Sources/Cacao/SDL.swift | 52 ++++-------------- Sources/Cacao/UIFont.swift | 4 +- Sources/Cacao/UILabel.swift | 12 ++--- Sources/Cacao/UIScreen.swift | 24 ++++----- Sources/Cacao/UITouchesEvent.swift | 6 +-- Sources/Cacao/UIView.swift | 33 ++++++------ Sources/CacaoDemo/AppDelegate.swift | 5 +- Sources/CacaoDemo/TableViewController.swift | 8 +-- Sources/CacaoDemo/main.swift | 1 - 14 files changed, 99 insertions(+), 198 deletions(-) delete mode 100644 Cartfile delete mode 100644 Package.pins diff --git a/Cartfile b/Cartfile deleted file mode 100644 index 227aaae..0000000 --- a/Cartfile +++ /dev/null @@ -1 +0,0 @@ -github "PureSwift/Silica" "master" \ No newline at end of file diff --git a/Package.pins b/Package.pins deleted file mode 100644 index ce049d2..0000000 --- a/Package.pins +++ /dev/null @@ -1,60 +0,0 @@ -{ - "autoPin": true, - "pins": [ - { - "package": "Cairo", - "reason": null, - "repositoryURL": "https://github.com/PureSwift/Cairo.git", - "version": "1.2.3" - }, - { - "package": "CCairo", - "reason": null, - "repositoryURL": "https://github.com/PureSwift/CCairo.git", - "version": "1.1.1" - }, - { - "package": "CFontConfig", - "reason": null, - "repositoryURL": "https://github.com/PureSwift/CFontConfig.git", - "version": "1.0.1" - }, - { - "package": "CFreeType", - "reason": null, - "repositoryURL": "https://github.com/PureSwift/CFreeType.git", - "version": "1.0.4" - }, - { - "package": "CLCMS", - "reason": null, - "repositoryURL": "https://github.com/PureSwift/CLCMS.git", - "version": "1.0.0" - }, - { - "package": "CSDL2", - "reason": null, - "repositoryURL": "https://github.com/PureSwift/CSDL2.git", - "version": "1.0.1" - }, - { - "package": "LittleCMS", - "reason": null, - "repositoryURL": "https://github.com/PureSwift/LittleCMS.git", - "version": "1.0.2" - }, - { - "package": "SDL", - "reason": null, - "repositoryURL": "https://github.com/PureSwift/SDL.git", - "version": "1.0.1" - }, - { - "package": "Silica", - "reason": null, - "repositoryURL": "https://github.com/PureSwift/Silica.git", - "version": "1.3.1" - } - ], - "version": 1 -} \ No newline at end of file diff --git a/Package.resolved b/Package.resolved index a564e77..e880839 100644 --- a/Package.resolved +++ b/Package.resolved @@ -5,9 +5,9 @@ "package": "Cairo", "repositoryURL": "https://github.com/PureSwift/Cairo.git", "state": { - "branch": null, + "branch": "master", "revision": "bc207ab2e367fbafef51a47b85a7e1eb2821ce57", - "version": "1.2.3" + "version": null } }, { @@ -37,49 +37,31 @@ "version": "1.0.4" } }, - { - "package": "CLCMS", - "repositoryURL": "https://github.com/PureSwift/CLCMS.git", - "state": { - "branch": null, - "revision": "20d18d62ebc642e52758512cab59af06f2d3655e", - "version": "1.0.0" - } - }, { "package": "CSDL2", "repositoryURL": "https://github.com/PureSwift/CSDL2.git", "state": { - "branch": null, - "revision": "a4798aa0174f6661404b9df4ccaf2ba1dc9cc92c", - "version": "1.0.1" - } - }, - { - "package": "LittleCMS", - "repositoryURL": "https://github.com/PureSwift/LittleCMS.git", - "state": { - "branch": null, - "revision": "4143dfd3916be5b1ccd3cc46c27d939e9b63c0dd", - "version": "1.0.2" + "branch": "master", + "revision": "7cfe34d9d12cf7fe958c42c2f37577bd01c703e4", + "version": null } }, { "package": "SDL", "repositoryURL": "https://github.com/PureSwift/SDL.git", "state": { - "branch": null, - "revision": "7d73084635022234e399385ae2724ca03d47d2a5", - "version": "1.0.1" + "branch": "master", + "revision": "fe4cdf67266afd5dc285dd6ebf54fb417b44a2a5", + "version": null } }, { "package": "Silica", "repositoryURL": "https://github.com/PureSwift/Silica.git", "state": { - "branch": null, - "revision": "6239656493fba609a11bef46efd495fca2379090", - "version": "1.3.2" + "branch": "master", + "revision": "7aeb8673927d2b69a0bab1679fcb8147426651cb", + "version": null } } ] diff --git a/Package.swift b/Package.swift index 12fbf48..b0aff18 100644 --- a/Package.swift +++ b/Package.swift @@ -1,23 +1,40 @@ -// swift-tools-version:3.0.2 -// The swift-tools-version declares the minimum version of Swift required to build this package. - +// swift-tools-version:4.1 import PackageDescription let package = Package( name: "Cacao", - targets: [ - Target( - name: "CacaoDemo", - dependencies: [.Target(name: "Cacao")]), - Target( - name: "Cacao" - ) - ], + products: [ + .library(name: "Cacao", targets: ["Cacao"]), + .executable(name: "CacaoDemo", targets: ["CacaoDemo"]), + ], dependencies: [ - .Package(url: "https://github.com/PureSwift/Silica.git", majorVersion: 1), - .Package(url: "https://github.com/PureSwift/Cairo.git", majorVersion: 1), - .Package(url: "https://github.com/PureSwift/SDL.git", majorVersion: 1), - .Package(url: "https://github.com/PureSwift/CSDL2.git", majorVersion: 1) + .package( + url: "https://github.com/PureSwift/Silica.git", + .branch("master") + ), + .package( + url: "https://github.com/PureSwift/Cairo.git", + .branch("master") + ), + .package( + url: "https://github.com/PureSwift/SDL.git", + .branch("master") + ) ], - exclude: ["iOS", "Resources"] + targets: [ + .target( + name: "Cacao", + dependencies: [ + "Silica", + "Cairo", + "SDL" + ] + ), + .target( + name: "CacaoDemo", + dependencies: [ + "Cacao" + ] + ), + ] ) diff --git a/Sources/Cacao/NSIndexPath+UITableView.swift b/Sources/Cacao/NSIndexPath+UITableView.swift index e0821a2..a3e9cc4 100755 --- a/Sources/Cacao/NSIndexPath+UITableView.swift +++ b/Sources/Cacao/NSIndexPath+UITableView.swift @@ -22,7 +22,7 @@ public extension IndexPath { } @inline(__always) - init(row: Int, in section: Int) { + init(row: Int, section: Int) { self.init(indexes: [section, row]) } diff --git a/Sources/Cacao/SDL.swift b/Sources/Cacao/SDL.swift index 5212809..55a96dc 100644 --- a/Sources/Cacao/SDL.swift +++ b/Sources/Cacao/SDL.swift @@ -18,7 +18,8 @@ internal func SDLEventRun() { assert(Thread.current.isMainThread, "Should only be called from main thread") #endif - SDL.initialize(subSystems: [.video]).sdlAssert() + do { try SDL.initialize(subSystems: [.video]) } + catch { fatalError("Could not initialize SDL: \(error)") } defer { SDL.quit() } @@ -26,7 +27,7 @@ internal func SDLEventRun() { let delegate = UIApplication.shared.delegate! - var windowOptions: Set = [.allowRetina, .opengl] + var windowOptions: BitMaskOptionSet = [.allowRetina, .opengl] if options.canResizeWindow { @@ -37,10 +38,14 @@ internal func SDLEventRun() { let initialWindowSize = preferredSize // can we query for screen resolution? - let window = Window(title: options.windowName, frame: (x: .centered, y: .centered, width: Int(initialWindowSize.width), height: Int(initialWindowSize.height)), options: windowOptions).sdlAssert() + let window = try! SDLWindow(title: options.windowName, + frame: (x: .centered, y: .centered, + width: Int(initialWindowSize.width), + height: Int(initialWindowSize.height)), + options: windowOptions) // create main UIScreen - let screen = UIScreen(window: window, size: initialWindowSize) + let screen = try! UIScreen(window: window, size: initialWindowSize) UIScreen._main = screen let framesPerSecond = screen.maximumFramesPerSecond @@ -84,7 +89,8 @@ internal func SDLEventRun() { if eventCount > 0 { print("Runloop took (\(SDL_GetTicks() - runLoopStartTime)ms)") } // render to screen - screen.update() + do { try screen.update() } + catch { fatalError("Could not render: \(error)") } // sleep to save energy let frameDuration = SDL_GetTicks() - startTime @@ -95,39 +101,3 @@ internal func SDLEventRun() { } } } - -// MARK: - Assertions - -internal extension Bool { - - @inline(__always) - func sdlAssert(function: String = #function, file: StaticString = #file, line: UInt = #line) { - - guard self else { sdlFatalError(function: function, file: file, line: line) } - } -} - -internal extension Optional { - - @inline(__always) - func sdlAssert(function: String = #function, file: StaticString = #file, line: UInt = #line) -> Wrapped { - - guard let value = self - else { sdlFatalError(function: function, file: file, line: line) } - - return value - } -} - -@_silgen_name("_cacao_sdl_fatal_error") -internal func sdlFatalError(function: String = #function, file: StaticString = #file, line: UInt = #line) -> Never { - - if let error = SDL.errorDescription { - - fatalError("SDL error: \(error)", file: file, line: line) - - } else { - - fatalError("An SDL error occurred", file: file, line: line) - } -} diff --git a/Sources/Cacao/UIFont.swift b/Sources/Cacao/UIFont.swift index 6eb0858..20462bb 100644 --- a/Sources/Cacao/UIFont.swift +++ b/Sources/Cacao/UIFont.swift @@ -45,12 +45,12 @@ public final class UIFont: Equatable { public extension UIFont { - static func systemFontOfSize(_ size: CGFloat) -> UIFont { + static func systemFont(ofSize size: CGFloat) -> UIFont { return UIFont(name: "HelveticaNeu", size: size)! } - static func boldSystemFontOfSize(_ size: CGFloat) -> UIFont { + static func boldSystemFont(ofSize size: CGFloat) -> UIFont { return UIFont(name: "HelveticaNeu-Bold", size: size)! } diff --git a/Sources/Cacao/UILabel.swift b/Sources/Cacao/UILabel.swift index 20728ad..e31d952 100644 --- a/Sources/Cacao/UILabel.swift +++ b/Sources/Cacao/UILabel.swift @@ -6,10 +6,7 @@ // Copyright © 2016 PureSwift. All rights reserved. // -import struct Foundation.CGFloat -import struct Foundation.CGPoint -import struct Foundation.CGSize -import struct Foundation.CGRect +import Foundation import Silica open class UILabel: UIView { @@ -25,7 +22,7 @@ open class UILabel: UIView { // MARK: - Properties - open var text: String = "" { didSet { setNeedsDisplay() } } + open var text: String? { didSet { setNeedsDisplay() } } open var font: UIFont = UIFont(name: "Helvetica", size: 17)! { didSet { setNeedsDisplay() } } @@ -35,8 +32,7 @@ open class UILabel: UIView { // MARK: - Draw - @nonobjc - open override func draw(_ rect: CGRect?) { + open override func draw(_ rect: CGRect) { guard let context = UIGraphicsGetCurrentContext() else { return } @@ -46,7 +42,7 @@ open class UILabel: UIView { attributes.color = textColor attributes.paragraphStyle.alignment = textAlignment - text.draw(in: self.bounds, context: context, attributes: attributes) + text?.draw(in: self.bounds, context: context, attributes: attributes) } } diff --git a/Sources/Cacao/UIScreen.swift b/Sources/Cacao/UIScreen.swift index 6e2d04e..8d05e9d 100644 --- a/Sources/Cacao/UIScreen.swift +++ b/Sources/Cacao/UIScreen.swift @@ -36,14 +36,14 @@ public final class UIScreen { public var maximumFramesPerSecond: Int { - return Int(window.displayMode?.refresh_rate ?? 60) + return try! window.displayMode().refreshRate } - internal let window: Window + internal let window: SDLWindow private var size: (window: CGSize, native: CGSize) { didSet { sizeChanged() } } - internal let renderer: Renderer + internal let renderer: SDLRenderer internal var needsDisplay = true @@ -57,15 +57,15 @@ public final class UIScreen { // MARK: - Intialization - internal init(window: Window, size: CGSize) { + internal init(window: SDLWindow, size: CGSize) throws { - self.renderer = Renderer(window: window).sdlAssert() + self.renderer = try SDLRenderer(window: window) self.window = window self.size = (size, size) // update values self.updateSize() - self.renderer.drawColor = (0x00, 0x00, 0x00, 0xFF) + try self.renderer.setDrawColor((0x00, 0x00, 0x00, 0xFF)) } // MARK: - Methods @@ -86,7 +86,7 @@ public final class UIScreen { } /// Layout (if needed) and redraw the screen - internal func update() { + internal func update() throws { // apply animations if UIView.animations.isEmpty == false { @@ -112,12 +112,12 @@ public final class UIScreen { defer { needsDisplay = false } - renderer.clear() + try renderer.clear() for window in windows { // render view hierarchy - render(view: window) + try render(view: window) } // render to screen @@ -134,7 +134,7 @@ public final class UIScreen { needsLayout = true } - private func render(view: UIView, origin: CGPoint = CGPoint()) { + private func render(view: UIView, origin: CGPoint = .zero) throws { guard view.shouldRender else { return } @@ -152,10 +152,10 @@ public final class UIScreen { h: Int32(view.bounds.size.height * scale)) // render view - view.render(on: self, in: rect) + try view.render(on: self, in: rect) // render subviews - view.subviews.forEach { render(view: $0, origin: relativeOrigin) } + try view.subviews.forEach { try render(view: $0, origin: relativeOrigin) } } internal func addWindow(_ window: UIWindow) { diff --git a/Sources/Cacao/UITouchesEvent.swift b/Sources/Cacao/UITouchesEvent.swift index 1f85188..70c6b31 100644 --- a/Sources/Cacao/UITouchesEvent.swift +++ b/Sources/Cacao/UITouchesEvent.swift @@ -37,13 +37,13 @@ internal final class UITouchesEvent: UIEvent { internal func views(for window: UIWindow) -> Set { - let views = self.touches(for: window)?.flatMap { $0.view } ?? [] - + let views = self.touches(for: window)?.compactMap { $0.view } ?? [] return Set(views) } internal override func gestureRecognizers(for window: UIWindow) -> Set { - return Set(touches(for: window)?.reduce([UIGestureRecognizer](), { $0.0 + ($0.1.gestureRecognizers ?? []) }) ?? []) + let touches = self.touches(for: window) ?? [] + return Set(touches.reduce([], { $0 + ($1.gestureRecognizers ?? []) })) } } diff --git a/Sources/Cacao/UIView.swift b/Sources/Cacao/UIView.swift index abc7fea..1122d29 100644 --- a/Sources/Cacao/UIView.swift +++ b/Sources/Cacao/UIView.swift @@ -700,7 +700,7 @@ open class UIView: UIResponder { /// (e.g. video streaming, SDL game) instead of the CoreGraphics drawing API. /// /// - Warning: Do not hold a reference to this object as it can be recreated as needed for rendering. - public private(set) var texture: Texture? + public private(set) var texture: SDLTexture? internal var shouldRender: Bool { return isHidden == false @@ -709,7 +709,7 @@ open class UIView: UIResponder { && bounds.size.height >= 1.0 // must be at least 1x1 } - internal final func render(on screen: UIScreen, in rect: SDL_Rect) { + internal final func render(on screen: UIScreen, in rect: SDL_Rect) throws { guard shouldRender else { return } @@ -718,33 +718,34 @@ open class UIView: UIResponder { let nativeSize = (width: Int(bounds.size.width * scale), height: Int(bounds.size.height * scale)) - let texture: Texture + let texture: SDLTexture // reuse cached texture if view hasn't been resized. if let cachedTexture = self.texture, - cachedTexture.width == nativeSize.width, - cachedTexture.height == nativeSize.height { + let attributes = try? cachedTexture.attributes(), + attributes.width == nativeSize.width, + attributes.height == nativeSize.height { texture = cachedTexture } else { - texture = Texture(renderer: screen.renderer, - format: PixelFormat.RawValue(SDL_PIXELFORMAT_ARGB8888), - access: .streaming, - width: nativeSize.width, - height: nativeSize.height).sdlAssert() + texture = try SDLTexture(renderer: screen.renderer, + format: .argb8888, // SDL_PIXELFORMAT_ARGB8888 + access: .streaming, + width: nativeSize.width, + height: nativeSize.height) - texture.blendMode = .alpha + try texture.setBlendMode([.alpha]) // cache for reuse if view size isn't changed self.texture = texture } // unlock and modify texture - texture.withUnsafeMutableBytes { + try texture.withUnsafeMutableBytes { - let surface = try! Cairo.Surface.Image(mutableBytes: $0.assumingMemoryBound(to: UInt8.self), + let surface = try Cairo.Surface.Image(mutableBytes: $0.assumingMemoryBound(to: UInt8.self), format: .argb32, width: nativeSize.width, height: nativeSize.height, @@ -764,7 +765,7 @@ open class UIView: UIResponder { surface.finish() } - screen.renderer.copy(texture, destination: rect) + try screen.renderer.copy(texture, destination: rect) } internal func draw(in context: Silica.CGContext) { @@ -1082,9 +1083,9 @@ final class ReferenceWritableKeyPath { let nativeSize = (width: Int(bounds.size.width * scale), height: Int(bounds.size.height * scale)) - let surface = cachedTexture.withUnsafeMutableBytes { + let surface = try! cachedTexture.withUnsafeMutableBytes { - try! Cairo.Surface.Image(mutableBytes: $0.assumingMemoryBound(to: UInt8.self), format: .argb32, width: nativeSize.width, height: nativeSize.height, stride: $1) + try Cairo.Surface.Image(mutableBytes: $0.assumingMemoryBound(to: UInt8.self), format: .argb32, width: nativeSize.width, height: nativeSize.height, stride: $1) } let data = try! surface!.writePNG() diff --git a/Sources/CacaoDemo/AppDelegate.swift b/Sources/CacaoDemo/AppDelegate.swift index 64b9829..57cb3c6 100644 --- a/Sources/CacaoDemo/AppDelegate.swift +++ b/Sources/CacaoDemo/AppDelegate.swift @@ -16,10 +16,13 @@ import Foundation import Silica #endif -@UIApplicationMain final class AppDelegate: NSObject, UIApplicationDelegate { + #if os(iOS) static var shared: AppDelegate { return UIApplication.shared.delegate as! AppDelegate } + #else + static let shared: AppDelegate = AppDelegate() + #endif var window: UIWindow? diff --git a/Sources/CacaoDemo/TableViewController.swift b/Sources/CacaoDemo/TableViewController.swift index 00f1a79..7c235f3 100644 --- a/Sources/CacaoDemo/TableViewController.swift +++ b/Sources/CacaoDemo/TableViewController.swift @@ -11,7 +11,7 @@ import Foundation import UIKit import CoreGraphics #else -//import Cacao +import Cacao import Silica #endif @@ -47,12 +47,6 @@ final class TableViewController: UITableViewController { } } - override func viewDidAppear(_ animated: Bool) { - super.viewDidAppear(animated) - - - } - // MARK: - UITableViewDataSource override func numberOfSections(in tableView: UITableView) -> Int { diff --git a/Sources/CacaoDemo/main.swift b/Sources/CacaoDemo/main.swift index 47b075d..9cd6285 100644 --- a/Sources/CacaoDemo/main.swift +++ b/Sources/CacaoDemo/main.swift @@ -18,7 +18,6 @@ import Cacao var options = CacaoOptions() options.windowName = "CacaoDemo" - UIApplicationMain(delegate: AppDelegate.shared, options: options) #endif