User defined functions?

Started by
3 comments, last by noodleBowl 8 years, 4 months ago

I was wondering if there is a way to have user defined functions?

Here is my situation, I'm trying my hand at multi-platform code.

I have a source file that has multiple main functions meant for their specific platform. When I compile a specific platform, the main function for that platform will be the one available and act as the programs entry point. So for example:

Desktop compile would have and use the typical main:


void main()
{
   //Program entry point
}


Where compiling for Android would use and have this main function:


ANativeActivity_createFunc AndroidStart;
void AndroidStart (ANativeActivity *activity, void *savedState, size_t savedStateSize)
{
   //Program entry point
}

Now I'm looking to have some user defined functions for Update and Render. Where these are automatically called by the internals of my code, but are what the user needs to have. Something like:

 
//In the internals of on my android application
void MainThreadRun()
{
     while(running)
     {
            HandlePollData();
 
            //User functions that the user made
            UserDefinedUpdate();
            UserDefinedRender();
     }
}
 

I was hoping I could do something like an extern method, but seeing that I want to package my code up as a SO / DLL library. Making methods like those and declaring them extern cause linker errors. Which makes sense cause they would not be declared in my code.

So what can I do? What are some better options?

Advertisement

These are called 'callback functions'. Your library code would use function pointers instead of functions directly. The user would pass the function pointers to your code.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

If you're making a dynamically loaded library, it doesn't really make sense to have the ordinary entry point. Your user's program is going to load your library and call into it to start it. (And your user's program will therefore need to have the appropriate entry point for the target platform.)

This means that you can define whatever initialization functions you want your users to call, and require that they pass whatever data or function pointers you need to call back into their code for custom behavior.

If you want to keep your current architecture of defining the entry point yourself, then it seems to me you should provide your framework as an executable, and then it would be your user's code which is a dynamic library, and you would load their library yourself, finding it based on path or program parameter or whatever other technique you choose. You'd then need to use the platform-appropriate calls to search for the proper initialization functions within your user's library and call those.

"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
The easiest and most intuitive way to allow users control over updating and rendering is to use polymorphism.
General Game/Engine Structure
Obviously this works over DLL’s as your DLL factory just needs to spit out the appropriate instance type.


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

The easiest and most intuitive way to allow users control over updating and rendering is to use polymorphism.
General Game/Engine Structure
Obviously this works over DLL’s as your DLL factory just needs to spit out the appropriate instance type.


L. Spiro

100% honest, this is Ironic

I am porting my engine over from Java and originally in my Java code I had a Engine / Application class. Where the user extended from this class and used override methods on things like Update, Render, etc. But when I came over to C++ I felt that this was "too" Java and went a completely different direction

This topic is closed to new replies.

Advertisement