home *** CD-ROM | disk | FTP | other *** search
/ Black Art of 3D Game Programming / Black_Art_of_3D_Game_Programming.iso / source / msc / chap_8 / jelly.c < prev    next >
Text File  |  1994-11-23  |  4KB  |  173 lines

  1.  
  2. // JELLY.C - A demo of latching onto the timer interrupt 0x1C
  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. #include "black8.h"
  21.  
  22.  
  23. // G L O B A L S  ////////////////////////////////////////////////////////////
  24.  
  25. pcx_picture image_pcx;  // general PCX image used to load background and imagery
  26.  
  27. sprite jelly;           // the jelly fish
  28.  
  29. int ISR_jelly_x = -20,  // global variables that track the position of the
  30.     ISR_jelly_y = 100;  // jelly fish and are updated by the ISR
  31.  
  32. // this holds the old timer keeper isr
  33.  
  34. void (_interrupt _far *Old_Timer_ISR)();
  35.  
  36. ////////////////////////////////////////////////////////////////////////////
  37.  
  38. void _interrupt _far Jelly_ISR(void)
  39. {
  40. // this function will update the global jelly fish position variables and
  41. // test if the jelly fish has moved off the screen, this function will be
  42. // called once every timer tick
  43.  
  44. _asm sti  ; re-enable interrupts
  45.  
  46. //////////////////////////////////////////////////
  47.  
  48. // move the jelly fish
  49.  
  50. if (++ISR_jelly_x > 320)
  51.    ISR_jelly_x = -20;
  52.  
  53. ISR_jelly_y = ISR_jelly_y - 1 + rand()%3;
  54.  
  55. // test y bounds
  56.  
  57. if (ISR_jelly_y > 200)
  58.     ISR_jelly_y=-20;
  59. else
  60. if (ISR_jelly_y < -20)
  61.     ISR_jelly_y=200;
  62.  
  63. /////////////////////////////////////////////////
  64.  
  65. // re-enable pic
  66.  
  67. _outp(PIC_ICR,PIC_EOI);
  68.  
  69. } // end Jelly_ISR
  70.  
  71. // M A I N //////////////////////////////////////////////////////////////////
  72.  
  73. void main(int argc, char **argv)
  74. {
  75.  
  76. int index;         // loop variable
  77.  
  78. // set the graphics mode to mode 13h
  79.  
  80. Set_Graphics_Mode(GRAPHICS_MODE13);
  81.  
  82. // create the double buffer
  83.  
  84. Create_Double_Buffer(200);
  85.  
  86. // load the imagery for the jelly fish
  87.  
  88. PCX_Init((pcx_picture_ptr)&image_pcx);
  89. PCX_Load("jelly.pcx", (pcx_picture_ptr)&image_pcx,1);
  90.  
  91. // intialize the jelly fish
  92.  
  93. Sprite_Init((sprite_ptr)&jelly,-20,100,16,16,0,0,0,0,0,0);
  94.  
  95. // extract the bitmaps for the speeder, there are 4 animation cells
  96.  
  97. for (index=0; index<4; index++)
  98.     PCX_Get_Sprite((pcx_picture_ptr)&image_pcx,(sprite_ptr)&jelly,index,index,0);
  99.  
  100. // done with this PCX file so delete memory associated with it
  101.  
  102. PCX_Delete((pcx_picture_ptr)&image_pcx);
  103.  
  104. // install the jelly fish motion interrupt
  105.  
  106. Old_Timer_ISR = _dos_getvect(TIME_KEEPER_INT);
  107.  
  108. _dos_setvect(TIME_KEEPER_INT,Jelly_ISR);
  109.  
  110. // scan background before entering event loop
  111.  
  112. Sprite_Under_Clip((sprite_ptr)&jelly,double_buffer);
  113.  
  114. // put up exit instructions
  115.  
  116. Print_String_DB(80,2,9,"Hit any key to exit",1);
  117.  
  118. // main event loop, process until keyboard hit
  119.  
  120. while(!kbhit())
  121.      {
  122.  
  123.      // do animation cycle
  124.  
  125.      // erase the jelly fish
  126.  
  127.      Sprite_Erase_Clip((sprite_ptr)&jelly,double_buffer);
  128.  
  129.      // move the jelly fish, note that we only copy global variables here
  130.      // the variables themselves are modified by the ISR
  131.  
  132.      jelly.x = ISR_jelly_x;
  133.      jelly.y = ISR_jelly_y;
  134.  
  135.      // animate the jelly fish
  136.  
  137.      if (++jelly.curr_frame == 4)
  138.         jelly.curr_frame = 0;
  139.  
  140.      // ready to draw speeder, but first scan background under it
  141.  
  142.      Sprite_Under_Clip((sprite_ptr)&jelly,double_buffer);
  143.  
  144.      Sprite_Draw_Clip((sprite_ptr)&jelly,double_buffer,1);
  145.  
  146.      // display double buffer
  147.  
  148.      Display_Double_Buffer(double_buffer,0);
  149.  
  150.      // lock onto 18 frames per second max
  151.  
  152.      Time_Delay(1);
  153.  
  154.      } // end while
  155.  
  156. // exit in a very cool way
  157.  
  158. Screen_Transition(SCREEN_DARKNESS);
  159.  
  160. // free up all resources
  161.  
  162. Sprite_Delete((sprite_ptr)&jelly);
  163. Delete_Double_Buffer();
  164.  
  165. // restore the old isr
  166.  
  167. _dos_setvect(TIME_KEEPER_INT,Old_Timer_ISR);
  168.  
  169. Set_Graphics_Mode(TEXT_MODE);
  170.  
  171. } // end main
  172.  
  173.