I have a question. I am trying to create the code to update my game but I have gotten into a "dilemma". I don't want to use virtual and the only reason for it is EVERYBODY i talked to (in forums, chats, friends) say that virtuals make the code really slow, so I did a research and found out that the lookup it does from vtable can decrease the performance by almost half. So, I use it for tasks that doesn't need to be updated every frame. Everything worked fine until I got to the update/render functions. Then I started thinking to find a workaround. Got an idea but first I would like to ask people who have knowledge on that one before implementing it.
My game engine is very event driven. I can send any kind of data using events in between subsystems (graphics, ui, scripting). So, I am thinking of sending an event "renderScene" every frame. It sounds great to me but there is a problem. The structure of the event handlers are not that great and I really don't want to improve this right now because it does a really decent job and my goal is to finish my game instead of fixing up the engine and never finish it (happened to me, so don't want to go back to it again).
My event handler have a function that registers events to functions (i call the handlers). But the problem with that function is, I need to do function bindings and stuff to register member functions. So, I found a workaround it - I create a static function and call the member function from it. This is how exactly a static function look like:
void GraphicsSubsystem::renderScene(Subsystem * subsystem, Event * event) {
GraphicsSubsystem * graphics = static_cast<GraphicsSubsystem *>(subsystem);
graphics->renderScene();
}
void ScriptingSubsystem::runLine(Subsystem * subsystem, Event * event) {
ScriptingSubsystem * scripting = static_cast<ScriptingSubsystem *>(subsystem);
Event1<String> * e = static_cast<Event1<String> *>(event);
scripting->runLine(e->getArg());
}
There arguments are always the abstract subsystem class and the base event class. The runLine function I have no problem casting because I dont run a line of code on every frame. However, the renderScene function makes me a little bit uncomfortable.
tl;dr So, here is my question. Is static casting an object on every frame faster than calling a virtual function on every frame?