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

  1. //-------------------------------------------------------------
  2. //
  3. // Program:    Demo 1
  4. //
  5. // Author:    John M Phillips
  6. //
  7. // Started:    20/08/00
  8. //
  9. // Remarks:    This is a simple demo program using the GameSystem library
  10. //            which opens a full-screen window and draws some moving sprites.
  11. //
  12. //-------------------------------------------------------------
  13.  
  14. #include "demo1.h"
  15.  
  16. //-------------------------------------------------------------
  17. // Constants
  18.  
  19. const char *GRAPHICS_DIRECTORY = "..\\..\\Demo1\\Graphics\\";
  20.  
  21. const char *SPRITE_IMAGE = "ship.bmp";
  22.  
  23. const int NUMBER_OF_SPRITES = 10;
  24.  
  25. //-------------------------------------------------------------
  26. // Create an instance of the application class
  27. // GameSystem will run this automatically
  28.  
  29. CDemo1 myApp("Demo 1");
  30.  
  31. //-------------------------------------------------------------
  32. // Initialize the demo
  33. //
  34. // Return: true if successful
  35.  
  36. bool CDemo1::initialize()
  37. {
  38.     // initialize the GameSystem framework
  39.  
  40.     if (!gsCApplication::initialize())
  41.         return false;
  42.  
  43.     // create the keyboard handler and screen window
  44.  
  45.     if (!m_keyboard.create())
  46.         return false;
  47.  
  48.     if (!m_screen.createWindowed(getWindow()))
  49.         return false;
  50.  
  51.     // load images and create some sprites
  52.  
  53.     if (!createSprites())
  54.         return false;
  55.  
  56.     // create a 16 layer starfield
  57.  
  58.     m_stars.initialize(16);
  59.  
  60.     return true;
  61. }
  62.  
  63. //-------------------------------------------------------------
  64. // Main loop : draws sprites to the screen and test the keyboard
  65. //
  66. // Return: true if successful
  67. //           false if error or ESCAPE pressed
  68.  
  69. bool CDemo1::mainloop()
  70. {
  71.     // clear the screen to black
  72.     
  73.     m_screen.clear(gsCColour(gsBLACK));
  74.  
  75.     // draw starfield and animate it
  76.     
  77.     m_stars.draw();
  78.     m_stars.move(4);
  79.  
  80.     // draw our sprites
  81.  
  82.     drawSprites();
  83.  
  84.     // flip the screen to view
  85.  
  86.     m_screen.flip();
  87.  
  88.     // move sprites for next frame
  89.  
  90.     moveSprites();
  91.  
  92.     // test for ESCAPE pressed and exit demo if so
  93.  
  94.     gsKeyCode key = m_keyboard.getKey();
  95.  
  96.     if (key == gsKEY_ESCAPE)
  97.         return false;
  98.  
  99.     return true;
  100. }
  101.  
  102. //-------------------------------------------------------------
  103. // Shutdown demo
  104.  
  105. bool CDemo1::shutdown()
  106. {
  107.     // destroy out sprites and images
  108.  
  109.     destroySprites();
  110.  
  111.     // destroy the screen and keyboard handler
  112.  
  113.     m_screen.destroy();
  114.     m_keyboard.destroy();
  115.     
  116.     // exit
  117.  
  118.     return gsCApplication::shutdown();
  119. }
  120.  
  121. //-------------------------------------------------------------
  122. // Create our images and sprites
  123. //
  124. // Return: true if success
  125. //           false if error
  126.  
  127. bool CDemo1::createSprites()
  128. {
  129.     // set the directory path for our images
  130.     
  131.     gsCFile::setDirectory(GRAPHICS_DIRECTORY);
  132.  
  133.     // load image
  134.  
  135.     if (!m_image.load(SPRITE_IMAGE))
  136.         return false;
  137.  
  138.     // treat magenta colour as being transparent
  139.  
  140.     m_image.enableColourKey(gsCColour(gsMAGENTA));
  141.  
  142.     // random number generator for sprite positions
  143.  
  144.     gsCRandom rnd;
  145.  
  146.     // now we create the sprites
  147.  
  148.     for (int i = 0; i < NUMBER_OF_SPRITES; i++) {
  149.  
  150.         // create a new sprite
  151.  
  152.         gsCSprite *spr = new gsCSprite;
  153.  
  154.         // add it to our list of sprites
  155.  
  156.         m_sprite.addItem(spr);
  157.  
  158.         // use the image we loaded earlier
  159.  
  160.         spr->setImage(&m_image);
  161.  
  162.         // set the centre of the sprite
  163.  
  164.         spr->setHotspot(m_image.getSize() / 2);
  165.  
  166.         // give it a random starting position on screen
  167.  
  168.         gsCPoint pos;
  169.  
  170.         pos.setX(rnd.getInt(0,m_screen.getSize().getX()));
  171.         pos.setY(rnd.getInt(0,m_screen.getSize().getY()));
  172.  
  173.         spr->setPosition(pos);
  174.  
  175.         // activate it
  176.         
  177.         spr->setActive(true);
  178.         }
  179.  
  180.     return true;
  181. }
  182.  
  183. //-------------------------------------------------------------
  184. // Destroy our images and sprites
  185.  
  186. bool CDemo1::destroySprites()
  187. {
  188.     // delete each sprite
  189.     
  190.     for (int i = 0; i < m_sprite.getSize(); i++)
  191.         delete m_sprite[i];
  192.  
  193.     // clear the sprite list
  194.  
  195.     m_sprite.clear();
  196.  
  197.     // destroy our image
  198.  
  199.     m_image.destroy();
  200.  
  201.     return true;
  202. }
  203.  
  204. //-------------------------------------------------------------
  205. // Draw all the sprites on screen
  206.  
  207. void CDemo1::drawSprites()
  208. {
  209.     for (int i = 0; i < m_sprite.getSize(); i++)
  210.         m_sprite[i]->draw();
  211. }
  212.  
  213. //-------------------------------------------------------------
  214. // Move all sprites
  215.  
  216. void CDemo1::moveSprites()
  217. {
  218.     gsCRect screen_rect = m_screen.getRect();
  219.  
  220.     // this basically moves each sprite up the screen
  221.  
  222.     // if the sprite goes off the screen it's wrapped
  223.     // around to the bottom
  224.  
  225.     for (int i = 0; i < m_sprite.getSize(); i++) {
  226.  
  227.         m_sprite[i]->move(gsCPoint(0,-((i & 3) + 1)));
  228.  
  229.         gsCRect sprite_rect = m_sprite[i]->getRect();
  230.  
  231.         if (!(sprite_rect.overlaps(screen_rect)))
  232.             m_sprite[i]->move(gsCPoint(0,m_screen.getSize().getY() + m_image.getSize().getY()));
  233.         }
  234. }
  235.  
  236. //-------------------------------------------------------------
  237.  
  238.