所以我正在做一个跨平台的视频游戏……因此,我需要能够在 windows 平台上使用 DirectX,在 mac、linux 和 android 平台上使用 OpenGL/ES,以及在 iOS 平台上使用 Metal。我搜索了库,但找不到任何东西,所以,我决定创建自己的 typedef,它将是宏控制的,例如,我将 COLOR 定义为 Windows 平台上的 D3DColor,iPhone 平台上的 UIColor , 和 glColor4f 在其他符合 GL 的平台上。这是 iOS 平台的函数所在的头文件 (CommonMetal.h):
#ifndef CommonMetal_h
#define CommonMetal_h
#if defined Metal
typedef UIColor* COLOR;
typedef GLKVector3 VECTOR3;
typedef GLKVector4 VECTOR4;
COLOR NEWCOLOR(float r, float g, float b, float a);
VECTOR3 NEWVECTOR3(float x, float y, float z);
VECTOR4 NEWVECTOR4(float x, float y, float z, float w);
VECTOR3 VECTOR3CrossProduct(VECTOR3 first, VECTOR3 second);
VECTOR4 VECTOR4CrossProduct(VECTOR4 first, VECTOR4 second);
float VECTOR3DotProduct(VECTOR3 first, VECTOR3 second);
float VECTOR4DotProduct(VECTOR4 first, VECTOR4 second);
VECTOR3 VECTOR3Normalize(VECTOR3 vector);
VECTOR4 VECTOR4Normalize(VECTOR4 vector);
float VECTOR3Length(VECTOR3 vector);
float VECTOR4Length(VECTOR4 vector);
VECTOR3 VECTOR3Lerp(VECTOR3 start, VECTOR3 end, float t);
VECTOR4 VECTOR4Lerp(VECTOR4 start, VECTOR4 end, float t);
#endif
#endif
这是 obj-c 源文件 (CommonMetal.m) 中的代码:
#import <Foundation/Foundation.h>
#import "CommonMetal.h"
#if defined Metal
COLOR NEWCOLOR(float r, float g, float b, float a)
{
return [UIColor colorWithRed:r green:g blue:b alpha:a];
}
VECTOR3 NEWVECTOR3(float x, float y, float z)
{
return GLKVector3Make(x, y, z);
}
VECTOR4 NEWVECTOR4(float x, float y, float z, float w)
{
return GLKVector4Make(x, y, z, w);
}
VECTOR3 VECTOR3CrossProduct(VECTOR3 first, VECTOR3 second)
{
return GLKVector3CrossProduct(first, second);
}
VECTOR4 VECTOR4CrossProduct(VECTOR4 first, VECTOR4 second)
{
return GLKVector4CrossProduct(first, second);
}
float VECTOR3DotProduct(VECTOR3 first, VECTOR3 second)
{
return GLKVector3DotProduct(first, second);
}
float VECTOR4DotProduct(VECTOR4 first, VECTOR4 second)
{
return GLKVector4DotProduct(first, second);
}
VECTOR3 VECTOR3Normalize(VECTOR3 vector)
{
return GLKVector3Normalize(vector);
}
VECTOR4 VECTOR4Normalize(VECTOR4 vector)
{
return GLKVector4Normalize(vector);
}
float VECTOR3Length(VECTOR3 vector)
{
return GLKVector3Length(vector);
}
float VECTOR4Length(VECTOR4 vector)
{
return GLKVector4Length(vector);
}
VECTOR3 VECTOR3Lerp(VECTOR3 start, VECTOR3 end, float t)
{
return GLKVector3Lerp(start, end, t);
}
VECTOR4 VECTOR4Lerp(VECTOR4 start, VECTOR4 end, float t)
{
return GLKVector4Lerp(start, end, t);
}
#endif
注意:Metal 是我创建的预定义宏,以确保这是一个兼容 metal 的平台,并且代码已放置在 Precompiled Header 中。
当我尝试编译项目时,我收到指向 typedef 行的头文件的错误。这是确切的错误:
.../CommonMetal.h:13:9:未知类型名称“UIColor”
.../CommonMetal.h:14:9: 未知类型名称 'GLKVector3'
.../CommonMetal.h:15:9: 未知类型名称 'GLKVector4'
我已经阅读了有关堆栈溢出的其他答案,但是每当我尝试添加时:
#import <UIKit/UIKit.h>
出现此问题/答案中描述的错误。因此,据我了解,该文件被认为是 c++ 文件而不是 obj-c 文件。我尝试使用上面链接中建议的答案,这确实消除了错误,但之前的 UIColor 错误未定义等仍然存在。如果需要任何其他信息,请添加评论,我一定会添加额外的信息。提前致谢。