So I'm working on a cross-platform video game... Therefore, I need to be able to use DirectX on windows platforms, OpenGL/ES on mac, linux and android platforms, and Metal on the iOS platform. I searched for libraries but couldn't find anything, so, I decided to create my own typedefs which will be macro-controlled, so that, for example, I'd define COLOR to be D3DColor on windows platforms, UIColor on the iPhone platform, and glColor4f on the other GL-compliant platforms. This is the header file (CommonMetal.h) in which the function for the iOS platform resides:
#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
and this is the code inside the obj-c source file (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
Note: Metal is the predefined macro that I created to ensure that this is a metal-compliant platform and the code has been placed in the Precompiled Header.
When I try to compile the project, I get an error pointing at the header file at the typedef lines. This is the exact error:
.../CommonMetal.h:13:9: Unknown type name 'UIColor'
.../CommonMetal.h:14:9: Unknown type name 'GLKVector3'
.../CommonMetal.h:15:9: Unknown type name 'GLKVector4'
I've read other answers on stack overflow, but whenever I try to add:
#import <UIKit/UIKit.h>
Errors just as described in this question/answer show up. Therefore, from what I understood is that the file is being thought of as being a c++ file and not an obj-c file. I tried using the answers suggested in the link above, which do remove the error, but the previous error of UIColor not being defined, etc, still remains there. If any other info is required, please add a comment and I will be sure to add that extra info. Thanks in advance.