I decided to make my own simple game. For "procedural texture drawing" I wanted to create color matrices and fill them, tried to do it directly:

struct blockColor{ int r; int g; int b; blockColor() { r = 0; g = 0; b = 0; }; }; blockColor brickColorMap[8][8]; brickColorMap[8][8].r = { {54, 54, 54, 54, 64, 54, 54, 54}, {54, 54, 54, 54, 64, 54, 54, 54}, {64, 64, 64, 64, 64, 64, 64, 64}, {54, 64, 54, 54, 54, 54, 64, 54}, {54, 64, 54, 54, 54, 54, 64, 54}, {64, 64, 64, 64, 64, 64, 64, 64}, {54, 54, 54, 64, 54, 54, 54, 54}, {54, 54, 54, 64, 54, 54, 54, 54} }; 

The compiler, of course, gives an error. How would it be correct to fill in such a matrix of fields?

    4 answers 4

    I would suggest just making the initialization function. Those. initialize the matrix at the start of the program from another array. Something like this:

     struct blockColor{ int r; int g; int b; blockColor() { r = 0; g = 0; b = 0; }; }; blockColor brickColorMap[8][8]; int r_color[8][8] = { { 54, 54, 54, 54, 64, 54, 54, 54 }, { 54, 54, 54, 54, 64, 54, 54, 54 }, { 64, 64, 64, 64, 64, 64, 64, 64 }, { 54, 64, 54, 54, 54, 54, 64, 54 }, { 54, 64, 54, 54, 54, 54, 64, 54 }, { 64, 64, 64, 64, 64, 64, 64, 64 }, { 54, 54, 54, 64, 54, 54, 54, 54 }, { 54, 54, 54, 64, 54, 54, 54, 54 } }; int g_color[8][8] = { // ... }; int b_color[8][8] = { // ... }; void initColor(int blockColor::*member, int data[8][8]) { for (int i = 0; i < 8; ++i) for (int j = 0; j < 8; ++j) brickColorMap[i][j].*member = data[i][j]; } int main(int argc, char** argv) { initColor(&blockColor::r, r_color); initColor(&blockColor::g, g_color); initColor(&blockColor::b, b_color); return 0; } 

    A pointer to a member of the structure is used as a parameter so that one initColor function can be reused for all 3 colors.

      In C ++ 11, initializer_list appeared. You can do this:

       #include <initializer_list> struct blockColor{ int r; int g; int b; blockColor() { r = 0; g = 0; b = 0; }; blockColor(std::initializer_list<int> init) { const int* it = init.begin(); r = *it++; g = *it++; b = *it; }; }; blockColor brickColorMap[3][2] = { { { 54, 54, 54 }, { 54, 54, 54 } }, { { 64, 64, 64 }, { 64, 64, 64 } }, { { 54, 64, 54 }, { 54, 54, 54 } } }; 
      • That is, in this way it is possible to specify all three fields at once in one two-dimensional array (r, g, b) instead of creating three matrices for each color separately? I tried your code - it is not compiled - "Initialization of blockColor type objects using the expression {....} is not allowed. I work in MVS2010 - Bovtik Bovtenko
      • 2010 does not support C ++ 11 - Nikolay
      • But this is terrible, the right word. What is the initializer_list here for, it is completely for other purposes ... - ixSci
      • Um .. And why initializer list, if the assignment of structures across the fields has been working since ancient times? - Qwertiy
      • The option proposed by @Qwertiy is really better and works in VS2010. - cybrex

      http://codepad.org/9Uxqmnn3

       struct blockColor { int r, g, b; }; blockColor brickColorMap[3][2] = { { { 54, 54, 54 }, { 54, 54, 54 } }, { { 64, 64, 64 }, { 64, 64, 64 } }, { { 54, 64, 54 }, { 54, 54, 54 } } }; 

        You just need to add a constructor for all 3 colors and use it:

         struct blockColor{ int r; int g; int b; blockColor() { r = 0; g = 0; b = 0; } blockColor(int r, int g, int b) { this->r = r; this->g = g; this->b = b; } }; blockColor brickColorMap[2][2] = { {blockColor(54, 54, 54), blockColor(55, 55, 55)}, {blockColor(56, 56, 56), blockColor(57, 57, 57)}, }; 

        Initialization in the style of modern C ++, which MSVC2010 does not fully support (in particular, such initialization will not work there):

         blockColor brickColorMap[2][2] = { {{54, 54, 54}, {55, 55, 55}}, {{56, 56, 56}, {57, 57, 57}}, }; 
        • If you demolish the designers, it probably will. At least, g ++ spits in the case of the present constructor. - Qwertiy
        • @Qwertiy, there's nothing to spit on. If you remove constructors, aggregation initialization will occur, which does not use a constructor. This is quite a method, I just don’t welcome aggregation initialization. - ixSci
        • Yes, I'm talking about aggregation initializer. With the constructor, it does not compile: codepad.org/9Uxqmnn3 and codepad.org/sPoMOYN4 - Qwertiy
        • @Qwertiy, I don’t know what the compiler is and the flags on this codepad, here’s your ideone - ixSci
        • Funny. There precisely there is an interpretation of warnings as errors. What else - I do not know. Perhaps some of the analytical checks worked. - Qwertiy