home *** CD-ROM | disk | FTP | other *** search
/ Teach Yourself Game Programming in 21 Days / TYGAMES_R.ISO / source / day_06 / lscape.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-22  |  4.4 KB  |  215 lines

  1.  
  2. // I N C L U D E S ///////////////////////////////////////////////////////////
  3.  
  4. #include <io.h>
  5. #include <conio.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <dos.h>
  9. #include <bios.h>
  10. #include <fcntl.h>
  11. #include <memory.h>
  12. #include <malloc.h>
  13. #include <math.h>
  14. #include <string.h>
  15.  
  16. #include <graph.h>
  17.  
  18. #include "graph3.h"
  19. #include "graph4.h"
  20.  
  21. #define SCROLL_WIDTH  (unsigned int)640
  22. #define SCROLL_HEIGHT (unsigned int)100
  23.  
  24. // G L O B A L S  ////////////////////////////////////////////////////////////
  25.  
  26. unsigned char far *scroll_buffer = NULL;  // pointer to the scrolling buffer
  27.                                           // which is 640x100 pixels
  28.  
  29. // F U N C T I O N S /////////////////////////////////////////////////////////
  30.  
  31. void Show_View_Port(char far *buffer,int pos)
  32. {
  33. // copy a portion of the scrolling buffer to the video buffer
  34.  
  35. unsigned int y,scroll_off, screen_off;
  36.  
  37. // there are 100 rows that need to be moved, move the data row by row
  38.  
  39. for (y=0; y<100; y++)
  40.     {
  41.  
  42.     // compute starting offset into scroll buffer
  43.     // y * 640 + pos
  44.  
  45.     scroll_off = ((y<<9) + (y<<7) + pos );
  46.  
  47.     // compute starting offset in video ram
  48.     // y * 320 + 80
  49.  
  50.     screen_off = (((y+50)<<8) + ((y+50)<<6) + 80 );
  51.  
  52.     // move the data
  53.  
  54.     _fmemmove((char far *)&video_buffer[screen_off],
  55.               (char far *)&buffer[scroll_off],160);
  56.  
  57.     } // end for y
  58.  
  59. } // end Show_View_Port
  60.  
  61.  
  62.  
  63. //////////////////////////////////////////////////////////////////////////////
  64.  
  65. void Plot_Pixel_Scroll(int x,int y,unsigned char color)
  66. {
  67. // plots pixels into the scroll buffer with our new virtual screen size
  68. // of 640x100
  69.  
  70. // use the fact that 640*y = 512*y + 128*y = y<<9 + y<<7
  71.  
  72. scroll_buffer[((y<<9) + (y<<7)) + x] = color;
  73.  
  74. } // end Plot_Pixel_Scroll
  75.  
  76. /////////////////////////////////////////////////////////////////////////////
  77.  
  78. void Draw_Terrain(void)
  79. {
  80.  
  81. // this function draws the terrain into the scroll buffer
  82. // which in this case is 640x100 pixels
  83.  
  84. int x,y=70,y1,index;
  85.  
  86. // clear out memory first
  87.  
  88. _fmemset(scroll_buffer,0,SCROLL_WIDTH*SCROLL_HEIGHT);
  89.  
  90. // draw a few stars
  91.  
  92. for (index=0; index<200; index++)
  93.     {
  94.     Plot_Pixel_Scroll(rand()%640,rand()%70,15);
  95.     } // end for index
  96.  
  97. // draw some moutains
  98.  
  99. for (x=0; x<640; x++)
  100.     {
  101.  
  102.     // compute offset
  103.  
  104.     y+=-1 + rand()%3;
  105.  
  106.     // make sure terrain stays within resonable boundary
  107.  
  108.     if (y>90) y=90;
  109.     else
  110.     if (y<40) y=40;
  111.  
  112.     // plot the dot in the double buffer
  113.  
  114.     Plot_Pixel_Scroll(x,y,2);
  115.  
  116.     for (y1=y+1; y1<100; y1++)
  117.          Plot_Pixel_Scroll(x,y1,10);
  118.  
  119.     } // end for x
  120.  
  121. } // end Draw_Terrain
  122.  
  123. // M A I N ///////////////////////////////////////////////////////////////////
  124.  
  125. void main(void)
  126. {
  127.  
  128. int done=0, // exit flag
  129.     sx=0;   // scrolling viewport position
  130.  
  131. // set the videomode to 320x256x256
  132.  
  133. _setvideomode(_MRES256COLOR);
  134.  
  135. // put up some information
  136.  
  137. _settextposition(0,0);
  138.  
  139. printf("Use < > to move. Press Q to quit.");
  140.  
  141. // draw a little window
  142.  
  143. _setcolor(1);
  144.  
  145. _rectangle(_GBORDER, 80-1,50-1,240+1,150+1);
  146.  
  147. // allocate memory for scrolling buffer
  148.  
  149. scroll_buffer = (char far *)_fmalloc(SCROLL_WIDTH * SCROLL_HEIGHT);
  150.  
  151. // draw the mountains
  152.  
  153. Draw_Terrain();
  154.  
  155. // show initial view
  156.  
  157. Show_View_Port(scroll_buffer,sx);
  158.  
  159. // main loop
  160.  
  161. while(!done)
  162.      {
  163.  
  164.      // has user hit a key
  165.  
  166.      if (kbhit())
  167.         {
  168.  
  169.         switch(getch())
  170.               {
  171.               case ',': // move window to left, but don't go too far
  172.                       {
  173.                       sx-=2;
  174.  
  175.                       if (sx<0)
  176.                           sx=0;
  177.  
  178.                       } break;
  179.  
  180.               case '.': // move window to right, but dont go too far
  181.                       {
  182.                       sx+=2;
  183.  
  184.                       if (sx > 640-160)
  185.                          sx=640-160;
  186.  
  187.                       } break;
  188.  
  189.               case 'q': // user trying to bail ?
  190.                       {
  191.                       done=1;
  192.  
  193.                       } break;
  194.  
  195.               } // end switch
  196.  
  197.         // copy view port to screen
  198.  
  199.         Show_View_Port(scroll_buffer,sx);
  200.  
  201.         _settextposition(24,0);
  202.  
  203.         printf("Viewport position = %d  ",sx);
  204.  
  205.         } // end if
  206.  
  207.      } // end while
  208.  
  209. // restore text video mode
  210.  
  211. _setvideomode(_DEFAULTMODE);
  212.  
  213. } // end main
  214.  
  215.