Architecture:Main Module: Difference between revisions

From Adonthell
Jump to navigation Jump to search
m Engine Startup: added link
m Using the Engine from Python: "python" -> "Python"
 
(4 intermediate revisions by one other user not shown)
Line 1: Line 1:
Although the various Adonthell [[Architecture:Overview#Architecture|modules]] can be used from custom programs without utilizing the main module, this is highly discouraged. Not only provides the main module convenient initialization methods, it also contains code to prepare the environment for modules like [[Architecture:gfx_module|gfx]] and [[Architecture:gfx_module|input]] on different operating systems.  
Although the various Adonthell [[Architecture:Overview#Architecture|modules]] can be used from custom programs without utilizing the main module, this is highly discouraged. Not only provides the main module convenient initialization methods, it also contains code to prepare the environment for modules like [[Architecture:Gfx Module|gfx]] and [[Architecture:Input Module|input]] on different operating systems.  


== Using the Engine from C++ ==
== Using the Engine from C++ ==
Line 17: Line 17:
             ...  
             ...  
         }  
         }  
     } theApp;
     };
   
    AdonthellApp myApp;


It must extend <tt>adonthell::app</tt> and implement the pure virtual method <tt>int main()</tt>.  
It must extend <tt>adonthell::app</tt> and implement the pure virtual method <tt>int main()</tt>.  
This is the method called after basic engine initialization. Exactly one instance of that class,
This is the method called after basic engine initialization. Exactly one instance of that class should be instanciated. It will be automatically picked up by the main module. The base class <tt>adonthell::app</tt> gives you access to commandline arguments and further initialization methods, described in more  
called <tt>theApp</tt> must be provided, so that the main module can find it. <tt>adonthell::app</tt>  
gives you access to commandline arguments and further initialization methods, described in more  
detail in the [http://adonthell.berlios.de/api/ API].  
detail in the [http://adonthell.berlios.de/api/ API].  


Line 28: Line 28:


The main module provides roughly the same functionality when being used from within a  
The main module provides roughly the same functionality when being used from within a  
[[Scripting:Contents|python script]], althuogh a few more lines of code are required:  
[[Scripting:Contents|Python script]], althuogh a few more lines of code are required:  


     from adonthell import main  
     from adonthell import main  
      
      
     class App (main.AdonthellApp):  
     class App (main.AdonthellApp):  
        def __init__ (self):
            main.AdonthellApp.__init__ (self)
      
      
         # -- your application entry point  
         # -- your application entry point  
Line 40: Line 38:
             self.init_modules (self.GFX | ...)  
             self.init_modules (self.GFX | ...)  
             ...  
             ...  
   
            return 0
           
     if __name__ == ’__main__’:  
     if __name__ == ’__main__’:  
         theApp = App ()  
         theApp = App ()  
Line 58: Line 57:
# '''Parse command line parameters'''<br>As some parameters override defaults or supply otherwise important information, they are read first. The backend used or the configuration file loaded can be influenced that way.  
# '''Parse command line parameters'''<br>As some parameters override defaults or supply otherwise important information, they are read first. The backend used or the configuration file loaded can be influenced that way.  
# '''Initialize base module'''<br>Amongst other things, the path to the configuration directory – required for the next step – will be set up. Other game related paths will be initialized too.  
# '''Initialize base module'''<br>Amongst other things, the path to the configuration directory – required for the next step – will be set up. Other game related paths will be initialized too.  
# '''Read configuration file'''<br>Unless given on the command line, the backend to use is read from the [[Architecture:Base Module|configuration]] file ''adonthell.xml''. A different config file can be chosen via the command line.  
# '''Read configuration file'''<br>Unless given on the command line, the backend to use is read from the [[Architecture:Base Module#Configuration Files|configuration]] file ''adonthell.xml''. A different config file can be chosen via the command line.  
# '''Perform platform specifc initialization'''<br>The main module’s backend is loaded. If none was given via command line or configuration file, <tt>sdl</tt> is used as the fallback. Once loaded, it performs the operating system dependent initialization.  
# '''Perform platform specifc initialization'''<br>The main module’s backend is loaded. If none was given via command line or configuration file, <tt>sdl</tt> is used as the fallback. Once loaded, it performs the operating system dependent initialization.
# '''Initialize national language support (NLS)'''<br>This causes the game specific language catalogue for the configured locale (or the system locale) to be loaded, if available.
# '''Call user-supplied main method'''<br>If all went well so far, the user supplied main method is executed. It should call the <tt>init_modules</tt> method to further initialize those parts of the engine it requires. Once the main method returns, the engine does some cleanup and finally exits.
# '''Call user-supplied main method'''<br>If all went well so far, the user supplied main method is executed. It should call the <tt>init_modules</tt> method to further initialize those parts of the engine it requires. Once the main method returns, the engine does some cleanup and finally exits.

Latest revision as of 16:29, 8 July 2010

Although the various Adonthell modules can be used from custom programs without utilizing the main module, this is highly discouraged. Not only provides the main module convenient initialization methods, it also contains code to prepare the environment for modules like gfx and input on different operating systems.

Using the Engine from C++

The main module provides a main function (hence the name), so your program will not need one when linking to the main module. Instead, it must provide a special class serving as entry point to your code once the startup process is complete. It should look as follows:

   #include "main/adonthell.h" 
   
   class AdonthellApp : public adonthell::app 
   { 
       // your application entry point 
       int main () 
       { 
           init_modules (GFX | ...); 
           ... 
       } 
   };
   
   AdonthellApp myApp;

It must extend adonthell::app and implement the pure virtual method int main(). This is the method called after basic engine initialization. Exactly one instance of that class should be instanciated. It will be automatically picked up by the main module. The base class adonthell::app gives you access to commandline arguments and further initialization methods, described in more detail in the API.

Using the Engine from Python

The main module provides roughly the same functionality when being used from within a Python script, althuogh a few more lines of code are required:

   from adonthell import main 
   
   class App (main.AdonthellApp): 
   
       # -- your application entry point 
       def main (self): 
           self.init_modules (self.GFX | ...) 
           ... 
           return 0
           
   if __name__ == ’__main__’: 
       theApp = App () 
       theApp.init (theApp.main)

The first difference to the C++ code is that we extend the class AdonthellApp, which is only available in the main module on Python side. It is in turn derived from the aforementioned class adonthell::app, so the methods it provides are available to Python too. The second difference is the call to theApp.init, which makes the applications entry point known to AdonthellApp. It also triggers the startup procedure described below, which will finally call the user supplied method. Note that code placed after theApp.init, will not be executed until the engine quits – if it is executed at all! You have been warned ...

Engine Startup

After having seen how the main module can be used, lets see what happens when using it.

  1. Parse command line parameters
    As some parameters override defaults or supply otherwise important information, they are read first. The backend used or the configuration file loaded can be influenced that way.
  2. Initialize base module
    Amongst other things, the path to the configuration directory – required for the next step – will be set up. Other game related paths will be initialized too.
  3. Read configuration file
    Unless given on the command line, the backend to use is read from the configuration file adonthell.xml. A different config file can be chosen via the command line.
  4. Perform platform specifc initialization
    The main module’s backend is loaded. If none was given via command line or configuration file, sdl is used as the fallback. Once loaded, it performs the operating system dependent initialization.
  5. Initialize national language support (NLS)
    This causes the game specific language catalogue for the configured locale (or the system locale) to be loaded, if available.
  6. Call user-supplied main method
    If all went well so far, the user supplied main method is executed. It should call the init_modules method to further initialize those parts of the engine it requires. Once the main method returns, the engine does some cleanup and finally exits.