c - 将变量分组到 C 中的结构或容器中

标签 c opengl data-structures grouping game-engine

我面临的问题是我想制作一个单个结构或容器,其中将包含使用 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/

相关文章:

c++ - 如何在 OSX 下的 SDL/OpenGL 应用程序中加载 JPG/PNG 纹理

c++ - 从过剩教程中了解宏测量数组计数

algorithm - 具有分摊 O(1) 删除和 O(log n) 搜索的数据结构

javascript - 如何在javascript中实现字典?

c - 如何在 C 中拥有像 'char *name[]' 这样的字符?

c++ - C 与 C++ 枚举与 extern "C"的链接

c - 我如何获得一个函数来替换数组中 char 的所有实例?

c - 在开头插入节点

opengl - Haskell 的 GLUT 替代品?

c# - C# 中类似缓存的数据结构的最佳结构?