home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / VGALIB.ZIP / VIO.C < prev    next >
C/C++ Source or Header  |  1992-08-19  |  5KB  |  301 lines

  1. /* test of VioGetPhysBuf()  */
  2. /* By John E. Stone  08/15/92   */
  3. /* E-mail Johns@cs.umr.edu      */
  4. /* Make sure that program type is set to NOTWINDOWCOMPAT before */
  5. /*  use... Otherwise VioSetMode will fail... */
  6.  
  7.  
  8.  
  9.  
  10. #define INCL_BASE
  11. #include <os2.h>
  12. #include <math.h>
  13. #include <string.h>
  14. #include <stdlib.h>
  15.  
  16. #define HANDLE 0
  17. #define WAIT 1
  18. #define BUFSIZE 12
  19.  
  20.  
  21. /******** CLS *******/
  22. void cls(ptr0,color)
  23. PCH ptr0;
  24. char color;
  25. {
  26. memset((PCH) ptr0,(color*256 + color),64000);
  27. }
  28.  
  29. /********* Putpixel ********/
  30. void putpixel(ptr0,x,y,color)
  31. PCH ptr0;
  32. unsigned int x;
  33. unsigned int y;
  34. char color;
  35. {
  36. *(PCH) (ptr0+(320*y)+x)=color;
  37. }
  38.  
  39. unsigned char getpixel(ptr0,x,y)
  40. PCH ptr0;
  41. unsigned int x,y;
  42. {
  43. return(*(PCH) (ptr0+x+(320*y))); 
  44. }
  45.  
  46.  
  47.  
  48. /******* midline ****************/
  49. void midline(ptr0,x1,y1,x2,y2,color)
  50. PCH ptr0;
  51. unsigned int x1,y1,x2,y2;
  52. char color;
  53. {
  54. unsigned int x,y;
  55.          int d,dx,dy,incrE,incrNE;
  56.  
  57. x=x1;
  58. y=y1;
  59. dx=x2-x1;
  60. dy=y2-y1;
  61. *(PCH) (ptr0+(320*y)+x)=color;
  62.  
  63. if (dy>=0) {
  64.     d=(2*dy) - dx;
  65.     incrE=2*dy;
  66.     incrNE=2*(dy-dx);
  67.  
  68. while (x<x2)
  69. {
  70.     if (d<=0)
  71.         {  d+=incrE;
  72.        x++; }
  73.     else
  74.     {
  75.        d+=incrNE;
  76.        x++;
  77.        y++;
  78.     }
  79.     *(PCH) (ptr0+(320*y)+x)=color;
  80. }
  81. }
  82. else {
  83.     d=(2*dy)+dx;
  84.     incrE=2*dy;
  85.     incrNE=2*(dy+dx);
  86. while (x<x2)
  87. {
  88.     if (d>=0)
  89.     { d+=incrE;
  90.       x++; }
  91.     else
  92.     { d+=incrNE;
  93.       x++;  y--; 
  94.         }
  95.     *(PCH) (ptr0+(320*y)+x)=color;
  96. }
  97.  
  98. } /* end of midline */
  99.  
  100. /****** line ******/
  101.  
  102. void line(ptr0,x1,y1,x2,y2,color)
  103. PCH ptr0;
  104. unsigned int x1,y1,x2,y2;
  105. char color;
  106. {
  107.  
  108. if (x1<=x2)
  109.    {
  110.    midline(ptr0,x1,y1,x2,y2,color);
  111.    }
  112. else
  113.    {
  114.    midline(ptr0,x2,y2,x1,y1,color); 
  115.    }
  116. } /* end of line */
  117.  
  118.  
  119. /******** Box **********/
  120. box(ptr0,x1,y1,x2,y2,color)
  121. PCH ptr0;
  122. unsigned int x1,y1,x2,y2;
  123. char color;
  124. {
  125. unsigned int y,lx,ly,ux,uy;
  126. register unsigned int x;
  127.  
  128.   lx=min(x1,x2);
  129.   ux=max(x1,x2);
  130.   ly=min(y1,y2);
  131.   uy=max(y1,y2);
  132.  
  133.   for (y=ly*320; y<=uy*320; y+=320) {
  134.     for (x=lx;x<=ux;x++) {
  135.       *(PCH) (ptr0+y+x)=color;
  136.     }
  137.   }
  138. }
  139.  
  140. circlepoints(ptr0,x,y,xc,yc,color)
  141. PCH ptr0;
  142. int x,y;
  143. unsigned int xc,yc;
  144. char color;
  145. {
  146. *(PCH) (ptr0+x+xc+(y+yc)*320)=color;
  147. *(PCH) (ptr0+y+xc+(x+yc)*320)=color;
  148. *(PCH) (ptr0+y+xc+(-x+yc)*320)=color;
  149. *(PCH) (ptr0+x+xc+(-y+yc)*320)=color;
  150. *(PCH) (ptr0-x+xc+(-y+yc)*320)=color;
  151. *(PCH) (ptr0-y+xc+(-x+yc)*320)=color;
  152. *(PCH) (ptr0-y+xc+(x+yc)*320)=color;
  153. *(PCH) (ptr0-x+xc+(y+yc)*320)=color;
  154. }
  155.  
  156. /***** Circle ******/
  157. void circle(ptr0,xc,yc,radius,color)
  158. PCH ptr0;
  159. unsigned int xc,yc,radius;
  160. char color;
  161. {
  162. int x,y,d;
  163.  
  164. x=0;
  165. y=radius;
  166. d=1-radius;
  167. circlepoints(ptr0,x,y,xc,yc,color);
  168.  
  169. while (y>x) {
  170.     if (d<0) {
  171.         d+=2*x+3;
  172.         x++;
  173.     }
  174.     else {
  175.         d+=2*(x-y)+5;
  176.         x++;
  177.         y--;
  178.     }
  179.      circlepoints(ptr0,x,y,xc,yc,color);
  180. }
  181. }
  182.  
  183.  
  184.  
  185.  
  186. main()
  187. {
  188.     struct _VIOPHYSBUF phys;
  189.     struct _VIOMODEINFO orig,moda;
  190.     char status;
  191.     unsigned int index;
  192.     unsigned rc;
  193.     unsigned short color;
  194.     PCH ptr0;
  195.     unsigned int x,y;
  196.     char * pal[1000];
  197.     char testcolor;
  198.  
  199. phys.pBuf=(unsigned char *) 0xA0000;
  200. phys.cb=65536;
  201.  
  202. moda.cb=BUFSIZE;
  203. moda.fbType=3;
  204. moda.color=8;
  205. moda.col=40;
  206. moda.row=25;
  207. moda.hres=320;
  208. moda.vres=200;
  209.  
  210. VioGetMode(&orig, HANDLE);
  211.  
  212. if (rc=VioSetMode(&moda, HANDLE) )
  213.    { printf("VioSetMode error=%u",rc);  exit(1); }
  214.  
  215. if (rc=VioGetPhysBuf(&phys, 0) )
  216.    { printf("VioGetPhysBuf error=%u",rc); exit(1); }
  217.  
  218. ptr0=MAKEP(phys.asel[0],0);
  219.  
  220. VioScrLock(WAIT, &status, HANDLE);
  221.  
  222. for (color=0; color<256; color++) {
  223. cls(ptr0,color);
  224. }
  225.  
  226. cls(ptr0,0);
  227.  
  228. for (x=0; x<320; x++) {
  229.   for (y=0; y<200; y++) {
  230.     putpixel(ptr0,x,y,(char) y);
  231.   }
  232. }
  233.  
  234. cls(ptr0,0);
  235.  
  236. VioScrUnLock(HANDLE);
  237. VioScrLock(WAIT,&status,HANDLE);
  238. for( x=0; x<750000; x++)
  239. {
  240.    *(PCH) (ptr0+ (unsigned int) (rand() % 32000) + (unsigned int) rand() % 32000)=(char) rand() % 256; 
  241. }
  242. cls(ptr0,0);
  243.  
  244. VioScrUnLock(HANDLE);
  245. VioScrLock(WAIT,&status,HANDLE);
  246.  
  247. for (x=0; x<100000; x++)
  248.  line(ptr0,(unsigned int) rand() % 320, (unsigned int) rand() % 200,
  249.            (unsigned int) rand() % 320, (unsigned int) rand() % 200, 
  250.        (char) rand() % 256);
  251. }
  252.  
  253. cls(ptr0,0);
  254.  
  255. for (x=0; x<1000; x++) {
  256. box(ptr0,(unsigned int) rand() % 320, (unsigned int) rand() % 200,
  257.       (unsigned int) rand() % 320, (unsigned int) rand() % 200,
  258.       (char) rand() % 256);
  259. }
  260.  
  261.  
  262. cls(ptr0,0);
  263.  
  264. for (x=0; x<50000; x++) {
  265. circle(ptr0,(unsigned int) (20 + rand() % 280),
  266.         (unsigned int) (20 + rand() % 160),
  267.         15,(char) rand() % 256 );
  268. }
  269.  
  270. cls(ptr0,0);
  271. VioScrUnLock(HANDLE);
  272. VioScrLock(WAIT,&status,HANDLE);
  273. for (x=0; x<256; x++) {
  274.   for (y=0; y<200; y++) {
  275.       putpixel(ptr0,x,y,(char) x);
  276.   }
  277. }
  278.  
  279.  
  280.  
  281.  
  282. for (x=0; x<100; x++) {
  283. testcolor=rand() % 256;
  284. putpixel(ptr0,x,x,testcolor);
  285. if (getpixel(ptr0,x,x)!=testcolor) { DosBeep(5000,500); }
  286. }
  287.  
  288. DosSleep(5000L);
  289.  
  290.  
  291. VioSetMode(&orig,HANDLE);
  292.  
  293. cls(ptr0,0);
  294.  
  295. VioScrUnLock(HANDLE);
  296. exit(0);
  297. }
  298.  
  299.