Using overridden classes with the engine?

    This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

    • Using overridden classes with the engine?

      In the teapot wars source code, WinMain passes control immediately over to the one for the engine. How then does the engine know to use the overridden classes i.e the engine does initInstance on gApp which is defined in the engine itself, but you'd need the engine to actually work with an object of type TeapotAppLayer : public BaseAppLayer. Similarly for other classes to implement the game-specific functionality. Sorry if its a stupid question; I feel like I may be missing something obvious :(
    • Through the magic of polymorphism. Here's a simple example:

      Source Code

      1. class Base
      2. {
      3. public:
      4. virtual void Test() { cout << "Base\n"; }
      5. };
      6. class Sub : public Base
      7. {
      8. public:
      9. virtual void Test() { cout << "Sub\n"; }
      10. };
      11. void main()
      12. {
      13. Base* pBase = new Sub;
      14. pBase->Test(); // this prints Sub
      15. delete pBase;
      16. }
      Display All


      That's basic polymorphism. Here's another way to look at it, which is what GCC does:

      Source Code

      1. class Base
      2. {
      3. public:
      4. Base() { g_pBase = this; }
      5. virtual void Test() { cout << "Base\n"; }
      6. };
      7. class Sub : public Base
      8. {
      9. public:
      10. virtual void Test() { cout << "Sub\n"; }
      11. };
      12. Base* g_pBase = nullptr;
      13. // This calls the Sub constructor, which in turn calls the Base constructor, which in
      14. // turn sets g_pBase to point to this instance.
      15. Sub g_sub;
      16. void main()
      17. {
      18. // This prints Sub because g_pBase is pointing to an instance of Sub.
      19. g_pBase->Test();
      20. }
      Display All


      Make sense?

      This is exactly how GCC works. TeapotWarsApp is instantiated on line 60 of TeapotWars.cpp. The GameCodeApp class sets the g_pApp pointer on line 81 of GameCode.cpp.

      -Rez