# User defined functions?

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

## Recommended Posts

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
{
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?

##### Share on other sites

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.

##### Share on other sites
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

##### Share on other sites

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

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 26
• 9
• 11
• 9
• 9
• ### Forum Statistics

• Total Topics
633711
• Total Posts
3013488
×