Modular

This is a discussion on Modular within the Game Programming forums, part of the General Programming Boards category; Ok dose every one remember this article http://www.cprogramming.com/tutorial/gamedesign.html I have made a few engines with the old school style and ...

  1. #1
    Call me AirBronto
    Join Date
    Sep 2004
    Location
    Indianapolis, Indiana
    Posts
    195

    Modular Game Design

    Ok dose every one remember this article http://www.cprogramming.com/tutorial/gamedesign.html
    I have made a few engines with the old school style and i like this approach better. But there is one thing i am having problems with.

    Ok the article stipulates that Execute() member function returns a pointer to a Module, which is the next Module. This all make nice since because the next Module will have the Module base class and we are only concerned with accessing the functions that the Module pointer can see. But the returned Module is a pointer to the next Module, So do i just make however many Module pointer slots i need in the derived class and in the Initialize member function assign all of them to a bunch of pre-made instances of Pointers to Modules that are assigned to a 'new' derived class instance? This is exactly what i did but the classes are having trouble seeing those instances. Am i going about this all wrong or what? Thanks for the help

    This is confusing to explain so here is the Test code that is not working

    Code:
    #include <iostream>
    using namespace std;
    
    
    class Module
    {
    public:
    	virtual void Initialize()=0;
    	virtual Module* Execute()=0;
    	virtual void Shutdown()=0;
    }
    
    class Test : public Module
    {
    protected:
              int number;
              char word;
              Module* NextModule;
              
               
    public:
           //general methods
           void Initialize();
    	   Module* Execute();
           void Shutdown();
           Test();
           ~Test();
           
           //accessor methods
           int GetNumber() {return number;};
           char GetChar() {return word;};
    }
    
    
    
    class TestB : public Module
    {
    protected:
              int number;
              char word;
              Module* NextModule;
              
               
    public:
           //general methods
           void Initialize();
    	   Module* Execute();
           void Shutdown();
           TestB();
           ~TestB();
           
           //accessor methods
           int GetNumber() {return number;};
           char GetChar() {return word;};
    }
    
        Module* pTestB = new TestB;
        Module* pTest = new Test;
        
        
        Module* CurrentModule;
        Module* LastModule;
    
    main()
    {
    
        
    
        
    
        int end = 0;
        
        CurrentModule = pTest;
        LastModule = CurrentModule;
        
        while(end == 0)
        {
          if((CurrentModule=CurrentModule->Initialize()) != LastModule)
          {
             cout<<"inside if"<<endl;
             LastModule->Shutdown();
             CurrentModule->Initialize();
             cout<<"Would You Like to Quit? " <<endl;
             cin>>endl;
          }
    if(end == 0)
    {
           cout<<"Inside Second If" <<endl;
           cout<<"Would You Like to Quit? " <<endl;
           cin>>end;
    }
    delete Test;
    delete pTestB;
    }
    }
    
    void Test::Initialize()
    {
         number = 1;
         word = 'F';
         NextModule = pTestB;
         Execute();
    }
    
    Module* Test::Execute()
    {
         cout <<number <<endl;
         cout <<word <<endl;
         return NextModule;
    }
    
    void Test::Shutdown()
    {
         number = 20;
         word = 'D';
         cout <<number <<endl;
         cout <<word <<endl;
    }
    
    
    
    void TestB::Initialize()
    {
         number = 500;
         word = 'S';
         NextModule = pTest;
         Execute();
    }
    
    Module* TestB::Execute()
    {
         cout <<number <<endl;
         cout <<word <<endl;
         return NextModule;
    }
    
    void TestB::Shutdown()
    {
         number = 20;
         word = 'D';
         cout <<number <<endl;
         cout <<word <<endl;
    }
    Last edited by loopshot; 01-19-2006 at 08:42 PM.

  2. #2
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    you're never calling Execute. You also have several compiler errors. If your compiler isn't squaking at you, I would like to know which compiler you're using.

  3. #3
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    you're also not keeping track of the current and last module correctly. I would suggest something more along the lines of the following:

    Code:
    LastModule = CurrentModule;
    CurrentModule = CurrentModule->Execute();
    
    if(CurrentModule != LastModule)
    {
        LastModule->Shutdown();
        CurrentModule->Initialize();
    }

  4. #4
    Call me AirBronto
    Join Date
    Sep 2004
    Location
    Indianapolis, Indiana
    Posts
    195
    First off Execute is called within the Initialize function. Second I fixed a problem with the loop, but that is not the problem. I dont even know if i am going about this in the way intended, or a legal way for that matter. And the classes are not seeing the Modular Pointers to 'new' instances of derived Modules. help on that plz

  5. #5
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    Ah yes, you do call execute with in Initialize. But that doesn't make sense. The point of the Init, Exec, and Shutdown functions is to seperate those responsibilities.

    I'm not quite sure what you mean by your classes not seeing the pointers. Are the pointers NULL, or perhaps it seems as if the expected functions aren't being called?

    I think you have the right idea, but need to take a step back, look at the program as a whole, and organize it a bit.

  6. #6
    Call me AirBronto
    Join Date
    Sep 2004
    Location
    Indianapolis, Indiana
    Posts
    195
    Why did i seperate the Execute and Intialize functions you say? Well when are you going to intialize an object and not Execute it. really all this is just for seperation and orginization

  7. #7
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    lets say you have 5 modules. You don't want to execute them all at once, but you may want to Initialize them all at program startup so that you have less load times later on when switching between modules.

    I'll also point out that you never capture the module returned by Execute, because you never explicitly call it, and Initialize does not return it.

    This is not to say that you're wrong for putting Execute in Initialize, but keep in mind what's happening in your code because of this.


    Code:
        while(end == 0)
        {
         
         //we Initialize a Test object, which also executes it.  So right from the get go, 
              //CurrentModule != LastModule
              //Also, this should give you an error, because Initialize does not return anything 
          if((CurrentModule=CurrentModule->Initialize()) != LastModule)
          {
             cout<<"inside if"<<endl;
             
             //shut down Test module,  and then Initialize testB module, and execute it.
                 //LastModule is never changed, so the test object is always the last module.
             LastModule->Shutdown();
             CurrentModule->Initialize();
             cout<<"Would You Like to Quit? " <<endl;
             cin>>endl;
          }
          if(end == 0)
          {
              cout<<"Inside Second If" <<endl;
              cout<<"Would You Like to Quit? " <<endl;
               cin>>end;
           }
    }


    I will also say once again that I'm surprised you get this code to compile. When I fixed the errors and compiled, the program runs as expected. Not as intended, as expected.

  8. #8
    Call me AirBronto
    Join Date
    Sep 2004
    Location
    Indianapolis, Indiana
    Posts
    195
    o yea i got it working and i call them separately now because i was experiencing those things and most compilers wont let you ignore a void return type by asking it to return. thanks for you input though

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Modular Programming
    By St0rM-MaN in forum C Programming
    Replies: 7
    Last Post: 05-10-2007, 02:56 AM
  2. Modular math
    By PJYelton in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 10-01-2003, 08:35 AM
  3. Modular programming
    By Longie in forum Linux Programming
    Replies: 1
    Last Post: 07-08-2003, 11:03 PM
  4. Modular Division problem
    By Malek in forum C++ Programming
    Replies: 7
    Last Post: 05-24-2003, 06:08 PM
  5. Completely modular motherboards?
    By SMurf in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 02-23-2003, 11:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21