quit.cpp

Go to the documentation of this file.
00001 
00002 //            Copyright (C) 2004-2007 by The Allacrost Project
00003 //                         All Rights Reserved
00004 //
00005 // This code is licensed under the GNU GPL version 2. It is free software 
00006 // and you may modify it and/or redistribute it under the terms of this license.
00007 // See http://www.gnu.org/copyleft/gpl.html for details.
00009 
00016 #include <iostream>
00017 
00018 #include "utils.h"
00019 #include "audio.h"
00020 #include "video.h"
00021 #include "input.h"
00022 #include "system.h"
00023 
00024 #include "quit.h"
00025 #include "boot.h"
00026 
00027 using namespace std;
00028 using namespace hoa_quit::private_quit;
00029 using namespace hoa_audio;
00030 using namespace hoa_video;
00031 using namespace hoa_mode_manager;
00032 using namespace hoa_input;
00033 using namespace hoa_system;
00034 using namespace hoa_boot;
00035 using namespace hoa_utils;
00036 
00037 namespace hoa_quit {
00038 
00039 bool QUIT_DEBUG = false;
00040 
00041 QuitMode::QuitMode() {
00042   if (QUIT_DEBUG) cout << "QUIT: QuitMode constructor invoked" << endl;
00043   mode_type = MODE_MANAGER_QUIT_MODE;
00044   
00045 //  switch (SystemManager->GetPauseVolumeAction()) {
00046 //    case SETTINGS_PAUSE_AUDIO:
00047 //      AudioManager->PauseAudio();
00048 //      break;
00049 //    case SETTINGS_ZERO_VOLUME:
00050 //      AudioManager->SetMusicVolume(0);
00051 //      AudioManager->SetSoundVolume(0);
00052 //      break;
00053 //    case SETTINGS_HALF_VOLUME:
00054 // //       AudioManager->SetMusicVolume(static_cast<int32>(SystemManager->music_vol * 0.5));
00055 // //       AudioManager->SetSoundVolume(static_cast<int32>(SystemManager->sound_vol * 0.5));
00056 //      // Note that the music_vol/sound_vol members of SystemManager aren't changed
00057 //      break;
00058 //    // Don't need to do anything for case SETTINGS_SAME_VOLUME
00059 //  }
00060 
00061   // Save a copy of the current screen to use as a backdrop
00062   if (!VideoManager->CaptureScreen(_saved_screen)) 
00063     if (QUIT_DEBUG) cerr << "PAUSE: ERROR: Couldn't save the screen!" << endl;
00064 
00065 
00066 // !@# Roots: I got rid of this code for now since CreateMenu() is defunct
00067 
00068 //  if(!VideoManager->CreateMenu(_quit_menu, 448, 80)) // create a menu
00069 //    cerr << "QUIT: ERROR: Couldn't create menu image!" << endl;
00070 
00071 
00072   // Initialize the option box
00073   _option_box.SetFont("default");
00074   _option_box.SetCellSize(250.0f, 50.0f);
00075   _option_box.SetSize(3, 1);
00076   _option_box.SetPosition(512.0f, 384.0f);
00077   _option_box.SetAlignment(VIDEO_X_CENTER, VIDEO_Y_CENTER);
00078   _option_box.SetOptionAlignment(VIDEO_X_CENTER, VIDEO_Y_CENTER);
00079   _option_box.SetSelectMode(VIDEO_SELECT_SINGLE);
00080   _option_box.SetCursorOffset(-58.0f, 18.0f);
00081   
00082   vector<ustring> options;
00083   options.push_back(MakeUnicodeString("Quit Game"));
00084   options.push_back(MakeUnicodeString("Quit to Main Menu"));
00085   options.push_back(MakeUnicodeString("Cancel"));
00086   
00087   _option_box.SetOptions(options);
00088   _option_box.SetSelection(QUIT_CANCEL);
00089 }
00090 
00091 
00092 
00093 // The destructor might possibly have to free any text textures we create...
00094 QuitMode::~QuitMode() {
00095   if (QUIT_DEBUG) cout << "QUIT: QuitMode destructor invoked" << endl;
00096 
00097   // Delete the background image
00098   VideoManager->DeleteImage(_saved_screen);
00099 }
00100 
00101 
00102 // Called whenever QuitMode is put on top of the stack
00103 void QuitMode::Reset() {
00104   _quit_type = QUIT_CANCEL;
00105   
00106   // Setup video engine constructs.
00107   VideoManager->SetCoordSys(0, 1024, 0, 768);
00108   if(!VideoManager->SetFont("default")) 
00109     cerr << "MAP: ERROR > Couldn't set map font!" << endl;
00110   VideoManager->SetDrawFlags(VIDEO_X_LEFT, VIDEO_Y_BOTTOM, VIDEO_BLEND, 0);
00111 
00112 }
00113 
00114 
00115 // Restores volume or unpauses audio, then pops itself from the game stack
00116 void QuitMode::Update() {
00117   uint32 time_elapsed = SystemManager->GetUpdateTime();
00118   
00119   // Dispatch input to option box
00120   
00121   if (InputManager->LeftPress())
00122     _option_box.HandleLeftKey();
00123   else if(InputManager->RightPress())
00124     _option_box.HandleRightKey();
00125   else if(InputManager->CancelPress())
00126     _option_box.HandleCancelKey();
00127   else if(InputManager->ConfirmPress())
00128     _option_box.HandleConfirmKey();
00129     
00130   // See if option box has any events
00131   
00132   int32 event = _option_box.GetEvent();
00133   int32 selection = _option_box.GetSelection();
00134     
00135   if(event == VIDEO_OPTION_CONFIRM)
00136   {
00137     switch(selection)
00138     {
00139       case QUIT_GAME:
00140         _QuitGame();
00141         break;
00142       case QUIT_TO_BOOTMENU:
00143         _QuitToBootMenu();
00144         break;      
00145       case QUIT_CANCEL:
00146         _Cancel();
00147         break;
00148       default:
00149       {
00150         if(QUIT_DEBUG)
00151           cerr << "QUIT ERROR: received confirm event, but option box selection was invalid" << endl;
00152         break;
00153       }
00154     };
00155   }
00156   else if(event == VIDEO_OPTION_CANCEL)
00157   {
00158     _Cancel();
00159   }
00160   
00161   // Update the option box
00162   _option_box.Update(time_elapsed);
00163   
00164   // Don't consume hoards of CPU time in quit mode
00165     
00166   SDL_Delay(50);
00167 }
00168 
00169 
00170 
00171 // Draws the saved screen, the quit prompt, the quit options, and highlights the selected option
00172 void QuitMode::Draw() {
00173   // Draw the saved screen background
00174   // For that, set the system coordinates to the size of the window (same with the save-screen)
00175   int32 width = VideoManager->GetWidth();
00176   int32 height = VideoManager->GetHeight();
00177   VideoManager->SetCoordSys (0.0f, static_cast<float>(width), 0.0f, static_cast<float>(height));
00178 
00179   VideoManager->SetDrawFlags(VIDEO_X_LEFT, VIDEO_Y_BOTTOM, 0);
00180   Color grayed(0.35f, 0.35f, 0.35f, 1.0f);
00181   VideoManager->Move(0, 0);
00182   VideoManager->DrawImage(_saved_screen, grayed);
00183 
00184   // Draw the quit menu
00185   // Restore the Coordinate system (that one is quit mode coodinate system)
00186   VideoManager->SetCoordSys (0.0f, 1024.0f, 0.0f, 768.0f);
00187 
00188   VideoManager->Move(512, 384);
00189   VideoManager->SetDrawFlags(VIDEO_X_CENTER, VIDEO_Y_CENTER, 0);
00190   VideoManager->DrawImage(_quit_menu);
00191 
00192   _option_box.Draw();
00193 }
00194 
00195 
00196 // Quit the game completely
00197 void QuitMode::_QuitGame() {
00198 
00199   SystemManager->ExitGame();
00200 }
00201 
00202 
00203 // Quit to the boot menu
00204 void QuitMode::_QuitToBootMenu() {
00205   // Restore the game audio, pop QuitMode off the stack, and push BootMode
00206 //  switch (SystemManager->GetPauseVolumeAction()) {
00207 //    case SETTINGS_PAUSE_AUDIO:
00208 //      AudioManager->ResumeAudio();
00209 //      break;
00210 //    case SETTINGS_ZERO_VOLUME:
00211 //    case SETTINGS_HALF_VOLUME:
00212 // //       AudioManager->SetMusicVolume(SystemManager->music_vol);
00213 // //       AudioManager->SetSoundVolume(SystemManager->sound_vol);
00214 //      break;
00215 //    // We don't need to do anything for case SETTINGS_SAME_VOLUME
00216 //  }
00217 
00218   ModeManager->PopAll(); // Remove and free every game mode
00219   BootMode *BM = new BootMode();
00220   ModeManager->Push(BM);
00221 }
00222 
00223 
00224 // Cancel out of quit mode
00225 void QuitMode::_Cancel() {
00226   // The user really doesn't want to quit after all, so restore the game audio and state
00227 //  switch (SystemManager->GetPauseVolumeAction()) {
00228 //    case SETTINGS_PAUSE_AUDIO:
00229 //      AudioManager->ResumeAudio();
00230 //      break;
00231 //    case SETTINGS_ZERO_VOLUME:
00232 //    case SETTINGS_HALF_VOLUME:
00233 // //       AudioManager->SetMusicVolume(SystemManager->music_vol);
00234 // //       AudioManager->SetSoundVolume(SystemManager->sound_vol);
00235 //      break;
00236 //    // Don't need to do anything for case SETTINGS_SAME_VOLUME
00237 //  }
00238   TEMP_HandlePause();
00239   ModeManager->Pop();
00240 }
00241 
00242 } // namespace hoa_quit

Generated on Fri Jul 6 23:11:21 2007 for Hero of Allacrost by  doxygen 1.5.1