diff --git a/.gitignore b/.gitignore index 496ee2c..5e5d5ce 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,63 @@ -.DS_Store \ No newline at end of file +# Xcode +# +# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore + +## Build generated +build/ +DerivedData + +## Various settings +*.pbxuser +!default.pbxuser +*.mode1v3 +!default.mode1v3 +*.mode2v3 +!default.mode2v3 +*.perspectivev3 +!default.perspectivev3 +xcuserdata + +## Other +*.xccheckout +*.moved-aside +*.xcuserstate +*.xcscmblueprint + +## Obj-C/Swift specific +*.hmap +*.ipa + +## Playgrounds +timeline.xctimeline +playground.xcworkspace + +# Swift Package Manager +# +# Add this line if you want to avoid checking in source code from Swift Package Manager dependencies. +# Packages/ +.build/ + +# CocoaPods +# +# We recommend against adding the Pods directory to your .gitignore. However +# you should judge for yourself, the pros and cons are mentioned at: +# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control +# +# Pods/ + +# Carthage +# +# Add this line if you want to avoid checking in source code from Carthage dependencies. +# Carthage/Checkouts + +Carthage/Build + +# fastlane +# +# It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the +# screenshots whenever they are needed. +# For more information about the recommended setup visit: +# https://github.com/fastlane/fastlane/blob/master/docs/Gitignore.md + +fastlane/report.xml +fastlane/screenshots diff --git a/iOSSwiftOpenGL.xcodeproj/project.pbxproj b/iOSSwiftOpenGL.xcodeproj/project.pbxproj index cf556a4..ba43836 100644 --- a/iOSSwiftOpenGL.xcodeproj/project.pbxproj +++ b/iOSSwiftOpenGL.xcodeproj/project.pbxproj @@ -18,6 +18,7 @@ AD88148E1960EFCE005F2E41 /* SimpleFragment.glsl in Resources */ = {isa = PBXBuildFile; fileRef = AD88148D1960EFCE005F2E41 /* SimpleFragment.glsl */; }; AD8814901960F047005F2E41 /* Images.xcassets in Sources */ = {isa = PBXBuildFile; fileRef = AD8814711960EEBF005F2E41 /* Images.xcassets */; }; AD8814911960F27B005F2E41 /* OpenGLView.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD8814871960EEDA005F2E41 /* OpenGLView.swift */; }; + CB2DB5921EDCB462008333C6 /* Launch Screen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = CB2DB5911EDCB462008333C6 /* Launch Screen.storyboard */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -45,6 +46,7 @@ AD8814891960EF38005F2E41 /* OpenGLES.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = OpenGLES.framework; path = System/Library/Frameworks/OpenGLES.framework; sourceTree = SDKROOT; }; AD88148B1960EFBA005F2E41 /* SimpleVertex.glsl */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = SimpleVertex.glsl; path = ../SimpleVertex.glsl; sourceTree = ""; }; AD88148D1960EFCE005F2E41 /* SimpleFragment.glsl */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = SimpleFragment.glsl; path = ../SimpleFragment.glsl; sourceTree = ""; }; + CB2DB5911EDCB462008333C6 /* Launch Screen.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = "Launch Screen.storyboard"; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -105,6 +107,7 @@ AD8814711960EEBF005F2E41 /* Images.xcassets */, AD8814691960EEBF005F2E41 /* iOSSwiftOpenGL.xcdatamodeld */, AD8814661960EEBF005F2E41 /* Info.plist */, + CB2DB5911EDCB462008333C6 /* Launch Screen.storyboard */, ); name = "Supporting Files"; sourceTree = ""; @@ -175,9 +178,12 @@ TargetAttributes = { AD8814611960EEBF005F2E41 = { CreatedOnToolsVersion = 6.0; + DevelopmentTeam = M3DTZN9V9B; + LastSwiftMigration = 0830; }; AD8814761960EEBF005F2E41 = { CreatedOnToolsVersion = 6.0; + LastSwiftMigration = 0830; TestTargetID = AD8814611960EEBF005F2E41; }; }; @@ -209,6 +215,7 @@ AD8814701960EEBF005F2E41 /* Main.storyboard in Resources */, AD88148E1960EFCE005F2E41 /* SimpleFragment.glsl in Resources */, AD8814721960EEBF005F2E41 /* Images.xcassets in Resources */, + CB2DB5921EDCB462008333C6 /* Launch Screen.storyboard in Resources */, AD88148C1960EFBA005F2E41 /* SimpleVertex.glsl in Resources */, ); runOnlyForDeploymentPostprocessing = 0; @@ -347,9 +354,11 @@ buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME = LaunchImage; + DEVELOPMENT_TEAM = M3DTZN9V9B; INFOPLIST_FILE = iOSSwiftOpenGL/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; }; name = Debug; }; @@ -358,9 +367,11 @@ buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME = LaunchImage; + DEVELOPMENT_TEAM = M3DTZN9V9B; INFOPLIST_FILE = iOSSwiftOpenGL/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; }; name = Release; }; @@ -380,6 +391,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; METAL_ENABLE_DEBUG_INFO = YES; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; TEST_HOST = "$(BUNDLE_LOADER)"; }; name = Debug; @@ -396,6 +408,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; METAL_ENABLE_DEBUG_INFO = NO; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; TEST_HOST = "$(BUNDLE_LOADER)"; }; name = Release; diff --git a/iOSSwiftOpenGL/AppDelegate.swift b/iOSSwiftOpenGL/AppDelegate.swift index a0517ed..39e06e9 100755 --- a/iOSSwiftOpenGL/AppDelegate.swift +++ b/iOSSwiftOpenGL/AppDelegate.swift @@ -14,44 +14,47 @@ class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? - func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool { + func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. return true } - func applicationWillResignActive(application: UIApplication) { + func applicationWillResignActive(_ application: UIApplication) { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. } - func applicationDidEnterBackground(application: UIApplication) { + func applicationDidEnterBackground(_ application: UIApplication) { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. } - func applicationWillEnterForeground(application: UIApplication) { + func applicationWillEnterForeground(_ application: UIApplication) { // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. } - func applicationDidBecomeActive(application: UIApplication) { + func applicationDidBecomeActive(_ application: UIApplication) { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. } - func applicationWillTerminate(application: UIApplication) { + func applicationWillTerminate(_ application: UIApplication) { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. // Saves changes in the application's managed object context before the application terminates. self.saveContext() } func saveContext () { - var error: NSError? = nil let managedObjectContext = self.managedObjectContext - if managedObjectContext.hasChanges && !managedObjectContext.save(&error) { - // Replace this implementation with code to handle the error appropriately. - // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. - //println("Unresolved error \(error), \(error.userInfo)") - abort() - } + if managedObjectContext.hasChanges { + do { + try managedObjectContext.save() + } catch { + // Replace this implementation with code to handle the error appropriately. + // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. + //println("Unresolved error \(error), \(error.userInfo)") + abort() + } + } } // #pragma mark - Core Data stack @@ -72,8 +75,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate { // If the model doesn't already exist, it is created from the application's model. var managedObjectModel: NSManagedObjectModel { if _managedObjectModel != nil { - let modelURL = NSBundle.mainBundle().URLForResource("iOSSwiftOpenGL", withExtension: "momd") - _managedObjectModel = NSManagedObjectModel(contentsOfURL: modelURL!) + let modelURL = Bundle.main.url(forResource: "iOSSwiftOpenGL", withExtension: "momd") + _managedObjectModel = NSManagedObjectModel(contentsOf: modelURL!) } return _managedObjectModel! } @@ -83,36 +86,37 @@ class AppDelegate: UIResponder, UIApplicationDelegate { // If the coordinator doesn't already exist, it is created and the application's store added to it. var persistentStoreCoordinator: NSPersistentStoreCoordinator { if _persistentStoreCoordinator != nil { - let storeURL = self.applicationDocumentsDirectory.URLByAppendingPathComponent("iOSSwiftOpenGL.sqlite") - var error: NSError? = nil + let storeURL = self.applicationDocumentsDirectory.appendingPathComponent("iOSSwiftOpenGL.sqlite") _persistentStoreCoordinator = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel) - if _persistentStoreCoordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: storeURL, options: nil, error: &error) == nil { - /* - Replace this implementation with code to handle the error appropriately. - - abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. - - Typical reasons for an error here include: - * The persistent store is not accessible; - * The schema for the persistent store is incompatible with current managed object model. - Check the error message to determine what the actual problem was. - - - If the persistent store is not accessible, there is typically something wrong with the file path. Often, a file URL is pointing into the application's resources directory instead of a writeable directory. - - If you encounter schema incompatibility errors during development, you can reduce their frequency by: - * Simply deleting the existing store: - NSFileManager.defaultManager().removeItemAtURL(storeURL, error: nil) - - * Performing automatic lightweight migration by passing the following dictionary as the options parameter: - [NSMigratePersistentStoresAutomaticallyOption: true, NSInferMappingModelAutomaticallyOption: true} - - Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration Programming Guide" for details. - - */ - //println("Unresolved error \(error), \(error.userInfo)") - abort() - } + do { + try _persistentStoreCoordinator!.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: storeURL, options: nil) + } catch { + /* + Replace this implementation with code to handle the error appropriately. + + abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. + + Typical reasons for an error here include: + * The persistent store is not accessible; + * The schema for the persistent store is incompatible with current managed object model. + Check the error message to determine what the actual problem was. + + + If the persistent store is not accessible, there is typically something wrong with the file path. Often, a file URL is pointing into the application's resources directory instead of a writeable directory. + + If you encounter schema incompatibility errors during development, you can reduce their frequency by: + * Simply deleting the existing store: + NSFileManager.defaultManager().removeItemAtURL(storeURL, error: nil) + + * Performing automatic lightweight migration by passing the following dictionary as the options parameter: + [NSMigratePersistentStoresAutomaticallyOption: true, NSInferMappingModelAutomaticallyOption: true} + + Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration Programming Guide" for details. + + */ + //println("Unresolved error \(error), \(error.userInfo)") + abort() + } } return _persistentStoreCoordinator! } @@ -121,9 +125,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate { // #pragma mark - Application's Documents directory // Returns the URL to the application's Documents directory. - var applicationDocumentsDirectory: NSURL { - let urls = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask) - return urls[urls.endIndex-1] as! NSURL + var applicationDocumentsDirectory: URL { + let urls = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask) + return urls[urls.endIndex-1] } } diff --git a/iOSSwiftOpenGL/Info.plist b/iOSSwiftOpenGL/Info.plist index 051e798..b53df96 100644 --- a/iOSSwiftOpenGL/Info.plist +++ b/iOSSwiftOpenGL/Info.plist @@ -22,6 +22,8 @@ 1 LSRequiresIPhoneOS + UILaunchStoryboardName + Launch Screen UIMainStoryboardFile Main UIRequiredDeviceCapabilities diff --git a/iOSSwiftOpenGL/Launch Screen.storyboard b/iOSSwiftOpenGL/Launch Screen.storyboard new file mode 100644 index 0000000..ba069b7 --- /dev/null +++ b/iOSSwiftOpenGL/Launch Screen.storyboard @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/iOSSwiftOpenGL/OpenGLView.swift b/iOSSwiftOpenGL/OpenGLView.swift index f1ad7a5..205f6a0 100755 --- a/iOSSwiftOpenGL/OpenGLView.swift +++ b/iOSSwiftOpenGL/OpenGLView.swift @@ -6,12 +6,8 @@ // Copyright (c) 2014 Bradley Griffith. All rights reserved. // -import Foundation import UIKit -import QuartzCore import OpenGLES -import GLKit - struct Vertex { @@ -35,33 +31,10 @@ var Indices: [GLubyte] = [ //helper extensions to pass arguments to GL land extension Array { func size () -> Int { - return self.count * sizeofValue(self[0]) - } -} - -extension Int32 { - func __conversion() -> GLenum { - return GLuint(self) - } - - func __conversion() -> GLboolean { - return GLboolean(UInt8(self)) + return self.count * MemoryLayout.size(ofValue: self[0]) } } -extension Int { - func __conversion() -> Int32 { - return Int32(self) - } - - func __conversion() -> GLubyte { - return GLubyte(self) - } - -} - - - class OpenGLView: UIView { var eaglLayer: CAEAGLLayer! @@ -77,7 +50,7 @@ class OpenGLView: UIView { /* Class Methods ------------------------------------------*/ - override class func layerClass() -> AnyClass { + override class var layerClass : AnyClass { // In order for our view to display OpenGL content, we need to set it's // default layer to be a CAEAGLayer return CAEAGLLayer.self @@ -87,7 +60,7 @@ class OpenGLView: UIView { /* Lifecycle ------------------------------------------*/ - required init(coder aDecoder: NSCoder) { + required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) self.setupLayer() @@ -108,23 +81,23 @@ class OpenGLView: UIView { // CALayer's are, by default, non-opaque, which is 'bad for performance with OpenGL', // so let's set our CAEAGLLayer layer to be opaque. self.eaglLayer = self.layer as! CAEAGLLayer - self.eaglLayer.opaque = true + self.eaglLayer.isOpaque = true } func setupContext() { // Just like with CoreGraphics, in order to do much with OpenGL, we need a context. // Here we create a new context with the version of the rendering API we want and // tells OpenGL that when we draw, we want to do so within this context. - var api: EAGLRenderingAPI = EAGLRenderingAPI.OpenGLES2 - self.context = EAGLContext(API: api) + let api: EAGLRenderingAPI = EAGLRenderingAPI.openGLES2 + self.context = EAGLContext(api: api) if (self.context == nil) { - println("Failed to initialize OpenGLES 2.0 context!") + print("Failed to initialize OpenGLES 2.0 context!") exit(1) } - if (!EAGLContext.setCurrentContext(self.context)) { - println("Failed to set current OpenGL context!") + if (!EAGLContext.setCurrent(self.context)) { + print("Failed to set current OpenGL context!") exit(1) } } @@ -132,7 +105,7 @@ class OpenGLView: UIView { func setupRenderBuffer() { glGenRenderbuffers(1, &self.colorRenderBuffer) glBindRenderbuffer(GLenum(GL_RENDERBUFFER), self.colorRenderBuffer) - self.context.renderbufferStorage(Int(GL_RENDERBUFFER), fromDrawable:self.eaglLayer) + self.context.renderbufferStorage(Int(GL_RENDERBUFFER), from:self.eaglLayer) } func setupFrameBuffer() { @@ -142,24 +115,26 @@ class OpenGLView: UIView { glFramebufferRenderbuffer(GLenum(GL_FRAMEBUFFER), GLenum(GL_COLOR_ATTACHMENT0), GLenum(GL_RENDERBUFFER), self.colorRenderBuffer) } - func compileShader(shaderName: String, shaderType: GLenum) -> GLuint { + func compileShader(_ shaderName: String, shaderType: GLenum) -> GLuint { // Get NSString with contents of our shader file. - var shaderPath: String! = NSBundle.mainBundle().pathForResource(shaderName, ofType: "glsl") - var error: NSError? = nil - var shaderString = NSString(contentsOfFile:shaderPath, encoding: NSUTF8StringEncoding, error: &error) - if (shaderString == nil) { - println("Failed to set contents shader of shader file!") + let shaderPath: String! = Bundle.main.path(forResource: shaderName, ofType: "glsl") + var shaderString : NSString! + do { + try shaderString = NSString(contentsOfFile:shaderPath, encoding: String.Encoding.utf8.rawValue) + + } catch { + print("Failed to set contents shader of shader file!") } // Tell OpenGL to create an OpenGL object to represent the shader, indicating if it's a vertex or a fragment shader. - var shaderHandle: GLuint = glCreateShader(shaderType) + let shaderHandle: GLuint = glCreateShader(shaderType) if shaderHandle == 0 { NSLog("Couldn't create shader") } // Conver shader string to CString and call glShaderSource to give OpenGL the source for the shader. - var shaderStringUTF8 = shaderString!.UTF8String + var shaderStringUTF8 = shaderString!.utf8String var shaderStringLength: GLint = GLint(Int32(shaderString!.length)) glShaderSource(shaderHandle, 1, &shaderStringUTF8, &shaderStringLength) @@ -170,7 +145,7 @@ class OpenGLView: UIView { var compileSuccess: GLint = GLint() glGetShaderiv(shaderHandle, GLenum(GL_COMPILE_STATUS), &compileSuccess) if (compileSuccess == GL_FALSE) { - println("Failed to compile shader!") + print("Failed to compile shader!") // TODO: Actually output the error that we can get from the glGetShaderInfoLog function. exit(1); } @@ -181,11 +156,11 @@ class OpenGLView: UIView { func compileShaders() { // Compile our vertex and fragment shaders. - var vertexShader: GLuint = self.compileShader("SimpleVertex", shaderType: GLenum(GL_VERTEX_SHADER)) - var fragmentShader: GLuint = self.compileShader("SimpleFragment", shaderType: GLenum(GL_FRAGMENT_SHADER)) + let vertexShader: GLuint = self.compileShader("SimpleVertex", shaderType: GLenum(GL_VERTEX_SHADER)) + let fragmentShader: GLuint = self.compileShader("SimpleFragment", shaderType: GLenum(GL_FRAGMENT_SHADER)) // Call glCreateProgram, glAttachShader, and glLinkProgram to link the vertex and fragment shaders into a complete program. - var programHandle: GLuint = glCreateProgram() + let programHandle: GLuint = glCreateProgram() glAttachShader(programHandle, vertexShader) glAttachShader(programHandle, fragmentShader) glLinkProgram(programHandle) @@ -194,7 +169,7 @@ class OpenGLView: UIView { var linkSuccess: GLint = GLint() glGetProgramiv(programHandle, GLenum(GL_LINK_STATUS), &linkSuccess) if (linkSuccess == GL_FALSE) { - println("Failed to create shader program!") + print("Failed to create shader program!") // TODO: Actually output the error that we can get from the glGetProgramInfoLog function. exit(1); } @@ -222,11 +197,11 @@ class OpenGLView: UIView { // let positionSlotFirstComponent : UnsafePointer(&0) glEnableVertexAttribArray(positionSlot) - glVertexAttribPointer(positionSlot, 3, GLenum(GL_FLOAT), GLboolean(UInt8(GL_FALSE)), GLsizei(sizeof(Vertex)), nil) + glVertexAttribPointer(positionSlot, 3, GLenum(GL_FLOAT), GLboolean(GL_FALSE), GLsizei(MemoryLayout.size), nil) glEnableVertexAttribArray(colorSlot) // let colorSlotFirstComponent = UnsafePointer(sizeof(Float) * 3) - glVertexAttribPointer(colorSlot, 4, GLenum(GL_FLOAT), GLboolean(UInt8(GL_FALSE)), GLsizei(sizeof(Vertex)), nil) + glVertexAttribPointer(colorSlot, 4, GLenum(GL_FLOAT), GLboolean(GL_FALSE), GLsizei(MemoryLayout.size), nil) glGenBuffers(1, &indexBuffer) glBindBuffer(GLenum(GL_ELEMENT_ARRAY_BUFFER), indexBuffer) @@ -238,7 +213,7 @@ class OpenGLView: UIView { func render() { glBindVertexArrayOES(VAO); - glViewport(0, 0, GLint(self.frame.size.width), GLint(self.frame.size.height)); + glViewport(0, 0, GLsizei(self.frame.size.width), GLsizei(self.frame.size.height)); glDrawElements(GLenum(GL_TRIANGLES), GLsizei(Indices.count), GLenum(GL_UNSIGNED_BYTE), nil) diff --git a/iOSSwiftOpenGLTests/iOSSwiftOpenGLTests.swift b/iOSSwiftOpenGLTests/iOSSwiftOpenGLTests.swift index b21ce51..95bfb7f 100644 --- a/iOSSwiftOpenGLTests/iOSSwiftOpenGLTests.swift +++ b/iOSSwiftOpenGLTests/iOSSwiftOpenGLTests.swift @@ -27,7 +27,7 @@ class iOSSwiftOpenGLTests: XCTestCase { func testPerformanceExample() { // This is an example of a performance test case. - self.measureBlock() { + self.measure() { // Put the code you want to measure the time of here. } }