home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / PROG_GEN / FACETV.ZIP / GRAFXSCR.C < prev    next >
C/C++ Source or Header  |  1994-01-04  |  12KB  |  464 lines

  1. /************************************************************************
  2. **
  3. ** @(#)grafxscr.c    10/08/93    Chris Ahlstrom
  4. **
  5. **    A module using Borland video functions, but meant to isolate
  6. ** this "Borlandness" from caller programs.  Essentially a much tighter
  7. ** version of screen.c and screen.h.  Still requires many many functions
  8. ** in the Borland graphics library.
  9. **
  10. **    Used mainly when desiring to implement graphics functions.
  11. ** Some of the routines are needed in order to determine how many
  12. ** pixels or character cells are available.
  13. **
  14. **    This module is self-contained -- it does not refer to any
  15. ** other structures or data types.  Hence, any program can use it,
  16. ** without the need for a seemingly endless list of secondary
  17. ** include files.
  18. **
  19. **    See bios_vid.h for more information.
  20. **
  21. *************************************************************************/
  22.  
  23. #define GRAFXSCR_c
  24.  
  25. #include <dos.h>
  26. #include <graphics.h>
  27. #include <alloc.h>
  28. #include <stdio.h>
  29. #include <string.h>
  30.  
  31. #include "bios_vid.h"
  32. #include "grafxscr.h"
  33.  
  34.  
  35. /************************************************************************
  36. ** graphicsScreen()
  37. **
  38. **    Puts the main adapter in graphics mode, assuming the VGA
  39. ** driver is part of the executable module (see the UTIL.DOC provided
  40. ** by Borland, I believe).
  41. **
  42. **    See initBGI() for a more universally applicable routine.
  43. **
  44. *************************************************************************/
  45.  
  46. GraphicsError
  47. graphicsScreen
  48. (
  49.     Screen *s
  50. )
  51. {
  52.     int gdrvr = (int) DETECT;    /* start with auto-detection        */
  53.     int gmode = (int) VGALO;    /* start with a "zero" value        */
  54.     int *gd = (int *)&gdrvr;    /* use DetectGraph (if 0), or driver    */
  55.     int *gm = (int *)&gmode;    /* if not zero, this is graphics mode    */
  56.     char *dpath = "";        /* location to look for the drivers    */
  57.     int error;            /* a return code            */
  58.  
  59.     if (registerbgidriver(EGAVGA_driver) < 0)
  60.     error = grInvalidDriver;
  61.     else if (registerbgifont(sansserif_font) < 0)
  62.     error = grInvalidFont;
  63.     else
  64.     {
  65.     *gd = DETECT;
  66.     initgraph(gd, gm, dpath);    /* initialize graphics board    */
  67.     error = graphresult();        /* check the status        */
  68.     if (s != NULL)
  69.     {
  70.         s->screen_gmax_x = getmaxx();
  71.         s->screen_gmax_y = getmaxy();
  72.     }
  73.     }
  74.     return (GraphicsError) error;    /* return possible error code    */
  75. }
  76.  
  77.  
  78. /************************************************************************
  79. ** setscreen()
  80. **
  81. **    Sets the video mode parameters.  Provides an alternative to
  82. ** setGraphicsScreen() in grafxlog.c, so that the user doesn't need
  83. ** to use the Screen structure.
  84. **
  85. *************************************************************************/
  86.  
  87. #pragma warn -par
  88.  
  89. int
  90. setscreen
  91. (
  92.     int mode,
  93.     int page,
  94.     int attribute,
  95.     int backattribute,
  96.     int rows,
  97.     int columns,
  98.     int currow,
  99.     int curcolumn,
  100.     int curstart,
  101.     int curend
  102. )
  103. {
  104.     union REGS regs;
  105.     int type;
  106.     int err = 0;
  107.  
  108.     /********************************************************************
  109.     ** Set the page number, if in the proper range.
  110.     *********************************************************************/
  111.  
  112.     if (page >= 0 && page < 8)
  113.     {
  114.     regs.h.ah = VIDEO_PAGE_SELECT;
  115.     regs.h.al = page;
  116.     int86(VIDEO_BIOS, ®s, ®s);    /* set video page no.    */
  117.     }
  118.     else
  119.     err = 1;
  120.  
  121.     /********************************************************************
  122.     ** Now set the mode of the page
  123.     *********************************************************************/
  124.  
  125.     if (mode > 80)            /* supports extended modes    */
  126.     {
  127.     mode += VID_TEXT_43_50;        /* IS THIS RIGHT? NO...        */
  128.     }
  129.     regs.h.ah = VIDEO_MODE;
  130.     regs.h.al = mode;
  131.     int86(VIDEO_BIOS, ®s, ®s);    /* set cursor position        */
  132.  
  133.     /********************************************************************
  134.     ** Set the cursor type.
  135.     *********************************************************************/
  136.  
  137.     type = curstart;            /* CH = starting (top) line    */
  138.     type = (type << 8) + curend;    /* CL = ending (bottom) line    */
  139.     regs.h.ah = VIDEO_CURSOR_SIZE;
  140.     regs.x.cx = type;            /* position of cursor bounds    */
  141.     int86(VIDEO_BIOS, ®s, ®s);    /* set cursor position        */
  142.  
  143.     /********************************************************************
  144.     ** Set the cursor position.  Use desired video page.
  145.     *********************************************************************/
  146.  
  147.     regs.h.ah = VIDEO_CURSOR_POSITION;
  148.     regs.h.bh = page;
  149.     regs.h.dh = currow;
  150.     regs.h.dl = curcolumn;
  151.     int86(VIDEO_BIOS, ®s, ®s);    /* set cursor position        */
  152.  
  153.     setcolor(attribute);
  154.     setbkcolor(backattribute);
  155.  
  156.     /********************************************************************
  157.     ** We cannot set the rest, so we dummy them for now.
  158.     ********************************************************************/
  159.  
  160.     rows    = 0;
  161.     columns    = 0;
  162.  
  163.     return err;
  164. }
  165.  
  166. #pragma warn .par
  167.  
  168.  
  169. /************************************************************************
  170. ** getScreenType()
  171. **
  172. **    Replaced by getGraphicsModes() in grafxlog.c...
  173. **
  174. *************************************************************************/
  175.  
  176.  
  177.  
  178. /************************************************************************
  179. **
  180. ** The routines above are essentially unchanged from screen.c, though
  181. ** there might be some additional functionality.
  182. **
  183. ** The routines below attempt to handle more than one adapter card.
  184. **
  185. *************************************************************************/
  186.  
  187. static AdapterSelect currentAdapter;    /* selects video card to use    */
  188. static Screen *currentScreen;        /* equal to one of following    */
  189. static Screen *mainScreen;        /* points to user's setup    */
  190. static Screen *altScreen;        /* points to user's setup    */
  191.  
  192.  
  193. static char bgiPath[] = "C:\BC\BGI";    /* we'll use env variables later*/
  194. #define ADAPTER_STRING    (currentAdapter ? "alternate" : "main")
  195.  
  196.  
  197. Screen *
  198. getScreenStructure (void)
  199. {
  200.     Screen *s = (Screen *) malloc(sizeof(Screen));
  201.     if (s != NULL)
  202.     s->adapter = ADAPTER_INACTIVE;    /* flag that it ain't ready yet    */
  203.  
  204.     return s;
  205. }
  206.  
  207.  
  208. void
  209. freeScreenStructure
  210. (
  211.     Screen *s
  212. )
  213. {
  214.     free((void *)s);
  215. }
  216.  
  217.  
  218. void
  219. initScreens
  220. (
  221.     Screen *main,
  222.     Screen *alt
  223. )
  224. {
  225.     if (main != NULL)
  226.     mainScreen = main;
  227.     if (alt != NULL)
  228.     altScreen = alt;
  229. }
  230.  
  231.  
  232. int
  233. initBGI
  234. (
  235.     Screen *s,                /* all screen parameters    */
  236.     int alt_adapter,            /* adapter to use (0 or 1)    */
  237.     int adapter_type            /* kind of adapter        */
  238. )
  239. {
  240.     int errcode = 0;            /* return code for errors    */
  241.     int xasp, yasp;            /* for reading the aspect ratio    */
  242.     char *bgi_path = &bgiPath[0];    /* path to find BGI support    */
  243.     int *gm, *gd;            /* for initgraph()        */
  244.  
  245.     if (s->adapter != ADAPTER_INACTIVE)
  246.     {
  247.     printf(ERR_SCREEN_BAD);
  248.     errcode = 2;
  249.     }
  250.     else
  251.     {
  252.     s->adapter = ADAPTER_MAIN;        /* start with main card    */
  253.     s->screen_driver = adapter_type;    /* get type of monitor    */
  254.     if (adapter_type != DETECT)
  255.     {
  256.         if (adapter_type == HERCMONO)
  257.         s->screen_mode = HERCMONOHI;
  258.         if (alt_adapter == ADAPTER_ALT)    /* alternate adapter?    */
  259.         toalternate();            /* switch to it        */
  260.     }
  261.  
  262.     s->adapter = alt_adapter;        /* choose the adapter    */
  263.     gd = (int *) &s->screen_driver;
  264.     gm = (int *) &s->screen_mode;
  265.     initgraph(gd, gm, bgi_path);
  266.     s->error_code = graphresult();        /* result of initialize    */
  267.     if (s->error_code != grOk)        /* init error occurred    */
  268.     {
  269.         if (alt_adapter == ADAPTER_ALT)
  270.         tomain();            /* can't use alternate!    */
  271.         printf
  272.         (
  273.         "%%Graphics error: %s, %s adapter\n",
  274.         grapherrormsg(s->error_code), ADAPTER_STRING
  275.         );
  276.         errcode = 1;
  277.     }
  278.     else
  279.     {
  280.         getpalette(&s->palette);        /* read card palette    */
  281.         s->screen_colors = getmaxcolor()+1;    /* max number of colors    */
  282.         s->screen_attribute        = getcolor();
  283.         s->screen_backattribute = getbkcolor();
  284.  
  285.         s->screen_gmax_x = getmaxx();
  286.         s->screen_gmax_y = getmaxy();    /* read size of screen    */
  287.  
  288.         getaspectratio(&xasp, &yasp);    /* read hardware aspect    */
  289.         s->screen_aspect = (double) xasp /
  290.         (double)yasp;            /* correction factor    */
  291.     }
  292.     }
  293.     return errcode;
  294. }
  295.  
  296.  
  297. /************************************************************************
  298. **
  299. **    We define VECTOR_FONT to make it easy to try out some
  300. ** other fonts.  Optionally, can also change the path where
  301. ** the fonts are stored.
  302. **
  303. *************************************************************************/
  304.  
  305. #define BOX_FONT    TRIPLEX_FONT    /* use Borland's fonts        */
  306. #define BOX_FONT_SIZE    4        /* size of normal letters    */
  307.  
  308. int
  309. fontinit
  310. (
  311.     Screen *s
  312. )
  313. {
  314.     int font = BOX_FONT;
  315.     int direction = HORIZ_DIR;
  316.     int charsize = BOX_FONT;
  317.     int rcode;
  318.  
  319.     if (s->adapter != ADAPTER_INACTIVE)
  320.     {
  321.     printf(ERR_SCREEN_BAD);
  322.     rcode = 1;
  323.     }
  324.     else
  325.     rcode = changetextstyle(s, font, direction, charsize);
  326.     return rcode;
  327. }
  328.  
  329.  
  330. /************************************************************************
  331. ** changetextstyle()
  332. **
  333. **    Similar to Borland's settextstyle(), but checks for
  334. ** errors that might occur while loading the font file.
  335. **
  336. *************************************************************************/
  337.  
  338. int
  339. changetextstyle
  340. (
  341.     Screen *s,
  342.     int font,
  343.     int direction,
  344.     int charsize
  345. )
  346. {
  347.     int errorcode;
  348.  
  349.     if (s->adapter == ADAPTER_MAIN)
  350.     tomain();
  351.     else if (s->adapter == ADAPTER_ALT)
  352.     toalternate();
  353.     else
  354.     return 1;
  355.  
  356.     graphresult();            /* clear the error code        */
  357.     settextstyle            /* set the current output style    */
  358.     (
  359.     font, direction, charsize
  360.     );
  361.     errorcode = graphresult();        /* check result         */
  362.     if (errorcode != grOk)        /* if error occured        */
  363.     {
  364.     closegraph();
  365.     printf
  366.     (
  367.         "%%Graphics error: %s, %s adapter\n",
  368.         grapherrormsg(errorcode), ADAPTER_STRING
  369.     );
  370.     }
  371.     return errorcode;
  372. }
  373.  
  374.  
  375. void
  376. video_main (void)
  377. {
  378.     asm push ds
  379.     asm push bp
  380.     asm mov bp,0
  381.     asm mov ds,bp
  382.     asm mov bp,449h
  383.     asm mov al,7
  384.     asm mov ds:[bp],al
  385.     asm mov bp,463h
  386.     asm mov bx,3b4h
  387.     asm mov ds:[bp],bx
  388.     asm pop bp
  389.     asm pop ds
  390. }
  391.  
  392.  
  393. void
  394. video_alternate (void)
  395. {
  396.     asm push ds
  397.     asm push bp
  398.     asm mov bp,0
  399.     asm mov ds,bp
  400.     asm mov bp,449h
  401.     asm mov al,20
  402.     asm mov ds:[bp],al
  403.     asm mov bp,463h
  404.     asm mov bx,3d4h
  405.     asm mov ds:[bp],bx
  406.     asm pop bp
  407.     asm pop ds
  408. }
  409.  
  410.  
  411. void
  412. tomain (void)
  413. {
  414.     video_main();
  415.     currentAdapter = ADAPTER_MAIN;
  416.     currentScreen = mainScreen;
  417. }
  418.  
  419.  
  420. void
  421. toalternate (void)
  422. {
  423.     video_alternate();
  424.     currentAdapter = ADAPTER_ALT;
  425.     currentScreen = altScreen;
  426. }
  427.  
  428.  
  429. /***********************************************************************
  430. ** init1()
  431. ************************************************************************/
  432.  
  433. int
  434. init1
  435. (
  436.     Screen *s,
  437.     char *basefont
  438. )
  439. {
  440.     int font = BOX_FONT;
  441.     int direction = HORIZ_DIR;
  442.     int charsize = BOX_FONT;
  443.     int rcode;
  444.  
  445.     if (basefont != NULL && *basefont != '\0')
  446.     {
  447.     if (strcmpi("TripleXFont", basefont) == 0)
  448.         font = TRIPLEX_FONT;
  449.     else if (strcmpi("DefaultFont", basefont) == 0)
  450.         font = DEFAULT_FONT;
  451.     else if (strcmpi("SmallFont", basefont) == 0)
  452.         font = SMALL_FONT;
  453.     else if (strcmpi("SansSerifFont", basefont) == 0)
  454.         font = SANS_SERIF_FONT;
  455.     else if (strcmpi("GothicFont", basefont) == 0)
  456.         font = GOTHIC_FONT;
  457.     }
  458.     cleardevice();
  459.     rcode = changetextstyle(s, font, direction, charsize);
  460.     return rcode;
  461. }
  462.  
  463.  
  464.