home *** CD-ROM | disk | FTP | other *** search
/ PC Format (South-Africa) 2001 June / PCFJune.iso / Xenon / XenonSource.exe / demo5 / gamestate.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-12  |  6.6 KB  |  286 lines

  1. //-------------------------------------------------------------
  2. //
  3. // Class:    CGameState
  4. //
  5. // Author:    John M Phillips
  6. //
  7. // Started:    06/05/00
  8. //
  9. // Base:    None
  10. //
  11. // Derived:    CMainMenuState
  12. //            COptionsMenuState
  13. //            CPlayGameState
  14. //            CScoreEntryState
  15. //            CScoreTableState
  16. //
  17. //-------------------------------------------------------------
  18.  
  19. #include "demo5.h"
  20.  
  21. //-------------------------------------------------------------
  22.  
  23. gsCScreen        CGameState::m_screen;
  24. gsCKeyboard        CGameState::m_keyboard;
  25. gsCSoundSystem    CGameState::m_sound_system;
  26.  
  27. gsCFont            CGameState::m_small_font;
  28. gsCFont            CGameState::m_medium_font;
  29. gsCStarfield    CGameState::m_starfield;
  30.  
  31. CLevel            CGameState::m_level;
  32. CScene            CGameState::m_scene;
  33. CDemo5 *        CGameState::m_demo5 = 0;
  34.  
  35. //-------------------------------------------------------------
  36.  
  37. CGameState::CGameState()
  38. {
  39. }
  40.  
  41. //-------------------------------------------------------------
  42.  
  43. CGameState::~CGameState()
  44. {
  45. }
  46.  
  47. //-------------------------------------------------------------
  48.  
  49. bool CGameState::initialize(CDemo5 *demo5)
  50. {
  51.     m_demo5 = demo5;
  52.  
  53.     gsCFile::setDirectory(DIRECTORY_ROOT);
  54.  
  55.     if (!m_keyboard.create())
  56.         return false;
  57.  
  58.     if (!m_screen.createWindowed(m_demo5->getWindow()))
  59.         return false;
  60.  
  61.     if (!m_sound_system.create())
  62.         return false;
  63.  
  64.     updateVolume();
  65.     
  66.     // pre-load music/soundfx
  67.  
  68.     gsCFile::setDirectory(DIRECTORY_MUSIC);
  69.  
  70.     if (!loadMusic())
  71.         return false;
  72.  
  73.     gsCFile::setDirectory(DIRECTORY_SOUNDS);
  74.  
  75.     if (!loadSoundEffects())
  76.         return false;
  77.  
  78.     // pre-load graphics
  79.  
  80.     gsCFile::setDirectory(DIRECTORY_GRAPHICS);
  81.  
  82.     if (!loadGraphics())
  83.         return false;
  84.  
  85.     m_starfield.initialize(8);
  86.  
  87.     m_demo5->changeState(CPlayGameState::instance());
  88.  
  89.     return true;
  90. }
  91.  
  92. //-------------------------------------------------------------
  93.  
  94. bool CGameState::shutdown()
  95. {
  96.     m_level.m_image.destroy();
  97.  
  98.     m_medium_font.destroy();
  99.     m_small_font.destroy();
  100.  
  101.     m_scene.destroyAll();
  102.  
  103.     m_screen.destroy();
  104.  
  105.     m_keyboard.destroy();
  106.  
  107.     m_sound_system.destroy();
  108.  
  109.     return true;
  110. }
  111.  
  112. //-------------------------------------------------------------
  113.  
  114. bool CGameState::changeState(CGameState *new_game_state)
  115. {
  116.     if (m_demo5)
  117.         return m_demo5->changeState(new_game_state);
  118.  
  119.     return false;
  120. }
  121.  
  122. //-------------------------------------------------------------
  123.  
  124. bool CGameState::create()
  125. {
  126.     return true;
  127. }
  128.  
  129. //-------------------------------------------------------------
  130.  
  131. bool CGameState::update()
  132. {
  133.     return true;
  134. }
  135.  
  136. //-------------------------------------------------------------
  137.  
  138. bool CGameState::destroy()
  139. {
  140.     return true;
  141. }
  142.  
  143. //-------------------------------------------------------------
  144.  
  145. bool CGameState::loadGraphics()
  146. {
  147.     if (!m_scene.loadImages())
  148.         return false;
  149.  
  150.     if (!m_small_font.load("font8x8.bmp"))
  151.         return false;
  152.  
  153.     m_small_font.setTileSize(gsCPoint(8,8));
  154.     m_small_font.enableColourKey(gsCColour(gsMAGENTA));
  155.  
  156.     if (!m_medium_font.load("font16x16.bmp"))
  157.         return false;
  158.  
  159.     m_medium_font.setTileSize(gsCPoint(16,16));
  160.     m_medium_font.enableColourKey(gsCColour(gsMAGENTA));
  161.  
  162.     return true;
  163. }
  164.  
  165. //-------------------------------------------------------------
  166.  
  167. bool CGameState::loadMusic()
  168. {
  169.     m_sound_system.addMusic("title.mp3");
  170.     m_sound_system.addMusic("intro.mp3");
  171.     m_sound_system.addMusic("game.mp3");
  172.     m_sound_system.addMusic("hiscore.mp3");
  173.     m_sound_system.addMusic("boss.mp3");
  174.     m_sound_system.addMusic("outro.mp3");
  175.  
  176.     return true;
  177. }
  178.  
  179. //-------------------------------------------------------------
  180.  
  181. bool CGameState::loadSoundEffects()
  182. {
  183.     m_sound_system.addSample("menu_selection.wav");
  184.     m_sound_system.addSample("menu_option.wav");
  185.     m_sound_system.addSample("menu_click.wav");
  186.     m_sound_system.addSample("menu_back.wav");
  187.  
  188.     m_sound_system.addSample("player_created.wav");
  189.     m_sound_system.addSample("player_destroyed.wav");
  190.     m_sound_system.addSample("fire_missile.wav");
  191.     m_sound_system.addSample("fire_homing_missile.wav");
  192.     m_sound_system.addSample("fire_laser.wav");
  193.     m_sound_system.addSample("small_explosion.wav");
  194.     m_sound_system.addSample("medium_explosion.wav");
  195.     m_sound_system.addSample("big_explosion.wav");
  196.     m_sound_system.addSample("asteroid_breakup.wav");
  197.     m_sound_system.addSample("pickup.wav");
  198.     m_sound_system.addSample("bonus.wav");
  199.     m_sound_system.addSample("dive_down.wav");
  200.     m_sound_system.addSample("dive_up.wav");
  201.     m_sound_system.addSample("hit_background.wav");
  202.  
  203.     m_sound_system.addSample("roar.wav");
  204.     m_sound_system.addSample("snort.wav");
  205.  
  206.     m_sound_system.addSample("checkpoint.wav");
  207.  
  208.     return true;
  209. }
  210.  
  211. //-------------------------------------------------------------
  212.  
  213. void CGameState::updateVolume()
  214. {
  215.     m_sound_system.setVolume(60,30);
  216. }
  217.  
  218. //-------------------------------------------------------------
  219.  
  220. void CGameState::playSample(GameSampleType sample)
  221. {
  222.     m_sound_system.playSample((int) sample);
  223. }
  224.  
  225. //-------------------------------------------------------------
  226. // play sample with stereo position based on screen x coordinate
  227.  
  228. void CGameState::playSample(GameSampleType sample,float x)
  229. {
  230.     int w2 = gsCApplication::getScreen()->getSize().getX() / 2;
  231.     m_sound_system.playSample((int) sample,100 * ((int) x - w2) / w2);
  232. }
  233.  
  234. //-------------------------------------------------------------
  235.  
  236. void CGameState::playMusic(GameMusicType music)
  237. {
  238.     m_sound_system.playMusic((int) music);
  239. };
  240.  
  241. //-------------------------------------------------------------
  242.  
  243. void CGameState::stopSamples()
  244. {
  245.     m_sound_system.stopSamples();
  246. }
  247.  
  248. //-------------------------------------------------------------
  249.  
  250. void CGameState::stopMusic()
  251. {
  252.     m_sound_system.stopMusic();
  253. }
  254.  
  255. //-------------------------------------------------------------
  256.  
  257. gsCApplication *CGameState::getApplication()
  258. {
  259.     return m_demo5;
  260. }
  261.  
  262. //-------------------------------------------------------------
  263.  
  264. gsKeyCode CGameState::getKey()
  265. {
  266.     gsKeyCode key = m_keyboard.getKey();
  267.  
  268.     return key;
  269. }
  270.  
  271. //-------------------------------------------------------------
  272.  
  273. void CGameState::getControl(Controls& controls,int player)
  274. {
  275.     controls.left = m_keyboard.testKey(gsKEY_LEFT);
  276.     controls.right = m_keyboard.testKey(gsKEY_RIGHT);
  277.     controls.up = m_keyboard.testKey(gsKEY_UP);
  278.     controls.down = m_keyboard.testKey(gsKEY_DOWN);
  279.     controls.fire = m_keyboard.testKey(gsKEY_LCONTROL,gsKEY_STATE);
  280.     controls.firePressed = m_keyboard.testKey(gsKEY_LCONTROL,gsKEY_PRESSED);
  281.     controls.divePressed = m_keyboard.testKey(gsKEY_LSHIFT,gsKEY_PRESSED);
  282.     controls.reversePressed = m_keyboard.testKey(gsKEY_ALT,gsKEY_PRESSED);
  283. }
  284.  
  285. //-------------------------------------------------------------
  286.