Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


graphics draw() design

This topic is 5690 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post

Link to post
Share on other sites
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:

class Entity
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.

Share this post

Link to post
Share on other sites
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.

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!