我面临的问题是我想制作一个单个结构或容器,其中将包含使用 c 语言的许多变量。 下面你可以看到我尝试制作一个 Color 类型和一个函数来更容易地定义一个。
// Color Type For reuse
typedef struct Color
{
GLfloat R;
GLfloat G;
GLfloat B;
GLfloat A;
} Color;
// Color Setter to Make using the Color Type Easier
Color DefineColor (GLfloat R,GLfloat G,GLfloat B,GLfloat A)
{
Color NewColor; // Combine GLfloat parameters into a Color Type
NewColor.R = R;
NewColor.G = G;
NewColor.B = B;
NewColor.A = A;
return NewColor;
}
我想说的是这样的
typedef struct ColorPalette
{
Color Red = DefineColor(1,0,0,1);
Color Green = DefineColor(0,1,0,1);
Color Blue = DefineColor(0,0,1,1);
Color Violet = DefineColor(1,0,0.5,1);
// ... ect more colors and more colors
} ColorPalette;
这样它就可以这样使用了。
ColorPalette.Red;
或者像这样
Object.attribute.color = ColorPalette.Violet;
Object.Color.ColorPalette.Red;
找到一种以这种方式对变量进行分组的方法在游戏编程、矩阵、数据排序等的其他部分可能非常有用。
最佳答案
我会这样去做-
#include <stdio.h>
typedef struct
{
GLfloat R;
GLfloat G;
GLfloat B;
GLfloat A;
} Color;
typedef struct
{
Color Red;
Color Green;
Color Blue;
Color Violet;
} Palette;
Palette ColorPalette =
{
/* red */
{
1, 0, 0, 1
},
/* green */
{
0, 1, 0, 1
},
/* blue */
{
0, 0, 1, 1
},
/* violet */
{
1, 0, 0.5, 1
}
};
这将为您提供一个包含所有颜色的变量 ColorPalette
。
关于c - 将变量分组到 C 中的结构或容器中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29480921/