开发者

Trying to create a C Function List

I have been trying to create a list of general global C functions for various classes to use, and although i've done this in the past, this one is having problems. Here are the .h and .c parts of the list.

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,
                   开发者_如何学JAVA        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;
}

When I don't import or any other framework, I receive 'Parse Error: unknown type name' for CGFloat and GLfloat. When I do inside the .h file, I get Parse and Semantic errors, where NSString is an unknown type name inside the framework, as well as other, 'Expected Identifier or (" errors.

I've never had to include this header for my original C function lists, i've gone through other example code from Apple and i've checked headers on other classes that use these functions and typedefs, and I cant find the problem.


CGFloat is part of the CoreGraphics Framework. If you want to use pure C you will not have access to the CGFloat and need to define it as just a float. If your class is only going to be used with Objective-C you can make it a .m file and you should not have any trouble.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜