I recently started to study programming in order to make my own 3D OpenGL game on iPhone and still have made quite a decent progress. I started using the basic OpenGL example that comes with the iPhone SDK, which helped me get a good start. However, when I started to understand things, it occurred to me that I needed to program in Objective-C, which would make it difficult to port the game to other platforms in the future. So I decided that it would be better to do it correctly with C ++ in order to avoid additional additional work later.
To clarify: I don't actually use any calls to Apple functions (Objective-C) or anything else, just that I based all my clans on the Objective-C style init / dealloc / etc, so my engine is used when using the Objective-C class. My goal is to replace all C objects with C ++ equivalents ... the problem is that, being fairly new to C ++, I'm not sure if it matches that!
Here is a simple example of one of my classes (myLight) in its current incarnation of Objective-C:
// myLight.h #import <OpenGLES/EAGL.h>
And the corresponding .mm file:
// myLight.m #import "myLight.h" @implementation myLight @synthesize name, ambient, diffuse, specular, position, spotDirection; - (id)initWithContentsFromDatastream:(NSData *)fileData { self = [super init]; NSData *fileContents = fileData; uint ptr = 0; Byte nameLength; [fileContents getBytes:&nameLength range: NSMakeRange(ptr, sizeof(Byte))]; ptr++; name = new char[nameLength]; [fileContents getBytes:name range: NSMakeRange(ptr, (nameLength * sizeof(char)) )]; ptr = ptr + (nameLength * sizeof(char) ); [fileContents getBytes:&lightType range: NSMakeRange(ptr, sizeof(Byte))]; ptr++; position = new GLfloat[4]; for(int j = 0; j < (4); j++) [fileContents getBytes:&position[j] range: NSMakeRange( (j* sizeof(float) ) + ptr, sizeof(float))]; ptr = ptr + (4 * sizeof(float)); if(lightType==2){ spotDirection = new GLfloat[3]; for(int j = 0; j < (3); j++) [fileContents getBytes:&spotDirection[j] range: NSMakeRange( (j* sizeof(float) ) + ptr, sizeof(float))]; ptr = ptr + (3 * sizeof(float)); [fileContents getBytes:&spotRadius range: NSMakeRange(ptr, sizeof(float))]; ptr = ptr + sizeof(float); } else spotDirection = NULL; diffuse = new GLfloat[4]; for(int j = 0; j < (4); j++) [fileContents getBytes:&diffuse[j] range: NSMakeRange( (j* sizeof(float) ) + ptr, sizeof(float))]; ptr = ptr + (4 * sizeof(float)); ambient = new GLfloat[4]; for(int j = 0; j < (4); j++) [fileContents getBytes:&ambient[j] range: NSMakeRange( (j* sizeof(float) ) + ptr, sizeof(float))]; ptr = ptr + (4 * sizeof(float)); specular = new GLfloat[4]; for(int j = 0; j < (4); j++) [fileContents getBytes:&specular[j] range: NSMakeRange( (j* sizeof(float) ) + ptr, sizeof(float))]; ptr = ptr + (4 * sizeof(float)); [self set]; return self; } - (void)set{ glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glLightfv(GL_LIGHT0, GL_AMBIENT, ambient); glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse); glLightfv(GL_LIGHT0, GL_SPECULAR, specular); glLightfv(GL_LIGHT0, GL_POSITION, position); if(lightType==2) glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, spotDirection); } - (void)dealloc { delete[] specular; delete[] ambient; delete[] diffuse; if (spotDirection) delete[] spotDirection; delete[] position; delete[] name; [super dealloc]; } @end
If someone can indicate which lines need to be changed, and, more importantly, that they need to be changed in order to compile it as pure C ++, I would really appreciate it.
Many thanks!