About Monkey 2 › Forums › Monkey 2 Programming Help › Anyone know how to convert this C/C++ initialiser?! { {0,0}, 0.5 }
Tagged: c/c++, GLSL, init
This topic contains 4 replies, has 2 voices, and was last updated by DruggedBunny 1 month ago.
-
AuthorPosts
-
February 1, 2019 at 12:38 pm #16031
Hi all,
Does anyone know how to translate this C/C++ array initialisation (with nested {} braces) into a loop?
The struct is just an array of RGB values as integers, and I need to iterate through each, but I can’t tell what the init part is actually doing!
Monkey
1234567struct MixingPlan{int colors[64];};MixingPlan result = { {0,0}, 0.5 };How would I translate this to something like this?
Monkey
123456MixingPlan result;for (int loop, etc){result.colors[loop] = er...}I’m trying to port this palette/dither code to GLSL, so can’t use this style of initialiser, and I don’t understand what the {{0,0},0.5} thing is doing in regards to an integer value!
February 1, 2019 at 2:42 pm #16032
Danilo
ParticipantWhen I search the site for “MixingPlan” I get more than one result, and it looks like you mix them up.
1st, 2nd, 3rd search result are in the same source code:
C++
12345678910struct MixingPlan{unsigned colors[2];double ratio; /* 0 = always index1, 1 = always index2, 0.5 = 50% of both */};MixingPlan DeviseBestMixingPlan(unsigned color){const unsigned r = color>>16, g = (color>>8)&0xFF, b = color&0xFF;MixingPlan result = { {0,0}, 0.5 };More search results containing “MixingPlan” show up.
Later there is a source code with a new MixingPlan struct:
C++
12345678struct MixingPlan{const unsigned n_colors = 16;unsigned colors[n_colors];};MixingPlan DeviseBestMixingPlan(unsigned color){MixingPlan result = { {0} };Next one:
C++
1typedef std::vector<unsigned> MixingPlan;And then, at round about the middle of the page (last search result):
C++
1234567struct MixingPlan{unsigned colors[64];};MixingPlan DeviseBestMixingPlan(unsigned color){MixingPlan result = { {0} };February 2, 2019 at 3:27 am #16034Ah, thanks for pointing that out… the article is a little confusing, though I think the intent is to offer different options… I’ll look into that a little more closely.
Any idea on the initialisation part? I just don’t get this, and it’s not something easily searched-for!
Monkey
1MixingPlan result = { {0,0}, 0.5 };February 2, 2019 at 5:28 am #16035
Danilo
ParticipantThis init part is for the struct with an array of 2 unsigned and 1 double.
C++
12345678struct MixingPlan{unsigned colors[2];double ratio;};MixingPlan result = { {0,0}, 0.5 };Your struct colors[64] does not use such initialization:
C++
1234567struct MixingPlan{unsigned colors[64];};MixingPlan result = { {0} };As you can see, ‘ = { values }; ‘ is used for init, and the inner ‘{ values }’ is for array init.
See also: cppreference.com >> C reference >> initialization >> array initialization
Initialization from brace-enclosed lists
C++
1int z[3] = {0}; // z has type int[3] and holds all zeroesNested arrays
C++
1234567891011int y[4][3] = { // array of 4 arrays of 3 ints each (4x3 matrix){ 1 }, // row 0 initialized to {1, 0, 0}{ 0, 1 }, // row 1 initialized to {0, 1, 0}{ [2]=1 }, // row 2 initialized to {0, 0, 1}}; // row 3 initialized to {0, 0, 0}struct { int a[3], b; } w[] = { { 1 }, 2 }; // array of structs// { 1 } is taken to be a fully-braced initializer for element #0 of the array// that element is initialized to { {1, 0, 0}, 0}// 2 is taken to be the first initialized for element #1 of the array// that element is initialized { {2, 0, 0}, 0}February 2, 2019 at 8:37 am #16036Oh, I see now! Thanks for that, Danilo, really appreciate it!
Hopefully I can get this converted today… thanks again.
-
AuthorPosts
You must be logged in to reply to this topic.