TURBO C GRAPHICS INFORMATION 卜卜卜卜卜卜卜卜卜卜卜卜卜卜卜卜卜卜卜卜 This file contains information about the turbo c graphics that is not contained in the additions and enhancements manual. please read this file in its entirety, referr ing to it when you encounter probl ems not addressed in the Additions and enhancements manual TABLE OF CONTENTS 卜卜卜卜卜卜卜F卜卜卜卜卜 1. Adding GRAPHICS. Lib to the Turbo C Librar ies 2. Switching to Text Mode 3. Internal Graphics Dr iver pointers 4. Graphics Examples on Ga or Hercules 5. IBM 8514 Support 6. Compaq III Portable 7. Loading dr ivers and Fonts at runtime 8. How to Compi le BGIDEMO 1. ADDING GRAPHICS. LIB TO THE TURBO C LIBRARIES 卜卜卜卜卜卜卜F卜卜卜卜卜卜卜卜F卜卜卜卜卜F卜卜卜卜卜卜卜卜F卜卜卜卜卜F卜F When you use graphics in a turbo c program you either have to include graphics. I ib on the command l ine ( if using tcc tcc foo. c graphics. lib")or create a pro ject with your program and graphics. lib in it (tc). To avoid doing this, you can add the graphics library modules to the standard c* LiB librar ies using tib. This may be accomp l i shed eas i ly by one or more of the fol low ing tib commands (don t leave out the .I ib extens ion on graphics. I ib) tib cs +graphics. lib (smal I model I ibrary tib cc +graphics. lib (compact model l ibrary) ib cm +graphics. lib (med i um model I ibrary) phics. I ib odel l ibrary) tib ch +graphics. lib (huge model l ibrary) It is probably a good idea to add the graphics modules to ali the tandard l ibrary files but if you know you will be us ing only some memory mode l s with graphics programs, or you are short on
TURBO C GRAPHICS INFORMATION トトトトトトトトトトトトトトトトトトトトトトトトトトトト This file contains information about the Turbo C graphics that is not contained in the Additions and Enhancements manual. Please read this file in its entirety, referring to it when you encounter problems not addressed in the Additions and Enhancements manual. TABLE OF CONTENTS トトトトトトトトトトトトトトトトト 1. Adding GRAPHICS.LIB to the Turbo C Libraries 2. Switching to Text Mode 3. Internal Graphics Driver Pointers 4. Graphics Examples on CGA or Hercules 5. IBM 8514 Support 6. Compaq III Portable 7. Loading Drivers and Fonts at Runtime 8. How to Compile BGIDEMO 1. ADDING GRAPHICS.LIB TO THE TURBO C LIBRARIES トトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトト When you use graphics in a Turbo C program you either have to include graphics.lib on the command line (if using TCC: "tcc foo.c graphics.lib") or create a project with your program and graphics.lib in it (TC). To avoid doing this, you can add the graphics library modules to the standard C*.LIB libraries using tlib. This may be accomplished easily by one or more of the following tlib commands (don't leave out the ".lib" extension on graphics.lib): tlib cs +graphics.lib (small model library) tlib cc +graphics.lib (compact model library) tlib cm +graphics.lib (medium model library) tlib cl +graphics.lib (large model library) tlib ch +graphics.lib (huge model library) It is probably a good idea to add the graphics modules to all the standard library files, but if you know you will be using only some memory models with graphics programs, or you are short on
disk space you might choose to change only some l ibrary files. Your current directory must be the directory where you keep the c I ibrar ies, or you must include complete paths in the tl ib command I ine(s). For example d turbo¥lib tib cm +graphics. lib tib turbocYlibYcm +fturbockl ibYgraphics l ib lote that tl ib will rename the or iginal C* lib library to c*. bAK: you might want to delete this file, or keep it around for reference 2. SWITCHING TO TEXT MODE 卜卜卜卜卜卜卜卜卜卜卜卜卜卜卜F卜卜 There are two rout ines that wil switch the screen to text mode textmode and restorer tmode.textmode shou ld on ly be used when the screen is in the text mode (presumably to change to a different text mode),restorecrtmode should only be used whe the screen is in graphics mode (to temporar i ly escape to text mode). These are the only contexts where textmode and restorecrtmode may be used 3. INTERNAL GRAPHICS DRIVER POINTERS 卜卜卜卜卜卜卜卜卜F卜卜卜F卜卜卜卜卜卜卜F卜卜卜卜卜卜F卜 There are several far function pointers that are used by the graphics ibrary to access the low-level graphics routines. these pointers are set to point to the internal graphics dr ivers whe initgraph" is called. Subsequent cal is to the graphics routines will then invoke these internal dr ivers by cal ls through the function pointers. The pointers are not tested for val idity (to handle the case when a graphics routine might be cal led before nitgraph), as that would introduce a lot of extra code, and s low down those graphics routines In the compact, medi um, large, and huge models, these pointers
disk space, you might choose to change only some library files. Your current directory must be the directory where you keep the C libraries, or you must include complete paths in the tlib command line(s). For example: cd \turboc\lib tlib cm +graphics.lib or tlib \turboc\lib\cm +\turboc\lib\graphics.lib Note that tlib will rename the original C*.LIB library to C*.BAK; you might want to delete this file, or keep it around for reference. 2. SWITCHING TO TEXT MODE トトトトトトトトトトトトトトトトトトトトトトトトト There are two routines that will switch the screen to text mode: "textmode" and "restorecrtmode". "textmode" should only be used when the screen is in the text mode (presumably to change to a different text mode), "restorecrtmode" should only be used when the screen is in graphics mode (to temporarily escape to text mode). These are the only contexts where textmode and restorecrtmode may be used. 3. INTERNAL GRAPHICS DRIVER POINTERS トトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトト There are several far function pointers that are used by the graphics library to access the low-level graphics routines. These pointers are set to point to the internal graphics drivers when "initgraph" is called. Subsequent calls to the graphics routines will then invoke these internal drivers by calls through the function pointers. The pointers are not tested for validity (to handle the case when a graphics routine might be called before initgraph), as that would introduce a lot of extra code, and slow down those graphics routines. In the compact, medium, large, and huge models, these pointers
are therefore initial ized to point to a rout ine that will displ ay an error message, and abort the progr am (via exito), in case a graphics routine is cal led before initgraph In the tiny mode i there is no way to initialize far pointers to point to a routine in the code. Furthermore, the tiny model shares the standard ibrary with the small mode l. Therefore, the graphics dr iver pointers cannot be initial ized in the tiny and smal I models, and programs that attempt to call a graphics routine before cal l ing initgraph will most l ikely crash the machine The fol lowing graphics rout i nes may be cal led before initgraph (or after clo segraph): detectgraph, getmoderange setgraphbufs ize registerbgidr iver and registerfarbgidr i ver, registerbgifont and registerfarbgifont 4. GRAPHICS EXAMPLES ON CGA OR HERCULES 卜卜卜卜卜卜卜卜F卜卜卜F卜卜卜卜卜卜卜F卜卜卜卜卜卜F卜卜卜F卜 Most of the graphics examples in the manual (addendum) will work best with an eGa or vGa adapter. When run on a cga or monochrome adapter, they mi ght not produce the expected resul ts. Therefore when running on non-EGA/VGA hardware, use the value 1"(one)in place of the symbol ic color constants, and use the fol low ing code as the example for the arc, circle, ellipse getar coords tio. and lice rout in #inc l ude main int grapher iver =deTEct, graphmode truct ar ccoordstype arcinfo lor initgraph &graphdr iver, &graphmode / draw a g0 degree arc with radius of 50* arc(100.120,0.89,50)
are, therefore, initialized to point to a routine that will display an error message, and abort the program (via exit()), in case a graphics routine is called before initgraph. In the tiny model there is no way to initialize far pointers to point to a routine in the code. Furthermore, the tiny model shares the standard library with the small model. Therefore, the graphics driver pointers cannot be initialized in the tiny and small models, and programs that attempt to call a graphics routine before calling initgraph will most likely crash the machine. The following graphics routines may be called before initgraph (or after closegraph): detectgraph, getmoderange, setgraphbufsize, registerbgidriver and registerfarbgidriver, registerbgifont and registerfarbgifont. 4. GRAPHICS EXAMPLES ON CGA OR HERCULES トトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトト Most of the graphics examples in the manual (addendum) will work best with an EGA or VGA adapter. When run on a CGA or monochrome adapter, they might not produce the expected results. Therefore, when running on non-EGA/VGA hardware, use the value "1" (one) in place of the symbolic color constants, and use the following code as the example for the arc, circle, ellipse, getarccoords, getaspectratio, and pieslice routines: #include main() { int graphdriver = DETECT, graphmode; struct arccoordstype arcinfo; int xasp, yasp; long xlong; initgraph(&graphdriver, &graphmode, ""); /* draw a 90 degree arc with radius of 50 */ arc( 100, 120, 0, 89, 50 );
/ get the coordinates of the arc and connect ends * getarccoords(& arcinfo ) I ine( arcinfo. xstart arcinfo. start arcinfo. xend arcinfo. yend /* draw a circle circle(100,120.80) / draw an el l ipse ins ide the circle * ellipse(100,120,.0,359,80,20); setfillstyle( HATCh_FILL, 1) pieslice(200,50,0.134.49) setfi l lst le( sLaSH fILL, 1) pieslice(200,50,135,225,49) setfi l style( WIDEDOT FILL, 1 pies ice(200,50,225,360,49) / draw a square" rectangle * getaspectratio(& xasp, asp *(long) yasp )/(long)xasp rectangle(0, 0,(int)xiong, 50); etch: closegraph o 5.IBM8514 卜卜卜卜卜卜卜F卜卜卜卜卜F卜卜 An additional dr iver has been inc luded with the bgi graphics ibrary. This dr iver interfaces to the IBM-8514 graphics card This is a new, hi-res graphics card capable of resolut ions up to 1024x768 pixels, and a color palette of 256 colors from a list of 256K colors. The dr iver file name is iBM8514 BGI The IBM 8514 graphics card cannot be proper ly autodetected (it will be recognized as vga by the autodetection logic). Therefore, to use the IBM 8514 card, the graphdr iver var iab le must be ass i gned the value I BM8514 (which is def ined in gRAPHICS. h)when initgraph s cal led. Detectgraph, or detect with initgraph should not be used with the IBM 8514 (unless the emulated vGa mode is des ired)
/* get the coordinates of the arc and connect ends */ getarccoords( & arcinfo ); line( arcinfo.xstart, arcinfo.ystart, arcinfo.xend, arcinfo.yend ); /* draw a circle */ circle( 100, 120, 80 ); /* draw an ellipse inside the circle */ ellipse( 100, 120, 0, 359, 80, 20 ); /* draw and fill a pieslice */ setfillstyle( HATCH_FILL, 1 ); pieslice( 200, 50, 0, 134, 49 ); setfillstyle( SLASH_FILL, 1 ); pieslice( 200, 50, 135, 225, 49 ); setfillstyle( WIDEDOT_FILL, 1 ); pieslice( 200, 50, 225, 360, 49 ); /* draw a "square" rectangle */ getaspectratio( & xasp, & yasp ); xlong = ( 50L * (long) yasp ) / (long) xasp; rectangle( 0, 0, (int) xlong, 50 ); getch(); closegraph(); } 5. IBM 8514 SUPPORT トトトトトトトトトトトトトトトトトトト An additional driver has been included with the BGI graphics library. This driver interfaces to the IBM-8514 graphics card. This is a new, hi-res graphics card capable of resolutions up to 1024x768 pixels, and a color palette of 256 colors from a list of 256K colors. The driver file name is IBM8514.BGI. The IBM 8514 graphics card CANNOT be properly autodetected (it will be recognized as VGA by the autodetection logic). Therefore, to use the IBM 8514 card, the graphdriver variable must be assigned the value IBM8514 (which is defined in GRAPHICS.H) when initgraph is called. Detectgraph, or DETECT with initgraph should not be used with the IBM 8514 (unless the emulated VGA mode is desired)
The supported modes of the IBM-8514 card are IBM8514L0( 640x480 pixel s), and IBM8514HI (1024x768 pixels). Both mode constants are def ined in GRAPHICS. H The ibm-8514 uses three 6-bit values to define colors. there is a 6-bit Red, Green, and blue component for each def ined color. To al low the user to def ine colors for the iBM-8514. a new routine was added to the bgi library. the rout ine is prototyped in GRAPHICS. H as fol lows void far setrgbpalette int colonum, int red, int green, int blue); The ar gument colonum def ines the palette entry to be loaded. colonum is an integer from 0-255(decimal). the ar guments red green, and blue def ine the component colors of the palette entry Only the lower byte of these values is used and out of this byte only the 6 most significant bits are loaded in the palette. The other palette mani pulation routines of the graphics library may not be used with the IBM-8514 driver (i. e, setal palette setpalette getpalette) For compatab i l ity with the bal ance of the IBM graphics adapters the bgi dr ver def ines the first 16 palette entr ies of the IBM-8514 to the defaul t colors of the EGA/VGA. these values can be used as is, or changed us ing the setrgbpalette routine The floodfili rout ine will not work with the ibm 8514 dr iver 6. COMPAQ III PORTABLE 卜卜卜卜卜卜卜卜卜F卜卜卜卜卜卜F卜卜 If you are us ing the compaq iii portable, enter the fol lowing command at the dos prompt before try ing to use the 400 line mode SET SEL=CGA 7. LOADING DRIVERS AND FONTS AT RUNT IME 卜卜卜卜卜卜卜卜卜卜卜卜卜卜卜卜卜卜卜卜卜卜卜卜卜卜卜卜
The supported modes of the IBM-8514 card are IBM8514LO (640x480 pixels), and IBM8514HI (1024x768 pixels). Both mode constants are defined in GRAPHICS.H. The IBM-8514 uses three 6-bit values to define colors. There is a 6-bit Red, Green, and Blue component for each defined color. To allow the user to define colors for the IBM-8514, a new routine was added to the BGI library. The routine is prototyped in GRAPHICS.H as follows: void far setrgbpalette(int colornum, int red, int green, int blue); The argument colornum defines the palette entry to be loaded. colornum is an integer from 0-255 (decimal). The arguments red, green, and blue define the component colors of the palette entry. Only the lower byte of these values is used, and out of this byte, only the 6 most significant bits are loaded in the palette. The other palette manipulation routines of the graphics library may not be used with the IBM-8514 driver (i.e., setallpalette, setpalette, getpalette). For compatability with the balance of the IBM graphics adapters, the BGI driver defines the first 16 palette entries of the IBM-8514 to the default colors of the EGA/VGA. These values can be used as is, or changed using the setrgbpalette routine. The floodfill routine will not work with the IBM 8514 driver. 6. COMPAQ III PORTABLE トトトトトトトトトトトトトトトトトトトトトト If you are using the Compaq III Portable, enter the following command at the DOS prompt before trying to use the 400 line mode: SET SEL=CGA 7. LOADING DRIVERS AND FONTS AT RUNTIME トトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトトト
In addition to i inking in dr ivers and fonts (converted by bgIoBj) you may load the dr iver and or font fi les directly into memory at runtime, and then register them through the register farbgidr iver and/or register farbgi font routines. the fol low ing example I lustrates this techique /* example of loading a font file into memory * #include #inc lude #inc lude #include #inc lude void main o void gothic_ fontp: / points to the font buffer in memory * hand le /* file handle used for 1/ 0 * uns i gned fsize: /* size of file (and buffer)*/ nt error code int grapher iver int graphmode /* open font file * handle open("GOTH. CHR", 0_RDONLY 0_BINARY i f ( handle ==-1) pr intf unable to open font file GOTH. CHR'Yn") fsize filelength (handle) /* find out size of the file * gothic font mal loc(fsize) /* al locate buffer if (gothic font = NULL) pr intf unable to al locate memory for font file GOTH. CHR'Yn): exit(1)
In addition to linking in drivers and fonts (converted by BGIOBJ), you may load the driver and/or font files directly into memory at runtime, and then register them through the registerfarbgidriver and/or registerfarbgifont routines. The following example illustrates this techique: /* example of loading a font file into memory */ #include #include #include #include #include #include #include #include void main() { void * gothic_fontp; /* points to the font buffer in memory */ int handle; /* file handle used for I/O */ unsigned fsize; /* size of file (and buffer) */ int errorcode; int graphdriver; int graphmode; /* open font file */ handle = open("GOTH.CHR", O_RDONLY|O_BINARY); if (handle == -1) { printf("unable to open font file 'GOTH.CHR'\n"); exit(1); }; fsize = filelength(handle); /* find out size of the file */ gothic_fontp = malloc(fsize); /* allocate buffer */ if (gothic_fontp == NULL) { printf("unable to allocate memory for font file 'GOTH.CHR'\n"); exit(1);
/* read font into memory * if (read (handle gothic_fontp, fsize)l= fsize pr intf c unable to read font file GOTH. CHR'Yn") exit(1) close(handle) /* close font file * ter font * if (register farbgi font (gothic font)I= GOTHIC FOND) pr intf unable to register font file GOTH. cHR'yn") exit(1) detect and initial ize grapher iver = DETECT initgraph(&graphdr iver, &graphmode ." errorcode graphresulto if errorcode I= grOk) pr intf( graphics error: %sYn, grapherrormsg(errorcode)) t(1) settext justify ( center teXt, CENTER_TEXT) settextstyle(GOTHIC_ FONT, HORIZ DIR, 4) outtextxy( getmaxxo/2. getmaxyo/ 2."Bor land Graph gotcha /* hit a key to terminate closegraph o shut down graphic system * 8. HOW TO COMPILE BGIDEMO 卜卜卜卜卜卜卜F卜卜卜卜卜卜卜F卜卜卜 With TC. EXE 1. Run TC. eXe
}; /* read font into memory */ if (read(handle, gothic_fontp, fsize) != fsize) { printf("unable to read font file 'GOTH.CHR'\n"); exit(1); }; close(handle); /* close font file */ /* register font */ if (registerfarbgifont(gothic_fontp) != GOTHIC_FONT) { printf("unable to register font file 'GOTH.CHR'\n"); exit(1); }; /* detect and initialize graphix */ graphdriver = DETECT; initgraph(&graphdriver, &graphmode, ".."); errorcode = graphresult(); if (errorcode != grOk) { printf("graphics error: %s\n",grapherrormsg(errorcode)); exit(1); }; settextjustify(CENTER_TEXT, CENTER_TEXT); settextstyle(GOTHIC_FONT, HORIZ_DIR, 4); outtextxy( getmaxx() / 2, getmaxy() / 2, "Borland Graphics Interface"); getch(); /* hit a key to terminate */ closegraph(); /* shut down graphics system */ } 8. HOW TO COMPILE BGIDEMO トトトトトトトトトトトトトトトトトトトトトトトトト With TC.EXE: 1. Run TC.EXE
2. In the project pu l I down menu speci fy the project name BGIDEMO PRJ 3.From the main menu select the Run option Compile from dos with the fol lowing command line tcc bg idemo graphics. lib Note that bgIDEMO will not run in the tiny mode l because of that nodeS memory I imitations END
2. In the Project pulldown menu specify the project name "BGIDEMO.PRJ" 3. From the main menu select the Run option With TCC.EXE: Compile from DOS with the following command line: TCC bgidemo graphics.lib Note that BGIDEMO will not run in the tiny model because of that model's memory limitations. END トトト