home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / w / wgt3_ex.zip / FOREST.C < prev    next >
C/C++ Source or Header  |  1992-09-11  |  3KB  |  120 lines

  1. #include <dos.h>
  2. #include <alloc.h>
  3. #include <stdio.h>
  4. #include <conio.h>
  5. #include <wgt.h>
  6. #include <scroll.h>
  7. /* WordUp Graphics Toolkit Scrolling Demo
  8.  This program demonstrates a method of animating the tiles in a map.
  9.  The basic idea is to scan through the map, looking for the tile
  10.  you want to animate. For every occurrence, store the x and y coordinates
  11.  in an array. Keep track of the number of tiles found.
  12.  In the main loop, keep changing the tiles with wputworldblock.
  13. */
  14.  
  15. wgtmap forestmap;            // our world map
  16.  
  17. unsigned char waterx[300],watery[300]; // x and y coords of animated tiles
  18. unsigned char wateranim[300]; // current tile number in animation
  19.  
  20. int number_of_water_blocks; // number of tiles needed to change
  21.  
  22. int kbdon[35]={0,0,0,0,0};    // our keyboard on/off array;
  23. int kbdscanlist[35]={72,80,75,77,1};    // our keyboard scan codes;
  24. // You must have the above two if you want the keyboard interrupt 
  25.  
  26. color palette[256];        // our palette of colours
  27.  
  28. block blocks[201];        // our blocks for the map
  29. block sprites[201];        // our sprites 
  30.  
  31. int i,xmove,ymove;
  32.  
  33. void scanwater(void);    // Search map for all water tiles.
  34. void movewater(void);   // Animate the tiles
  35.  
  36. void main(void)
  37. {
  38. vga256();                    // init
  39. wloadsprites(&palette,"forest.spr",blocks);        // load blocks
  40. forestmap=wloadmap("forest.wmp");        // load our world map
  41.  
  42. winitscroll(13,10);                // make a 13x10 box
  43.                         // for the scrolling
  44.  
  45. wnormscreen();                    // go back to normal screen
  46. wcls(0);                    // clear it
  47.  
  48. wshowwindow(0,0,forestmap);            // start looking at world
  49.                         // at 0,0
  50. installkbd();                    // start new keyboard interrupt
  51.  
  52. scanwater();                      // find the tiles
  53. do
  54. {
  55.  
  56. xmove=0;
  57. ymove=0;
  58. if (kbdon[2]==1)        // Pressing left
  59.   xmove=-8;
  60. else if (kbdon[3]==1)        // Pressing right
  61.   xmove=8;
  62. if (kbdon[0]==1)        // Pressing up
  63.   ymove=-8;
  64. else if (kbdon[1]==1)        // Pressing down
  65.   ymove=8;
  66.  
  67. wscrollwindow(xmove,ymove,forestmap);    // update the scrolling window
  68.  
  69. movewater();                // animate the tiles
  70. wcopyscroll(0,0);            // copy to visual screen
  71. } while (kbdon[4] !=1);            // until ESC is pressed
  72.  
  73. uninstallkbd();                // remove keyboard interrupt
  74.  
  75. wendscroll();                // close down the scrolling
  76. wfreesprites(blocks);
  77. free(forestmap);
  78. textmode(C80);
  79. }
  80.  
  81.  
  82. void scanwater(void)
  83. {
  84. int i,j,temp;
  85.  
  86. number_of_water_blocks=0;
  87. for (i=0; i<=mapheight; i++)
  88.   for (j=0; j<=mapwidth; j++)
  89.      {
  90.      temp=wgetworldblock(j*16,i*16,forestmap);
  91.      if (temp==2) // it is water
  92.        {
  93.        waterx[number_of_water_blocks]=j;        // store x and y
  94.        watery[number_of_water_blocks]=i;
  95.        wateranim[number_of_water_blocks]=2;
  96.        number_of_water_blocks++;
  97.        }
  98.      }
  99. }
  100.  
  101.  
  102. void movewater(void)
  103. {
  104. int j;
  105.  
  106.   for (j=0; j<=number_of_water_blocks; j++)
  107.      {
  108.       if ((waterx[j]*16>worldx-16) &
  109.       (waterx[j]*16<worldx+windowmaxx+16) &
  110.       (watery[j]*16>worldy-16) &
  111.       (watery[j]*16<worldy+windowmaxy+16))
  112.       {
  113.       wateranim[j]++;
  114.       if (wateranim[j]>4)
  115.       wateranim[j]=2;
  116.       wputworldblock(waterx[j]*16,watery[j]*16,wateranim[j],forestmap);
  117.        }
  118.        }
  119. }
  120.