Example #1
0
//******************************************************************************
void Initialize(){                      //  I N I T I A L I Z E
//******************************************************************************

  //define transition events for game states
  g_pStateIntro->AddTransitionEvent(GO_MENU, g_pStateMenu);
  
  g_pStateMenu->AddTransitionEvent(GO_QUIT, g_pStateQuit);
  g_pStateMenu->AddTransitionEvent(GO_RULES, g_pStateRules);
  g_pStateMenu->AddTransitionEvent(GO_SCORE, g_pStateScore);
  g_pStateMenu->AddTransitionEvent(GO_PLAY, g_pStatePlay);
  
  g_pStateRules->AddTransitionEvent(GO_MENU, g_pStateMenu);
  
  g_pStateScore->AddTransitionEvent(GO_MENU, g_pStateMenu);
  
  g_pStatePlay->AddTransitionEvent(GO_MENU, g_pStateMenu);
  g_pStatePlay->AddTransitionEvent(GO_SCORE, g_pStateScore);//if new high score
 
  //perform any required game state initialization
  g_pStateIntro->Initialize();
  g_pStateMenu->Initialize();
  g_pStateQuit->Initialize();
  g_pStateRules->Initialize();
  g_pStateScore->Initialize();
  g_pStatePlay->Initialize();

  //start game
  g_pCurrent = g_pStateIntro;//g_pStatePlay;// 

  //initialize sound if audio initializes correctly
  if(g_audio.IsValidSound() == true){
    g_audio.LoadSample(0, "sounds\\burns_fired.wav");
    g_audio.LoadSample(1, "sounds\\Kids_Music_1.wav");
    g_audio.LoadSample(2, "sounds\\Kids_Music_2.wav");
    g_audio.LoadSample(3, "sounds\\Kids_Music_3.wav");
    g_audio.LoadSample(4, "sounds\\victory_fanfare.mp3");
    g_audio.LoadSample(5, "sounds\\burns_started.wav");
    g_audio.LoadSample(6, "sounds\\burns_succeed.wav");
    g_audio.LoadSample(7, "sounds\\truckhorn.wav");
    g_audio.LoadSample(8, "sounds\\trucks021.wav");
  }
}
Example #2
0
/************************************************************************************************
  WinMain (...)
************************************************************************************************/
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	// Always perform a leak check just before app exits.
	int nDbgFlags = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
	nDbgFlags |= _CRTDBG_LEAK_CHECK_DF;
	_CrtSetDbgFlag(nDbgFlags);

  //instantiate log object and log
  CLog *pLog = CLog::Instance();
  pLog->Log("*********************************************************");
  pLog->Log("Program starting..................");
  pLog->SetDelimiter(':');
  pLog->LogDate();
  pLog->LogTime();
  pLog->Log("************************ *********************************");


  pLog->Log ("***********************************************");
  pLog->Log("Game Challenge 6");
  pLog->LogDate();
  pLog->LogTime();
  pLog->Log("***********************************************");

  Graphics g_con;  
  CAudioManager *pAudio = CAudioManager::Instance();

  //***********************************************************************
  //read config.ini file
  // C O N F I G . I N I
  //***********************************************************************
  pLog->Log("************ Config.ini ************");
  CINIReader config;          //reads and stores INI file data
  config.LoadFile("config.ini");
  std::string sValue;
  std::string sParameter;
  int nParameterValue;

  //default values
  g_Global.g_screenWidth = 1024;
  g_Global.g_screenHeight = 768;

  if(config.IsValid() == true){
    pLog->Log("Valid config.ini file");
    pLog->Log("Numbers of lines in config file",config.GetNumberOfLines());

    for(int j=0; j< config.GetNumberOfLines();j++){
      sParameter = config.GetTerm(config.GetLineFromFile(j),1);
      sValue = config.GetTerm(config.GetLineFromFile(j),2);
      nParameterValue =  atoi(sValue.c_str());

      pLog->Log(sParameter, nParameterValue);      
      
      //load g_Global object with config.ini data
      if(sParameter == "WindowedMode" && nParameterValue == 1)
        g_Global.g_WindowedMode = true;
      else if(sParameter == "WindowedMode" && nParameterValue == 0)
        g_Global.g_WindowedMode = false;
      //else if(sParameter == "ScreenWidth" && nParameterValue > 0)
      //  g_Global.g_screenWidth = nParameterValue;
      //else if(sParameter == "ScreenHeight" && nParameterValue > 0)
      //  g_Global.g_screenHeight = nParameterValue;
      //else if(sParameter == "GameLevel" && nParameterValue > 0 && nParameterValue < 6)
      //  g_Global.g_GameLevel = nParameterValue;
      //else if(sParameter == "GameSnow" && nParameterValue == 1)
      //  g_Global.g_bGameSnow = true;
      //else if(sParameter == "GameSnow" && nParameterValue == 0)
      //  g_Global.g_bGameSnow = false;
      else if(sParameter == "GameAudio" && nParameterValue == 1)
        g_Global.g_bGameAudio = true;
      else if(sParameter == "GameAudio" && nParameterValue == 0)
        g_Global.g_bGameAudio = false;
      else if(sParameter == "FrameRate" && nParameterValue == 1)
        g_Global.g_bDisplayFramerate = true;
      else if(sParameter == "FrameRate" && nParameterValue == 0)
        g_Global.g_bDisplayFramerate = false;
    }
  } 
  pLog->Log("Reading Config.ini file is complete!");
	// Break on specific memory allocation number
	//_CrtSetBreakAlloc(175);
  
  MSG msg;    
  g_bRunning = true;//Start program running

  //Make a window and initialize DirectX in windowed mode
  MakeWindow(hInstance, g_Global.g_WindowedMode, g_con);

  //map game state information
  g_pStateIntro->addTransitionEvent(EVENT_GO_MAIN, g_pStateMain);
  g_pStateMain->addTransitionEvent(EVENT_GO_HISTORY, g_pStateHistory);
  g_pStateMain->addTransitionEvent(EVENT_GO_RELOAD, g_pStateReload);
  g_pStateMain->addTransitionEvent(EVENT_GO_CREDITS, g_pStateCredits);
  g_pStateMain->addTransitionEvent(EVENT_GO_SELECT, g_pStateSelect);
  g_pStateHistory->addTransitionEvent(EVENT_GO_MAIN, g_pStateMain);
  g_pStateReload->addTransitionEvent(EVENT_GO_MAIN, g_pStateMain);
  g_pStateCredits->addTransitionEvent(EVENT_GO_END, g_pStateEnd);
  g_pStateSelect->addTransitionEvent(EVENT_GO_AWARDS, g_pStateAwards);
  g_pStateAwards->addTransitionEvent(EVENT_GO_SELECT, g_pStateSelect);  
  g_pStateSelect->addTransitionEvent(EVENT_GO_PLAY, g_pStatePlay);
  g_pStateSelect->addTransitionEvent(EVENT_GO_MAIN, g_pStateMain);
  g_pStatePlay->addTransitionEvent(EVENT_GO_WIN, g_pStateWin);
  g_pStatePlay->addTransitionEvent(EVENT_GO_LOSE, g_pStateLose);
  g_pStateWin->addTransitionEvent(EVENT_GO_SELECT, g_pStateSelect);
  g_pStateLose->addTransitionEvent(EVENT_GO_SELECT, g_pStateSelect);
  g_pStatePlay->addTransitionEvent(EVENT_GO_SELECT, g_pStateSelect);
  g_pStatePlay->addTransitionEvent(EVENT_GO_CONTROL, g_pStateControl);
  
  //sonar transitions
  g_pStateSonar->addTransitionEvent(EVENT_GO_RADAR, g_pStateRadar);
  g_pStateSonar->addTransitionEvent(EVENT_GO_FIRECONTROL, g_pStateFireControl);
  g_pStateSonar->addTransitionEvent(EVENT_GO_STATUS, g_pStateStatus);
  g_pStateSonar->addTransitionEvent(EVENT_GO_CHART, g_pStateChart);  
  g_pStateSonar->addTransitionEvent(EVENT_GO_CONTROL, g_pStateControl);

  //radar transitions
  g_pStateRadar->addTransitionEvent(EVENT_GO_SONAR, g_pStateSonar);
  g_pStateRadar->addTransitionEvent(EVENT_GO_FIRECONTROL, g_pStateFireControl);
  g_pStateRadar->addTransitionEvent(EVENT_GO_STATUS, g_pStateStatus);
  g_pStateRadar->addTransitionEvent(EVENT_GO_CHART, g_pStateChart);  
  g_pStateRadar->addTransitionEvent(EVENT_GO_CONTROL, g_pStateControl);

  //scope transitions
  g_pStateScope->addTransitionEvent(EVENT_GO_CONTROL, g_pStateControl);
  g_pStateScope->addTransitionEvent(EVENT_GO_WIN, g_pStateWin);
  g_pStateScope->addTransitionEvent(EVENT_GO_LOSE, g_pStateLose);
  
  //conn transitions
  g_pStateControl->addTransitionEvent(EVENT_GO_PLAY, g_pStatePlay);
  g_pStateControl->addTransitionEvent(EVENT_GO_SONAR, g_pStateSonar);
  g_pStateControl->addTransitionEvent(EVENT_GO_RADAR, g_pStateRadar);
  g_pStateControl->addTransitionEvent(EVENT_GO_FIRECONTROL, g_pStateFireControl);
  g_pStateControl->addTransitionEvent(EVENT_GO_STATUS, g_pStateStatus);
  g_pStateControl->addTransitionEvent(EVENT_GO_CHART, g_pStateChart);  
  g_pStateControl->addTransitionEvent(EVENT_GO_SCOPE, g_pStateScope);
  g_pStateControl->addTransitionEvent(EVENT_GO_SELECT, g_pStateSelect);
  g_pStateControl->addTransitionEvent(EVENT_GO_WIN, g_pStateWin);
  g_pStateControl->addTransitionEvent(EVENT_GO_LOSE, g_pStateLose);

  //fire control transitions
  g_pStateFireControl->addTransitionEvent(EVENT_GO_SONAR, g_pStateSonar);
  g_pStateFireControl->addTransitionEvent(EVENT_GO_RADAR, g_pStateRadar);
  g_pStateFireControl->addTransitionEvent(EVENT_GO_STATUS, g_pStateStatus);
  g_pStateFireControl->addTransitionEvent(EVENT_GO_CHART, g_pStateChart);  
  g_pStateFireControl->addTransitionEvent(EVENT_GO_CONTROL, g_pStateControl);

  //status transitions
  g_pStateStatus->addTransitionEvent(EVENT_GO_SONAR, g_pStateSonar);
  g_pStateStatus->addTransitionEvent(EVENT_GO_RADAR, g_pStateRadar);
  g_pStateStatus->addTransitionEvent(EVENT_GO_FIRECONTROL, g_pStateFireControl);
  g_pStateStatus->addTransitionEvent(EVENT_GO_CHART, g_pStateChart);  
  g_pStateStatus->addTransitionEvent(EVENT_GO_CONTROL, g_pStateControl);

  //chart transitions
  g_pStateChart->addTransitionEvent(EVENT_GO_SONAR, g_pStateSonar);
  g_pStateChart->addTransitionEvent(EVENT_GO_RADAR, g_pStateRadar);
  g_pStateChart->addTransitionEvent(EVENT_GO_FIRECONTROL, g_pStateFireControl);
  g_pStateChart->addTransitionEvent(EVENT_GO_STATUS, g_pStateStatus);
  g_pStateChart->addTransitionEvent(EVENT_GO_CONTROL, g_pStateControl);
  g_pStateChart->addTransitionEvent(EVENT_GO_WIN, g_pStateWin);
  g_pStateChart->addTransitionEvent(EVENT_GO_LOSE, g_pStateLose);

  g_Timer.initialize();
  g_LoopTimer.initialize();
  g_FPS_Timer.initialize();
  g_con.InitD3D (g_hWnd, g_Global.g_WindowedMode);

  //***********************************************************************
  //load textures from graphics file
  // G R A P H I C F I L E S . D A T
  //***********************************************************************
  pLog->Log("************ Graphicfiles.dat ************");
  CFileReader* cfr = new CFileReader;
  cfr->LoadFile("data\\graphicfiles.dat");
  int fileNumber;
  std::string fileName;
  if(cfr->IsValid()== true){
    pLog->Log("Numbers of lines in file",cfr->GetNumberOfLines());
    for(int j=0; j< cfr->GetNumberOfLines();j++){
      sValue =cfr->GetTerm(cfr->GetLineFromFile(j),1);
      fileNumber = atoi(sValue.c_str());
      sValue = cfr->GetTerm(cfr->GetLineFromFile(j),2);
      fileName = "assets\\artwork\\";
      fileName = fileName + sValue;
      //pLog->Log("Loaded file",fileName);
      pLog->Log(fileName, fileNumber);
      g_con.LoadTexture(fileName, fileNumber);
    }
  } 
  else{
    pLog->Log("ERROR****************** Failure to load textures (graphicfiles.dat)");
    delete cfr;
    g_con.CloseD3D();
    pLog->Log("DirectX closed!");
    Shutdown();
    return 0;
  }

  if(g_Sprite.LoadSprites() == true){
    pLog->Log("Sprites loaded!");
  }
  else{
    pLog->Log("ERROR****************** Failure to sprite data (sprites.dat)");
  }

  //used for framerate display
  std::ostringstream oss;
  std::string sFramerate;
  std::string sText;
  int nDisplayFPSCount = 0;

  //initialize audio manager
  //***************************************************************
  if(g_Global.g_bGameAudio == true){
    pLog->Log("Loading audio clip...");
    if(pAudio->IsValidSound())
      pLog->Log("Audio system is okay!");
    else
      pLog->Log("Audio system failure!");
    
    //load sounds
    pAudio->LoadSample(SOUND_BEEP, "assets\\sounds\\beep-03.wav");
    pAudio->LoadSample(SOUND_REMEMBER, "assets\\sounds\\remember.mp3");
    pAudio->LoadSample(SOUND_HYMN, "assets\\sounds\\navy_hymn.mp3");
    pAudio->LoadSample(SOUND_PERISCOPE, "assets\\sounds\\periscop.wav");
    pAudio->LoadSample(SOUND_BUTTON_CLICK, "assets\\sounds\\button_click.wav");
    pAudio->LoadSample(SOUND_DEPTH_CHARGE1,"assets\\sounds\\dc1.mp3");
    pAudio->LoadSample(SOUND_DEPTH_CHARGE2,"assets\\sounds\\dc2.mp3");
    pAudio->LoadSample(SOUND_TORPEDO1,"assets\\sounds\\torpedo3.mp3");
    pAudio->LoadSample(SOUND_TORPEDO2,"assets\\sounds\\torpedo4.mp3");
    pAudio->LoadSample(SOUND_AMBIENCE1,"assets\\sounds\\ambience1.mp3");
    pAudio->LoadSample(SOUND_CLEAR_BRIDGE,"assets\\sounds\\clear_bridge.wav");
    pAudio->LoadSample(SOUND_DIVING,"assets\\sounds\\diving.mp3");
    pAudio->LoadSample(SOUND_GQ,"assets\\sounds\\general_quarters.wav");
    pAudio->LoadSample(SOUND_ANCHORS,"assets\\sounds\\AnchorsAway.mp3");
    pAudio->LoadSample(SOUND_TAPS,"assets\\sounds\\Taps.mp3");
    pAudio->LoadSample(SOUND_SINKING,"assets\\sounds\\sinking2.mp3");
    pAudio->LoadSample(SOUND_ANTHEM,"assets\\sounds\\USA.mp3");
    pAudio->LoadSample(SOUND_FUNERAL,"assets\\sounds\\fnrlMrch.wav");
    pAudio->LoadSample(SOUND_PING,"assets\\sounds\\sonar1.mp3");
    
    //load stream
    pAudio->LoadStream(SOUND_BIO, "assets\\sounds\\fish.mp3");
    pAudio->AddStreamClip(SOUND_BIO1, SOUND_BIO, 0, 17708);
    pAudio->AddStreamClip(SOUND_BIO2, SOUND_BIO, 17708, 26434);
    pAudio->AddStreamClip(SOUND_BIO3, SOUND_BIO, 26434, 42859);
    pAudio->AddStreamClip(SOUND_BIO4, SOUND_BIO, 42859, 59541);
    pAudio->AddStreamClip(SOUND_BIO5, SOUND_BIO, 59541, 80073);

    for(int i = 0; i < pAudio->Size(); i++){
      pLog->Log(pAudio->GetFilename(i), i); //write filename and file index to log file
    }
  }
  else{
    pLog->Log("Audio disabled");
  }

  pLog->Log("Main game loop entered!");
  ::ShowCursor(false); 
  //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Version Number
  //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Version Number
  //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Version Number
  //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Version Number
  g_strFPS = "Game Challenge 6 (Chuck Bolin) - \"Sinking of the Rising Sun\", v0.047 Final Version";
  ::SetWindowText(g_hWnd, g_strFPS.c_str());
  double timeDelta;

  g_con.InitializePyro();

  //Main application loop
  //*******************************************************
  while (g_bRunning)
  {
    //Handle messages
    if (PeekMessage (&msg,NULL,0,0,PM_REMOVE)){
        TranslateMessage (&msg);
        DispatchMessage (&msg);
    }        
    else{}
      timeDelta = g_LoopTimer.getTimeDifference();
      g_pNext = g_pCurrent->update(timeDelta);
      if(g_pNext == g_pStateEnd)
        g_bRunning = false;
      else if(NULL != g_pNext)
	    {
        g_pCurrent->deactivate();
        g_pCurrent = g_pNext;
        g_pCurrent->activate();
      }
     
	  // Render our current game object
    g_pCurrent->render(g_con);

    //call each second update FPS
    nDisplayFPSCount++;
    if(g_FPS_Timer.getTimer(1)){
      //g_FPS_Counter++;
      oss.str(""); //clear oss
      oss << (int) nDisplayFPSCount;
      //if(g_FPS_Counter > 60){  //call every 60 seconds
      //  pLog->Log("FPS",oss.str());
      //  g_FPS_Counter == 0;
      //}

  	  // update our FPS string.
	    // This thing can be rendered inside any of the game objects.
      g_strFPS = "FPS " + oss.str();
      g_Global.g_strFPS = "FPS " + oss.str();

      nDisplayFPSCount=0;//reset frame count
    }
  }//while(...
  g_con.ClosePyro();
  g_con.CloseD3D();
  pLog->Log("DirectX closed!");
  Shutdown();

  return 0;//(int)msg.wParam;  //returns exit code to operating system
}