home *** CD-ROM | disk | FTP | other *** search
/ C++ Games Programming / CPPGAMES.ISO / mt / videofnc.c < prev    next >
C/C++ Source or Header  |  1989-02-24  |  7KB  |  312 lines

  1. /* videofnc.c   compiler graphics library dependent functions */
  2. /* `MIDI Sequencing In C', Jim Conger, M&T Books, 1989 */
  3.  
  4. /* #define TURBOC 1   Define if using TURBOC, leave out for Microsoft */
  5.  
  6. #include <stdio.h>
  7. #include <conio.h>
  8.  
  9. #ifdef TURBOC
  10.     #include <graphics.h>
  11. #else
  12.     #include <graph.h>
  13. #endif
  14.  
  15. #include "standard.h"
  16. #include "screenf.h"
  17. #include "video.h"
  18.  
  19. /* write string to x,y on screen.  Colors determinded by attrib. */ 
  20. /* slow version using library functions */
  21. void
  22. writeword(char *string, int x, int y, int attrib)
  23. {
  24. #ifdef TURBOC
  25.     textattr(attrib);
  26.     gotoxy(x, y);
  27.     cputs(string);
  28. #else
  29.     _setbkcolor((long)(attrib & 0x00F0) >> 4);
  30.     _settextcolor(attrib & 0x000F);
  31.     _settextposition(y, x);
  32.     _outtext(string);
  33. #endif
  34. }
  35.  
  36.  
  37. void
  38. clearscreen(int attrib)     /* clears screen and homes cursor */
  39. {
  40. #ifdef TURBOC
  41.     textattr(attrib);
  42.     clrscr();
  43. #else
  44.     _setbkcolor((long)(attrib & 0xF0) >> 4);
  45.     _settextcolor(attrib & 0x000F);
  46.     _clearscreen(_GCLEARSCREEN);
  47.     _settextposition(1, 1);
  48. #endif
  49. }
  50.  
  51.  
  52. void
  53. clearline(int lineno, int attrib)           /* blank out a line */
  54. {
  55.     static char buf[] = "                                                                              ";
  56.  
  57. #ifdef TURBOC
  58.     textattr(attrib);
  59.     gotoxy(1, lineno);
  60.     cputs(buf);
  61. #else
  62.     _setbkcolor((long)(attrib & 0x00F0) >> 4);
  63.     _settextcolor(attrib & 0x000F);
  64.     _settextposition(lineno, 0);
  65.     _outtext(buf);
  66. #endif
  67. }
  68.  
  69.  
  70. void
  71. csrplot(int x, int y)       /* moves text cursor to x, y on screen */
  72. {
  73. #ifdef TURBOC
  74.     gotoxy(x, y);
  75. #else
  76.     _settextposition(x, y);
  77. #endif
  78. }
  79.  
  80.  
  81. void
  82. setvideomode(int mode)      /* sets the video mode for screen output */
  83. {
  84. #ifdef TURBOC
  85.     int gdriver, gmode, gerror;
  86.  
  87.     if (mode <= 3 || mode == 7){
  88.         closegraph();
  89.         textmode(mode);
  90.         directvideo = 1;    /* use direct video RAM output */
  91.     }
  92.     else{   /* use Turbo C's ability to find highest res. graph mode */
  93.         detectgraph(&gdriver, &gmode);
  94.         if (gdriver < 0){
  95.             writerr("Did not detect graphics hardware.", g_text_char_v,
  96.                 g_norm_attrib, g_emph_attrib);
  97.             return;
  98.         }
  99.         switch (gdriver){   /* translate BIOS mode #'s to Turbo C codes */
  100.         case (EGA):
  101.         case (EGA64):
  102.             if (mode == 15)
  103.                 gmode = 1;
  104.             else
  105.                 gmode = 0;
  106.             break;
  107.         case (EGAMONO):
  108.             gmode = 3;
  109.             break;
  110.         case (HERCMONO):
  111.             gmode = 0;
  112.             break;
  113.         case (VGA):
  114.             if (mode == 14)
  115.                 gmode = 0;
  116.             else if (mode == 18)
  117.                 gmode = 2;
  118.             else
  119.                 gmode = 1;
  120.             break;
  121.         case (CGA):
  122.         case (MCGA):
  123.             gmode = 4;
  124.             break;
  125.         default:
  126.             gmode = 0;
  127.         }
  128.         initgraph(&gdriver, &gmode, "");
  129.         gerror = graphresult();
  130.         if (gerror < 0){
  131.             writerr("Could not enter default graphics mode.", g_text_char_v,
  132.                 g_norm_attrib, g_emph_attrib);
  133.             return;
  134.         }
  135.         directvideo = 0;    /* use BIOS calls during graphics output*/
  136.     }
  137. #else
  138.         _setvideomode(mode);
  139. #endif
  140. }
  141.  
  142.  
  143. void
  144. dotplot(int x, int y, int color) /* sets one pixel color on screen at x, y */
  145. {
  146. #ifdef TURBOC
  147.     putpixel(x, y, color);
  148. #else
  149.     _setcolor(color);
  150.     _setpixel(x, y);
  151. #endif
  152. }
  153.  
  154.  
  155. /* draws a rectangle on screen from top left x1,y1 to bot right x2,y2 */
  156. /* if fill != 0, retangle area is filled in; else just border drawn */
  157. void
  158. draw_rectangle(int fill, int x1, int y1, int x2, int y2, int color)
  159. {
  160. #ifdef TURBOC
  161.     if (fill){
  162.         setfillstyle(SOLID_FILL, color);
  163.         bar(x1, y1, x2, y2);
  164.     }
  165.     else{
  166.         setcolor(color);
  167.         setlinestyle(SOLID_LINE, 0xFFFF, NORM_WIDTH);
  168.         rectangle(x1, y1, x2, y2);
  169.     }
  170. #else
  171.     _setcolor(color);
  172.     if (fill)
  173.         _rectangle(_GFILLINTERIOR, x1, y1, x2, y2);
  174.     else
  175.         _rectangle(_GBORDER, x1, y1, x2, y2);
  176. #endif
  177. }
  178.  
  179.  
  180. /* draws a line between x1,y1 and x2,y2 with given color */
  181. void
  182. drawline(int x1, int y1, int x2, int y2, int color)
  183. {
  184. #ifdef TURBOC
  185.     setcolor(color);
  186.     moveto(x1, y1);
  187.     lineto(x2, y2);
  188. #else
  189.     _setcolor(color);
  190.     _moveto(x1, y1);
  191.     _lineto(x2, y2);
  192. #endif
  193. }
  194.  
  195.  
  196. /* underline a string at character location x,y (not pixel x,y) */
  197. /* mode is video mode - used to determine charater heights in pixels */
  198. void
  199. wordul(int mode, char *string, int x, int y, int color)
  200. {
  201.     int chary, charx, ypos, xpos, xorcolor, nchar, dotnow;
  202.  
  203.     switch(mode){
  204.     case(7):
  205.         chary = 16;
  206.         charx = 9;
  207.         break;
  208.     case(15):
  209.     case(16):
  210.         chary = 14;
  211.         charx = 8;
  212.         break;
  213.     case(17):
  214.     case(18):
  215.         chary = 16;
  216.         charx = 8;
  217.         break;
  218.     default:
  219.         chary = 8;
  220.         charx = 8;
  221.     }
  222.     ypos = (y * chary) - 1;
  223.     xpos = (x - 1) * charx;
  224.  
  225. #ifdef TURBOC
  226.     dotnow = getpixel(xpos, ypos);
  227. #else
  228.     dotnow = _getpixel(xpos, ypos);
  229. #endif
  230.     xorcolor = dotnow ^ color;
  231.     nchar = 0;
  232.     while (*string++)
  233.         nchar++;
  234.     
  235.     drawline(xpos, ypos, xpos + (nchar * charx), ypos, xorcolor);
  236. }
  237.  
  238.  
  239.  
  240. /* draw sprite on screen at xpos,ypos */
  241. void
  242. draw_sprite(struct sprite spr, int xpos, int ypos, int line_color, 
  243.     int back_color)
  244. {
  245.     int i, j, k, x, y;
  246.     char *sp;
  247.     unsigned int drawbyte, dotput;
  248.  
  249.     y = ypos;
  250.     sp = spr.sdata;
  251.     
  252.     for(i = 0; i < spr.stall; i++){
  253.         x = xpos;
  254.         for(j = 0; j < spr.swide; j++){
  255.             drawbyte = *sp++;
  256.             for(k = 0; k < 8; k++){
  257.                 dotput = (drawbyte & 0x80);
  258. #ifdef TURBOC
  259.                 if (dotput)
  260.                     putpixel(x++, y, line_color);
  261.                 else
  262.                     putpixel(x++, y, back_color);
  263. #else
  264.                 if(dotput)
  265.                     _setcolor(line_color);
  266.                 else
  267.                     _setcolor(back_color);
  268.                 _setpixel(x++, y);
  269. #endif
  270.                 drawbyte <<= 1;
  271.             }
  272.         }
  273.         y++;
  274.     }
  275. }
  276.  
  277.  
  278. /* XOR sprite on screen at xpos,ypos */
  279. /* color is color spite is to have over a black background */
  280. void
  281. xsprite(struct sprite spr, int xpos, int ypos, int color)
  282. {
  283.     int i, j, k, x, y;
  284.     unsigned int drawbyte, dotnow;
  285.     char *sp;
  286.  
  287.     y = ypos;
  288.     sp = spr.sdata;
  289.     
  290.     for(i = 0; i < spr.stall; i++){
  291.         x = xpos;
  292.         for(j = 0; j < spr.swide; j++){
  293.             drawbyte = *sp++;
  294.             for(k = 0; k < 8; k++){
  295.                 if(drawbyte & 0x80){
  296. #ifdef TURBOC
  297.                     dotnow = getpixel(x, y);
  298.                     putpixel(x, y, dotnow ^ color);  /* XOR color */
  299. #else
  300.                     dotnow = _getpixel(x, y);
  301.                     _setcolor(dotnow ^ color);
  302.                     _setpixel(x, y);
  303. #endif
  304.                 }
  305.                 x++;
  306.                 drawbyte <<= 1;
  307.             }
  308.         }
  309.         y++;
  310.     }
  311. }
  312.