Page 1 of 1

Can you have a struct pointer ?

Posted: Mon Aug 09, 2010 10:35 am
by seulater
Dont laugh to hard if this is a ridiculous question.

I have a group of fonts that are set up as structs as follows.
struct XySize
{
BYTE bSize;
BYTE xSize;
BYTE ySize;
};

const struct Font12T
{
XySize sizeInfo;
BYTE fontdata[24];
}
Font12T[95] =
{

/* Element 0x0010 - Char 0x0030 */
{{2, 8, 12},{
0x00,0x00, /* ................ */
0x00,0x00, /* ................ */
0x00,0x00, /* ................ */
0x38,0x00, /* ..%%%........... */
0x44,0x00, /* .%...%.......... */
0x44,0x00, /* .%...%.......... */
0x44,0x00, /* .%...%.......... */
0x44,0x00, /* .%...%.......... */
0x44,0x00, /* .%...%.......... */
0x38,0x00, /* ..%%%........... */
0x00,0x00, /* ................ */
0x00,0x00 /* ................ */
}}
,

/* Element 0x0011 - Char 0x0031 */
{{2, 8, 12},{
0x00,0x00, /* ................ */
0x00,0x00, /* ................ */
0x00,0x00, /* ................ */
0x10,0x00, /* ...%............ */
0x30,0x00, /* ..%%............ */
0x10,0x00, /* ...%............ */
0x10,0x00, /* ...%............ */
0x10,0x00, /* ...%............ */
0x10,0x00, /* ...%............ */
0x38,0x00, /* ..%%%........... */
0x00,0x00, /* ................ */
0x00,0x00 /* ................ */
}}
,... and so on.

Now, as you see here is was for "Font12T" then i have "Font14T", "Font16T" and so on.

For me to get the data i do:

BYTE x_size = Font12T[0].sizeInfo.bSize;
BYTE x_size = Font12T[0].sizeInfo.xSize;
BYTE y_size = Font12T[0].sizeInfo.ySize;
const BYTE *pdata = Font12F[0].fontdata;


ok, here is my issue, i have a routine called
"void LCD_String(char *lcd_string, BYTE font_type, WORD x, WORD y)"

where i pass my string to be printed to the LCD, i want to tell it font type on the fly. But since my fonts are in these structures, i have a fixed statement like
x_size = Font12T[0].sizeInfo.bSize;

I was wondering if i could create another structure, and have it point to the start of the font struct i wanted. here is what i am looking to do if you will.


//This will be my temp struct., now this is probably not set up right, buy you get the idea.

struct Temp_XySize
{
BYTE bSize;
BYTE xSize;
BYTE ySize;
};

struct Temp_Font
{
XySize sizeInfo;
BYTE fontdata[24];
}


then in my main code i could do this.

#define FONT12T (0)
#define FONT14T (1)

LCD_String("Hello World", FONT12T, 0,0);


Then inside LCD_String i would do this.

void LCD_String(char *lcd_string, BYTE font_type, WORD x, WORD y)
{

switch(font_type)
{

case FONT12T:

// I know this is jacked up, but i need my temp structure to point to the real font one.
Temp_Font = Font12T[0];

break;

case FONT14T:

// I know this is jacked up, but i need my temp structure to point to the real font one.
Temp_Font = Font14T[0];

break;

}

}


then i can now just use this and it will be pointing to the font i have chosen.

BYTE x_size = Temp_Font[0].sizeInfo.bSize;
BYTE x_size = Temp_Font[0].sizeInfo.xSize;
BYTE y_size = Temp_Font[0].sizeInfo.ySize;


hopefully i was able to make sense of what i am trying to do.

Re: Can you have a struct pointer ?

Posted: Mon Aug 09, 2010 12:09 pm
by tod
Yes you can have a pointer to a struct, but I fear you may be headed down the rabbit hole. Here's the direct answer to the question in the title (using my previous code example, not yours - just so you can see the syntax)

Code: Select all

	Font14::Font* my_font_ptr = &Font14::style[0];
	BYTE x_from_ptr = my_font_ptr->sizeInfo.xSize;
	cout << "Value from x ptr:"<< x_from_ptr+0 << endl;



Now the problem you are going to have is with Temp_Font and in particular the array for fontData. From previous discussions Temp_Font is not going to be the same for all fonts is it? When you try to do something like this
Temp_Font* my_temp_font_ptr = &YourFont12;
The compiler will generate a conversion error. C++ will let you get yourself in a lot of trouble if you let it. You can get by the compiler error by casting a pointer from any font to Temp_Font but as soon as you have a mismatch on the underlying structure you're likely to start writing into memory that belongs to something else and you'll end up trapping.

I said this before but I'll say it again a little more forcefully, even though I don't know your exact problem and constraints. This all sounds like you just need a single Font class. It would have a constructor that takes the x.size, y.size and size of the font data. The class would new up the storage needed for the font data (and delete it properly in the constructor). (Personally I would use a vector - but I'm going to assume you don't want to use the STL). Then your lcd routine would simply take a const ref to a Font object. If you want to keep using your initializer syntax you can easily do that for the fontData piece. Your class should probably have some helper functions that return the size of the font data. If you want to copy fonts to temp versions (that are deep copies) then you also want a copy constructor and an assignment operator. Otherwise the defaults will give you shallow copies.

Re: Can you have a struct pointer ?

Posted: Mon Aug 09, 2010 12:53 pm
by seulater
I said this before but I'll say it again a little more forcefully, even though I don't know your exact problem and constraints. This all sounds like you just need a single Font class.
What i am understanding you saying is that i should place all my fonts into one big font class.
I.E. take my 20 or so different font types and put them together into just one file. If this is so, i dont want to do that as it would chew up allot of flash. My goal is this. In a project depending on the size of the LCD i want to use. i may only use 3 different font sizes. 3 for this project and say a different 3 for another. if i pull in this whole Font class, it will pull all of them in and be wasting allot of flash when i am only using 3. I want the ability to have them all there and use them at will, and have the compiler only pull in what i am using.
what i have so far does this, but at this moment its seems i am making it more complex than it needs to be.

The main reason i also chose to do it this way is the program that i have that will take a font and spit out a header file for it spits it out the way i have shown. So rather than edit a ton of
lines in 20+ files. i figured i would just use what it produced.

I am open to really any suggestions, except to place them all into one large file.

Re: Can you have a struct pointer ?

Posted: Mon Aug 09, 2010 7:04 pm
by tod
No it would not be my intent to have all your fonts in a single class or file. The idea is to have a single Type for all your fonts. Right now all your fonts are from different structs. I think what you want is either to derive all your fonts from a single class or have a Font* member in each class.

Re: Can you have a struct pointer ?

Posted: Mon Aug 09, 2010 8:00 pm
by seulater
No it would not be my intent to have all your fonts in a single class or file
oh, ok, i thought it was, sorry for my misunderstanding.

its not clear to me how to put all these in a single Type.

Re: Can you have a struct pointer ?

Posted: Tue Aug 10, 2010 10:05 am
by tod
I don't think I can give you the OOP background you need in forum posts. So just be careful with your current approach and think through how you copy your structs, making sure you don't overflow any memory, and remember the sizeof() operator is your friend.

Re: Can you have a struct pointer ?

Posted: Tue Aug 10, 2010 11:41 am
by seulater
I understand, thanks for your tips.