尝试创建C函数列表

时间:2011-06-24 13:26:25

标签: objective-c cocoa opengl

我一直在尝试为各种类创建一个通用全局C函数列表,尽管我过去已经这样做了,但这个函数存在问题。以下是列表中的.h和.c部分。

CGGeometry.h

//CDPoint
//////////////////////////////////////////////////////////////////////

typedef struct CDPoint {
    CGFloat x, y, z;

} CDPoint;

// Creates a CDPoint from 3 float numbers
CDPoint CDPointMake(float x, float y, float z);



//CDLine
//////////////////////////////////////////////////////////////////////

typedef struct CDLine {
    CDPoint a, b;

} CDLine;

// Creates a CDPoint from 2 CDPoints
CDLine CDLineMake(CDPoint a, CDPoint b);



//CDVector
//////////////////////////////////////////////////////////////////////

typedef struct CDVector {
    CDPoint start, finish;
    CDPoint gradient;

} CDVector;

// Creates a CDVector from 2 CDPoints
CDVector CDVectorMake(CDPoint startPoint, CDPoint endPoint);

// Returns a point travelled to on a given vector, using a start point and a distance        scalar.
CDPoint CDVectorTrace(CDVector vecToTrace, CDPoint startPoint, float distance);


//CDExtra
//////////////////////////////////////////////////////////////////////

// This is stuff that shoudn't really be in this section, but are for convenience      purposes until it has enough functions to be standalone.

GLfloat* CDMeshColorsCreateGrey(CGFloat bValue, CGFloat vertCount);

CGFloat* CDMeshVertexesCreateRectangle(CGFloat height, CGFloat width);

CDGeometry.c

#include "CDGeometry.h"

//CDGeometry.c

/* A collection of functions and typedefs that aid 2D and 3D environment positioning and  provides methods for objects and processes.  Also includes elements relevant to collision detection. */


//CDPoint
//////////////////////////////////////////////////////////////////////

CDPoint CDPointMake(float x, float y, float z)
{
    return (CDPoint) {x, y, z};
}



//CDLine
//////////////////////////////////////////////////////////////////////

CDLine CDLineMake(CDPoint a, CDPoint b) 
{
    return (CDLine) {a, b};
}




//CDVector
//////////////////////////////////////////////////////////////////////

CDVector CDVectorMake(CDPoint startPoint, CDPoint endPoint)
{
CDPoint grad = CDPointMake(startPoint.x / endPoint.x,
                           startPoint.y / endPoint.y,
                           startPoint.z / endPoint.z);

return (CDVector) {startPoint, endPoint, grad};
}



//CDExtra
///////////////////////////////////////////////////////////////////// 

GLfloat* CDMeshColorsCreateGrey(CGFloat bValue, CGFloat vertCount) 
{
GLfloat *greyColor = (GLfloat *) malloc(vertCount * 4 * sizeof(GLfloat));

int index = 0;
for (index = 0; index < (vertCount); index++)
{
    int position = index * 4;
    greyColor[position] = bValue;
    greyColor[position + 1] = bValue;
    greyColor[position + 2] = bValue;
    greyColor[position + 3] = 1.0;
}

return greyColor;
}

CGFloat* CDMeshVertexesCreateRectangle(CGFloat height, CGFloat width) {
CGFloat *squareVertexes = (CGFloat *) malloc(8 * sizeof(CGFloat));
squareVertexes[0] = -(width / 2);
squareVertexes[1] = -(height / 2);
squareVertexes[2] = (width / 2);
squareVertexes[3] = -(height / 2);
squareVertexes[4] = (width / 2);
squareVertexes[5] = (height / 2);
squareVertexes[6] = -(width / 2);
squareVertexes[7] = (height / 2);

return squareVertexes;
}

当我不导入或任何其他框架时,我收到CGFloat和GLfloat的'Parse Error:unknown type name'。当我在.h文件中执行时,我得到Parse和Semantic错误,其中NSString是框架内的未知类型名称,以及其他“预期标识符”或“错误。

我从来没有必要为我的原始C函数列表包含此标题,我已经浏览了Apple的其他示例代码,并且我已经检查了使用这些函数和typedef的其他类的标题,我无法找到问题

1 个答案:

答案 0 :(得分:6)

CGFloat是CoreGraphics Framework的一部分。如果要使用纯C,则无法访问CGFloat,只需将其定义为浮点数即可。如果你的课程只与Objective-C一起使用,你可以把它变成一个.m文件,你不应该有任何麻烦。