I'm having trouble with retrieving some values from a struct. In the below simplified snippets, struct Model's vertices member contains an array of values that can be retrieved correctly if buildModel() is called with drawModel() called within it. However, if I call buildModel() and then drawModel() afterwards, there are no errors, but the vertices are not retrieved with the correct values. This leads me to believe either a variable scope ended, I am passing the references incorrectly, or the vertices member needs to be defined on the heap with malloc.
MODEL.H:
typedef struct Model{
Vertex *vertices;
} Model;
Model* newModel();
Model* setVertices(Vertex *vertices, Model *model);
MODEL.CPP:
Model* newModel(){
Model* model;
model = (Model* )malloc(sizeof(Model));
//model->vertices = (Vertex *)malloc(sizeof(Vertex)); This did not help...
return model;
}
Model* setVertices(Vertex *vertices, Model *model){
model->vertices = vertices;
return model;
}
DRAWING.CPP:
Model* buildModel(){
Model* model = newModel();
Vertex vertices[] = {
{ XMFLOAT3(-1.0f, 5.0f, -1.0f), (XMFLOAT4)colorX},
... //Abbreviated declaration
};
model = setVertices(vertices, model);
//drawModel(model); Calling drawModel() here retrieves vertices correctly
return model;
}
void drawModel(Model *model){
loadVertices(d3dDeviceRef, 11, model->vertices); //Trying to pass vertices array here
}
This has been very useful in learning, and am trying to use as few classes as possible and go more the C route than C++ when I can.
Any help would be much appreciated,
Thanks.