3

The Problem I'm facing is that I want to make a single structure or container that will contain many variables using the c language. Below you can see that I tried to make a Color type and a function to define one more easily.

// 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;
}

What I'm trying to get at is something like this

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;

So that it could be used as such.

ColorPalette.Red;

or like this

 Object.attribute.color = ColorPalette.Violet;
 Object.Color.ColorPalette.Red;

finding a way to group variables in this way could be very useful in other parts of Game Programming, Matrices , Data sorting and such.

Saul does Code
  • 882
  • 2
  • 10
  • 13
  • 1
    1) lose the typedef modifier. 2) each color entry, during the initial declaration of an instance of the struct could be set via: struct ColorPalette myColorPalette = {{1,0,0,1},{0,1,0,1}, ... }; – user3629249 Apr 06 '15 at 23:22
  • What is your question? You already showed code that seems to do what you are trying to do. – M.M Apr 07 '15 at 00:02

2 Answers2

2

I would go about it like this -

#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
    }
};

This will give you a variable ColorPalette that contains all of your colors.

embedded_guy
  • 1,939
  • 3
  • 24
  • 39
  • this seems to be working just fine , thank you , is there perhaps an easier way to serialize all the colors? Perhaps using a loop to generate them – Saul does Code Apr 07 '15 at 00:00
  • Certainly! It all depends on what you are trying to accomplish. Do you want to assign values to each color within the source code so that they are defined at compile time (perhaps even keeping the data constant), or would you rather have a function you can call to initialize every element (or the user can use to initialize values)? You could go a completely different route and allocate memory dynamically to add or remove colors from a `ColorPalette` - perhaps through a linked list. It all depends on what you are trying to accomplish. – embedded_guy Apr 07 '15 at 00:18
  • I suppose it depends in the amount of colors , I mean one does not need all the colors , take css for example their pre-defined word colors. All I really wish to accomplish with this is to make a useful tool for working with shaders, ui , materials and what not , but , I suppose in terms of serialization an unorganised container like a C++ vector could work and allow users to define their own colors and add or remove them. – Saul does Code Apr 07 '15 at 00:23
  • Perhaps it's worth considering rewriting this thing in C++ with maps and vectors and algorithm support. – Saul does Code Apr 07 '15 at 00:25
1

You can't directly use your variable when you define your structure. Try like this :

typedef struct ColorPalette
{
  Color Red;
  Color Green;
  Color Blue;
  Color Violet;
  // ... ect more colors and more colors                                                                                                                                                      
} ColorPalette;

And then, you have to create a function that will store all the values on the variable which are on your structure ColorPalette. For exemple :

ColorPalette    *fulfill_color(void)
{
  ColorPalette  *color;

  if ((color = malloc(sizeof(ColorPalette))) == NULL)
      return (NULL);
  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,1);
  return (color);
}

And, it seems obvious, but you can call the function fulfill_color() to store all the colors on a variable with the type ColorPalette. For exemple :

int             main()
{
  ColorPalette  *color;

  color = fulfill_color();
}

After the call of the function, you can type ..

color.Red.R (Value 1)

color.Red.G (Value 0)

etc..

.. to reach your desired values.

Good luck!

Community
  • 1
  • 1
Gangai Johann
  • 881
  • 12
  • 17
  • 1
    adding manual memory management to this makes everything harder to use for no reason – M.M Apr 07 '15 at 00:03
  • Hi Gangai. You have the right idea but your `fulfill_color()` function will not work as you intended. See this question: http://stackoverflow.com/questions/7754986/returning-pointer-from-a-function – embedded_guy Apr 07 '15 at 00:11
  • Hi embedded_guy ! It's just a mistake, but yes, you have to allow the sufficient memory space and gets the return value of malloc with the pointer color declared. Thank you for your response, I appreciate. – Gangai Johann Apr 07 '15 at 00:13
  • It's now functional. And, Mtt McNabb, i disagree your note. It's not complicated, and concerning its problem, using pointers can be helpful if he wants to retrieve the colors on another function, without filling the structure at each new function. – Gangai Johann Apr 07 '15 at 00:18