Tuesday, May 18, 2010

Function Pointers

I implemented the restructuring of the level editor away from using game states by using function pointers. It makes it much much easier to define the actions of a button and it breaks up the code in a more meaningful fashion. I am able to perform actions without the delay of wait for the game states to propagate.

The refactoring also gave a clear way to direct the keyboard/mouse input to the proper GUI panes. Before it was directed based on the game state, but now there is a pane designate with the focus. Change the focus between panes simply determined by mouse location, so there is no need to click to change focus.

Memory management is cleaner. Few state changes meant few creation and deletion of objects. This refactoring also gave me an opportunity to replace many heap allocations to stack allocations. Dialog panes are also reused; they are turned non-visible instead of destroying them.

The header files are much cleaner. When I originally wrote version 1.0, I hadn't quite mastered the art of resolving circular dependencies. During my revisit I added forward declarations and removed unnecessary include files.

Overall there is a lot more work to be done. It was an excellent learning process for the use of function pointers in C++, an amazing feature which I plan to use all the time.

No comments:

Post a Comment