home *** CD-ROM | disk | FTP | other *** search
/ PC Format (South-Africa) 2001 June / PCFJune.iso / Xenon / XenonSource.exe / demo3 / demo3.cpp next >
Encoding:
C/C++ Source or Header  |  2000-09-11  |  3.7 KB  |  185 lines

  1. //-------------------------------------------------------------
  2. //
  3. // Program:    Demo 3
  4. //
  5. // Author:    John M Phillips
  6. //
  7. // Started:    20/08/00
  8. //
  9. // Remarks:    This is a simple program using the GameSystem library
  10. //            which lets you view a 2 layer scrolling tile map.
  11. //
  12. //-------------------------------------------------------------
  13.  
  14. #include "demo3.h"
  15.  
  16. //-------------------------------------------------------------
  17. // Constants
  18.  
  19. const char *GRAPHICS_DIRECTORY = "..\\..\\Demo3\\Graphics\\";
  20. const char *LEVELS_DIRECTORY = "..\\..\\Demo3\\Levels\\";
  21.  
  22. const char *LEVEL_NAME = "test.fmp";
  23.  
  24. const int PLAYER_START_OFFSET = 64;
  25.  
  26. //-------------------------------------------------------------
  27. // Create an instance of the application class
  28. // GameSystem will run this automatically
  29.  
  30. CDemo3 myApp("Demo 3");
  31.  
  32. //-------------------------------------------------------------
  33. // Initialize the demo
  34. //
  35. // Return: true if successful
  36.  
  37. bool CDemo3::initialize()
  38. {
  39.     // initialize the GameSystem framework
  40.  
  41.     if (!gsCApplication::initialize())
  42.         return false;
  43.  
  44.     // create the keyboard handler and screen window
  45.  
  46.     if (!m_keyboard.create())
  47.         return false;
  48.  
  49.     if (!m_screen.createWindowed(getWindow()))
  50.         return false;
  51.  
  52.     // load level
  53.  
  54.     if (!createLevel())
  55.         return false;
  56.  
  57.     m_stars.initialize(16);
  58.  
  59.     return true;
  60. }
  61.  
  62. //-------------------------------------------------------------
  63. // Main loop : draws level to the screen and test the keyboard
  64. //
  65. // Return: true if successful
  66. //           false if error or ESCAPE pressed
  67.  
  68. bool CDemo3::mainloop()
  69. {
  70.     // clear the screen to black
  71.     
  72.     m_screen.clear(gsCColour(gsBLACK));
  73.  
  74.     // draw starfield
  75.  
  76.     m_stars.draw();
  77.  
  78.     // draw our level
  79.  
  80.     drawLevel();
  81.  
  82.     // flip the screen to view
  83.  
  84.     m_screen.flip();
  85.  
  86.     // move level for next frame
  87.  
  88.     moveLevel();
  89.  
  90.     // test for ESCAPE pressed and exit demo if so
  91.  
  92.     gsKeyCode key = m_keyboard.getKey();
  93.  
  94.     if (key == gsKEY_ESCAPE)
  95.         return false;
  96.  
  97.     return true;
  98. }
  99.  
  100. //-------------------------------------------------------------
  101. // Shutdown demo
  102.  
  103. bool CDemo3::shutdown()
  104. {
  105.     destroyLevel();
  106.  
  107.     // destroy the screen and keyboard handler
  108.  
  109.     m_screen.destroy();
  110.     m_keyboard.destroy();
  111.     
  112.     // exit
  113.  
  114.     return gsCApplication::shutdown();
  115. }
  116.  
  117. //-------------------------------------------------------------
  118. // Load our level
  119. //
  120. // Return: true if success
  121. //           false if error
  122.  
  123. bool CDemo3::createLevel()
  124. {
  125.     if (!m_level.load(LEVEL_NAME,LEVELS_DIRECTORY,GRAPHICS_DIRECTORY))
  126.         return false;
  127.  
  128.     m_level.reset();
  129.  
  130.     m_ship_y = m_level.m_back_layer.getSizeInPixels().getY() - PLAYER_START_OFFSET;
  131.     
  132.     return true;
  133. }
  134.  
  135. //-------------------------------------------------------------
  136. // Destroy our level
  137.  
  138. void CDemo3::destroyLevel()
  139. {
  140.     m_level.destroy();
  141. }
  142.  
  143. //-------------------------------------------------------------
  144. // Draw the level
  145.  
  146. void CDemo3::drawLevel()
  147. {
  148.     setLayerPositions(m_ship_y);
  149.  
  150.     m_level.m_back_layer.draw();
  151.     m_level.m_front_layer.draw();
  152. }
  153.  
  154. //-------------------------------------------------------------
  155. // Move the level
  156.  
  157. void CDemo3::moveLevel()
  158. {
  159.     m_stars.move(4);
  160.  
  161.     if (m_keyboard.testKey(gsKEY_UP)) {
  162.         m_ship_y -= 4;
  163.         }
  164.     if (m_keyboard.testKey(gsKEY_DOWN)) {
  165.         m_ship_y += 4;
  166.         }
  167. }
  168.  
  169. //-------------------------------------------------------------
  170.  
  171. void CDemo3::setLayerPositions(int ship_y)
  172. {
  173.     int mh = m_level.m_back_layer.getSizeInPixels().getY();
  174.  
  175.     int by = -(mh - (mh - ship_y) / 2 + PLAYER_START_OFFSET / 2 - m_screen.getSize().getY());
  176.  
  177.     m_level.m_back_layer.setPosition(gsCPoint(0,by));
  178.  
  179.     int fy = -(ship_y + PLAYER_START_OFFSET - m_screen.getSize().getY());
  180.  
  181.     m_level.m_front_layer.setPosition(gsCPoint(0,fy));
  182. }
  183.  
  184. //-------------------------------------------------------------
  185.