澄清一下:我实际上并没有使用任何对Apple(Objective C)函数或其他任何函数的调用,只是因为我将所有的条款都基于Objective C风格的init / dealloc / etc,所以我的引擎看起来像Objective使用C类时.我的目标是用C等价替换所有目标C的东西…麻烦的是,对于C来说是新手,我不确定什么对应于什么!
这是我的一个类(mylight)的一个简单示例,在其当前的Objective C化身中:
@H_403_15@// 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文件:
@H_403_15@// 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(或任何其他语言/框架)中编码之间的习语存在许多差异.
总结以上是内存溢出为你收集整理的将Objective C格式的代码转换为纯C全部内容,希望文章能够帮你解决将Objective C格式的代码转换为纯C所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)