Skip to content

Commit f9e37bd

Browse files
committed
Merge pull request #3 from neoneye/master
Compile with latest XCode 6.4 (6E35B) and iOS 8.4
2 parents b878f0f + b590b14 commit f9e37bd

File tree

3 files changed

+68
-67
lines changed

3 files changed

+68
-67
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.DS_Store

iOSSwiftOpenGL/AppDelegate.swift

100644100755
+6-11
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
1414

1515
var window: UIWindow?
1616

17-
18-
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
17+
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {
1918
// Override point for customization after application launch.
2019
return true
2120
}
@@ -47,27 +46,23 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
4746
func saveContext () {
4847
var error: NSError? = nil
4948
let managedObjectContext = self.managedObjectContext
50-
if managedObjectContext != nil {
5149
if managedObjectContext.hasChanges && !managedObjectContext.save(&error) {
5250
// Replace this implementation with code to handle the error appropriately.
5351
// 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.
5452
//println("Unresolved error \(error), \(error.userInfo)")
5553
abort()
5654
}
57-
}
5855
}
5956

6057
// #pragma mark - Core Data stack
6158

6259
// Returns the managed object context for the application.
6360
// If the context doesn't already exist, it is created and bound to the persistent store coordinator for the application.
6461
var managedObjectContext: NSManagedObjectContext {
65-
if !_managedObjectContext {
62+
if _managedObjectContext != nil {
6663
let coordinator = self.persistentStoreCoordinator
67-
if coordinator != nil {
6864
_managedObjectContext = NSManagedObjectContext()
6965
_managedObjectContext!.persistentStoreCoordinator = coordinator
70-
}
7166
}
7267
return _managedObjectContext!
7368
}
@@ -76,9 +71,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
7671
// Returns the managed object model for the application.
7772
// If the model doesn't already exist, it is created from the application's model.
7873
var managedObjectModel: NSManagedObjectModel {
79-
if !_managedObjectModel {
74+
if _managedObjectModel != nil {
8075
let modelURL = NSBundle.mainBundle().URLForResource("iOSSwiftOpenGL", withExtension: "momd")
81-
_managedObjectModel = NSManagedObjectModel(contentsOfURL: modelURL)
76+
_managedObjectModel = NSManagedObjectModel(contentsOfURL: modelURL!)
8277
}
8378
return _managedObjectModel!
8479
}
@@ -87,7 +82,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
8782
// Returns the persistent store coordinator for the application.
8883
// If the coordinator doesn't already exist, it is created and the application's store added to it.
8984
var persistentStoreCoordinator: NSPersistentStoreCoordinator {
90-
if !_persistentStoreCoordinator {
85+
if _persistentStoreCoordinator != nil {
9186
let storeURL = self.applicationDocumentsDirectory.URLByAppendingPathComponent("iOSSwiftOpenGL.sqlite")
9287
var error: NSError? = nil
9388
_persistentStoreCoordinator = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
@@ -128,7 +123,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
128123
// Returns the URL to the application's Documents directory.
129124
var applicationDocumentsDirectory: NSURL {
130125
let urls = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)
131-
return urls[urls.endIndex-1] as NSURL
126+
return urls[urls.endIndex-1] as! NSURL
132127
}
133128

134129
}

iOSSwiftOpenGL/OpenGLView.swift

100644100755
+61-56
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,36 @@ var Indices: [GLubyte] = [
3232
]
3333

3434

35+
//helper extensions to pass arguments to GL land
36+
extension Array {
37+
func size () -> Int {
38+
return self.count * sizeofValue(self[0])
39+
}
40+
}
41+
42+
extension Int32 {
43+
func __conversion() -> GLenum {
44+
return GLuint(self)
45+
}
46+
47+
func __conversion() -> GLboolean {
48+
return GLboolean(UInt8(self))
49+
}
50+
}
51+
52+
extension Int {
53+
func __conversion() -> Int32 {
54+
return Int32(self)
55+
}
56+
57+
func __conversion() -> GLubyte {
58+
return GLubyte(self)
59+
}
60+
61+
}
62+
63+
64+
3565
class OpenGLView: UIView {
3666

3767
var eaglLayer: CAEAGLLayer!
@@ -57,7 +87,7 @@ class OpenGLView: UIView {
5787
/* Lifecycle
5888
------------------------------------------*/
5989

60-
init(coder aDecoder: NSCoder!) {
90+
required init(coder aDecoder: NSCoder) {
6191
super.init(coder: aDecoder)
6292

6393
self.setupLayer()
@@ -77,7 +107,7 @@ class OpenGLView: UIView {
77107
func setupLayer() {
78108
// CALayer's are, by default, non-opaque, which is 'bad for performance with OpenGL',
79109
// so let's set our CAEAGLLayer layer to be opaque.
80-
self.eaglLayer = self.layer as CAEAGLLayer
110+
self.eaglLayer = self.layer as! CAEAGLLayer
81111
self.eaglLayer.opaque = true
82112
}
83113

@@ -88,7 +118,7 @@ class OpenGLView: UIView {
88118
var api: EAGLRenderingAPI = EAGLRenderingAPI.OpenGLES2
89119
self.context = EAGLContext(API: api)
90120

91-
if (!self.context) {
121+
if (self.context == nil) {
92122
println("Failed to initialize OpenGLES 2.0 context!")
93123
exit(1)
94124
}
@@ -101,41 +131,44 @@ class OpenGLView: UIView {
101131

102132
func setupRenderBuffer() {
103133
glGenRenderbuffers(1, &self.colorRenderBuffer)
104-
glBindRenderbuffer(GL_RENDERBUFFER, self.colorRenderBuffer)
134+
glBindRenderbuffer(GLenum(GL_RENDERBUFFER), self.colorRenderBuffer)
105135
self.context.renderbufferStorage(Int(GL_RENDERBUFFER), fromDrawable:self.eaglLayer)
106136
}
107137

108138
func setupFrameBuffer() {
109139
var frameBuffer: GLuint = GLuint()
110140
glGenFramebuffers(1, &frameBuffer)
111-
glBindFramebuffer(GL_FRAMEBUFFER.asUnsigned(), frameBuffer)
112-
glFramebufferRenderbuffer(GL_FRAMEBUFFER.asUnsigned(), GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, self.colorRenderBuffer)
141+
glBindFramebuffer(GLenum(GL_FRAMEBUFFER), frameBuffer)
142+
glFramebufferRenderbuffer(GLenum(GL_FRAMEBUFFER), GLenum(GL_COLOR_ATTACHMENT0), GLenum(GL_RENDERBUFFER), self.colorRenderBuffer)
113143
}
114144

115-
func compileShader(shaderName: NSString, shaderType: GLenum) -> GLuint {
145+
func compileShader(shaderName: String, shaderType: GLenum) -> GLuint {
116146

117147
// Get NSString with contents of our shader file.
118-
var shaderPath: NSString = NSBundle.mainBundle().pathForResource(shaderName, ofType: "glsl")
148+
var shaderPath: String! = NSBundle.mainBundle().pathForResource(shaderName, ofType: "glsl")
119149
var error: NSError? = nil
120-
var shaderString = NSString.stringWithContentsOfFile(shaderPath, encoding: NSUTF8StringEncoding, error: &error)
121-
if (!shaderString) {
150+
var shaderString = NSString(contentsOfFile:shaderPath, encoding: NSUTF8StringEncoding, error: &error)
151+
if (shaderString == nil) {
122152
println("Failed to set contents shader of shader file!")
123153
}
124154

125155
// Tell OpenGL to create an OpenGL object to represent the shader, indicating if it's a vertex or a fragment shader.
126156
var shaderHandle: GLuint = glCreateShader(shaderType)
127157

158+
if shaderHandle == 0 {
159+
NSLog("Couldn't create shader")
160+
}
128161
// Conver shader string to CString and call glShaderSource to give OpenGL the source for the shader.
129-
var shaderStringUTF8: CString = shaderString!.UTF8String
130-
var shaderStringLength: GLint = GLint.convertFromIntegerLiteral(Int32(shaderString!.length))
162+
var shaderStringUTF8 = shaderString!.UTF8String
163+
var shaderStringLength: GLint = GLint(Int32(shaderString!.length))
131164
glShaderSource(shaderHandle, 1, &shaderStringUTF8, &shaderStringLength)
132165

133166
// Tell OpenGL to compile the shader.
134167
glCompileShader(shaderHandle)
135168

136169
// But compiling can fail! If we have errors in our GLSL code, we can here and output any errors.
137170
var compileSuccess: GLint = GLint()
138-
glGetShaderiv(shaderHandle, GL_COMPILE_STATUS, &compileSuccess)
171+
glGetShaderiv(shaderHandle, GLenum(GL_COMPILE_STATUS), &compileSuccess)
139172
if (compileSuccess == GL_FALSE) {
140173
println("Failed to compile shader!")
141174
// TODO: Actually output the error that we can get from the glGetShaderInfoLog function.
@@ -148,8 +181,8 @@ class OpenGLView: UIView {
148181
func compileShaders() {
149182

150183
// Compile our vertex and fragment shaders.
151-
var vertexShader: GLuint = self.compileShader("SimpleVertex", shaderType: GL_VERTEX_SHADER)
152-
var fragmentShader: GLuint = self.compileShader("SimpleFragment", shaderType: GL_FRAGMENT_SHADER)
184+
var vertexShader: GLuint = self.compileShader("SimpleVertex", shaderType: GLenum(GL_VERTEX_SHADER))
185+
var fragmentShader: GLuint = self.compileShader("SimpleFragment", shaderType: GLenum(GL_FRAGMENT_SHADER))
153186

154187
// Call glCreateProgram, glAttachShader, and glLinkProgram to link the vertex and fragment shaders into a complete program.
155188
var programHandle: GLuint = glCreateProgram()
@@ -159,7 +192,7 @@ class OpenGLView: UIView {
159192

160193
// Check for any errors.
161194
var linkSuccess: GLint = GLint()
162-
glGetProgramiv(programHandle, GL_LINK_STATUS, &linkSuccess)
195+
glGetProgramiv(programHandle, GLenum(GL_LINK_STATUS), &linkSuccess)
163196
if (linkSuccess == GL_FALSE) {
164197
println("Failed to create shader program!")
165198
// TODO: Actually output the error that we can get from the glGetProgramInfoLog function.
@@ -171,8 +204,8 @@ class OpenGLView: UIView {
171204

172205
// Finally, call glGetAttribLocation to get a pointer to the input values for the vertex shader, so we
173206
// can set them in code. Also call glEnableVertexAttribArray to enable use of these arrays (they are disabled by default).
174-
self.positionSlot = glGetAttribLocation(programHandle, "Position")
175-
self.colorSlot = glGetAttribLocation(programHandle, "SourceColor")
207+
self.positionSlot = GLuint(glGetAttribLocation(programHandle, "Position"))
208+
self.colorSlot = GLuint(glGetAttribLocation(programHandle, "SourceColor"))
176209
glEnableVertexAttribArray(self.positionSlot)
177210
glEnableVertexAttribArray(self.colorSlot)
178211
}
@@ -184,64 +217,36 @@ class OpenGLView: UIView {
184217
glBindVertexArrayOES(VAO);
185218

186219
glGenBuffers(1, &vertexBuffer)
187-
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer)
188-
glBufferData(GL_ARRAY_BUFFER, Vertices.size(), Vertices, GL_STATIC_DRAW)
220+
glBindBuffer(GLenum(GL_ARRAY_BUFFER), vertexBuffer)
221+
glBufferData(GLenum(GL_ARRAY_BUFFER), Vertices.size(), Vertices, GLenum(GL_STATIC_DRAW))
189222

190-
let positionSlotFirstComponent = ConstUnsafePointer<Int>(0)
223+
// let positionSlotFirstComponent : UnsafePointer<Int>(&0)
191224
glEnableVertexAttribArray(positionSlot)
192-
glVertexAttribPointer(positionSlot, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), positionSlotFirstComponent)
225+
glVertexAttribPointer(positionSlot, 3, GLenum(GL_FLOAT), GLboolean(UInt8(GL_FALSE)), GLsizei(sizeof(Vertex)), nil)
193226

194227
glEnableVertexAttribArray(colorSlot)
195-
let colorSlotFirstComponent = ConstUnsafePointer<Int>(sizeof(Float) * 3)
196-
glVertexAttribPointer(colorSlot, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), colorSlotFirstComponent)
228+
// let colorSlotFirstComponent = UnsafePointer<Int>(sizeof(Float) * 3)
229+
glVertexAttribPointer(colorSlot, 4, GLenum(GL_FLOAT), GLboolean(UInt8(GL_FALSE)), GLsizei(sizeof(Vertex)), nil)
197230

198231
glGenBuffers(1, &indexBuffer)
199-
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer)
200-
glBufferData(GL_ELEMENT_ARRAY_BUFFER, Indices.size(), Indices, GL_STATIC_DRAW)
232+
glBindBuffer(GLenum(GL_ELEMENT_ARRAY_BUFFER), indexBuffer)
233+
glBufferData(GLenum(GL_ELEMENT_ARRAY_BUFFER), Indices.size(), Indices, GLenum(GL_STATIC_DRAW))
201234

202-
glBindBuffer(GL_ARRAY_BUFFER, 0)
235+
glBindBuffer(GLenum(GL_ARRAY_BUFFER), 0)
203236
glBindVertexArrayOES(0)
204237
}
205238

206239
func render() {
207240
glBindVertexArrayOES(VAO);
208241
glViewport(0, 0, GLint(self.frame.size.width), GLint(self.frame.size.height));
209242

210-
glDrawElements(GL_TRIANGLES, Indices.count, GL_UNSIGNED_BYTE, nil)
243+
glDrawElements(GLenum(GL_TRIANGLES), GLsizei(Indices.count), GLenum(GL_UNSIGNED_BYTE), nil)
211244

212245
self.context.presentRenderbuffer(Int(GL_RENDERBUFFER))
213246

214247
glBindVertexArrayOES(0)
215248
}
216249
}
217250

218-
219-
//helper extensions to pass arguments to GL land
220-
extension Array {
221-
func size () -> Int {
222-
return self.count * sizeofValue(self[0])
223-
}
224-
}
225-
226-
extension Int32 {
227-
func __conversion() -> GLenum {
228-
return GLuint(self.asUnsigned())
229-
}
230-
231-
func __conversion() -> GLboolean {
232-
return GLboolean.convertFromIntegerLiteral(UInt8(self))
233-
}
234-
}
235-
236-
extension Int {
237-
func __conversion() -> Int32 {
238-
return Int32(self)
239-
}
240-
241-
func __conversion() -> GLubyte {
242-
return GLubyte(self)
243-
}
244-
245-
}
246251
///////////////////////////////////////
247252

0 commit comments

Comments
 (0)