加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 18:57:25 所属栏目:百科 来源:网络整理
导读:我最近开始学习编程,在iPhone上制作我自己的3D OpenGL游戏,到目前为止已取得了相当不错的进展.我开始使用随iPhone SDK提供的基本OpenGL示例,这帮助我开始了一个良好的开端.然而,当我开始掌握一些事情时,我发现我在Objective C中进行了不必要的编程,这将使得
我最近开始学习编程,在iPhone上制作我自己的3D OpenGL游戏,到目前为止已取得了相当不错的进展.我开始使用随iPhone SDK提供的基本OpenGL示例,这帮助我开始了一个良好的开端.然而,当我开始掌握一些事情时,我发现我在Objective C中进行了不必要的编程,这将使得将来将游戏移植到其他平台变得更加困难.所以我认为现在最好把它做成C以避免以后的大量额外工作.

澄清一下:我实际上并没有使用任何对Apple(Objective C)函数或其他任何函数的调用,只是因为我将所有的条款都基于Objective C风格的init / dealloc / etc,所以我的引擎看起来像Objective使用C类时.我的目标是用C等价替换所有目标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
>用#include指令替换#import,因为此指令仅存在于Objective-C中(确保包含的头文件不受多个包含的影响)
>摆脱NS …类和方法的用法,因为它们是Cocoa框架的一部分,很可能无法移植

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读