Customizing Your Game with Inheritance


The main reason I delved into the topic of inheritance was so that we could use it in games. If you're wondering how inheritance might be used in your games, the answer is that you already know how. The class in Listing 6.5 repeats the game class you saw in Listing 4.3 in chapter 4, "Introducing the LlamaWorks2D Game Engine." At the time I originally presented this game class, I didn't explain what the statement

 : public game 


was at the end of the first line of the class. Now you know.

Listing 6.5. A game class that uses inheritance

 1    class my_game : public game 2    { 3    public: 4        bool OnAppLoad(); 5        bool InitGame(); 6        bool UpdateFrame(); 7        bool RenderFrame(); 8 9        ATTACH_MESSAGE_MAP; 10 11   private: 12       sprite theBall; 13   }; 

Every game you write with LlamaWorks2D requires that you create your own game class. Your game class inherits from a class I've provided called game. If your game class doesn't inherit from game, LlamaWorks2D simply won't recognize it as a game class.

If you glance back at chapter 4, you'll see that I indicated that if you don't provide the functions whose prototypes are shown on lines 47 of Listing 6.5, the LlamaWorks2D provides them for you. Those functions are in the LlamaWorks2D game class. When you write the functions such as OnAppLoad() or InitGame() in your derived class, you're overriding the functions in the base class. If you don't override the functions in the base class, your game class inherits these functions from the LlamaWorks2D game class. Inheritance is one of the main ways that LlamaWorks2D provides you with reasonable default implementations of tasks that need to be done in games.



Creating Games in C++(c) A Step-by-Step Guide
Creating Games in C++: A Step-by-Step Guide
ISBN: 0735714347
EAN: 2147483647
Year: N/A
Pages: 148

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net