c++ - 将 Objective C 格式的代码转换为纯 C++

标签 c++ iphone objective-c xcode

我最近开始学习编程,以便在 iPhone 上制作我自己的 3D OpenGL 游戏,到目前为止取得了相当不错的进步。我开始使用 iPhone SDK 提供的基本 OpenGL 示例,这帮助我有了一个良好的开端。然而,当我开始掌握一些窍门时,我突然想到我不必要地使用 Objective C 进行编程,这将使将来将游戏移植到其他平台变得更加困难。所以我认为现在最好将其正确地用 C++ 来避免以后的大量额外工作。

澄清一下:我实际上并没有使用任何对 Apple( Objective-C )函数的调用或任何东西,只是我将我的所有类都基于 Objective-C 风格的 init/dealloc/等,所以我的引擎使用时看起来像 Objective C 类。我的目标是用 C++ 等价物替换所有 Objective-C 的东西......问题是,作为 C++ 的新手,我不确定什么对应什么!

这是我的一个类 (myLight) 的一个简单示例,在其当前的 Objective C 化身中:

//  myLight.h

#import <OpenGLES/EAGL.h>
#import <OpenGLES/ES1/gl.h>
#import <OpenGLES/ES1/glext.h>

@interface myLight : NSObject {
    char *name;
    GLfloat *ambient, *diffuse, *specular, *position, *spotDirection;
    GLfloat spotRadius;

    GLfloat *matAmbient, *matDiffuse, *matSpecular;
    GLfloat shininess;
    Byte lightType;
}
@property (readonly) char *name;
@property (assign) GLfloat *position;
@property (assign) GLfloat *spotDirection;

@property (assign) GLfloat *ambient;
@property (assign) GLfloat *diffuse;
@property (assign) GLfloat *specular;

- (id)initWithContentsFromDatastream:(NSData *)fileData;
- (void)set;

@end

以及对应的.mm文件:

//  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

如果有人能指出哪些行需要更改,更重要的是,应该更改哪些行以使其编译为纯 C++,我将不胜感激。

非常感谢!

最佳答案

首先,请记住 Objective-C 不仅仅是 C 的“不同格式”,它是一种独立的语言,更重要的是,就 Cocoa 而言,它是一个独立的框架。因此,如果你希望将来能够将你的项目移植到其他平台,你不仅要摆脱 Objective-C,还要摆脱 Cocoa 框架。

要将类从 Objective-C 映射到 C++,您必须执行以下操作:

  • 创建一个新的 C++ 类来替换旧类
  • -init... 方法创建构造函数
  • -dealloc 方法创建析构函数
  • 创建复制功能的其他方法
  • 对于属性,您可能会改为创建 getter 和 setter
  • #import 替换为 #include 指令,因为该指令仅存在于 Objective-C 中(确保您包含的 header 不受多次包含的影响)
  • 摆脱 NS...类和方法的使用,因为它们是 Cocoa 框架的一部分,很可能无法移植

您应该花一些时间考虑如何使用您正在编写的代码。 1:1 移植可能不是一个好主意,因为 Cocoa 和 C++(或任何其他语言/框架)编码之间的习惯用语有很多差异。

关于c++ - 将 Objective C 格式的代码转换为纯 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3613310/

相关文章:

ios - 如何在应用程序委托(delegate)上的服务器调用后更改根 Controller ?

iphone - 适用于 ios 3.0 和更新版本的 UIWebView 的捏缩放

iphone - 当UITabBarController加载而不是在ViewController上时添加角标(Badge)

ios - 我对如何绘制空心圆角矩形感到很困惑。我怎样才能做到这一点?

c++ - 在白色背景上绘制球体(C++ 和 OpenGL)

iPad 上的 iPhone 应用程序 : UILabel pixelated during rotation?

ios - UIImageView 加载较大图像后调整大小

c++ - 如何创建一个结构体来存储和返回 float 组?

c++ - 如果一个线程在关键部分调用 Acquire(),如果另一个线程调用 Release(),该锁是否会被释放?

C++ Boost线程库拉入整个开发环境