home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 15 / 15.iso / s / s300 / 1.ddi / CHAP7 / CON_MSC.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-18  |  3.9 KB  |  152 lines

  1. /* Console control for a character-oriented screen --
  2. This version is set up for the Microsoft C compiler.
  3.  
  4. The coordinates used for input to all functions are in the
  5. graphics coordinate space, with the origin as (0,0) at the
  6. lower left.
  7.  
  8. copyright (c) 1987-88 D.M.Auslander
  9.  
  10. Created:    10-July-87
  11. Updates:
  12.         15-July-87, DMA, fix CGA1 color table
  13.         18-Aug-88, adapt for Microsoft C Compiler
  14.  
  15. This version is set up for various graphic modes on the IBM.
  16. The control "defines" are:
  17. CGA1    color graphics adapter, 640x200, monochrome
  18. CGA2    color graphics adapter, 320x200, color
  19. EGA        enhanced graphics adapter, 640x320, color
  20. VGA    video graphics array, 640x480, color
  21. */
  22.  
  23. #include <stdio.h>
  24. #include <graph.h>
  25. #include <dos.h>
  26.  
  27.  
  28. #define EGA
  29.  
  30. #ifdef EGA
  31. static int srow = 350, scol = 640,smode = _ERESCOLOR,rmode = 3;
  32. static int crow = 25, ccol = 80;    /* Size of character screen */
  33. /* Output color map */
  34. static int c_out[] =
  35.     {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
  36. #endif
  37.  
  38. #ifdef CGA1
  39. static int srow = 200, scol = 640,smode = _HRESBW,rmode = 3;
  40. static int crow = 25, ccol = 80;    /* Size of character screen */
  41. /* Output color map */
  42. static int c_out[] = {0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
  43. #endif
  44.  
  45. #ifdef CGA2
  46. static int srow = 200, scol = 320,smode = _MRES4COLOR,rmode = 3;
  47. static int crow = 25, ccol = 80;    /* Size of character screen */
  48. /* Output color map */
  49. static int c_out[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
  50. #endif
  51.  
  52. static float sht = 20.0, swdth = 20.0;    /* Screen height and width (cm) */
  53.  
  54. init_scr()        /* Initialize the screen 
  55. ----------        */
  56. {
  57. _setvideomode(smode);
  58. }
  59.  
  60. rst_scr()    /* Restore the screen to its original state
  61. ----------    */
  62. {
  63. _setvideomode(_DEFAULTMODE);
  64. }
  65.  
  66. mv_cur(x,y)    /* Move the cursor to a location specified in the **graphic**
  67. -----------    coordinate space.  Origin is lower left. */
  68. float x,y;
  69. {
  70. int r,c;
  71.  
  72. r = (sht - y) * (crow -1) / sht + 1.5;    /* Compute row/col coordinates */
  73. c = x * (ccol - 1) / swdth + 1.5;
  74. _settextposition(r,c);          /* Move the cursor */
  75. }
  76.  
  77. pstr(s)        /* Write the string to the screen at the 
  78. -------        current cursor location */
  79. char *s;
  80. {
  81. fputs(s,stdout);
  82. }
  83.  
  84. drawl(x1,y1,x2,y2,color)    /* Draw a line from (x1,y1) to (x2,y2)
  85. ------------------------    */
  86. float x1,y1,x2,y2;
  87. int color;
  88. {
  89. int xi1,xi2,yi1,yi2,dx,dy,incx = 1, incy = 1, reg,i,n,ix,iy;
  90. int clr;
  91.  
  92. yi1 = (sht - y1) * (srow -1) / sht + 1.5;    /* Compute row/col coordinates */
  93. xi1 = x1 * (scol - 1) / swdth + 1.5;
  94. yi2 = (sht - y2) * (srow -1) / sht + 1.5;    /* Compute row/col coordinates */
  95. xi2 = x2 * (scol - 1) / swdth + 1.5;
  96.  
  97. sclim(&xi1,&yi1,&xi2,&yi2);        /* Limit values to inside the screen */
  98.  
  99. clr = _setcolor(c_out[color]);
  100. _moveto(xi1,yi1);
  101. _lineto(xi2,yi2);
  102. _setcolor(clr);     /* Set color back to default */
  103. }
  104.  
  105. sclim(x1,y1,x2,y2)        /* Limit values to inside the screen
  106. ----------------------        */
  107. int *x1,*x2,*y1,*y2;
  108. {
  109. *x1 = ilim(*x1,1,scol);
  110. *x2 = ilim(*x2,1,scol);
  111. *y1 = ilim(*y1,1,srow);
  112. *y2 = ilim(*y2,1,srow);
  113. }
  114.  
  115. ilim(x,xmin,xmax)    /* Limit value of x to <xmin,xmax>
  116. -----------------    */
  117. int x,xmin,xmax;
  118. {
  119. if(x < xmin)return(xmin);
  120. if(x > xmax)return(xmax);
  121. return(x);
  122. }
  123.  
  124. /* Register definitions for DOS calls -- structure definitions are
  125. in dos.h
  126. */
  127.  
  128. static struct WORDREGS wregs;    /* Word registers */
  129. static struct BYTEREGS bregs;    /* Byte registers */
  130. static union REGS r;
  131.  
  132. char_in()    /* Check for a character from the keyboard 
  133. ---------    Return 0 if no character or the ASCII code if a character
  134.         is detected */
  135.         
  136. {
  137. if(kbhit())
  138.     {
  139.     r.h.ah = 7;    /* Get a character */
  140.     intdos(&r,&r);    /* Call DOS */
  141.     if(r.h.al != 0)return(r.h.al & 0x7f);       /* Return character value */
  142.     else
  143.         {    /* Get extended code */
  144.         r.h.ah = 7;
  145.         intdos(&r,&r);    /* Call again */
  146.         return(r.h.al | 0x80 & 0xff);
  147.                     /* Set high bit for extended codes */
  148.         }
  149.     }
  150. return(0);    /* No character ready */
  151. }
  152.