Hi there,
so I realize virtual functions are a very contentious topic here.
I just recently added a feature to my engine where I can hot reload C++ game code while the engine i still running in the background (small demo
https://twitter.com/RicoTweet/status/1392135424324345866).
In order to be able to do that, basically I compile my game as a dll with no outside dependencies.
To be able for my dll to communicate with my engine I provide one global function set_context that simply sets a global context pointer like that:
| static Context* context = nullptr;
extern "C" __declspec(dllexport) void set_context(Context* new_context) { context = new_context; }
|
Context is a pure virtual interface that looks kinda like that
| struct Context {
virtual ~Context() {}
virtual void draw_cube(vec3f pos, vec3f size) = 0;
};
|
Now whenever I change and recompile my game dll I load my library and call set_context where I set the pointer to my engine context.
This works very well
Now my question: is there a way to improve this approach avoiding the virtual function calls?
Honestly even when I don't use virtual functions, I would still have to design my context in a way that it basically is just a collection of function pointers, which factually would make no difference.
In the long run I would have to add static linking without dlls anyway, when I wanna build my games for something other than a PC anyway.
Feedback is appreciated.
~Rico