//---------------------------------------------------------------------------------------------
// Torque Game Builder
// Copyright (C) GarageGames.com, Inc.
//---------------------------------------------------------------------------------------------

//---------------------------------------------------------------------------------------------
// initializeProject
// Perform game initialization here.
//---------------------------------------------------------------------------------------------
function initializeProject()
{  
   echo("INIT LOAD RESOLUTION");

   LoadResolution();
   
   echo("INIT POPULATE FONTS");
   
   populateFonts();
   
   exec("./gameScripts/audio.cs"); 
   
      
   // Load up the in game gui.
   exec("~/gui/RoadGangsMain.gui");

   // Exec game scripts.
   exec("./gameScripts/game.cs");
   exec("~/gui/tgbsplash.gui");

   Canvas.setContent(TgbSplash);
   canvas.CursorOff();

   // This is where the game starts. Right now, we are just starting the first level. You will
   // want to expand this to load up a splash screen followed by a main menu depending on the
   // specific needs of your game. Most likely, a menu button will start the actual game, which
   // is where startGame should be called from.
   
   echo("ABOUT TO START GAME");
   schedule(2000, 0, "startGame");
}

//---------------------------------------------------------------------------------------------
// shutdownProject
// Clean up your game objects here.
//---------------------------------------------------------------------------------------------
function shutdownProject()
{
   endGame();
}

//---------------------------------------------------------------------------------------------
// setupKeybinds
// Bind keys to actions here..
//---------------------------------------------------------------------------------------------

function populateFonts()
{      
      %font = "Forgotten Futurist";      
      %sizes = "16 20 24 30 36";      
      for (%i = 0; %i < getWordCount(%sizes); %i++)            
            populateFontCacheRange(%font, getWord(%sizes, %i), 32, 126);      

      writeFontCache();
}

function LoadResolution()
{
   echo("START LOAD RESOLUTION");
   
   %fo = new fileobject();
  
   %file = "~/resolution.sav";

   %ret = %fo.OpenForRead(%file);
   
   // if there is no resolution file let's default it
   
   if(%ret == 0)
   {
      $selectedRes = "1024 768";
      echo("SETTING 1024x768");
      %ret = setScreenMode(1024, 768, 32, true);      
      
      if(%ret $= false)
      {
         echo("1024x768 FAILED GOING to 1024x600");

         %ret = setScreenMode(1024, 600, 32, true);      

         if(%ret $= false)
         {
            echo("1024x600 FAILED CAN'T START GAME");
            alxPlay(buzzer);
            quit();                        
         }
      }
      
      echo("DONE SETTING DEFAULT RESOLUTION");      
      $fullscreen = true;
      $displayDriver = "OpenGL";
   }
   else
   {         
      echo("LOOKING FOR A RES FILE");
      
      for(%line = %fo.ReadLine();%line !$= "";%line = %fo.ReadLine())
      {
         %word = GetWord(%line, 0);

         switch$ (%word)
         {         
            case "fullscreen":
               $fullscreen = restWords(%line);
            case "currentscontrol":
               $CurrentDSControl = restWords(%line);
            case "Resolution":
               $selectedRes = restWords(%line);
               
               if($selectedRes !$= "")
               {
                  %width = getWord($selectedRes, 0);
                  %height = getWord($selectedRes, 1);
                  %color = getWord($selectedRes, 2);
                         
                  setScreenMode(%width, %height, %color, $fullscreen);                              
               }
         }
      }
   }
}
