//---------------------------------------------------------------------------------------------
// 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");
   
      $selectedRes = "1024 768";
      echo("SETTING 1024x768");
      %ret = setScreenMode(1024, 768, 32, false);      
      
      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 = false;
      $displayDriver = "OpenGL";
   
}

$qInExtentChange = false;  
function sceneWindow2D::onExtentChange( %this, %data )  
{  
   echo( "EXTENT CHANGE");
 if( $qInExtentChange ) return;  
 $qInExtentChange = true;  
 setAcceptableScreenRatio();    
 $qInExtentChange = false;  
 }  

function setAcceptableScreenRatio()  
{  
 %screenWidth = getWord( Canvas.extent, 0 );  
 %screenHeight = getWord( Canvas.extent, 1 );  

 %screenRatio = %screenWidth / %screenHeight;  
 %optimalRatio = 1024.0 / 768.0;  // WLS - Our Design resolution  

 %newWidth = %screenWidth;  
 %newHeight = %screenHeight;  
 if( %screenRatio < %optimalRatio )  
   %newHeight = %screenWidth / %optimalRatio;  
 else if( %screenRatio > %optimalRatio )  
   %newWidth = %screenHeight * %optimalRatio;  
 %newX = (%screenWidth - %newWidth) / 2.0;  
 %newY = (%screenHeight - %newHeight) / 2.0;  
 sceneWindow2D.setPosition( %newX, %newY );  
 sceneWindow2D.setExtent( %newWidth, %newHeight );  
}  
