home *** CD-ROM | disk | FTP | other *** search
/ Teach Yourself Game Programming in 21 Days / TYGAMES_R.ISO / source / day_13 / velocity.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-18  |  3.6 KB  |  162 lines

  1.  
  2.  
  3. // I N C L U D E S ///////////////////////////////////////////////////////////
  4.  
  5. #include <io.h>
  6. #include <conio.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <dos.h>
  10. #include <bios.h>
  11. #include <fcntl.h>
  12. #include <memory.h>
  13. #include <malloc.h>
  14. #include <math.h>
  15. #include <string.h>
  16.  
  17. #include "graph3.h"  // include our graphics stuff
  18. #include "graph4.h"
  19. #include "graph6.h"
  20.  
  21. // P R O T O T Y P E S //////////////////////////////////////////////////////
  22.  
  23. // D E F I N E S /////////////////////////////////////////////////////////////
  24.  
  25. #define VELOCITY 4   // constant speed of ship
  26.  
  27. // S T R U C T U R E S ///////////////////////////////////////////////////////
  28.  
  29.  
  30. // G L O B A L S  ////////////////////////////////////////////////////////////
  31.  
  32. pcx_picture imagery_pcx,      // the game imagery
  33.             background_pcx;   // the backdrop
  34.  
  35.  
  36. // the sprites used in the game
  37.  
  38. sprite object;                // the object
  39.  
  40. // F U N C T I O N S //////////////////////////////////////////////////////////
  41.  
  42. void main(void)
  43. {
  44. // this is the main function
  45.  
  46. // SECTION 1 //////////////////////////////////////////////////////////////////
  47.  
  48. // set video mode to 320x200 256 color mode
  49.  
  50. Set_Video_Mode(VGA256);
  51.  
  52. // create a double buffer
  53.  
  54. if (!Create_Double_Buffer(SCREEN_HEIGHT))
  55.    {
  56.    printf("\nNot enough memory to create double buffer.");
  57.  
  58.    } // end if
  59.  
  60. // clear the double buffer
  61.  
  62. Fill_Double_Buffer(0);
  63.  
  64. // SECTION 2 //////////////////////////////////////////////////////////////////
  65.  
  66. // load in the background image into the double buffer
  67.  
  68. PCX_Init((pcx_picture_ptr)&background_pcx);
  69.  
  70. PCX_Load("velback.pcx", (pcx_picture_ptr)&background_pcx,1);
  71.  
  72. // copy the background into the double buffer
  73.  
  74. _fmemcpy((char far *)double_buffer,
  75.          (char far *)(background_pcx.buffer),
  76.          SCREEN_WIDTH*SCREEN_HEIGHT);
  77.  
  78. PCX_Delete((pcx_picture_ptr)&background_pcx);
  79.  
  80. // load in imagery for object
  81.  
  82. PCX_Init((pcx_picture_ptr)&imagery_pcx);
  83.  
  84. PCX_Load("viper.pcx", (pcx_picture_ptr)&imagery_pcx,1);
  85.  
  86. // SECTION 3 //////////////////////////////////////////////////////////////////
  87.  
  88. // initialize player and extract bitmaps
  89.  
  90. sprite_width  = 50;
  91. sprite_height = 24;
  92.  
  93. Sprite_Init((sprite_ptr)&object,0,0,0,0,0,0);
  94.  
  95. PCX_Grab_Bitmap((pcx_picture_ptr)&imagery_pcx,(sprite_ptr)&object,0,0,0);
  96.  
  97. object.x          = 0;
  98. object.y          = 100;
  99. object.curr_frame = 0;
  100. object.state      = 1;
  101.  
  102. // scan behind all objects before entering event loop
  103.  
  104. Behind_Sprite_DB((sprite_ptr)&object);
  105.  
  106. // SECTION 4 //////////////////////////////////////////////////////////////////
  107.  
  108. // main event loop
  109.  
  110. while(!kbhit())
  111.      {
  112.  
  113.      // erase all objects
  114.  
  115.      Erase_Sprite_DB((sprite_ptr)&object);
  116.  
  117. // SECTION 5 //////////////////////////////////////////////////////////////////
  118.  
  119.      // move object with costant velocity
  120.  
  121.      object.x+=VELOCITY;
  122.  
  123.      // test if object is beyond edge of screen, if so send back to
  124.      // other edge
  125.  
  126.      if (object.x>319-50)
  127.          object.x = 0;
  128.  
  129. // SECTION 6 //////////////////////////////////////////////////////////////////
  130.  
  131.      // scan background under objects
  132.  
  133.      Behind_Sprite_DB((sprite_ptr)&object);
  134.  
  135.      // draw all the imagery
  136.  
  137.      Draw_Sprite_DB((sprite_ptr)&object);
  138.  
  139.      // copy the double buffer to the screen
  140.  
  141.      Show_Double_Buffer(double_buffer);
  142.  
  143.      // wait a sec
  144.  
  145.      Delay(1);
  146.  
  147.      } // end while
  148.  
  149. // SECTION 7 //////////////////////////////////////////////////////////////////
  150.  
  151. // reset the video mode back to text
  152.  
  153. Set_Video_Mode(TEXT_MODE);
  154.  
  155. // free the double buffer
  156.  
  157. Delete_Double_Buffer();
  158.  
  159. } // end main
  160.  
  161.  
  162.