officedog is very pleased. After much painstaking work, the "label" struct is almost complete. The base struct (Rectangle) functions seem to work and there is an added layer now which is the text label itself. I share some of the code for others who may be interested:
This is the new struct "Label"
Code:
typedef struct label {
Rectangle base; //the foundations
//Font
void * font; //to hold GLUT bitmap fonts
int fontHeightInPixels;
int fontWidthInPixels;
float fontHeightInOrtho;
float fontWidthInOrtho;
float textColour[4]; //rgba
//Label
char label[MAX_SIZE_LABEL];
int LabelLengthInChars;
float labelLengthInOrtho;
float labelHeightInOrtho;
float indentFromLeftInOrtho; // = 1 character
float indentFromRightInOrtho; // = 1 character
char justify; // 'l', 'c', 'r' == left, centre, right
float rasterStartPos[2];
/*******************************************/
// Function pointers - work in progress.....
void (*init)(struct label *lbl, float coords[4], const char *str);
//destructor
}Label;
This is the function which dynamically allocates the struct and calls the init function:
Code:
Label * labelNew(float coords[4], const char * str) {
Label *lbl = malloc(sizeof(Label));
if (lbl != NULL) {
lbl->init = labelInit;
lbl->init(lbl, coords, str); // calling labelInit via the struct function pointer
}
return lbl;
}
And this is function which initialises the struct, also calling on the base (Rectangle) init function
Code:
void labelInit(Label *lbl, float coords[4], const char * str) {
lbl->base.init = rectInit;
lbl->base.init(&(lbl->base), coords);
lbl->font = GLUT_BITMAP_8_BY_13; //Default font
lbl->justify = 'c'; //Default: text is centred
labelUpdateFontDimensions(lbl);
labelSetLabel(lbl, str);
labelSetTextColour(lbl, black); //default text col = black
}
Finally here's a snippet of the code which draws the label (using openGL and GLUT):
Code:
void ODDisplay()
{
glClear(GL_COLOR_BUFFER_BIT);
float red[4] = {1,0,0,0};
float coords[4] = {1,1,2,2};
Label *lbl = labelNew(coords, "hello");
labelPrint(lbl);
lbl->base.setColour(&(lbl->base), red); //calls base function via function pointer
labelDraw(lbl);
glFlush();
}