home *** CD-ROM | disk | FTP | other *** search
/ Black Art of 3D Game Programming / Black_Art_of_3D_Game_Programming.iso / source / borland / chap_4 / alien.c next >
Encoding:
C/C++ Source or Header  |  1994-10-19  |  5.6 KB  |  221 lines

  1.  
  2. // ALIEN.C - A demo of parallax scrolling
  3.  
  4. // I N C L U D E S ///////////////////////////////////////////////////////////
  5.  
  6. #include <io.h>
  7. #include <conio.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <dos.h>
  11. #include <bios.h>
  12. #include <fcntl.h>
  13. #include <memory.h>
  14. #include <malloc.h>
  15. #include <math.h>
  16. #include <string.h>
  17.  
  18. #include "black3.h"
  19. #include "black4.h"
  20.  
  21. // D E F I N E S /////////////////////////////////////////////////////////////
  22.  
  23. #define MOUNTAIN_Y      106   // the starting vertical position of the mountains
  24.  
  25. #define GRASS_1_Y       150   // the starting vertical positions of the grass
  26. #define GRASS_2_Y       155   // layers
  27. #define GRASS_3_Y       163
  28. #define GRASS_4_Y       172
  29. #define GRASS_5_Y       188
  30.  
  31. #define MOUNTAIN_HEIGHT (1+149-106)  // the height of each layer
  32.  
  33. #define GRASS_1_HEIGHT  (1+154-150)
  34. #define GRASS_2_HEIGHT  (1+162-155)
  35. #define GRASS_3_HEIGHT  (1+171-163)
  36. #define GRASS_4_HEIGHT  (1+187-172)
  37. #define GRASS_5_HEIGHT  (1+199-188)
  38.  
  39. // G L O B A L S  ////////////////////////////////////////////////////////////
  40.  
  41. pcx_picture image_pcx;  // general PCX image used to load background and imagery
  42.  
  43. sprite alien;      // our rocket sleding alien
  44.  
  45. layer mountains,   // the layers for the mountains and grass
  46.       grass_1,
  47.       grass_2,
  48.       grass_3,
  49.       grass_4,
  50.       grass_5;
  51.  
  52. int mountain_x=0,  // positions of scan window in each layer
  53.     grass_1_x=0,
  54.     grass_2_x=0,
  55.     grass_3_x=0,
  56.     grass_4_x=0,
  57.     grass_5_x=0;
  58.  
  59. RGB_color fire_color = {63,0,0};   // used for engines
  60.  
  61. // M A I N //////////////////////////////////////////////////////////////////
  62.  
  63. void main(int argc, char **argv)
  64. {
  65. int done=0;  // main event loop exit flag
  66.  
  67. // set the graphics mode to mode 13h
  68.  
  69. Set_Graphics_Mode(GRAPHICS_MODE13);
  70.  
  71. // create the double buffer
  72.  
  73. Create_Double_Buffer(200);
  74.  
  75. // load the imagery
  76.  
  77. PCX_Init((pcx_picture_ptr)&image_pcx);
  78.  
  79. PCX_Load("alienimg.pcx", (pcx_picture_ptr)&image_pcx,1);
  80.  
  81. // intialize the alien sprite
  82.  
  83. Sprite_Init((sprite_ptr)&alien,160,160,32,18,0,0,0,0,0,0);
  84.  
  85. // extract the bitmap for the alien
  86.  
  87. PCX_Get_Sprite((pcx_picture_ptr)&image_pcx,(sprite_ptr)&alien,0,0,0);
  88.  
  89. // done with this PCX file so delete memory associated with it
  90.  
  91. PCX_Delete((pcx_picture_ptr)&image_pcx);
  92.  
  93. // now load the background that will be scrolled
  94.  
  95. PCX_Init((pcx_picture_ptr)&image_pcx);
  96.  
  97. PCX_Load("alienwld.pcx",(pcx_picture_ptr)&image_pcx,1);
  98.  
  99. PCX_Copy_To_Buffer((pcx_picture_ptr)&image_pcx,double_buffer);
  100.  
  101. PCX_Delete((pcx_picture_ptr)&image_pcx);
  102.  
  103. // create the layers
  104.  
  105. Layer_Create((layer_ptr)&mountains,SCREEN_WIDTH,MOUNTAIN_HEIGHT);
  106.  
  107. Layer_Create((layer_ptr)&grass_1,  SCREEN_WIDTH,GRASS_1_HEIGHT);
  108. Layer_Create((layer_ptr)&grass_2,  SCREEN_WIDTH,GRASS_2_HEIGHT);
  109. Layer_Create((layer_ptr)&grass_3,  SCREEN_WIDTH,GRASS_3_HEIGHT);
  110. Layer_Create((layer_ptr)&grass_4,  SCREEN_WIDTH,GRASS_4_HEIGHT);
  111. Layer_Create((layer_ptr)&grass_5,  SCREEN_WIDTH,GRASS_5_HEIGHT);
  112.  
  113. // scan layers out of double buffer, could have easily scanned from PCX
  114. // file...just personal taste
  115.  
  116. Layer_Build((layer_ptr)&mountains,0,0,
  117.             double_buffer,0,MOUNTAIN_Y,SCREEN_WIDTH,MOUNTAIN_HEIGHT);
  118.  
  119. Layer_Build((layer_ptr)&grass_1,0,0,
  120.             double_buffer,0,GRASS_1_Y,SCREEN_WIDTH,GRASS_1_HEIGHT);
  121.  
  122. Layer_Build((layer_ptr)&grass_2,0,0,
  123.             double_buffer,0,GRASS_2_Y,SCREEN_WIDTH,GRASS_2_HEIGHT);
  124.  
  125. Layer_Build((layer_ptr)&grass_3,0,0,
  126.             double_buffer,0,GRASS_3_Y,SCREEN_WIDTH,GRASS_3_HEIGHT);
  127.  
  128. Layer_Build((layer_ptr)&grass_4,0,0,
  129.             double_buffer,0,GRASS_4_Y,SCREEN_WIDTH,GRASS_4_HEIGHT);
  130.  
  131. Layer_Build((layer_ptr)&grass_5,0,0,
  132.             double_buffer,0,GRASS_5_Y,SCREEN_WIDTH,GRASS_5_HEIGHT);
  133.  
  134. // main event loop, process until keyboard hit
  135.  
  136. while(!kbhit())
  137.      {
  138.  
  139.      // move the alien
  140.  
  141.      if ( (alien.x+=2) > 320)
  142.        alien.x = -32;
  143.  
  144.      // move each layer
  145.  
  146.      if ((mountain_x+=1) >= 319)
  147.          mountain_x -= 320;
  148.  
  149.      if ((grass_1_x+=2) > 319)
  150.          grass_1_x -= 320;
  151.  
  152.      if ((grass_2_x+=4) > 319)
  153.          grass_2_x -= 320;
  154.  
  155.      if ((grass_3_x+=8) > 319)
  156.          grass_3_x -= 320;
  157.  
  158.      if ((grass_4_x+=14) > 319)
  159.          grass_4_x -= 320;
  160.  
  161.      if ((grass_5_x+=24) > 319)
  162.          grass_5_x -= 320;
  163.  
  164.      // draw layers
  165.  
  166.      Layer_Draw((layer_ptr)&mountains,mountain_x,0,
  167.                 double_buffer,MOUNTAIN_Y,MOUNTAIN_HEIGHT,0);
  168.  
  169.      // update background layer positions
  170.  
  171.      Layer_Draw((layer_ptr)&grass_1,grass_1_x,0,
  172.                 double_buffer,GRASS_1_Y,GRASS_1_HEIGHT,0);
  173.  
  174.      Layer_Draw((layer_ptr)&grass_2,grass_2_x,0,
  175.                 double_buffer,GRASS_2_Y,GRASS_2_HEIGHT,0);
  176.  
  177.      Layer_Draw((layer_ptr)&grass_3,grass_3_x,0,
  178.                 double_buffer,GRASS_3_Y,GRASS_3_HEIGHT,0);
  179.  
  180.      Layer_Draw((layer_ptr)&grass_4,grass_4_x,0,
  181.                 double_buffer,GRASS_4_Y,GRASS_4_HEIGHT,0);
  182.  
  183.      Layer_Draw((layer_ptr)&grass_5,grass_5_x,0,
  184.                 double_buffer,GRASS_5_Y,GRASS_5_HEIGHT,0);
  185.  
  186.      // draw the sprite on top of layers
  187.  
  188.      alien.visible = 1;
  189.      Sprite_Draw_Clip((sprite_ptr)&alien,double_buffer,1);
  190.  
  191.      // change color of fire
  192.  
  193.      fire_color.red = 20 + rand() % 44;
  194.  
  195.      Write_Color_Reg(32,(RGB_color_ptr)&fire_color);
  196.  
  197.      // display double buffer
  198.  
  199.      Display_Double_Buffer(double_buffer,0);
  200.  
  201.      // lock onto 18 frames per second max
  202.  
  203.      Time_Delay(1);
  204.  
  205.      } // end while
  206.  
  207. // exit in a very cool way
  208.  
  209. Screen_Transition(SCREEN_SWIPE_Y);
  210.  
  211. // free up all resources
  212.  
  213. Sprite_Delete((sprite_ptr)&alien);
  214.  
  215. Delete_Double_Buffer();
  216.  
  217. Set_Graphics_Mode(TEXT_MODE);
  218.  
  219. } // end main
  220.  
  221.