home *** CD-ROM | disk | FTP | other *** search
/ 3D Game Programming All in One / 3D Game Programming All in One Disc.iso / 3D2E / demo / main.cs < prev   
Encoding:
Text File  |  2005-11-23  |  4.4 KB  |  159 lines

  1. //-----------------------------------------------------------------------------
  2. // Torque Game Engine 
  3. // Copyright (C) GarageGames.com, Inc.
  4. //-----------------------------------------------------------------------------
  5.  
  6. // Load up common script base
  7. loadDir("common");
  8.  
  9. //-----------------------------------------------------------------------------
  10. // Load up defaults console values.
  11.  
  12. // Defaults console values
  13. exec("./client/defaults.cs");
  14. exec("./server/defaults.cs");
  15.  
  16. // Preferences (overide defaults)
  17. exec("./client/prefs.cs");
  18. exec("./server/prefs.cs");
  19.  
  20.  
  21. //-----------------------------------------------------------------------------
  22. // Package overrides to initialize the mod.
  23. package demo {
  24.  
  25. function displayHelp() {
  26.    Parent::displayHelp();
  27.    error(
  28.       "Demo options:\n"@
  29.       "  -dedicated             Start as dedicated server\n"@
  30.       "  -connect <address>     For non-dedicated: Connect to a game at <address>\n" @
  31.       "  -mission <filename>    For dedicated: Load the mission\n"
  32.    );
  33. }
  34.  
  35. function parseArgs()
  36. {
  37.    Parent::parseArgs();
  38.  
  39.    // Arguments, which override everything else.
  40.    for (%i = 1; %i < $Game::argc ; %i++)
  41.    {
  42.       %arg = $Game::argv[%i];
  43.       %nextArg = $Game::argv[%i+1];
  44.       %hasNextArg = $Game::argc - %i > 1;
  45.    
  46.       switch$ (%arg)
  47.       {
  48.          //--------------------
  49.          case "-dedicated":
  50.             $Server::Dedicated = true;
  51.             enableWinConsole(true);
  52.             $argUsed[%i]++;
  53.  
  54.          //--------------------
  55.          case "-mission":
  56.             $argUsed[%i]++;
  57.             if (%hasNextArg) {
  58.                $missionArg = %nextArg;
  59.                $argUsed[%i+1]++;
  60.                %i++;
  61.             }
  62.             else
  63.                error("Error: Missing Command Line argument. Usage: -mission <filename>");
  64.  
  65.          //--------------------
  66.          case "-connect":
  67.             $argUsed[%i]++;
  68.             if (%hasNextArg) {
  69.                $JoinGameAddress = %nextArg;
  70.                $argUsed[%i+1]++;
  71.                %i++;
  72.             }
  73.             else
  74.                error("Error: Missing Command Line argument. Usage: -connect <ip_address>");
  75.       }
  76.    }
  77. }
  78.  
  79. function onStart()
  80. {
  81.    Parent::onStart();
  82.    echo("\n--------- Initializing MOD: Torque Demo ---------");
  83.  
  84.    // Load the scripts that start it all...
  85.    exec("./client/init.cs");
  86.    exec("./server/init.cs");
  87.    exec("./data/init.cs");
  88.  
  89.    // Server gets loaded for all sessions, since clients
  90.    // can host in-game servers.
  91.    initServer();
  92.  
  93.    // Start up in either client, or dedicated server mode
  94.    if ($Server::Dedicated)
  95.       initDedicated();
  96.    else
  97.       initClient();
  98. }
  99.  
  100. function onExit()
  101. {
  102.    echo("Exporting client prefs");
  103.    export("$pref::*", "./client/prefs.cs", False);
  104.  
  105.    echo("Exporting client config");
  106.    if (isObject(moveMap))
  107.       moveMap.save("./client/config.cs", false);
  108.  
  109.    echo("Exporting server prefs");
  110.    export("$Pref::Server::*", "./server/prefs.cs", False);
  111.    BanList::Export("./server/banlist.cs");
  112.  
  113.    Parent::onExit();
  114. }
  115.  
  116. }; // Client package
  117. activatePackage(demo);
  118.  
  119. $testscript::log = "";
  120. $testscript::scenecount = 0;
  121. $testscript::grabcount = 0;
  122. $testscript::grabcounttotal = 0;
  123. $testscript::fpsavg = 0;
  124. $testscript::numscenes = 22;
  125. $testscript::numgrabs = 10;
  126.  
  127. function testScript(%appname) {
  128.     $testscript::log = new FileObject();
  129.     $testscript::log.openForAppend("demo/framecount.txt");
  130.     $testscript::log.writeLine("=== " @ %appname @ " ===");
  131.  
  132.     loadFeatureMission();
  133.     schedule(10000,0,testThread);
  134. }
  135.  
  136. function testThread() {
  137.     // grab & average.
  138.     %fps = $fps::real;
  139.     %fps = (( $testscript::fpsavg * $testscript::grabcounttotal) + %fps ) / ( $testscript::grabcounttotal + 1);
  140.     $testscript::fpsavg = %fps;
  141.     $testscript::grabcounttotal++;
  142.     $testscript::grabcount++;
  143.     // if grabcount > numgrabs, next scene & reset
  144.     if( $testscript::grabcount >= $testscript::numgrabs ) {
  145.         SceneGui.nextScene();
  146.         $testscript::grabcount = 0;
  147.         $testscript::scenecount++;
  148.     }
  149.     // inc scenecount, if over numscenes, write file, close file, quit
  150.     if( $testscript::scenecount > $testscript::numscenes ) {
  151.         $testscript::log.writeLine(" avg fps:" SPC $testscript::fpsavg );
  152.         $testscript::log.writeLine(" samples:" SPC $testscript::grabcounttotal );
  153.         $testscript::log.close();
  154.         quit();
  155.     }
  156.     // schedule self.
  157.     schedule(1000,0,testThread);
  158. }
  159.