home *** CD-ROM | disk | FTP | other *** search
/ Computer Tool Software / soft.iso / Multimed / WINFAST / WFST230 / DOS / EXAMPLE1 / DRAW.C next >
Encoding:
C/C++ Source or Header  |  1996-04-19  |  9.3 KB  |  242 lines

  1. // DRAW.C
  2.  
  3. #include <dos.h>
  4. #include <conio.h>
  5. #include "draw.h"
  6.  
  7. #define VIDEO               0x10
  8. #define ESC                 27
  9.  
  10. typedef struct tagDrawPattern
  11. {
  12.     unsigned        left, top, width, height;
  13.     unsigned        color;
  14.     unsigned char  *ptrPattern;
  15. } DrawPattern;
  16.  
  17. DrawPattern PatD;
  18.  
  19. static unsigned char   DataD[40*40] = { 
  20.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  21.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  22.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  23.     0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  24.     0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,
  25. //           |         |         |         |         |         |         |         |    
  26.     0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,
  27.     0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,
  28.     0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,
  29.     0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,
  30.     0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0,
  31. //           |         |         |         |         |         |         |         |    
  32.     0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0,0,
  33.     0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0,
  34.     0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,
  35.     0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,
  36.     0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,
  37. //           |         |         |         |         |         |         |         |    
  38.     0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,
  39.     0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,
  40.     0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,
  41.     0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,
  42.     0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,
  43. //           |         |         |         |         |         |         |         |    
  44.     0,0,0,0,1,1,1,1,1,0,0,0,1,1,1,1,1,0,0,0,1,1,1,1,1,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,
  45.     0,0,0,0,1,1,1,1,1,0,0,0,1,1,1,1,1,1,0,0,0,1,1,1,1,1,0,0,0,0,0,1,1,1,1,1,0,0,0,0,
  46.     0,0,0,0,1,1,1,1,1,0,0,0,0,1,1,1,1,1,1,0,0,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,0,0,0,0,
  47.     0,0,0,0,1,1,1,1,1,0,0,0,0,0,1,1,1,1,1,0,0,0,1,1,1,1,1,0,0,0,1,1,1,1,1,1,0,0,0,0,
  48.     0,0,0,0,1,1,1,1,1,0,0,0,0,0,1,1,1,1,1,1,0,0,0,1,1,1,1,1,0,1,1,1,1,1,1,1,0,0,0,0,
  49. //           |         |         |         |         |         |         |         |    
  50.     0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,
  51.     0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,
  52.     0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,
  53.     0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,
  54.     0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,
  55. //           |         |         |         |         |         |         |         |    
  56.     0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,
  57.     0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,
  58.     0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,0,0,0,1,1,1,1,1,1,0,0,0,0,0,0,
  59.     0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,0,0,0,1,1,1,1,1,1,0,0,0,0,0,
  60.     0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,0,0,0,0,
  61. //           |         |         |         |         |         |         |         |    
  62.     0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,0,0,0,1,1,1,1,1,0,0,0,0,
  63.     0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,0,0,1,1,1,1,1,1,0,0,0,
  64.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  65.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  66.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  67. };   
  68.  
  69.  
  70. void    _MovePalette(unsigned int, unsigned int, unsigned int);
  71. void    _ShowPattern(void);
  72. void    _PokeDot(unsigned uX, unsigned uY, unsigned uColor);
  73.  
  74. //
  75. // DRAW_LeadtekPattern
  76. //
  77. void DRAW_LeadtekPattern()
  78. {                   
  79.                              
  80.     int             idx1, idx2;                          
  81.     unsigned int    wColor;   
  82.                           
  83.     PatD.width      = 40;
  84.     PatD.height     = 40;
  85.     
  86.     for(idx1 = 20; idx1 < 600; idx1 += 40) {                                  
  87.         wColor = 31;
  88.         for(idx2 = 0; idx2 < 480; idx2 += 40) {   
  89.             wColor = 32 + (idx2 / 40) + ((idx1-20) / 100);
  90.             
  91.             PatD.left       = idx1;
  92.             PatD.top        = idx2;
  93.             PatD.color      = wColor;   
  94.             PatD.ptrPattern = DataD;
  95.  
  96.             _ShowPattern(); 
  97.         }   
  98.     }
  99. }                                     
  100.           
  101.  
  102. //
  103. // DRAW_Delay_MovPal_WaitKey
  104. //
  105. int DRAW_Delay_MovPal_WaitKey()
  106. {
  107.     int     iCount = 0;
  108.  
  109.     while(!kbhit()) {
  110.         DRAW_Delay(2);
  111.         _MovePalette(32, 55, 1);        
  112.         if(iCount++==25) return 1;
  113.     }
  114.     while(kbhit()) if(getch()==ESC) return 0;
  115. }
  116.  
  117.  
  118. //
  119. // DRAW_MovPal_WaitKey
  120. //
  121. void DRAW_MovPal_WaitKey()
  122. {
  123.     while(!kbhit()) {
  124.         DRAW_Delay(2);
  125.         _MovePalette(32, 55, 1);        
  126.     }
  127.     while(kbhit()) if(getch()==ESC) return;
  128. }
  129.  
  130. //
  131. // _MovePalette
  132. //
  133. void _MovePalette(unsigned int wStart, unsigned int wEnd, unsigned int wDistance)
  134. {   
  135.     static const unsigned DAC_ADDR_READ     = 0x3c7;
  136.     static const unsigned DAC_ADDR_WRITE    = 0x3c8;
  137.     static const unsigned DAC_DATA          = 0x3c9;    
  138.                                 
  139.     unsigned  wFirstRed, wFirstGreen, wFirstBlue;                                 
  140.     unsigned  wRed, wGreen, wBlue;                  
  141.     unsigned  idx;
  142.  
  143.     _outp(DAC_ADDR_READ, wStart);
  144.     wFirstRed   = _inp(DAC_DATA);
  145.     wFirstGreen = _inp(DAC_DATA);
  146.     wFirstBlue  = _inp(DAC_DATA);
  147.     
  148.     for(idx = wStart + wDistance; idx<= wEnd; idx += wDistance) {
  149.         _outp(DAC_ADDR_READ, idx);
  150.         wRed   = _inp(DAC_DATA);
  151.         wGreen = _inp(DAC_DATA);
  152.         wBlue  = _inp(DAC_DATA);    
  153.         
  154.         _outp(DAC_ADDR_WRITE, idx - wDistance);
  155.         _outp(DAC_DATA, wRed);
  156.         _outp(DAC_DATA, wGreen);
  157.         _outp(DAC_DATA, wBlue);                                          
  158.     }                            
  159.     _outp(DAC_ADDR_WRITE,wEnd);
  160.     _outp(DAC_DATA, wFirstRed);
  161.     _outp(DAC_DATA, wFirstGreen);
  162.     _outp(DAC_DATA, wFirstBlue);    
  163. }
  164.  
  165.  
  166. /*---------------------------------------------------------------------------
  167. |                                                                           |
  168. |  Function: void _CheckKB(void)                                            |
  169. |                                                                           |
  170. |  Purpose: Check keyboard:                                                 |
  171. |           1. Press ESC key to exit any time.                              |
  172. |           2. First times,  key is pressed to pause.                       |
  173. |           3. Second times, key is pressed to continue.                    |
  174. |                                                                           |
  175. ---------------------------------------------------------------------------*/
  176. int DRAW_CheckKB()
  177. {
  178.     if(!kbhit()) return 1;
  179.     while(kbhit()) if(getch()==ESC) return 0;
  180.     while(!kbhit());
  181.     while(kbhit()) if(getch()==ESC) return 0;
  182.     return 1;
  183. }
  184.  
  185.  
  186. /*---------------------------------------------------------------------------
  187. |                                                                           |
  188. |  Function:  DRAW_Delay(int)                                               |      
  189. |                                                                           |
  190. |  Purpose: Delay a little time.                                            |
  191. |                                                                           |
  192. ---------------------------------------------------------------------------*/
  193. void DRAW_Delay(int iDelay)            //delay is in 1/10 seconds
  194. {
  195.     register int i, k;
  196.  
  197.     k = iDelay;
  198.     if(iDelay > 0) 
  199.         for(i=0; i<k; i++) {
  200.         while(!(inp(0x3da) & 0x08));
  201.         while((inp(0x3da)  & 0x08));
  202.         }
  203. }
  204.  
  205.  
  206. //
  207. // _ShowPattern
  208. //
  209. void _ShowPattern() 
  210. {
  211.     unsigned idx1, idx2;
  212.             
  213.     for(idx1 = 0; idx1 < PatD.height; idx1++) {
  214.         for(idx2 = 0; idx2 < PatD.width; idx2++) {
  215.             if(PatD.ptrPattern[idx1 * PatD.width + idx2])
  216.                 _PokeDot(idx2 + PatD.left, idx1 + PatD.top, PatD.color);
  217.         }                                         
  218.     }   
  219. }                                                                         
  220.  
  221.  
  222. //
  223. // _PokeDot
  224. //
  225. #define _P_SEG          0x3cd
  226. #define _X_RES          640L
  227. #define _BASE_SEG       0xa0000000L
  228.  
  229. void _PokeDot(unsigned uX, unsigned uY, unsigned uColor) 
  230. {                     
  231.     static int              iSeg = 0;
  232.     unsigned char _far     *lpData; 
  233.             
  234.     if(iSeg != (int)(((unsigned long)uY * _X_RES + (unsigned long)uX) >> 16)) {
  235.         iSeg = (int)(((unsigned long)uY * _X_RES + (unsigned long)uX) >> 16);
  236.         _outp(_P_SEG, iSeg);
  237.     }          
  238.     lpData = (unsigned char _far*)( _BASE_SEG |
  239.               (((unsigned long)uY * _X_RES + (unsigned  long)uX) & 0xffffL) );
  240.     *lpData = (unsigned char) uColor;
  241. }
  242.