graphics draw() design
Say in main.c we have the main loop that is something like
void gameloop(void) {
getinput();
...
drawEverythingThereistodraw();
}
(No, this is not how my code actually looks like).
Over in video.c we have
void initVideo(void) {
...
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, 1.0, 1.5, 20.0);
}
void drawEverythingThereistodraw(void) {
...
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(...);
// draw all the crap here
...
}
Now there are potentially many files that need to get things drawn. For example entity.c might have a draw function to (very surprisingly) draw the entity. So how should I arrange things so that this single function drawEverythingThereistodraw(void) calls all the necessary draw functions which are defined in many different files. A simple obvious would be to store a collection of function pointers say like
void (*drawitfunc)(/*parameters which need to be decided correctly the first time or else later problems*/);
So in some sense this is a data structure design question I guess. Then in drawEverythingThereistodraw I could just go through all the function pointers and call the draw functions they point to. In any event somehow all these draw calls need to be call through a single point it just makes sense that way I can''t see how you would organize your code in a different way that would be good.
Yes I would say that your array of function pointers is pretty much the way to go in C. C++ makes things much nicer with base classes - eg:
This is what you put in a single header file. The "actual" nitty-gritty Entities would inherit from this base Entity and implement all the good stuff.
Here I have added "Move" so you can update you animation in a similar way - just running through your list of pointers.
The equivalent tin in C woud be to have an array-of-structures-of-function-pointers (and maybe a "void *data element").
The next thing you woudl to is put a "factory" function/class in a common header file, eg:
Entity *CreateEntityFromMD3(const char *med_model_name);
And imlpement this in one of your C files. The main point being here that you can be as complex as you want in the C file and your Engine code need never know.
If you are looking at maybe a structure of function points with void * data, you are 90% of the way to mastering C++ virtual classes - come over to the dark side - there''s no going back.
class Entity
{
public:
virtual ~Entity(){}
virtual void Draw( context ? )=0;
virtual void Move( double time_step )=0;
...
};
This is what you put in a single header file. The "actual" nitty-gritty Entities would inherit from this base Entity and implement all the good stuff.
Here I have added "Move" so you can update you animation in a similar way - just running through your list of pointers.
The equivalent tin in C woud be to have an array-of-structures-of-function-pointers (and maybe a "void *data element").
The next thing you woudl to is put a "factory" function/class in a common header file, eg:
Entity *CreateEntityFromMD3(const char *med_model_name);
And imlpement this in one of your C files. The main point being here that you can be as complex as you want in the C file and your Engine code need never know.
If you are looking at maybe a structure of function points with void * data, you are 90% of the way to mastering C++ virtual classes - come over to the dark side - there''s no going back.
Also, where you have:
(/*parameters which need to be decided correctly the first time or else later problems*/);
It is much easier to have:
(*draw)( MyDrawContext *context );
Here you can add members to your MyDrawContext structure without having to change 4 million lines of code/header files.
(/*parameters which need to be decided correctly the first time or else later problems*/);
It is much easier to have:
(*draw)( MyDrawContext *context );
Here you can add members to your MyDrawContext structure without having to change 4 million lines of code/header files.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement