how to store font references in an array?

Hi,

I am developing an interactive touchscreen app and it needs to be flexible in every aspects. User can change not only colors of text but can change the fonts.

I am using adafruit GFX and fonts, so far everything is good. The problem starts with multiple font selection.
I included a couple fonts and use it like

tft->setFont(&FreeMono9pt7b);

that works. I can use this technique by putting in a if or switch statement like

#define MONO 1
#define SANS 2
void selectFont(int selection){
switch(selection) {
case MONO:
 tft.setFont(&FreeMono9pt7b);
 break;
case SANS:
 tft.setFont(&FreeSans9pt7b);
 break;
default:
 tft.setFont();
 break;
}
}

this method works however it costs speed and program memory, thats why i tried to store font references in an array like

GFXfont *myfonts[] = {&FreeMono9pt7b,&FreeSans9pt7b,&FreeSerif9pt7b};

but it gives me millions of error like

SerialConsole.h:49:14: error: initializer for flexible array member 'GFXfont* SerialConsole::myfonts []'

i trieed this code int my computer nimgw copliler and it works but it didnt arduino too. i think the compiler manages pointer differently.

    struct myStruct{        
    int number;
    };

    myStruct no1,no2,no3;

    no1.number=15;
    no2.number=30;
    no3.number=45;

   myStruct *arrayOfPointers[] ={&no1,&no2,&no3};

    for(int i = 0;i<sizeof(arrayOfPointers);i++){
        cout<<arrayOfPointers[i]<<":"<<arrayOfPointers[i]->number<<endl;
    }

any guidence will be much appreciated, thanks.

Hello again i made a deep searching and found out it is not about the compiler or structs it is about the memory the fonts are stored. Since they are not stored in ram, they stored in flash memory, the arrays of your pointers must be stored in const and predefined size. So my final array looks like this:

    const GFXfont *myfonts[7]= {
      &FreeMono9pt7b,
      &FreeSans9pt7b,
      &FreeSerif9pt7b,
      &FreeMonoBold9pt7b,
      &FreeMonoBoldOblique9pt7b,
      &FreeMonoOblique9pt7b,
      &FreeSansBold9pt7b
      };

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.