============
GRAPHICS.H
============
Functions
=========
arc
bar
bar3d
circle
cleardevice
clearviewport
closegraph
detectgraph
drawpoly
ellipse
fillellipse
fillpoly
floodfill
getarccoords
getaspectratio
getbkcolor
getcolor
getdefaultpalette
getdrivername
getfillpattern
getfillsettings
getgraphmode
getimage
getlinesettings
getmaxcolor
getmaxmode
getmaxx
getmaxy
getmodename
getmoderange
getpalette
getpalettesize
getpixel
gettextsettings
getviewsettings
getx
gety
graphdefaults
grapherrormsg
_graphfreemem
_graphgetmem
graphresult
imagesize
initgraph
installuserdriver
installuserfont
line
linerel
lineto
moverel
moveto
outtext
outtextxy
pieslice
putimage
putpixel
rectangle
registerbgidriver
registerfarbgidriver
registerbgifont
registerfarbgifont
restorecrtmode
sector
setactivepage
setallpalette
setaspectratio
setbkcolor
setcolor
setfillpattern
setfillstyle
setgraphbufsize
setgraphmode
setlinestyle
setpalette
setrgbpalette
settextjustify
settextstyle
setusercharsize
setviewport
setvisualpage
setwritemode
textheight
textwidth
Constants, data types, and global variables
===========================================
arccoordstype
CGA_COLORS
COLORS
EGA_colors
fill_patterns
fillsettingstype
font_names
graphics_drivers
graphics_errors
graphics_modes
HORIZ_DIR
line_styles
line_widths
linesettingstype
MAXCOLORS
palettetype
pointtype
putimage_ops
text_just
text directions
textsettingstype
USER_CHAR_SIZE
VERT_DIR
viewporttype
See Also
========
List of all Header files
USER_CHAR_SIZE
================
User-defined charsize (charsize = character
magnification in graphics output)
Values define
Value| Display size of 8 x 8 bit-mapped fonts
-----+----------------------------------------
1 | Display chars in 8 x 8 box onscreen
2 | Display chars in 16 x 16 box onscreen
... | ...
10 | Display chars in 80 x 80 box onscreen
See Also:
gettextsettings
settextstyle
setusercharsize
textsettingstype
HORIZ_DIR and VERT_DIR <GRAPHICS.H>
========================
#defines that set the direction of graphics output
Name |Value| Direction
-----------+-----+---------------------------
HORIZ_DIR | 0 | Left to right
VERT_DIR | 1 | Bottom to top (rotated 90
| | degrees counterclockwise)
See Also:
gettextsettings
settextstyle
setusercharsize
MAXCOLORS <GRAPHICS.H>
===========
Defines the maximum number of color entries
for the color array field in the palettetype.
See Also:
setallpalette
===================
Enumeration Types
===================
CGA_COLORS
COLORS
EGA_COLORS
fill_patterns
font_names
graphics_drivers
graphics_errors
graphics_modes
line_styles
line_widths
putimage_ops
text_just
text_modes
Enumeration Constants
=======================
AND_PUT
ATT400
ATT400C0
ATT400C1
ATT400C2
ATT400C3
ATT400HI
ATT400MED
BKSLASH_FILL
BLACK
BLINK
BLUE
BOTTOM_TEXT
BROWN
BW40
BW80
C40
C4350
C80
CENTER_LINE
CENTER_TEXT
CGA_BROWN
CGA_CYAN
CGA_GREEN
CGA_LIGHTCYAN
CGA_LIGHTGRAY
CGA_LIGHTGREEN
CGA_LIGHTMAGENTA
CGA_LIGHTRED
CGA_MAGENTA
CGA_RED
CGA_WHITE
CGA_YELLOW
CGA
CGAC0
CGAC1
CGAC2
CGAC3
CGAHI
CLOSE_DOT_FILL
COPY_PUT
CYAN
DARKGRAY
DASHED_LINE
DEFAULT_FONT
DETECT
DOTTED_LINE
EGA_BLACK
EGA_BLUE
EGA_BROWN
EGA_CYAN
EGA_DARKGRAY
EGA_GREEN
EGA_LIGHTBLUE
EGA_LIGHTCYAN
EGA_LIGHTGRAY
EGA_LIGHTGREEN
EGA_LIGHTMAGENTA
EGA_LIGHTRED
EGA_MAGENTA
EGA_RED
EGA_WHITE
EGA_YELLOW
EGA
EGA64
EGA64HI
EGA64LO
EGAHI
EGALO
EGAMONO
EGAMONOHI
EMPTY_FILL
GOTHIC_FONT
GREEN
grError
grFileNotFound
grFontNotFound
grInvalidDeviceNum
grInvalidDriver
grInvalidFont
grInvalidFontNum
grInvalidMode
grInvalidVersion
grIOerror
grNoFloodMem
grNoFontMem
grNoInitGraph
grNoLoadMem
grNoScanMem
grNotDetected
grOk
HATCH_FILL
HERCMONO
HERCMONOHI
IBM8514
IBM8514HI
IBM8514LO
INTERLEAVE_FILL
LASTMODE
LEFT_TEXT
LIGHTBLUE
LIGHTCYAN
LIGHTGRAY
LIGHTGREEN
LIGHTMAGENTA
LIGHTRED
LINE_FILL
LTBKSLASH_FILL
LTSLASH_FILL
MAGENTA
MCGA
MCGAC0
MCGAC1
MCGAC2
MCGAC3
MCGAHI
MCGAMED
MONO
NORM_WIDTH
NOT_PUT
OR_PUT
PC3270
PC3270HI
RED
RIGHT_TEXT
SANS_SERIF_FONT
SLASH_FILL
SMALL_FONT
SOLID_FILL
SOLID_LINE
THICK_WIDTH
TOP_TEXT
TRIPLEX_FONT
USER_FILL
USERBIT_LINE
VGA
VGAHI
VGALO
VGAMED
WHITE
WIDE_DOT_FILL
XHATCH_FILL
XOR_PUT
YELLOW
graphics_errors <GRAPHICS.H>
========================
Enum: Error return code from graphresult
Error| graphics_errors |
code | constant | Corresponding error message string
------+-------------------+-----------------------------------------------
0 | grOk | No error
-1 | grNoInitGraph | (BGI) graphics not installed (use initgraph)
-2 | grNotDetected | Graphics hardware not detected
-3 | grFileNotFound | Device driver file not found
-4 | grInvalidDriver | Invalid device driver file
-5 | grNoLoadMem | Not enough memory to load driver
-6 | grNoScanMem | Out of memory in scan fill
-7 | grNoFloodMem | Out of memory in flood fill
-8 | grFontNotFound | Font file not found
-9 | grNoFontMem | Not enough memory to load font
-10 | grInvalidMode | Invalid graphics mode for selected driver
-11 | grError | Graphics error
-12 | grIOerror | Graphics I/O error
-13 | grInvalidFont | Invalid font file
-14 | grInvalidFontNum | Invalid font number
-15 | grInvalidDeviceNum| Invalid device number
-18 | grInvalidVersion | Invalid version number
graphics_drivers <GRAPHICS.H>
=========================
Enum: BGI graphics drivers
Constant | Value
----------+---------------------------------
DETECT | 0 (requests autodetection)
CGA | 1
MCGA | 2
EGA | 3
EGA64 | 4
EGAMONO | 5
IBM8514 | 6
HERCMONO | 7
ATT400 | 8
VGA | 9
PC3270 | 10
See Also:
graphics_modes
initgraph
graphics_modes <GRAPHICS.H>
=======================
Enum: Graphics modes for each BGI driver
Graphics|
driver |graphics_modes|Value|Column x Row| Palette |Pages
---------+--------------+-----+------------+---------+----------
CGA | CGAC0 | 0 | 320 x 200 | C0 | 1
| CGAC1 | 1 | 320 x 200 | C1 | 1
| CGAC2 | 2 | 320 x 200 | C2 | 1
| CGAC3 | 3 | 320 x 200 | C3 | 1
| CGAHI | 4 | 640 x 200 | 2 color| 1
---------+--------------+-----+------------+---------+-------
MCGA | MCGAC0 | 0 | 320 x 200 | C0 | 1
| MCGAC1 | 1 | 320 x 200 | C1 | 1
| MCGAC2 | 2 | 320 x 200 | C2 | 1
| MCGAC3 | 3 | 320 x 200 | C3 | 1
| MCGAMED | 4 | 640 x 200 | 2 color| 1
| MCGAHI | 5 | 640 x 480 | 2 color| 1
---------+--------------+-----+------------+---------+-------
EGA | EGALO | 0 | 640 x 200 | 16 color| 4
| EGAHI | 1 | 640 x 350 | 16 color| 2
---------+--------------+-----+------------+---------+-------
EGA64 | EGA64LO | 0 | 640 x 200 | 16 color| 1
| EGA64HI | 1 | 640 x 350 | 4 color| 1
---------+--------------+-----+------------+---------+-------
EGA-MONO| EGAMONOHI | 3 | 640 x 350 | 2 color| 1*
| EGAMONOHI | 3 | 640 x 350 | 2 color| 2**
---------+--------------+-----+------------+---------+-------
HERC | HERCMONOHI | 0 | 720 x 348 | 2 color| 2
---------+--------------+-----+------------+---------+-------
ATT400 | ATT400C0 | 0 | 320 x 200 | C0 | 1
| ATT400C1 | 1 | 320 x 200 | C1 | 1
| ATT400C2 | 2 | 320 x 200 | C2 | 1
| ATT400C3 | 3 | 320 x 200 | C3 | 1
| ATT400MED | 4 | 640 x 200 | 2 color| 1
| ATT400HI | 5 | 640 x 400 | 2 color| 1
---------+--------------+-----+------------+---------+-------
VGA | VGALO | 0 | 640 x 200 | 16 color| 2
| VGAMED | 1 | 640 x 350 | 16 color| 2
| VGAHI | 2 | 640 x 480 | 16 color| 1
---------+--------------+-----+------------+---------+-------
PC3270 | PC3270HI | 0 | 720 x 350 | 2 color| 1
---------+--------------+-----+------------+---------+-------
IBM8514 | IBM8514HI | 1 | 1024 x 760 |256 color|
| IBM8514LO | 0 | 640 x 480 |256 color|
* 64K on EGAMONO card
** 256K on EGAMONO card
See Also:
detectgraph
graphics_drivers
initgraph
line_styles <GRAPHICS.H>
====================
Enum: Line styles for getlinesettings and setlinestyle.
Name |Value| Meaning
-------------+-----+-------------------------
SOLID_LINE | 0 | Solid line
DOTTED_LINE | 1 | Dotted line
CENTER_LINE | 2 | Centered line
DASHED_LINE | 3 | Dashed line
USERBIT_LINE| 4 | User-defined line style
See Also:
line_widths
line_widths <GRAPHICS.H>
====================
Enum: Line widths for getlinesettings and setlinestyle.
Name |Value| Meaning
------------+-----+--------------
NORM_WIDTH | 1 | 1 pixel wide
THICK_WIDTH| 3 | 3 pixels wide
See Also:
line_styles
font_names <GRAPHICS.H>
====================
Enum: Names for BGI fonts
Name |Value| Meaning
----------------+-----+-------------------------
DEFAULT_FONT | 0 | 8x8 bit-mapped font
TRIPLEX_FONT | 1 | Stroked triplex font
SMALL_FONT | 2 | Stroked small font
SANS_SERIF_FONT| 3 | Stroked sans-serif font
GOTHIC_FONT | 4 | Stroked gothic font
See Also:
settextstyle
fill_patterns <GRAPHICS.H>
======================
Enum: Fill patterns for getfillsettings and setfillstyle.
Names |Value| Means Fill With...
-----------------+-----+----------------------------
EMPTY_FILL | 0 | Background color
SOLID_FILL | 1 | Solid fill
LINE_FILL | 2 | ---
LTSLASH_FILL | 3 | ///
SLASH_FILL | 4 | ///, thick lines
BKSLASH_FILL | 5 | \\\, thick lines
LTBKSLASH_FILL | 6 | \\\
HATCH_FILL | 7 | Light hatch
XHATCH_FILL | 8 | Heavy crosshatch
INTERLEAVE_FILL | 9 | Interleaving lines
WIDE_DOT_FILL | 10 | Widely spaced dots
CLOSE_DOT_FILL | 11 | Closely spaced dots
USER_FILL | 12 | User-defined fill pattern
All but EMPTY_FILL fill with the current fill
color. EMPTY_FILL uses the current background
color.
putimage_ops <GRAPHICS.H>
=====================
Enum: Operators for putimage
Constant|Value| Meaning
---------+-----+-------------------------------------------------------
COPY_PUT| 0 | Copies source bitmap onto screen
XOR_PUT | 1 | Exclusive ORs source image with that already onscreen
OR_PUT | 2 | Inclusive ORs image with that already onscreen
AND_PUT | 3 | ANDs image with that already onscreen
NOT_PUT | 4 | Copy the inverse of the source
text_just <GRAPHICS.H>
==================
Enum: Horizontal and vertical justification for settextjustify
Argument| Constant |Value| Meaning
---------+-------------+-----+---------------------
horiz | LEFT_TEXT | 0 | Left-justify text
| CENTER_TEXT | 1 | Center text
| RIGHT_TEXT | 2 | Right-justify text
vert | BOTTOM_TEXT | 0 | Justify from bottom
| CENTER_TEXT | 1 | Center text
| TOP_TEXT | 2 | Justify from top
In calls to settextjustify, if
* horiz = LEFT_TEXT and
* direction = HORIZ_DIR
the CP's x component is advanced after a call
to outtext(string) by textwidth(string).
palettetype <GRAPHICS.H>
=============
Contains palette information for the current
graphics driver when calling getpalette,
setpalette, and setallpalette.
struct palettetype {
unsigned char size;
signed char colors[MAXCOLORS+1];
};
Element| What It Is/Does
--------+-------------------------------------------------------------------
size | Gives the number of colors in the palette for the current
| graphics driver in the current mode.
colors | An array of size bytes containing the actual raw color numbers
| for each entry in the palette. If an element of colors is
| -1, the palette color for that entry is not changed.
linesettingstype <GRAPHICS.H>
==================
Used by getlinesettings and setlinestyle
to adjust how lines are drawn.
struct linesettingstype {
int linestyle;
unsigned upattern;
int thickness;
};
Element | What It Is/Does
-----------+--------------------------------------------------------------
upattern | The user-defined bit pattern used when linestyle is set to
| USERBIT_LINE.
linestyle | Specifies in which style subsequent lines will be drawn
| (such as solid, dotted, centered, dashed).
thickness | Specifies whether the width of subsequent lines drawn will be
| normal or thick. (enum line_widths)
upattern is a 16-bit pattern that applies
only if linestyle is USERBIT_LINE (4). In
that case, whenever a bit in the pattern word
is 1, the corresponding pixel in the line is
drawn in the current drawing color.
For example, a solid line corresponds to a
upattern of 0xFFFF (all pixels drawn), while
a dashed line can correspond to a upattern of
0x3333 or 0x0F0F or 0x3F3F.
16-bit pattern | upattern
------------------+-----------------------
..xx..xx..xx..xx | 0x3333 (short dashes)
....xxxx....xxxx | 0x0F0F (long dashes)
..xxxxxx..xxxxxx | 0x3F3F (longer dashes)
xxxxxxxxxxxxxxxx | 0xFFFF (solid line)
textsettingstype <GRAPHICS.H>
==================
Used to get the current text settings from
gettextsettings.
struct textsettingstype {
int font;
int direction;
int charsize;
int horiz;
int vert;
};
fillsettingstype <GRAPHICS.H>
==================
Used to get current fill settings from
getfillsettings.
struct fillsettingstype {
int pattern; /* current fill pattern */
int color; /* current fill color */
};
The functions bar, bar3d, fillpoly, pieslice,
and floodfill all fill an area with the
current fill pattern in the current fill
color.
If pattern = 12 (USER_FILL), a user-defined
fill pattern is being used.
Otherwise, pattern gives the number of a
predefined pattern.
There are 11 predefined fill pattern styles
(solid, crosshatch, dotted, etc.).
Symbolic names for the predefined patterns
are provided by the enumerated type
fill_patterns in GRAPHICS.H. You can also
define your own fill pattern.
Symbolic names for colors are also defined in
GRAPHICS.H.
pointtype <GRAPHICS.H>
===========
Coordinates of a point.
struct pointtype {
int x ;
int y ;
};
viewporttype <GRAPHICS.H>
==============
Used to get current viewport settings from
getviewsettings.
struct viewporttype {
int left;
int top;
int right;
int bottom;
int clip;
};
arccoordstype <GRAPHICS.H>
===============
Used to get current viewport settings from
getarccoords.
struct arccoordstype {
int x, y; /* center point of arc*/
int xstart, ystart; /* start position */
int xend, yend; /* end position */
};
These values are useful if you need to make a
line meet the end of an arc.
See Also:
arc
=====
bar <GRAPHICS.H>
=====
Draws a bar
Declaration: void far bar(int left, int top, int right, int bottom);
Remarks:
bar draws a filled-in, rectangular,
two-dimensional bar.
The bar is filled using the current fill
pattern and fill color. bar does not outline
the bar.
To draw an outlined two-dimensional bar, use
bar3d with depth = 0.
Parameters | What they are
-----------------+--------------------------------------
(left, top) | the rectangle's upper left corner
(right, bottom) | the rectangle's lower right corner
The coordinates are in pixels.
Return Value: None
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | | | | |
+-----+------+---------+--------+----------+
See Also:
bar3d
rectangle
setcolor
setfillstyle
setlinestyle
Example:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int midx, midy, i;
/* initialize graphics and local
variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error
occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error
code */
}
midx = getmaxx() / 2;
midy = getmaxy() / 2;
/* loop through the fill patterns */
for (i=SOLID_FILL; i<USER_FILL; i++)
{
/* set the fill style */
setfillstyle(i, getmaxcolor());
/* draw the bar */
bar(midx-50, midy-50, midx+50,
midy+50);
getch();
}
/* clean up */
closegraph();
return 0;
}
=======
bar3d <GRAPHICS.H>
=======
Draws a 3-D bar
Declaration: void far bar3d(int left, int top, int right, int bottom,
int depth, int topflag);
Remarks:
bar3d draws a three-dimensional rectangular
bar, then fills it using the current fill
pattern and fill color.
The three-dimensional outline of the bar is
drawn in the current line style and color.
Parameter | What It Is/Does
-----------------+--------------------------------------
depth | Bar's depth in pixels
topflag | Governs whether a three-dimensional
| top is put on the bar
(left, top) | Rectangle's upper left corner
(right, bottom) | Rectangle's lower right corner
If topflag is non-zero, a top is put on the
bar. If topflag is 0, no top is put on the
bar: This makes it possible to stack several
bars on top of one another.
To calculate a typical depth for bar3d, take
25% of the width of the bar, like this:
bar3d(left, top, right, bottom, (right-left)/4, 1);
Return Value: None
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | | | | |
+-----+------+---------+--------+----------+
See Also:
bar
rectangle
setcolor
setfillstyle
setlinestyle
Example:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int midx, midy, i;
/* initialize graphics, local variables*/
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error
occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with error code */
}
midx = getmaxx() / 2;
midy = getmaxy() / 2;
/* loop through the fill patterns */
for (i=EMPTY_FILL; i<USER_FILL; i++)
{
/* set the fill style */
setfillstyle(i, getmaxcolor());
/* draw the 3-d bar */
bar3d(midx-50, midy-50, midx+50,
midy+50, 10, 1);
getch();
}
/* clean up */
closegraph();
return 0;
}
=======================
arc, circle, pieslice <GRAPHICS.H>
=======================
* arc draws a circular arc
* circle draws a circle
* pieslice draws and fills a circular pie slice
Declaration:
* void far arc(int x, int y, int stangle, int endangle, int radius);
* void far circle(int x, int y, int radius);
* void far pieslice(int x, int y, int stangle, int endangle, int radius);
Remarks:
arc draws a circular arc in the current
drawing color.
circle draws a circle in the current drawing
color.
pieslice draws a pie slice in the current
drawing color, then fills it using the
current fill pattern and fill color.
Argument | What It Is/Does
----------+--------------------------------------------
(x,y) | Center point of arc, circlew, or pie slice
stangle | Start angle in degrees
endangle | End angle in degrees
radius | Radius of arc, circle, and pieslice
The arc or slice travels from stangle to
endangle.
If stangle = 0 and endangle = 360, the call
to arc draws a complete circle.
If your circles are not perfectly round, use
setaspectratio to adjust the aspect ratio.
Angle for arc, circle, and pieslice (counter-clockwise)
=======================================================
90
degrees
|
|
180 ------+------ 0 degrees,
degrees | 360 degrees
|
270
degrees
The linestyle parameter does not affect arcs,
circles, ellipses, or pie slices. Only the
thickness parameter is used.
If you're using a CGA in high resolution mode
or a monochrome graphics adapter, examples in
this Help system that show how to use
graphics functions might not produce the
expected results.
If your system runs on a CGA or monochrome
adapter, pass 1 to those functions that alter
the fill or drawing color (don't pass one of
the symbolic color constants defined in
GRAPHICS.H).
Return Value: None
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | | | | |
+-----+------+---------+--------+----------+
See Also:
ellipse
fill_patterns
fillellipse
getarccoords
getaspectratio
graphresult
setfillpattern
setfillstyle
setgraphbufsize
Examples:
arc example
circle example
pieslice example
arc example
=============
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int midx, midy;
int stangle = 45, endangle = 135;
int radius = 100;
/* initialize graphics and local
variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
/* an error occurred */
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
midx = getmaxx() / 2;
midy = getmaxy() / 2;
setcolor(getmaxcolor());
/* draw arc */
arc(midx, midy, stangle, endangle, radius);
/* clean up */
getch();
closegraph();
return 0;
}
circle example
================
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int midx, midy;
int radius = 100;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
midx = getmaxx() / 2;
midy = getmaxy() / 2;
setcolor(getmaxcolor());
/* draw the circle */
circle(midx, midy, radius);
/* clean up */
getch();
closegraph();
return 0;
}
pieslice example
==================
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int midx, midy;
int stangle = 45, endangle = 135, radius = 100;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
midx = getmaxx() / 2;
midy = getmaxy() / 2;
/* set fill style and draw a pie slice */
setfillstyle(EMPTY_FILL, getmaxcolor());
pieslice(midx, midy, stangle, endangle, radius);
/* clean up */
getch();
closegraph();
return 0;
}
=============
cleardevice <GRAPHICS.H>
=============
Clears the graphics screen
Declaration: void far cleardevice(void);
Remarks:
cleardevice erases the entire graphics screen
and moves the CP (current position) to home
(0,0).
(Erasing consists of filling with the current
background color.)
Return Value: None
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | | | | |
+-----+------+---------+--------+----------+
It works only with IBM PCs and compatibles
equipped with supported graphics display
adapters.
See Also:
clearviewport
Example:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int midx, midy;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
midx = getmaxx() / 2;
midy = getmaxy() / 2;
setcolor(getmaxcolor());
/* for centering screen messages */
settextjustify(CENTER_TEXT, CENTER_TEXT);
/* output a message to the screen */
outtextxy(midx, midy, "press any key to clear the screen:");
/* wait for a key */
getch();
/* clear the screen */
cleardevice();
/* output another message */
outtextxy(midx, midy, "press any key to quit:");
/* clean up */
getch();
closegraph();
return 0;
}
===============
clearviewport <GRAPHICS.H>
===============
Clears the current viewport
Declaration: void far clearviewport(void);
Remarks:
clearviewport erases the viewport and moves
the CP (current position) to home (0,0),
relative to the viewport.
Return Value: None
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | | | | |
+-----+------+---------+--------+----------+
See Also:
cleardevice
getviewsettings
setviewport
Example:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#define CLIP_ON 1 /* activates clipping in viewport */
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int ht;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
setcolor(getmaxcolor());
ht = textheight("W");
/* message in default full-screen viewport */
outtextxy(0, 0, "* <-- (0, 0) in default viewport");
/* create a smaller viewport */
setviewport(50, 50, getmaxx()-50, getmaxy()-50, CLIP_ON);
/* display some messages */
outtextxy(0, 0, "* <-- (0, 0) in smaller viewport");
outtextxy(0, 2*ht, "Press any key to clear viewport:");
/* wait for a key */
getch();
/* clear the viewport */
clearviewport();
/* output another message */
outtextxy(0, 0, "Press any key to quit:");
/* clean up */
getch();
closegraph();
return 0;
}
============
closegraph <GRAPHICS.H>
============
Shuts down the graphics system
Declaration: void far closegraph(void);
Remarks:
closegraph deallocates all memory allocated
by the graphics system.
It then restores the screen to the mode it
was in before you called initgraph.
(The graphics system deallocates memory, such
as the drivers, fonts, and an internal
buffer, through a call to _graphfreemem.)
Return Value: None
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | | | | |
+-----+------+---------+--------+----------+
See Also:
initgraph
setgraphbufsize
Example:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int x, y;
/* initialize graphics mode */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
x = getmaxx() / 2;
y = getmaxy() / 2;
/* output a message */
settextjustify(CENTER_TEXT, CENTER_TEXT);
outtextxy(x, y, "Press a key to close the graphics system:");
/* wait for a key */
getch();
/* closes down the graphics system */
closegraph();
printf("We're now back in text mode.\n");
printf("Press any key to halt:");
getch();
return 0;
}
=============
detectgraph <GRAPHICS.H>
=============
Determines graphics driver and mode to use by checking the hardware.
Declaration:
void far detectgraph(int far *graphdriver, int far *graphmode);
Remarks:
detectgraph detects your system's graphics
adapter and chooses the mode that provides
the highest resolution for that adapter.
If no graphics hardware is detected,
detectgraph sets *graphdriver to
grNotDetected, and graphresult returns
grNotDetected.
The main reason to call detectgraph directly
is to override the graphics mode that
detectgraph recommends to initgraph.
*graphdriver is an integer that specifies the
graphics driver to be used. You can give
*graphdriver a value using a constant of the
graphics_drivers enum type, defined in
GRAPHICS.H.
*graphmode specifies the initial graphics
mode. However, if *graphdriver = DETECT,
*graphmode is set to the highest resolution
available for the detected driver.
You can give *graphmode a value using a
constant of the graphics_modes enum type,
also defined in GRAPHICS.H.
Return Value: None
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | | | | |
+-----+------+---------+--------+----------+
See Also:
graphresult
initgraph
Example:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
/* names of the various cards supported */
char *dname[] = { "requests detection",
"a CGA",
"an MCGA",
"an EGA",
"a 64K EGA",
"a monochrome EGA",
"an IBM 8514",
"a Hercules monochrome",
"an AT&T 6300 PC",
"a VGA",
"an IBM 3270 PC"
};
int main(void)
{
/* returns detected hardware info. */
int gdriver, gmode, errorcode;
/* detect graphics hardware available */
detectgraph(&gdriver, &gmode);
/* read result of detectgraph call */
errorcode = graphresult();
if (errorcode != grOk) /* an error
occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
/* display the information detected */
clrscr();
printf("You have %s video display card.\n", dname[gdriver]);
printf("Press any key to halt:");
getch();
return 0;
}
====================
drawpoly, fillpoly <GRAPHICS.H>
====================
* drawpoly draws the outline of a polygon
* fillpoly draws and fills a polygon
Declaration:
* void far drawpoly(int numpoints, int far *polypoints);
* void far fillpoly(int numpoints, int far *polypoints);
Remarks:
drawpoly draws a polygon using the current
line style and color.
fillpoly draws the outline of a polygon using
the current line style and color, then fills
the polygon using the current fill pattern
and fill color.
Argument | What It Does
-------------+-------------------------------------------------
numpoints | Specifies number of points
*polypoints | Points to a sequence of (numpoints x 2) integers
Each pair of integers gives the x and y
coordinates of a point on the polygon.
To draw a closed figure with N vertices, you
must pass N+1 coordinates to drawpoly, where
the Nth coordinate == the 0th coordinate.
Return Value: None
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | | | | |
+-----+------+---------+--------+----------+
See Also:
fill_patterns
floodfill
graphresult
setfillstyle
setwritemode
Examples:
drawpoly example
fillpoly example
drawpoly example
==================
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int maxx, maxy;
/* our polygon array */
int poly[10];
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk)
/* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
/* terminate with an error code */
exit(1);
}
maxx = getmaxx();
maxy = getmaxy();
poly[0] = 20; /* 1st vertext */
poly[1] = maxy / 2;
poly[2] = maxx - 20; /* 2nd */
poly[3] = 20;
poly[4] = maxx - 50; /* 3rd */
poly[5] = maxy - 20;
poly[6] = maxx / 2; /* 4th */
poly[7] = maxy / 2;
/*
drawpoly doesn't automatically close
the polygon, so we close it.
*/
poly[8] = poly[0];
poly[9] = poly[1];
/* draw the polygon */
drawpoly(5, poly);
/* clean up */
getch();
closegraph();
return 0;
}
fillpoly example
==================
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int i, maxx, maxy;
/* our polygon array */
int poly[8];
/* initialize graphics, local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk)
/* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
/* terminate with an error code */
}
maxx = getmaxx();
maxy = getmaxy();
poly[0] = 20; /* 1st vertex */
poly[1] = maxy / 2;
poly[2] = maxx - 20; /* 2nd */
poly[3] = 20;
poly[4] = maxx - 50; /* 3rd */
poly[5] = maxy - 20;
/*
4th vertex. fillpoly automatically
closes the polygon.
*/
poly[6] = maxx / 2;
poly[7] = maxy / 2;
/* loop through the fill patterns */
for (i=EMPTY_FILL; i<USER_FILL; i++)
{
/* set fill pattern */
setfillstyle(i, getmaxcolor());
/* draw a filled polygon */
fillpoly(4, poly);
getch();
}
/* clean up */
closegraph();
return 0;
}
===========
floodfill <GRAPHICS.H>
===========
Flood-fills a bounded region
Declaration: void far floodfill(int x, int y, int border);
Remarks:
floodfill fills an enclosed area on bitmap
devices.
The area bounded by the color border is
flooded with the current fill pattern and
fill color.
(x,y) is a "seed point".
* If the seed is within an enclosed area,
the inside will be filled.
* If the seed is outside the enclosed area,
the exterior will be filled.
Use fillpoly instead of floodfill whenever
possible so you can maintain code
compatibility with future versions.
floodfill does not work with the IBM-8514
driver.
Return Value:
If an error occurs while flooding a region,
graphresult returns -7.
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | | | | |
+-----+------+---------+--------+----------+
See Also:
drawpoly
fill_patterns
fillpoly
graphresult
setfillstyle
Example:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int maxx, maxy;
/* initialize graphics, local variables
*/
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk)
/* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
/* terminate with an error code */
}
maxx = getmaxx();
maxy = getmaxy();
/* select drawing color */
setcolor(getmaxcolor());
/* select fill color */
setfillstyle(SOLID_FILL, getmaxcolor());
/* draw a border around the screen */
rectangle(0, 0, maxx, maxy);
/* draw some circles */
circle(maxx / 3, maxy /2, 50);
circle(maxx / 2, 20, 100);
circle(maxx-20, maxy-50, 75);
circle(20, maxy-20, 25);
/* wait for a key */
getch();
/* fill in bounded region */
floodfill(2, 2, getmaxcolor());
/* clean up */
getch();
closegraph();
return 0;
}
==============
getarccoords <GRAPHICS.H>
==============
Gets coordinates of the last call to arc
Declaration:
void far getarccoords(struct arccoordstype far *arccoords);
Remarks:
getarccoords puts information about the last
call to arc in the arccoordstype structure
*arccoords.
Return Value: None
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | | | | |
+-----+------+---------+--------+----------+
See Also:
arc
fillellipse
sector
Example:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
struct arccoordstype arcinfo;
int midx, midy;
int stangle = 45, endangle = 270;
char sstr[80], estr[80];
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
/* an error occurred */
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
/* terminate with an error code */
exit(1);
}
midx = getmaxx() / 2;
midy = getmaxy() / 2;
/* draw arc and get coordinates */
setcolor(getmaxcolor());
arc(midx, midy, stangle, endangle, 100);
getarccoords(&arcinfo);
/* convert arc information into strings */
sprintf(sstr, "*- (%d, %d)", arcinfo.xstart, arcinfo.ystart);
sprintf(estr, "*- (%d, %d)", arcinfo.xend, arcinfo.yend);
/* output the arc information */
outtextxy(arcinfo.xstart,
arcinfo.ystart, sstr);
outtextxy(arcinfo.xend,
arcinfo.yend, estr);
/* clean up */
getch();
closegraph();
return 0;
}
=================
getaspectratio, <GRAPHICS.H>
setaspectratio
=================
* getaspectratio gets the current graphics mode's aspect ratio
* setaspectratio sets the graphics aspect ratio
Declaration:
* void far getaspectratio(int far *xasp, int far *yasp);
* void far setaspectratio(int xasp, int yasp);
Remarks:
getaspectratio gets the aspect-ratio values
in *xasp and *yasp.
setaspectratio changes the default aspect
ratio of the graphics system.
Arg. | Function | What It Is/Does
------+----------------+--------------------------------
xasp | getaspectratio | Points to the x aspect factor
| setaspectratio | New value for x aspect factor
| |
yasp | getaspectratio | Points to the y aspect factor
| setaspectratio | New value for y aspect factor
Return Value: None
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | | | | |
+-----+------+---------+--------+----------+
See Also:
arc
circle
ellipse
fillellipse
pieslice
sector
Example (for both functions):
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int xasp, yasp, midx, midy;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
midx = getmaxx() / 2;
midy = getmaxy() / 2;
setcolor(getmaxcolor());
/* get current aspect ratio settings */
getaspectratio(&xasp, &yasp);
/* draw normal circle */
circle(midx, midy, 100);
getch();
/* clear the screen */
cleardevice();
/* adjust the aspect for a wide circle */
setaspectratio(xasp/2, yasp);
circle(midx, midy, 100);
getch();
/* adjust the aspect for a narrow circle */
cleardevice();
setaspectratio(xasp, yasp/2);
circle(midx, midy, 100);
/* clean up */
getch();
closegraph();
return 0;
}
Aspect Ratio
==============
The graphics system uses the aspect ratio to
make sure that circles are round onscreen.
If circles appear elliptical, the monitor is
not aligned properly. You could correct this
in the hardware by realigning the monitor,
but it's easier to change in the software by
using setaspectratio to set the aspect ratio.
To obtain the current aspect ratio from the
system, call getaspectratio.
*yasp is normalized to 10,000. In general,
the relationship between *yasp and *xasp is
*yasp = 10,000
*xasp <= 10,000
On all graphics adapters except the VGA,
*xasp < *yasp (because the pixels are
taller than they are wide).
On the VGA, which has square pixels,
*xasp = *yasp.
========================
getbkcolor, setbkcolor <GRAPHICS.H>
========================
* getbkcolor returns the current background color
* setbkcolor sets the current background color using the palette
Declaration:
* int far getbkcolor(void);
* void far setbkcolor(int color);
Remarks:
getbkcolor returns the current background
color.
setbkcolor sets the background to the color
specified by color.
color
=====
color is either a number or symbolic name
specifying the color to set.
For example, if you want to set the
background color to blue, you can call
setbkcolor(BLUE) /* or */ setbkcolor(1)
On CGA and EGA systems, setbkcolor changes
the background color by changing the first
entry in the palette.
On an EGA or a VGA, if you call setpalette or
setallpalette to change the palette colors,
the defined symbolic constants might not give
the correct color.
This is because the color parameter to
setbkcolor indicates the entry number in the
current palette, rather than a specific
color. (Except 0, which always sets the
background color to black).
Return Value:
* getbkcolor returns the current background
color.
* setbkcolor does not return.
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | | | | |
+-----+------+---------+--------+----------+
See Also:
getcolor
getmaxcolor
getpalette
setallpalette
setcolor
setpalette
Examples:
getbkcolor example
setbkcolor example
getbkcolor example
====================
#include <graphics.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int bkcolor, midx, midy;
char bkname[35];
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
/* an error occurred */
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
/* terminate with an error code */
exit(1);
}
midx = getmaxx() / 2;
midy = getmaxy() / 2;
setcolor(getmaxcolor());
/* for centering text on the display */
settextjustify(CENTER_TEXT, CENTER_TEXT);
/* get the current background color */
bkcolor = getbkcolor();
/* convert color value into a string */
itoa(bkcolor, bkname, 10);
strcat(bkname, " is the current background color.");
/* display a message */
outtextxy(midx, midy, bkname);
/* clean up */
getch();
closegraph();
return 0;
}
setbkcolor example
====================
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* select a driver and mode that supports */
/* multiple background colors. */
int gdriver = EGA, gmode = EGAHI, errorcode;
int bkcol, maxcolor, x, y;
char msg[80];
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
/* maximum color index supported */
maxcolor = getmaxcolor();
/* for centering text messages */
settextjustify(CENTER_TEXT, CENTER_TEXT);
x = getmaxx() / 2;
y = getmaxy() / 2;
/* loop through the available colors */
for (bkcol=0; bkcol<=maxcolor; bkcol++)
{
/* clear the screen */
cleardevice();
/* select a new background color */
setbkcolor(bkcol);
/* output a messsage */
if (bkcol == WHITE)
setcolor(EGA_BLUE);
sprintf(msg, "Background color: %d", bkcol);
outtextxy(x, y, msg);
getch();
}
/* clean up */
closegraph();
return 0;
}
====================
getcolor, setcolor <GRAPHICS.H>
====================
* getcolor returns the current drawing color
* setcolor sets the current drawing color
Declaration:
* int far getcolor(void);
* void far setcolor(int color);
Remarks:
getcolor returns the current drawing color.
setcolor sets the current drawing color to
color, which can range from 0 to getmaxcolor.
To select a drawing color with setcolor, you
can pass either the color number or the
equivalent color name.
The drawing color is the value that pixels
are set to when the program draws lines, etc.
For example, in CGAC0 mode (palette number
0), the palette contains four colors
(background, light green, light red, and
yellow):
* If getcolor returns 1, the current drawing
color is light green.
* Either setcolor(3) or setcolor(CGA_YELLOW)
selects yellow as the drawing color.
In CGAC3 mode, if getcolor returns 1, the
current drawing color is cyan.
Return Value:
* getcolor returns the current drawing color.
* setcolor does not return
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | | | | |
+-----+------+---------+--------+----------+
See Also:
getbkcolor
getmaxcolor
getpalette
graphresult
setallpalette
setbkcolor
setpalette
Examples:
getcolor example
setcolor example
getcolor example
==================
#include <graphics.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int color, midx, midy;
char colname[35];
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
/* an error occurred */
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
/* terminate with an error code */
exit(1);
}
midx = getmaxx() / 2;
midy = getmaxy() / 2;
setcolor(getmaxcolor());
/* for centering text on the display */
settextjustify(CENTER_TEXT, CENTER_TEXT);
/* get the current drawing color */
color = getcolor();
/* convert color value into a string */
itoa(color, colname, 10);
strcat(colname,
" is the current drawing color.");
/* display a message */
outtextxy(midx, midy, colname);
/* clean up */
getch();
closegraph();
return 0;
}
setcolor example
==================
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* select a driver and mode that supports */
/* multiple drawing colors. */
int gdriver = EGA, gmode = EGAHI, errorcode;
int color, maxcolor, x, y;
char msg[80];
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
/* maximum color index supported */
maxcolor = getmaxcolor();
/* for centering text messages */
settextjustify(CENTER_TEXT, CENTER_TEXT);
x = getmaxx() / 2;
y = getmaxy() / 2;
/* loop through the available colors */
for (color=1; color<=maxcolor; color++)
{
/* clear the screen */
cleardevice();
/* select a new background color */
setcolor(color);
/* output a messsage */
sprintf(msg, "Color: %d", color);
outtextxy(x, y, msg);
getch();
}
/* clean up */
closegraph();
return 0;
}
===============================
getdefaultpalette,
getpalette, and setallpalette <GRAPHICS.H>
===============================
* getdefaultpalette returns the palette definition structure
* getpalette gets information about the current palette
* setallpalette changes all palette colors as specified
Declaration:
* struct palettetype *far getdefaultpalette(void);
* void far getpalette(struct palettetype far *palette);
* void far setallpalette(struct palettetype far *palette);
Remarks:
* getdefaultpalette finds the palettetype
structure that contains the palette
initialized by the driver during initgraph.
* getpalette fills the palettetype structure
*palette with information about the current
palette's size and colors.
* setallpalette sets the current palette to
the values given in the palettetype structure
*palette. setallpalette can partially (or
completely) change the colors in the EGA/VGA
palette.
+----------------------------------------+
| NOTE: getpalette and setallpalette can |
| NOT be used with the IBM-8514 |
| driver. See setrgbpalette. |
+----------------------------------------+
Valid colors depend on the current graphics
driver and current graphics mode.
Changes made to the palette appear onscreen
immediately. Each time a palette color
changes, all occurrences of that color change
to the new color value.
Return Value:
* getdefault returns a pointer to the
default palette set up by the current
driver when that driver was initialized.
* getpalette and setallpalette do not
return.
If invalid input is passed to setallpalette,
graphresult returns -11 (grError), and the
current palette remains unchanged.
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | | | | |
+-----+------+---------+--------+----------+
See Also:
getbkcolor
getcolor
getmaxcolor
getpalettesize
graphresult
setbkcolor
setcolor
Examples:
getdefaultpalette example
getpalette example
setallpalette example
getdefaultpalette example
===========================
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int i;
/* structure for returning palette copy */
struct palettetype far *pal=(void *) 0;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
/* an error occurred */
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
/* terminate with an error code */
exit(1);
}
setcolor(getmaxcolor());
/* return a pointer to the default palette */
pal = getdefaultpalette();
for (i=0; i<16; i++)
{
printf("colors[%d] = %d\n", i, pal->colors[i]);
getch();
}
/* clean up */
getch();
closegraph();
return 0;
}
setallpalette example
=======================
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
struct palettetype pal;
int color, maxcolor, ht;
int y = 10;
char msg[80];
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
maxcolor = getmaxcolor();
ht = 2 * textheight("W");
/* grab a copy of the palette */
getpalette(&pal);
/* display the default palette colors */
for (color=1; color<=maxcolor; color++)
{
setcolor(color);
sprintf(msg, "Color: %d", color);
outtextxy(1, y, msg);
y += ht;
}
/* wait for a key */
getch();
/* black out the colors one by one */
for (color=1; color<=maxcolor; color++)
{
setpalette(color, BLACK);
getch();
}
/* restore the palette colors */
setallpalette(&pal);
/* clean up */
getch();
closegraph();
return 0;
}
getpalette example
====================
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
struct palettetype pal;
char psize[80], pval[20];
int i, ht;
int y = 10;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
/* an error occurred */
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
/* terminate with an error code */
exit(1);
}
/* grab a copy of the palette */
getpalette(&pal);
/* convert palette info. into strings */
sprintf(psize, "The palette has %d modifiable entries.", pal.size);
/* display the information */
outtextxy(0, y, psize);
if (pal.size != 0)
{
ht = textheight("W");
y += 2*ht;
outtextxy(0, y, "Here are the current values:");
y += 2*ht;
for (i=0; i<pal.size; i++, y+=ht)
{
sprintf(pval, "palette[%02d]: 0x%02X", i, pal.colors[i]);
outtextxy(0, y, pval);
}
}
/* clean up */
getch();
closegraph();
return 0;
}
===============
getdrivername <GRAPHICS.H>
===============
Returns a pointer to the name of the current graphics driver
Declaration: char *far getdrivername(void);
Remarks:
After a call to initgraph, getdrivername
returns the name of the driver that is
currently loaded.
Return Value:
Returns a pointer to a string with the name
of the currently loaded graphics driver.
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | | | | |
+-----+------+---------+--------+----------+
See Also:
initgraph
Example:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
/* stores the device driver name */
char *drivername;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
/* an error occurred */
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
/* terminate with an error code */
exit(1);
}
setcolor(getmaxcolor());
/* get name of the device driver in use */
drivername = getdrivername();
/* for centering text on the screen */
settextjustify(CENTER_TEXT, CENTER_TEXT);
/* output the name of the driver */
outtextxy(getmaxx() / 2, getmaxy() / 2,
drivername);
/* clean up */
getch();
closegraph();
return 0;
}
================================
getfillpattern, setfillpattern <GRAPHICS.H>
================================
* getfillpattern copies a user-defined fill pattern into memory
* setfillpattern selects a user-defined fill pattern
Declaration:
* void far getfillpattern(char far *pattern);
* void far setfillpattern(char far *upattern, int color);
Remarks:
getfillpattern copies the user-defined fill
pattern (set by setfillpattern) into the
8-byte area *pattern.
setfillpattern sets the current fill pattern
to a user-defined 8x8 pattern.
Argument | Function | What Argument Is/Does
----------+----------+------------------------------------------------------
pattern | (get...) | Points to a sequence of 8 bytes; each byte
| | corresponds to 8 pixels in the pattern fetched
upattern | (set...) | Points to a sequence of 8 bytes; each byte
| | corresponds to 8 pixels in the user-defined pattern
Whenever a bit in a pattern's byte is set to
1, the corresponding pixel is plotted.
For example, the following user-defined fill
pattern represents a checkerboard:
char checkerboard[8] = {
0xAA, /* 10101010 = = = = = */
0x55, /* 01010101 = = = = = */
0xAA, /* 10101010 = = = = = */
0x55, /* 01010101 = = = = = */
0xAA, /* 10101010 = = = = = */
0x55, /* 01010101 = = = = = */
0xAA, /* 10101010 = = = = = */
0x55 /* 01010101 = = = = = */
};
Return Value: None
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | | | | |
+-----+------+---------+--------+----------+
See Also:
fill_patterns
getfillsettings
graphresult
sector
setfillstyle
Example (for both functions):
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int maxx, maxy;
char pattern[8] = {0x00, 0x70, 0x20, 0x27, 0x25, 0x27, 0x04, 0x04};
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
maxx = getmaxx();
maxy = getmaxy();
setcolor(getmaxcolor());
/* select a user defined fill pattern */
setfillpattern(pattern, getmaxcolor());
/* fill the screen with the pattern */
bar(0, 0, maxx, maxy);
getch();
/* get the current user defined fill pattern */
getfillpattern(pattern);
/* alter the pattern we grabbed */
pattern[4] -= 1;
pattern[5] -= 3;
pattern[6] += 3;
pattern[7] -= 4;
/* select our new pattern */
setfillpattern(pattern, getmaxcolor());
/* fill the screen with the new pattern */
bar(0, 0, maxx, maxy);
/* clean up */
getch();
closegraph();
return 0;
}
=================
getfillsettings <GRAPHICS.H>
=================
Gets information about current fill pattern and color.
Declaration:
void far getfillsettings (struct fillsettingstype far *fillinfo);
Remarks:
getfillsettings fills in a structure with
information about the current fill pattern
and fill color.
Argument | What It Is/Does
----------+---------------------------------
fillinfo | Points to the fillsettingstype
| structure where getfillsettings
| stores the current fill pattern
| and color
Return Value: None
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | | | | |
+-----+------+---------+--------+----------+
See Also:
getfillpattern
setfillpattern
setfillstyle
Example:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
/* the names of the fill styles supported */
char *fname[] = { "EMPTY_FILL",
"SOLID_FILL",
"LINE_FILL",
"LTSLASH_FILL",
"SLASH_FILL",
"BKSLASH_FILL",
"LTBKSLASH_FILL",
"HATCH_FILL",
"XHATCH_FILL",
"INTERLEAVE_FILL",
"WIDE_DOT_FILL",
"CLOSE_DOT_FILL",
"USER_FILL"
};
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
struct fillsettingstype fillinfo;
int midx, midy;
char patstr[40], colstr[40];
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
midx = getmaxx() / 2;
midy = getmaxy() / 2;
/* get information about current fill pattern and color */
getfillsettings(&fillinfo);
/* convert fill information into strings */
sprintf(patstr, "%s is the fill style.", fname[fillinfo.pattern]);
sprintf(colstr, "%d is the fill color.", fillinfo.color);
/* display the information */
settextjustify(CENTER_TEXT, CENTER_TEXT);
outtextxy(midx, midy, patstr);
outtextxy(midx, midy+2*textheight("W"), colstr);
/* clean up */
getch();
closegraph();
return 0;
}
============================
getgraphmode, setgraphmode <GRAPHICS.H>
============================
* getgraphmode returns the current graphics mode
* setgraphmode sets the system to graphics mode, clears the screen
Declaration:
* int far getgraphmode(void);
* void far setgraphmode(int mode);
Remarks:
* getgraphmode returns the current graphics
mode.
NOTE: Your program must make a successful
call to initgraph or setgraphmode BEFORE
calling getgraphmode.
* setgraphmode selects a graphics mode
different than the default one set by
initgraph. It clears the screen and resets
all graphics settings to their defaults.
mode must be a valid mode for the current
device driver.
The enumeration graphics_modes, defined in
GRAPHICS.H, gives names for the predefined
graphics modes.
You can use setgraphmode in conjunction with
restorecrtmode to switch back and forth
between text and graphics modes.
Return Value:
* getgraphmode returns the graphics mode
set by initgraph or setgraphmode
* setgraphmode does not return.
If you give setgraphmode an invalid mode for
the current device driver, graphresult
returns -10 (grInvalidMode).
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | | | | |
+-----+------+---------+--------+----------+
See Also:
getmoderange
graphresult
Examples:
getgraphmode example
setgraphmode example
getgraphmode example
======================
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int midx, midy, mode;
char numname[80], modename[80];
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
/* an error occurred */
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
/* terminate with an error code */
exit(1);
}
midx = getmaxx() / 2;
midy = getmaxy() / 2;
/* get mode number and name strings */
mode = getgraphmode();
sprintf(numname, "%d is the current mode number.", mode);
sprintf(modename, "%s is the current graphics mode", getmodename(mode));
/* display the information */
settextjustify(CENTER_TEXT, CENTER_TEXT);
outtextxy(midx, midy, numname);
outtextxy(midx, midy+2*textheight("W"), modename);
/* clean up */
getch();
closegraph();
return 0;
}
setgraphmode example
======================
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int x, y;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
x = getmaxx() / 2;
y = getmaxy() / 2;
/* output a message */
settextjustify(CENTER_TEXT, CENTER_TEXT);
outtextxy(x, y, "Press any key to exit graphics:");
getch();
/* restore system to text mode */
restorecrtmode();
printf("We're now in text mode.\n");
printf("Press any key to return to graphics mode:");
getch();
/* return to graphics mode */
setgraphmode(getgraphmode());
/* output a message */
settextjustify(CENTER_TEXT, CENTER_TEXT);
outtextxy(x, y, "We're back in graphics mode.");
outtextxy(x, y+textheight("W"), "Press any key to halt:");
/* clean up */
getch();
closegraph();
return 0;
}
====================
getimage, putimage <GRAPHICS.H>
====================
* getimage saves a bit image of the specified region into memory
* putimage outputs a bit image onto the screen
Declaration:
* void far getimage(int left, int top, int right, int bottom,
void far *bitmap);
* void far putimage(int left, int top, void far *bitmap, int op);
Remarks:
* getimage copies an image from the screen to
memory.
* putimage puts the bit image previously
saved with getimage back onto the screen,
with the upper left corner of the image
placed at (left,top).
Argument | What It Is/Does
----------+----------------------------------------------------------------
bitmap | Points to the area in memory where the bit image is stored.
| The first two words of this area are used for the width and
| height of the rectangle. The remainder holds the image itself.
----------+----------------------------------------------------------------
bottom | (left, top) and (right, bottom) define the rectangular screen
left | area from which getimage copies the bit image.
right | (left, top) is where putimage places the upper left corner of
top | the stored image.
----------+----------------------------------------------------------------
op | Specifies a combination operator that controls how the color
| for each destination pixel onscreen is computed, based on the
| pixel already onscreen and the corresponding source pixel in
| memory.
The enumeration putimage_ops, defined in
GRAPHICS.H, gives names to the putimage
combination operators.
Return Value: None
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | | | | |
+-----+------+---------+--------+----------+
See Also:
imagesize
putpixel
setvisualpage
Examples:
getimage example
putimage example
getimage example
==================
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <alloc.h>
void save_screen(void far *buf[4]);
void restore_screen(void far *buf[4]);
int maxx, maxy;
int main(void)
{
int gdriver=DETECT, gmode, errorcode;
void far *ptr[4];
/* auto-detect the graphics driver and mode */
initgraph(&gdriver, &gmode, "");
errorcode = graphresult(); /* check for any errors */
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
maxx = getmaxx();
maxy = getmaxy();
/* draw an image on the screen */
rectangle(0, 0, maxx, maxy);
line(0, 0, maxx, maxy);
line(0, maxy, maxx, 0);
save_screen(ptr); /* save the current screen */
getch(); /* pause screen */
cleardevice(); /* clear screen */
restore_screen(ptr); /* restore the screen */
getch(); /* pause screen */
closegraph();
return 0;
}
void save_screen(void far *buf[4])
{
unsigned size;
int ystart=0, yend, yincr, block;
yincr = (maxy+1) / 4;
yend = yincr;
size = imagesize(0, ystart, maxx, yend);
/* get byte size of image */
for (block=0; block<=3; block++)
{
if ((buf[block] = farmalloc(size)) == NULL)
{
closegraph();
printf("Error: not enough heap space in save_screen().\n");
exit(1);
}
getimage(0, ystart, maxx, yend, buf[block]);
ystart = yend + 1;
yend += yincr + 1;
}
}
void restore_screen(void far *buf[4])
{
int ystart=0, yend, yincr, block;
yincr = (maxy+1) / 4;
yend = yincr;
for (block=0; block<=3; block++)
{
putimage(0, ystart, buf[block], COPY_PUT);
farfree(buf[block]);
ystart = yend + 1;
yend += yincr + 1;
}
}
putimage example
==================
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#define ARROW_SIZE 10
void draw_arrow(int x, int y);
int main(void)
{
/* request autodetection */
int gdriver = DETECT, gmode, errorcode;
void *arrow;
int x, y, maxx;
unsigned int size;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
maxx = getmaxx();
x = 0;
y = getmaxy() / 2;
/* draw the image to be grabbed */
draw_arrow(x, y);
/* calculate the size of the image */
size = imagesize(x, y-ARROW_SIZE, x+(4*ARROW_SIZE), y+ARROW_SIZE);
/* allocate memory to hold the image */
arrow = malloc(size);
/* grab the image */
getimage(x, y-ARROW_SIZE, x+(4*ARROW_SIZE), y+ARROW_SIZE, arrow);
/* repeat until a key is pressed */
while (!kbhit())
{
/* erase old image */
putimage(x, y-ARROW_SIZE, arrow, XOR_PUT);
x += ARROW_SIZE;
if (x >= maxx)
x = 0;
/* plot new image */
putimage(x, y-ARROW_SIZE, arrow, XOR_PUT);
}
/* clean up */
free(arrow);
closegraph();
return 0;
}
void draw_arrow(int x, int y)
{
/* draw an arrow on the screen */
moveto(x, y);
linerel(4*ARROW_SIZE, 0);
linerel(-2*ARROW_SIZE, -1*ARROW_SIZE);
linerel(0, 2*ARROW_SIZE);
linerel(2*ARROW_SIZE, -1*ARROW_SIZE);
}
=================
getlinesettings <GRAPHICS.H>
=================
Gets the current line style, pattern, and thickness
Declaration:
void far getlinesettings(struct linesettingstype far *lineinfo);
Remarks:
getlinesettings fills the linesettingstype
structure *lineinfo with information about
the current line style, pattern, and
thickness.
Return Value: None
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | | | | |
+-----+------+---------+--------+----------+
See Also:
setlinestyle
Example:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
/* the names of the line styles supported */
char *lname[] = { "SOLID_LINE",
"DOTTED_LINE",
"CENTER_LINE",
"DASHED_LINE",
"USERBIT_LINE"
};
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
struct linesettingstype lineinfo;
int midx, midy;
char lstyle[80], lpattern[80], lwidth[80];
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
midx = getmaxx() / 2;
midy = getmaxy() / 2;
/* get information about current line settings */
getlinesettings(&lineinfo);
/* convert line information into strings */
sprintf(lstyle, "%s is the line style.", lname[lineinfo.linestyle]);
sprintf(lpattern, "0x%X is the user-defined line pattern.",
lineinfo.upattern);
sprintf(lwidth, "%d is the line thickness.",
lineinfo.thickness);
/* display the information */
settextjustify(CENTER_TEXT, CENTER_TEXT);
outtextxy(midx, midy, lstyle);
outtextxy(midx, midy+2*textheight("W"), lpattern);
outtextxy(midx, midy+4*textheight("W"), lwidth);
/* clean up */
getch();
closegraph();
return 0;
}
=============
getmaxcolor <GRAPHICS.H>
=============
Returns maximum color value
Declaration: int far getmaxcolor(void);
Remarks:
getmaxcolor returns the highest valid color
value that can be passed to setcolor for the
current graphics driver and mode.
For example, on a 256K EGA, getmaxcolor
always returns 15. This means that any call
to setcolor with a value from 0 to 15 is
valid.
On a CGA in high-resolution mode (or on a
Hercules monochrome adapter) getmaxcolor
returns 1.
Return Value:
Returns the highest available color value.
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | | | | |
+-----+------+---------+--------+----------+
See Also:
getbkcolor
getcolor
getpalette
getpalettesize
setcolor
Example:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int midx, midy;
char colstr[80];
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
midx = getmaxx() / 2;
midy = getmaxy() / 2;
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* */
/* grab the color info. and convert it to a string */
sprintf(colstr, "This mode supports colors 0..%d", getmaxcolor());
/* display the information */
settextjustify(CENTER_TEXT, CENTER_TEXT);
outtextxy(midx, midy, colstr);
/* */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* clean up */
getch();
closegraph();
return 0;
}
============
getmaxmode <GRAPHICS.H>
============
Returns maximum graphics mode number for current driver
Declaration: int far getmaxmode(void);
Remarks:
getmaxmode lets you find out the maximum mode
number for the currently loaded driver,
directly from the driver.
This gives it an advantage over getmoderange,
which works for Borland drivers only.
The minimum mode is 0.
Return Value:
getmaxmode returns the maximum mode number
for the current driver.
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | | | | |
+-----+------+---------+--------+----------+
See Also:
getmodename
getmoderange
Example:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int midx, midy;
char modestr[80];
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
midx = getmaxx() / 2;
midy = getmaxy() / 2;
/* grab the mode info. and convert it to a string */
sprintf(modestr, "This driver supports modes 0..%d", getmaxmode());
/* display the information */
settextjustify(CENTER_TEXT, CENTER_TEXT);
outtextxy(midx, midy, modestr);
/* clean up */
getch();
closegraph();
return 0;
}
=====================
getmaxx and getmaxy <GRAPHICS.H>
=====================
Returns maximum x or y screen coordinate
Declaration:
* int far getmaxx(void);
* int far getmaxy(void);
Remarks:
* getmaxx returns the maximum x value
(screen-relative) for the current
graphics driver and mode.
* getmaxy returns the maximum y value
(screen-relative) for the current
graphics driver and mode.
For example, on a CGA in 320 x 200 mode,
getmaxx returns 319 and getmaxy returns 199.
Return Value:
* getmaxx: maximum x screen coordinate
* getmaxy: maximum y screen coordinate
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | | | | |
+-----+------+---------+--------+----------+
See Also:
getx
gety
Example:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int midx, midy;
char xrange[80], yrange[80];
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
midx = getmaxx() / 2;
midy = getmaxy() / 2;
/* convert max resolution values into strings */
sprintf(xrange, "X values range from 0..%d", getmaxx());
sprintf(yrange, "Y values range from 0..%d", getmaxy());
/* display the information */
settextjustify(CENTER_TEXT, CENTER_TEXT);
outtextxy(midx, midy, xrange);
outtextxy(midx, midy+textheight("W"), yrange);
/* clean up */
getch();
closegraph();
return 0;
}
=============
getmodename <GRAPHICS.H>
=============
Returns the name of a specified graphics mode
Declaration: char * far getmodename(int mode_number);
Remarks:
getmodename accepts a graphics mode number as
input and returns a string containing the
name of the corresponding graphics mode.
The mode names are embedded in each driver.
The return values are useful for building
menus or displaying status.
Return Value:
getmodename returns a pointer to a string
contining the name of the graphics mode.
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | | | | |
+-----+------+---------+--------+----------+
See Also:
getmaxmode
getmoderange
Example:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request autodetection */
int gdriver = DETECT, gmode, errorcode;
int midx, midy, mode;
char numname[80], modename[80];
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
midx = getmaxx() / 2;
midy = getmaxy() / 2;
/* get mode number and name strings */
mode = getgraphmode();
sprintf(numname, "%d is the current mode number.", mode);
sprintf(modename, "%s is the current graphics mode.",
getmodename(mode));
/* display the information */
settextjustify(CENTER_TEXT, CENTER_TEXT);
outtextxy(midx, midy, numname);
outtextxy(midx, midy+2*textheight("W"), modename);
/* clean up */
getch();
closegraph();
return 0;
}
==============
getmoderange <GRAPHICS.H>
==============
Gets the range of modes for a given graphics driver
Declaration:
void far getmoderange(int graphdriver, int far *lomode, int far*himode);
Remarks:
getmoderange gets the range of valid graphics
modes for the given graphics driver.
Argument | What It Is/Does
-------------+-------------------------------------------------------------
graphdriver | Specified graphics driver
| * If graphdriver = -1, getmoderange gets the currently
| loaded driver modes.
| * If graphdriver specifies an invalid graphics driver,
| both *lomode and *himode are set to -1.
|
lomode | Points to location where lowest permissible mode value
| is returned.
himode | Points to location where highest permissible mode value
| is returned.
Return Value: None
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | | | | |
+-----+------+---------+--------+----------+
See Also:
getgraphmode
getmaxmode
getmodename
initgraph
setgraphmode
Example:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int midx, midy;
int low, high;
char mrange[80];
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
midx = getmaxx() / 2;
midy = getmaxy() / 2;
/* get the mode range for this driver */
getmoderange(gdriver, &low, &high);
/* convert mode range info. into strings */
sprintf(mrange, "This driver supports modes %d..%d", low, high);
/* display the information */
settextjustify(CENTER_TEXT, CENTER_TEXT);
outtextxy(midx, midy, mrange);
/* clean up */
getch();
closegraph();
return 0;
}
================
getpalettesize <GRAPHICS.H>
================
Returns size of palette color lookup table
Declaration: int far getpalettesize(void);
Remarks:
getpalettesize is used to determine how many
palette entries can be set for the current
graphics mode. For example, the EGA in color
mode returns 16.
Return Value:
getpalettesize returns the number of palette
entries in the current palette.
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | | | | |
+-----+------+---------+--------+----------+
See Also:
setallpalette
setpalette
Example:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main()
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int midx, midy;
char psize[80];
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
midx = getmaxx() / 2;
midy = getmaxy() / 2;
/* convert palette size info. into string */
sprintf(psize, "The palette has %d modifiable entries.",
getpalettesize());
/* display the information */
settextjustify(CENTER_TEXT, CENTER_TEXT);
outtextxy(midx, midy, psize);
/* clean up */
getch();
closegraph();
return 0;
}
====================
getpixel, putpixel <GRAPHICS.H>
====================
* getpixel gets the color of a specified pixel
* putpixel plots a pixel at a specified point
Declaration:
* unsigned far getpixel(int x, int y);
* void far putpixel(int x, int y, int color);
Remarks:
getpixel gets the color of the pixel located
at (x,y).
putpixel plots a point in the color defined
by color at (x,y).
Return Value:
* getpixel returns the color of the given pixel
* putpixel does not return
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | | | | |
+-----+------+---------+--------+----------+
See Also:
getimage/putimage
Example (for both functions):
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <dos.h>
#define PIXEL_COUNT 1000
#define DELAY_TIME 100 /* in milliseconds */
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int i, x, y, color, maxx, maxy,
maxcolor, seed;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
/* an error occurred */
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
/* terminate with an error code */
exit(1);
}
maxx = getmaxx() + 1;
maxy = getmaxy() + 1;
maxcolor = getmaxcolor() + 1;
while (!kbhit())
{
/* seed the random number generator */
seed = random(32767);
srand(seed);
for (i=0; i<PIXEL_COUNT; i++)
{
x = random(maxx);
y = random(maxy);
color = random(maxcolor);
putpixel(x, y, color);
}
delay(DELAY_TIME);
srand(seed);
for (i=0; i<PIXEL_COUNT; i++)
{
x = random(maxx);
y = random(maxy);
color = random(maxcolor);
if (color == getpixel(x, y))
putpixel(x, y, 0);
}
}
/* clean up */
getch();
closegraph();
return 0;
}
=================
gettextsettings <GRAPHICS.H>
=================
Gets information about the current graphic text font
Declaration:
void far gettextsettings(struct textsettingstype far *texttypeinfo);
Remarks:
gettextsettings fills a structure with
information about the current text font,
direction, size, and justification.
Argument | What It Is/Does
--------------+------------------------------------------
texttypeinfo | Points to the textsettingstype structure
| that gettextsettings fills in
Return Value: None
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | | | | |
+-----+------+---------+--------+----------+
See Also:
outtext
outtextxy
registerbgifont
settextjustify
settextstyle
setusercharsize
textheight
textwidth
Example:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
/* the names of the fonts supported */
char *font[] = { "DEFAULT_FONT",
"TRIPLEX_FONT",
"SMALL_FONT",
"SANS_SERIF_FONT",
"GOTHIC_FONT"
};
/* the names of the text directions supported */
char *dir[] = { "HORIZ_DIR", "VERT_DIR" };
/* horizontal text justifications supported */
char *hjust[] = { "LEFT_TEXT", "CENTER_TEXT", "RIGHT_TEXT" };
/* vertical text justifications supported */
char *vjust[] = { "BOTTOM_TEXT", "CENTER_TEXT", "TOP_TEXT" };
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
struct textsettingstype textinfo;
int midx, midy, ht;
char fontstr[80], dirstr[80], sizestr[80];
char hjuststr[80], vjuststr[80];
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
midx = getmaxx() / 2;
midy = getmaxy() / 2;
/* get information about current text settings */
gettextsettings(&textinfo);
/* convert text information into strings */
sprintf(fontstr, "%s is the text style.", font[textinfo.font]);
sprintf(dirstr, "%s is the text direction.", dir[textinfo.direction]);
sprintf(sizestr, "%d is the text size.", textinfo.charsize);
sprintf(hjuststr, "%s is the horizontal justification.",
hjust[textinfo.horiz]);
sprintf(vjuststr, "%s is the vertical justification.",
vjust[textinfo.vert]);
/* display the information */
ht = textheight("W");
settextjustify(CENTER_TEXT, CENTER_TEXT);
outtextxy(midx, midy, fontstr);
outtextxy(midx, midy+2*ht, dirstr);
outtextxy(midx, midy+4*ht, sizestr);
outtextxy(midx, midy+6*ht, hjuststr);
outtextxy(midx, midy+8*ht, vjuststr);
/* clean up */
getch();
closegraph();
return 0;
}
=================
getviewsettings <GRAPHICS.H>
=================
Gets information about the current viewport
Declaration:
void far getviewsettings (struct viewporttype far *viewport);
Remarks:
getviewsettings fills a structure with
information about the current viewport.
Argument | What It Is/Does
----------+---------------------------------
viewport | Points to viewporttype structure
| that getviewsettings fills
Return Value: None
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | | | | |
+-----+------+---------+--------+----------+
See Also:
clearviewport
getx
gety
setviewport
Example:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
char *clip[] = { "OFF", "ON" };
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
struct viewporttype viewinfo;
int midx, midy, ht;
char topstr[80], botstr[80], clipstr[80];
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
midx = getmaxx() / 2;
midy = getmaxy() / 2;
/* get information about current viewport */
getviewsettings(&viewinfo);
/* convert text information into strings */
sprintf(topstr, "(%d, %d) is the upper left viewport corner.",
viewinfo.left, viewinfo.top);
sprintf(botstr, "(%d, %d) is the lower right viewport corner.",
viewinfo.right, viewinfo.bottom);
sprintf(clipstr, "Clipping is turned %s.", clip[viewinfo.clip]);
/* display the information */
settextjustify(CENTER_TEXT, CENTER_TEXT);
ht = textheight("W");
outtextxy(midx, midy, topstr);
outtextxy(midx, midy+2*ht, botstr);
outtextxy(midx, midy+4*ht, clipstr);
/* clean up */
getch();
closegraph();
return 0;
}
============
getx, gety <GRAPHICS.H>
============
* getx returns the current position's x coordinate
* gety returns the current position's y coordinate
Declaration:
* int far getx(void);
* int far gety(void);
Remarks:
* getx returns the x-coordinate of the
current graphics position.
* gety returns the y-coordinate of the
current graphics position.
The values are viewport-relative.
Return Value:
* getx: x-coordinate of current position
* gety: y-coordinate of current position
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | | | | |
+-----+------+---------+--------+----------+
See Also:
getmaxx/getmaxy
getviewsettings
moveto
Example (for both functions):
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
char msg[80];
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
/* move to the screen center point */
moveto(getmaxx() / 2, getmaxy() / 2);
/* create a message string */
sprintf(msg, "<-(%d, %d) is here.", getx(), gety());
/* display the message */
outtext(msg);
/* clean up */
getch();
closegraph();
return 0;
}
===============
graphdefaults <GRAPHICS.H>
===============
Resets all graphics settings to their defaults
Declaration: void far graphdefaults(void);
Remarks:
graphdefaults resets all graphics settings to
their defaults:
* sets the viewport to the entire screen.
* moves the current position to (0,0).
* sets the default palette colors, background color, and drawing color.
* sets the default fill style and pattern.
* sets the default text font and justification.
Return Value: None
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | | | | |
+-----+------+---------+--------+----------+
See Also:
initgraph
setgraphmode
Example:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int maxx, maxy;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "c:\\bor\\turbo5\\bgi");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
maxx = getmaxx();
maxy = getmaxy();
/* output line with non-default settings */
setlinestyle(DOTTED_LINE, 0, 3); line(0, 0, maxx, maxy);
outtextxy(maxx/2, maxy/3, "Before default values are restored.");
getch();
/* restore default values for everything */
graphdefaults();
/* clear the screen */
cleardevice();
/* output line with default settings */
line(0, 0, maxx, maxy);
outtextxy(maxx/2, maxy/3, "After restoring default values.");
/* clean up */
getch();
closegraph();
return 0;
}
===============
grapherrormsg <GRAPHICS.H>
===============
Returns a pointer to an error message string
Declaration: char *far grapherrormsg(int errorcode);
Remarks:
grapherrormsg returns a pointer to the error
message string associated with errorcode, the
value returned by graphresult.
See the errno Help screen for a list of error
messages and mnemonics.
Return Value:
Returns a pointer to an error message string.
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | | | | |
+-----+------+---------+--------+----------+
See Also:
graphresult
Example:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#define NONSENSE -50
int main(void)
{
/* FORCE AN ERROR TO OCCUR */
int gdriver = NONSENSE, gmode, errorcode;
/* initialize graphics mode */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
/* if an error occurred, then output a */
/* descriptive error message. */
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
/* draw a line */
line(0, 0, getmaxx(), getmaxy());
/* clean up */
getch();
closegraph();
return 0;
}
=============================
_graphfreemem, _graphgetmem <GRAPHICS.H>
=============================
User hooks into graphics memory deallocation
Declaration:
* void far _graphfreemem(void far *ptr, unsigned size);
* void far * far _graphgetmem(unsigned size);
Remarks:
Routines in the graphics library (not in
your program) normally call _graphgetmem
to allocate memory for internal buffers,
graphics drivers, and character sets.
The graphics library calls _graphfreemem to
release memory previously allocated through
_graphgetmem.
You can to control the graphics library
memory management by defining your own
versions of _graphfreemem and _graphgetmem.
(You must declare them exactly as shown.)
The default version of _graphgetmem calls
malloc; the default version of _graphfreemem
calls free.
Return Value: None
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | | | | |
+-----+------+---------+--------+----------+
See Also:
initgraph
setgraphbufsize
Example (for both functions):
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <alloc.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int midx, midy;
/* clear the text screen */
clrscr();
printf("Press any key to initialize graphics mode:");
getch();
clrscr();
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
midx = getmaxx() / 2;
midy = getmaxy() / 2;
/* display a message */
settextjustify(CENTER_TEXT, CENTER_TEXT);
outtextxy(midx, midy, "Press any key to exit graphics mode:");
/* clean up */
getch();
closegraph();
return 0;
}
/* called by the graphics kernel to allocate memory */
void far * far _graphgetmem(unsigned size)
{
printf("_graphgetmem called to allocate %d bytes.\n", size);
printf("press any key:");
getch();
printf("\n");
/* allocate memory from far heap */
return farmalloc(size);
}
/* called by the graphics kernel to free memory */
void far _graphfreemem(void far *ptr, unsigned size)
{
printf("_graphfreemem called to free %d bytes.\n", size);
printf("press any key:");
getch();
printf("\n");
/* free ptr from far heap */
farfree(ptr);
}
=============
graphresult <GRAPHICS.H>
=============
Returns an error code for the last unsuccessful graphics operation
Declaration: int far graphresult(void);
Remarks:
graphresult returns the error code for the
last graphics operation that reported an
error, then resets the error level to grOk.
The enumerated type graphics_errors defines
the error codes.
The variable maintained by graphresult is
reset to 0 after graphresult has been called.
Therefore, you should store the value of
graphresult into a temporary variable and
then test it.
Return Value:
Returns the current graphics error number,
(an integer in the range -15 to 0).
NOTE: grapherrormsg returns a pointer to a
string associated with the integer value
returned by graphresult.
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | | | | |
+-----+------+---------+--------+----------+
See Also:
detectgraph
drawpoly
fillpoly
floodfill
grapherrormsg
initgraph
pieslice
registerbgidriver
registerbgifont
setallpalette
setcolor
setfillstyle
setgraphmode
setlinestyle
setpalette
settextjustify
settextstyle
setusercharsize
setviewport
setvisualpage
Example:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
/* draw a line */
line(0, 0, getmaxx(), getmaxy());
/* clean up */
getch();
closegraph();
return 0;
}
===========
imagesize <GRAPHICS.H>
===========
Returns the number of bytes required to store a bit image
Declaration:
unsigned far imagesize(int left, int top, int right, int bottom);
Remarks:
imagesize determines the size of the memory
area required to store a bit image.
Return Value:
* On success, returns the size of the
required memory area in bytes.
* On error (if the size required for the
selected image is >= (64K - 1) bytes),
returns 0xFFFF (-1)
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | | | | |
+-----+------+---------+--------+----------+
See Also:
getimage
putimage
Example:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#define ARROW_SIZE 10
void draw_arrow(int x, int y);
int main(void)
{
/* request autodetection */
int gdriver = DETECT, gmode, errorcode;
void *arrow;
int x, y, maxx;
unsigned int size;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
maxx = getmaxx();
x = 0;
y = getmaxy() / 2;
/* draw the image to be grabbed */
draw_arrow(x, y);
/* calculate the size of the image */
size = imagesize(x, y-ARROW_SIZE, x+(4*ARROW_SIZE), y+ARROW_SIZE);
/* allocate memory to hold the image */
arrow = malloc(size);
/* grab the image */
getimage(x, y-ARROW_SIZE, x+(4*ARROW_SIZE), y+ARROW_SIZE, arrow);
/* repeat until a key is pressed */
while (!kbhit())
{
/* erase old image */
putimage(x, y-ARROW_SIZE, arrow, XOR_PUT);
x += ARROW_SIZE;
if (x >= maxx)
x = 0;
/* plot new image */
putimage(x, y-ARROW_SIZE, arrow, XOR_PUT);
}
/* clean up */
free(arrow);
closegraph();
return 0;
}
void draw_arrow(int x, int y)
{
/* draw an arrow on the screen */
moveto(x, y);
linerel(4*ARROW_SIZE, 0);
linerel(-2*ARROW_SIZE, -1*ARROW_SIZE);
linerel(0, 2*ARROW_SIZE);
linerel(2*ARROW_SIZE, -1*ARROW_SIZE);
}
===========
initgraph <GRAPHICS.H>
===========
Initializes the graphics system
Declaration:
void far initgraph(int far *graphdriver,
int far *graphmode, char far *pathtodriver);
Remarks:
To start the graphics system, you must first
call initgraph.
initgraph initializes the graphics system by
loading a graphics driver from disk (or
validating a registered driver) then putting
the system into graphics mode.
initgraph also resets all graphics settings
(color, palette, current position, viewport,
etc.) to their defaults, then resets
graphresult to 0.
Argument | What It Is/Does
--------------+-----------------------------------------------------------
*graphdriver | Integer that specifies the graphics driver to be used.
| You can give graphdriver a value using a constant of
| the graphics_drivers enumeration type.
--------------+-----------------------------------------------------------
*graphmode | Integer that specifies the initial graphics mode
| (unless *graphdriver = DETECT).
| If *graphdriver = DETECT, initgraph sets *graphmode to
| the highest resolution available for the detected driver.
| You can give *graphmode a value using a constant of
| the graphics_modes enumeration type.
--------------+-----------------------------------------------------------
pathtodriver | Specifies the directory path where initgraph looks for
| graphics drivers (*.BGI) first.
| * If they're not there, initgraph looks in the current
| directory.
| * If pathtodriver is null, the driver files must be in
| the current directory.
| This is also the path settextstyle searches for the stroked
| character font files (*.CHR).
*graphdriver and *graphmode must be set to
valid graphics_drivers and graphics_mode
values or you'll get unpredictable results.
(The exception is graphdriver = DETECT.)
After a call to initgraph, *graphdriver is
set to the current graphics driver, and
*graphmode is set to the current graphics
mode.
You can tell initgraph to use a particular
graphics driver and mode, or to autodetect
the attached video adapter at run time and
pick the corresponding driver.
If you tell initgraph to autodetect, it calls
detectgraph to select a graphics driver and
mode.
Normally, initgraph loads a graphics driver
by allocating memory for the driver (through
_graphgetmem), then loading the appropriate
.BGI file from disk.
As an alternative to this dynamic loading
scheme, you can link a graphics driver file
(or several of them) directly into your
executable program file.
Return Value:
initgraph always sets the internal error
code.
* On success, initgraph sets the code to 0
* On error, initgraph sets *graphdriver to
-2, -3, -4, or -5, and graphresult
returns the same value.
See the enumeration graphics_errors for
definitions of error codes and graphresult
return values.
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | | | | |
+-----+------+---------+--------+----------+
See Also:
closegraph
getdefaultpalette
getdrivername
getgraphmode
getmoderange
graphdefaults
installuserdriver
registerbgidriver
registerbgifont
restorecrtmode
setgraphbufsize
setgraphmode
Example:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
/* initialize graphics mode */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* return with error code */
}
/* draw a line */
line(0, 0, getmaxx(), getmaxy());
/* clean up */
getch();
closegraph();
return 0;
}
===================
installuserdriver <GRAPHICS.H>
===================
Installs a vendor-added device driver to the BGI device driver table
Declaration:
int far installuserdriver(char far *name, int huge (*detect)(void));
Remarks:
With installuserdriver, you can add a
vendor-added device driver to the BGI
internal table.
Argument| What It Is/Does
--------+------------------------------------------------------------------
name | Name of the new device driver file (filename.BGI)
detect | Points to an optional autodetect function that can accompany the
| new driver. This autodetect function takes no parameters and
| returns an integer value.
You can install up to 10 drivers at one time.
There are two ways to use the vendor-supplied
driver:
1) passing the driver number directly to
initgraph, or
2) linking in an autodetect function.
Passing driver number directly to initgraph
===========================================
Assume you have a new video card called the
Spiffy Graphics Array (SpGA) and that the
SpGA manufacturer provided you with a BGI
device driver (SPGA.BGI).
The easiest way to use this driver is to
install it by calling installuserdriver and
then passing the return value (the assigned
driver number) directly to initgraph.
Linking in an autodetect function
=================================
The more general way to use this hypothetical
SpGA driver is to link in an autodetect
function that will be called by initgraph as
part of its hardware-detection logic.
(Presumably, the manufacturer of the SpGA
gave you this autodetect function).
When you install the driver (by calling
installuserdriver), you pass the address of
this autodetect function, along with the
device driver's file name.
After you install the device driver's file
name and the SpGA autodetect function, call
initgraph and let it go through its normal
autodetection process.
Before initgraph calls its own built-in
autodetection function (detectgraph), it
first calls the SpGA autodetect function.
* If the SpGA autodetect function doesn't
find the SpGA hardware, it returns a value
of -11 (grError), and initgraph proceeds with
its normal hardware detection logic.
(This can include calling any other
vendor-supplied autodetection functions in
the order in which they were "installed").
* If, however, the autodetect function
determines that an SpGA is present, it
returns a non-negative mode number.
initgraph then:
1) locates and loads SPGA.BGI
2) puts the hardware into the default
graphics mode recommended by the
autodetect function
3) returns control to your program.
Return Value:
Returns the driver number you pass to
initgraph to manually select the newly
installed driver.
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | | | | |
+-----+------+---------+--------+----------+
See Also:
initgraph
registerbgidriver
Example:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
/* function prototypes */
int huge detectEGA(void);
void checkerrors(void);
int main(void)
{
int gdriver, gmode;
/* install a user written device driver */
gdriver = installuserdriver("EGA", detectEGA);
/* must force use of detection routine */
gdriver = DETECT;
/* check for any installation errors */
checkerrors();
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* check for any initialization errors */
checkerrors();
/* draw a line */
line(0, 0, getmaxx(), getmaxy());
/* clean up */
getch();
closegraph();
return 0;
}
/* detects EGA or VGA cards */
int huge detectEGA(void)
{
int driver, mode, sugmode = 0;
detectgraph(&driver, &mode);
if ((driver == EGA) || (driver == VGA))
/* return suggested video mode number */
return sugmode;
else
/* return an error code */
return grError;
}
/* check for and report any graphics errors */
void checkerrors(void)
{
int errorcode;
/* read result of last graphics operation */
errorcode = graphresult();
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
}
=================
installuserfont <GRAPHICS.H>
=================
Loads a font file (.CHR) that is not built into the BGI system
Declaration: int far installuserfont(char far *name);
Remarks:
Arg. | What It Is/Does
------+--------------------------------------
name | Path name to a font file containing
| a stroked font
You can install up to 20 fonts at one time.
installuserfont returns a font ID number that
can then be passed to settextstyle to select
the corresponding font.
Return Value:
* On success, returns a font ID number.
* On error (if the internal font table is
full), returns -11 (grError).
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | | | | |
+-----+------+---------+--------+----------+
See Also:
settextstyle
Example:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
/* function prototype */
void checkerrors(void);
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode;
int userfont;
int midx, midy;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
midx = getmaxx() / 2;
midy = getmaxy() / 2;
/* check for any initialization errors */
checkerrors();
/* install a user defined font file */
userfont = installuserfont("USER.CHR");
/* check for any installation errors */
checkerrors();
/* select the user font */
settextstyle(userfont, HORIZ_DIR, 4);
/* output some text */
outtextxy(midx, midy, "Testing!");
/* clean up */
getch();
closegraph();
return 0;
}
/* check for and report any graphics errors */
void checkerrors(void)
{
int errorcode;
/* read result of last graphics operation */
errorcode = graphresult();
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
}
=======================
line, linerel, lineto <GRAPHICS.H>
=======================
* line draws a line between two specified points
* linerel draws a line a relative distance from the current position (CP)
* lineto draws a line from the current position (CP) to (x,y)
Declaration:
* void far line(int x1, int y1, int x2, int y2);
* void far linerel(int dx, int dy);
* void far lineto(int x, int y);
Remarks:
* line draws a line from (x1, y1) to (x2, y2)
using the current color, line style, and
thickness. It does not update the current
position (CP).
* linerel draws a line from the CP to a point
that is a relative distance (dx, dy) from the
CP, then advances the CP by (dx, dy).
* lineto draws a line from the CP to (x, y),
then moves the CP to (x, y).
Return Value: None
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | | | | |
+-----+------+---------+--------+----------+
See Also:
getlinesettings
setcolor
setlinestyle
setvisualpage
setwritemode
Examples:
line example
linerel example
lineto example
line example
==============
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int xmax, ymax;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
/* an error occurred */
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
setcolor(getmaxcolor());
xmax = getmaxx();
ymax = getmaxy();
/* draw a diagonal line */
line(0, 0, xmax, ymax);
/* clean up */
getch();
closegraph();
return 0;
}
linerel example
=================
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
char msg[80];
/* initialize graphics and local
variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
/* move the C.P. to location (20, 30) */
moveto(20, 30);
/* create and output a
message at (20, 30) */
sprintf(msg, " (%d, %d)", getx(), gety());
outtextxy(20, 30, msg);
/* draw a line to a point a relative
distance away from the current
value of C.P. */
linerel(100, 100);
/* create and output a message at C.P. */
sprintf(msg, " (%d, %d)", getx(), gety());
outtext(msg);
/* clean up */
getch();
closegraph();
return 0;
}
lineto example
================
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
char msg[80];
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
/* move the C.P. to location (20, 30) */
moveto(20, 30);
/* create and output a
message at (20, 30) */
sprintf(msg, " (%d, %d)", getx(), gety());
outtextxy(20, 30, msg);
/* draw a line to (100, 100) */
lineto(100, 100);
/* create and output a message at C.P. */
sprintf(msg, " (%d, %d)", getx(), gety());
outtext(msg);
/* clean up */
getch();
closegraph();
return 0;
}
=================
moverel, moveto <GRAPHICS.H>
=================
* moverel moves the current position (CP) a relative distance
* moveto moves the CP to (x, y)
Declaration:
* void far moverel(int dx, int dy);
* void far moveto(int x, int y);
Remarks:
* moverel moves the current position (CP) dx
pixels in the x direction and dy pixels in
the y direction.
* moveto moves the current position (CP) to
viewport position (x, y).
Return Value: None
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | | | | |
+-----+------+---------+--------+----------+
Examples:
moverel example
moveto example
moverel example
=================
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
char msg[80];
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
/* move the C.P. to location (20, 30) */
moveto(20, 30);
/* plot a pixel at the C.P. */
putpixel(getx(), gety(), getmaxcolor());
/* create and output a message at (20, 30) */
sprintf(msg, " (%d, %d)", getx(), gety());
outtextxy(20, 30, msg);
/* move to a point a relative distance */
/* away from the current value of C.P. */
moverel(100, 100);
/* plot a pixel at the C.P. */
putpixel(getx(), gety(), getmaxcolor());
/* create and output a message at C.P. */
sprintf(msg, " (%d, %d)", getx(), gety());
outtext(msg);
/* clean up */
getch();
closegraph();
return 0;
}
moveto example
================
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
char msg[80];
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
/* move the C.P. to location (20, 30) */
moveto(20, 30);
/* plot a pixel at the C.P. */
putpixel(getx(), gety(), getmaxcolor());
/* create and output a message at (20, 30) */
sprintf(msg, " (%d, %d)", getx(), gety());
outtextxy(20, 30, msg);
/* move to (100, 100) */
moveto(100, 100);
/* plot a pixel at the C.P. */
putpixel(getx(), gety(), getmaxcolor());
/* create and output a message at C.P. */
sprintf(msg, " (%d, %d)", getx(), gety());
outtext(msg);
/* clean up */
getch();
closegraph();
return 0;
}
====================
outtext, outtextxy <GRAPHICS.H>
====================
* outtext displays a string in the viewport (graphics mode)
* outtextxy displays a string at the specified location (graphics mode)
Declaration:
* void far outtext(char far *textstring);
* void far outtextxy(int x, int y, char far *textstring);
Remarks:
outtext and outtextxy display a text string,
using the current justification settings and
the current font, direction, and size.
* outtext outputs textstring at the current position (CP)
* outtextxy displays textstring in the viewport at the position (x, y)
To maintain code compatibility when using
several fonts, use textwidth and textheight
to determine the dimensions of the string.
If a string is printed with the default font
using outtext or outtextxy, any part of the
string that extends outside the current
viewport is truncated.
With outtext, if the horizontal text
justification is LEFT_TEXT and the text
direction is HORIZ_DIR, the CP's x-coordinate
is advanced by textwidth(textstring).
Otherwise, the CP remains unchanged.
outtext and outtextxy are for use in graphics
mode; they will not work in text mode.
Return Value: None
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | | | | |
+-----+------+---------+--------+----------+
See Also:
gettextsettings
settextjustify
Examples:
outtext example
outtextxy example
outtext example
=================
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int midx, midy;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
midx = getmaxx() / 2;
midy = getmaxy() / 2;
/* move the C.P. to the center of the screen */
moveto(midx, midy);
/* output text starting at the C.P. */
outtext("This ");
outtext("is ");
outtext("a ");
outtext("test.");
/* clean up */
getch();
closegraph();
return 0;
}
outtextxy example
===================
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int midx, midy;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
midx = getmaxx() / 2;
midy = getmaxy() / 2;
/* output text at the center of the screen */
/* Note: the C.P. doesn't get changed. */
outtextxy(midx, midy, "This is a test.");
/* clean up */
getch();
closegraph();
return 0;
}
===========
rectangle <GRAPHICS.H>
===========
Draws a rectangle (graphics mode)
Declaration:
void far rectangle(int left, int top, int right, int bottom);
Remarks:
rectangle draws a rectangle in the current
line style, thickness, and drawing color.
(left,top) is the upper left corner of the
rectangle, and (right,bottom) is its lower
right corner.
Return Value: None
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | | | | |
+-----+------+---------+--------+----------+
See Also:
bar
bar3d
setcolor
setlinestyle
Example:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int left, top, right, bottom;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
left = getmaxx() / 2 - 50;
top = getmaxy() / 2 - 50;
right = getmaxx() / 2 + 50;
bottom = getmaxy() / 2 + 50;
/* draw a rectangle */
rectangle(left,top,right,bottom);
/* clean up */
getch();
closegraph();
return 0;
}
======================
registerbgidriver, <GRAPHICS.H>
registerfarbgidriver
======================
Registers linked-in graphics driver
Declaration:
* int registerbgidriver(void (*driver)(void));
* int far registerfarbgidriver(void far *driver);
Remarks:
* registerbgidriver enables a user to load a
driver file and "register" the driver.
* registerfarbgidriver is used to register
far drivers.
Once the driver's memory location has been
passed to registerbgidriver, initgraph uses
the registered driver.
A user-registered driver can be loaded from
disk onto the heap, or converted to an .OBJ
file (using BINOBJ.EXE) and linked into the
.EXE.
Calling registerbgidriver informs the BGI
graphics system that the driver *driver was
included at link time.
registerbgidriver checks the linked-in code
for the specified driver. If the code is
valid, it registers the code in internal
tables.
Linked-in drivers are discussed in detail in
UTIL.DOC.
By using the name of a linked-in driver in a
call to registerbgidriver, you also tell the
compiler (and linker) to link in the object
file with that public name.
Far drivers
===========
Far drivers are created with the /F switch of
the BGIOBJ utility. This switch is described
in UTIL.DOC (an online text file included
with your distribution disks).
Return Value
* On success, returns a negative graphics
error code if the specified driver or
font is invalid.
* Otherwise, returns the driver number.
If you register a user-supplied driver, you
MUST pass the result of registerbgidriver to
initgraph as the drive number to be used.
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | | | | |
+-----+------+---------+--------+----------+
See Also:
graphresult
initgraph
installuserdriver
registerbgifont
Example (registerbgidriver only):
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
/* register a driver that was added into graphics.lib */
/* For information on adding the driver, see the
/* BGIOBJ section of UTIL.DOC */
errorcode = registerbgidriver(EGAVGA_driver);
/* report any registration errors */
if (errorcode < 0)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
/* draw a line */
line(0, 0, getmaxx(), getmaxy());
/* clean up */
getch();
closegraph();
return 0;
}
=================
registerbgifont <GRAPHICS.H>
=================
Registers linked-in stroked-font code
Declaration:
* int registerbgifont(void (*font)(void));
* int registerfarbgifont(void far *font);
Remarks:
* Calling registerbgifont informs the
graphics system that the font *font was
included at link time.
* registerfarbgifont is used to register far
fonts.
registerbgidriver checks the linked-in code
for the specified font. If the code is valid,
it registers the code in internal tables.
Linked-in fonts are discussed in detail under
BGIOBJ in UTIL.DOC (an online text file
included with your distribution disks).
By using the name of a linked-in font in a
call to registerbgifont, you also tell the
compiler (and linker) to link in the object
file with that public name.
If you register a user-supplied font, you
MUST pass the result of registerbgifont to
settextstyle as the font number to be used.
Far fonts are created with the /F switch of
the BGIOBJ utility. This switch is described
in UTIL.DOC.
Return Value
* On success, returns a negative graphics error code if the specified
font is invalid.
* Otherwise, returns the font number of the registered font.
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | | | | |
+-----+------+---------+--------+----------+
See Also:
graphresult
initgraph
installuserdriver
registerbgidriver
settextstyle
Example (registerbgifont only):
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int midx, midy;
/* register a font file that was added into graphics.lib */
/* For information on adding the font, see the
/* BGIOBJ section of UTIL.DOC */
errorcode = registerbgifont(triplex_font);
/* report any registration errors */
if (errorcode < 0)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
midx = getmaxx() / 2;
midy = getmaxy() / 2;
/* select the registered font */
settextstyle(TRIPLEX_FONT, HORIZ_DIR, 4);
/* output some text */
settextjustify(CENTER_TEXT, CENTER_TEXT);
outtextxy(midx, midy, "The TRIPLEX FONT");
/* clean up */
getch();
closegraph();
return 0;
}
================
restorecrtmode <GRAPHICS.H>
================
Restores screen mode to pre-initgraph setting
Declaration: void far restorecrtmode(void);
Remarks:
restorecrtmode restores the original video
mode detected by initgraph.
This function can be used in conjunction with
setgraphmode to switch back and forth between
text and graphics modes.
* NOTE: Do NOT use textmode for this purpose.
Use textmode only when the screen is in text
mode, to change to a different text mode.
Return Value:
None
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | | | | |
+-----+------+---------+--------+----------+
See Also:
getgraphmode
initgraph
setgraphmode
Example:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int x, y;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
x = getmaxx() / 2;
y = getmaxy() / 2;
/* output a message */
settextjustify(CENTER_TEXT, CENTER_TEXT);
outtextxy(x, y, "Press any key to exit graphics:");
getch();
/* restore system to text mode */
restorecrtmode();
printf("We're now in text mode.\n");
printf("Press any key to return to graphics mode:");
getch();
/* return to graphics mode */
setgraphmode(getgraphmode());
/* output a message */
settextjustify(CENTER_TEXT, CENTER_TEXT);
outtextxy(x, y, "We're back in graphics mode.");
outtextxy(x, y+textheight("W"), "Press any key to halt:");
/* clean up */
getch();
closegraph();
return 0;
}
==============================
ellipse, fillellipse, sector <GRAPHICS.H>
==============================
* ellipse draws an elliptical arc
* fillellipse draws and fills an ellipse
* sector draws and fills an elliptical pie slice
Declaration:
* void far ellipse(int x, int y, int stangle, int endangle,
int xradius, int yradius);
* void far fillellipse(int x, int y,
int xradius, int yradius);
* void far sector(int x, int y, int stangle, int endangle,
int xradius, int yradius);
Remarks:
ellipse draws an elliptical arc in the
current drawing color.
fillellipse draws an ellipse, then fills the
ellipse with the current fill color and fill
pattern.
sector draws and fills an elliptical pie
slice in the current drawing color, then
fills it using the pattern and color defined
by setfillstyle or setfillpattern.
Argument | What It Is
----------+-------------------
(x,y) | Center of ellipse
xradius | Horizontal axis
yradius | Vertical axis
stangle | Starting angle
endangle | Ending angle
The ellipse or sector travels from stangle to
endangle.
If stangle = 0 and endangle = 360, the call
to ellipse draws a complete ellipse.
Angle for ellipse, fillellipse, and sector (counter-clockwise)
==============================================================
90
degrees
|
|
180 ------+------ 0 degrees,
degrees | 360 degrees
|
270 degrees
The linestyle parameter does not affect arcs,
circles, ellipses, or pie slices. Only the
thickness parameter is used.
Return Value: None
If an error occurs while the elliptical pie
slice is filling, graphresult returns -6
(grNoScanMem).
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | | | | |
+-----+------+---------+--------+----------+
See Also
arc
circle
getaspectratio
pieslice
setaspectratio
Example:
ellipse example
fillellipse example
sector example
fillellipse example
=====================
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request autodetection */
int gdriver = DETECT, gmode, errorcode;
int midx, midy, i;
int xradius = 100, yradius = 50;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) { /* an error occurred */
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
midx = getmaxx() / 2;
midy = getmaxy() / 2;
/* loop through the fill patterns */
for (i = EMPTY_FILL; i < USER_FILL; i++) {
/* set fill pattern */
setfillstyle(i, getmaxcolor());
/* draw a filled ellipse */
fillellipse(midx, midy, xradius, yradius);
getch();
}
/* clean up */
closegraph();
return 0;
}
ellipse example
=================
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int midx, midy;
int stangle = 0, endangle = 360;
int xradius = 100, yradius = 50;
/* initialize graphics, local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk)
/* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
/* terminate with an error code */
}
midx = getmaxx() / 2;
midy = getmaxy() / 2;
setcolor(getmaxcolor());
/* draw ellipse */
ellipse(midx, midy, stangle, endangle,
xradius, yradius);
/* clean up */
getch();
closegraph();
return 0;
}
sector example
================
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int midx, midy, i;
int stangle = 45, endangle = 135;
int xrad = 100, yrad = 50;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
midx = getmaxx() / 2;
midy = getmaxy() / 2;
/* loop through the fill patterns */
for (i=EMPTY_FILL; i<USER_FILL; i++)
{
/* set the fill style */
setfillstyle(i, getmaxcolor());
/* draw the sector slice */
sector(midx, midy, stangle, endangle, xrad, yrad);
getch();
}
/* clean up */
closegraph();
return 0;
}
==============================
setactivepage, setvisualpage <GRAPHICS.H>
==============================
* setactivepage sets the active page for graphics output
* setvisualpage sets the visual graphics page number
Declaration:
* void far setactivepage(int page);
* void far setvisualpage(int page);
Remarks:
* setactivepage makes page the active
graphics page. All subsequent graphics output
will be directed to that graphics page.
* setvisualpage makes page the visual
graphics page.
The active graphics page might not be the one
you see onscreen, depending on how many
graphics pages are available on your system.
Only the EGA, VGA, and Hercules graphics
cards support multiple pages.
The visual page is the one actually displayed
on the screen. Graphics functions write
output to the active page.
Return Value:
None
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | | | | |
+-----+------+---------+--------+----------+
See Also:
graphresult
Example (for both functions):
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* select a driver and mode that supports multiple pages. */
int gdriver = EGA, gmode = EGAHI, errorcode;
int x, y, ht;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
x = getmaxx() / 2;
y = getmaxy() / 2;
ht = textheight("W");
/* select the off screen page for drawing */
setactivepage(1);
/* draw a line on page #1 */
line(0, 0, getmaxx(), getmaxy());
/* output a message on page #1 */
settextjustify(CENTER_TEXT, CENTER_TEXT);
outtextxy(x, y, "This is page #1:");
outtextxy(x, y+ht, "Press any key to halt:");
/* select drawing to page #0 */
setactivepage(0);
/* output a message on page #0 */
outtextxy(x, y, "This is page #0.");
outtextxy(x, y+ht, "Press any key to view page #1:");
getch();
/* select page #1 as the visible page */
setvisualpage(1);
/* clean up */
getch();
closegraph();
return 0;
}
==============
setfillstyle <GRAPHICS.H>
==============
Sets the fill pattern and color
Declaration: void far setfillstyle(int pattern, int color);
Remarks:
setfillstyle sets the current fill pattern
and fill color.
To set a user-defined fill pattern, do not
give a pattern of 12 (USER_FILL) to
setfillstyle; instead, call setfillpattern.
The enumeration fill_patterns, defined in
GRAPHICS.H, gives names for the predefined
fill patterns, plus an indicator for a
user-defined pattern.
Return Value: None
If invalid input is passed to setfillstyle,
graphresult returns -11 (grError), and the
current fill pattern and fill color remain
unchanged.
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | | | | |
+-----+------+---------+--------+----------+
See Also:
bar
bar3d
fillpoly
floodfill
getfillsettings
graphresult
pieslice
sector
Example:
#include <graphics.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>
/* the names of the fill styles supported */
char *fname[] = { "EMPTY_FILL",
"SOLID_FILL",
"LINE_FILL",
"LTSLASH_FILL",
"SLASH_FILL",
"BKSLASH_FILL",
"LTBKSLASH_FILL",
"HATCH_FILL",
"XHATCH_FILL",
"INTERLEAVE_FILL",
"WIDE_DOT_FILL",
"CLOSE_DOT_FILL",
"USER_FILL"
};
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int style, midx, midy;
char stylestr[40];
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
midx = getmaxx() / 2;
midy = getmaxy() / 2;
for (style = EMPTY_FILL; style < USER_FILL; style++)
{
/* select the fill style */
setfillstyle(style, getmaxcolor());
/* convert style into a string */
strcpy(stylestr, fname[style]);
/* fill a bar */
bar3d(0, 0, midx-10, midy, 0, 0);
/* output a message */
outtextxy(midx, midy, stylestr);
/* wait for a key */
getch();
cleardevice();
}
/* clean up */
getch();
closegraph();
return 0;
}
=================
setgraphbufsize <GRAPHICS.H)
=================
Changes the size of the internal graphics buffer
Declaration: unsigned far setgraphbufsize(unsigned bufsize);
Remarks:
Some of the graphics routines (such as
floodfill) use a memory buffer that is
allocated when initgraph is called, and
released when closegraph is called.
The default size of this buffer, allocated by
_graphgetmem, is 4,096 bytes.
You can make this buffer smaller (to save
memory space) or bigger (if, for example,
a call to floodfill produces error -7: Out of
flood memory).
setgraphbufsize tells initgraph how much
memory to allocate for this internal graphics
buffer when it calls _graphgetmem.
You must call setgraphbufsize before calling
initgraph. Once initgraph has been called,
all calls to setgraphbufsize are ignored
until after the next call to closegraph.
Return Value:
setgraphbufsize returns the previous size of
the internal buffer.
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | | | | |
+-----+------+---------+--------+----------+
See Also:
_graphfreemem
_graphgetmem
closegraph
initgraph
sector
Example:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#define BUFSIZE 1000 /* internal graphics
buffer size */
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int x, y, oldsize;
char msg[80];
/* set the size of the internal graphics buffer */
/* before making a call to initgraph. */
oldsize = setgraphbufsize(BUFSIZE);
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
x = getmaxx() / 2;
y = getmaxy() / 2;
/* output some messages */
sprintf(msg, "Graphics buffer size: %d", BUFSIZE);
settextjustify(CENTER_TEXT, CENTER_TEXT);
outtextxy(x, y, msg);
sprintf(msg, "Old graphics buffer size: %d", oldsize);
outtextxy(x, y+textheight("W"), msg);
/* clean up */
getch();
closegraph();
return 0;
}
==============
setlinestyle <GRAPHICS.H>
==============
Sets the current line style and width or pattern
Declaration:
void far setlinestyle(int linestyle, unsigned upattern, int thickness);
Remarks:
setlinestyle sets the style for all lines
drawn by line, lineto, rectangle, drawpoly,
etc.
Return Value:
If invalid input is passed to setlinestyle,
graphresult returns -11, and the current line
style remains unchanged.
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | | | | |
+-----+------+---------+--------+----------+
See Also:
arc
bar3d
circle
drawpoly
ellipse
getlinesettings
graphresult
line
linerel
lineto
pieslice
rectangle
sector
Example:
#include <graphics.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>
/* the names of the line styles supported */
char *lname[] = {
"SOLID_LINE",
"DOTTED_LINE",
"CENTER_LINE",
"DASHED_LINE",
"USERBIT_LINE"
};
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int style, midx, midy, userpat;
char stylestr[40];
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
midx = getmaxx() / 2;
midy = getmaxy() / 2;
/* a user defined line pattern */
/* binary: "0000000000000001" */
userpat = 1;
for (style=SOLID_LINE; style<=USERBIT_LINE; style++)
{
/* select the line style */
setlinestyle(style, userpat, 1);
/* convert style into a string */
strcpy(stylestr, lname[style]);
/* draw a line */
line(0, 0, midx-10, midy);
/* draw a rectangle */
rectangle(0, 0, getmaxx(), getmaxy());
/* output a message */
outtextxy(midx, midy, stylestr);
/* wait for a key */
getch();
cleardevice();
}
/* clean up */
closegraph();
return 0;
}
============
setpalette <GRAPHICS.H>
============
Changes one palette color
Declaration: void far setpalette(int colornum, int color);
Remarks:
setpalette changes the colornum entry in the
palette to color.
For example, setpalette(0,5) changes the
first color in the current palette (the
background color) to actual color number 5.
If size is the number of entries in the
current palette, colornum can range between 0
and (size - 1).
You can partially (or completely) change the
colors in the EGA/VGA palette with
setpalette.
On a CGA, you can only change the first entry
in the palette (colornum = 0, the background
color) with a call to setpalette.
The color parameter passed to setpalette can
be represented by symbolic constants defined
in GRAPHICS.H.
Valid colors depend on the current graphics
driver and current graphics mode.
Changes made to the palette are seen
immediately onscreen. Each time a palette
color is changed, all occurrences of that
color onscreen change to the new color value.
+-------------------------------------------+
| NOTE: setpalette can't be used with the |
| IBM-8514 driver. Use setrgbpalette |
| instead. |
+-------------------------------------------+
Return Value:
If invalid input is passed to setpalette,
graphresult returns -11, and the current
palette remains unchanged.
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | | | | |
+-----+------+---------+--------+----------+
See Also:
getpalette
graphresult
setallpalette
setbkcolor
setcolor
Example:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int color, maxcolor, ht;
int y = 10;
char msg[80];
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
maxcolor = getmaxcolor();
ht = 2 * textheight("W");
/* display the default colors */
for (color=1; color<=maxcolor; color++)
{
setcolor(color);
sprintf(msg, "Color: %d", color);
outtextxy(1, y, msg);
y += ht;
}
/* wait for a key */
getch();
/* black out the colors one by one */
for (color=1; color<=maxcolor; color++)
{
setpalette(color, BLACK);
getch();
}
/* clean up */
closegraph();
return 0;
}
===============
setrgbpalette <GRAPHICS.H>
===============
Defines colors for IBM-8514 graphics card
Declaration:
void far setrgbpalette(int colornum, int red, int green, int blue);
Remarks:
setrgbpalette can be used with the IBM 8514
and VGA drivers.
colornum defines the palette entry to be
loaded, while red, green, and blue define the
component colors of the palette entry.
For the IBM 8514 display (and the VGA in 256K
color mode), colornum is in the range 0 to
255.
For the remaining modes of the VGA, colornum
is in the range 0 to 15.
Only the lower byte of red, green, or blue is
used, and out of each byte, only the 6 most
significant bits are loaded in the palette.
For compatibility with other 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 they can
be changed with setrgbpalette.
Return Value: None
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | | | | |
+-----+------+---------+--------+----------+
See Also:
setpalette
Example:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* select a driver and mode that supports the use */
/* of the setrgbpalette function. */
int gdriver = VGA, gmode = VGAHI, errorcode;
struct palettetype pal;
int i, ht, y, xmax;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
/* grab a copy of the palette */
getpalette(&pal);
/* create gray scale */
for (i=0; i<pal.size; i++)
setrgbpalette(pal.colors[i], i*4, i*4, i*4);
/* display the gray scale */
ht = getmaxy() / 16;
xmax = getmaxx();
y = 0;
for (i=0; i<pal.size; i++)
{
setfillstyle(SOLID_FILL, i);
bar(0, y, xmax, y+ht);
y += ht;
}
/* clean up */
getch();
closegraph();
return 0;
}
================
settextjustify <GRAPHICS.H>
================
Sets text justification for graphics mode
Declaration: void far settextjustify(int horiz, int vert);
Remarks:
Text output after a call to settextjustify is
justified around the current position (CP)
horizontally and vertically, as specified.
The default justification settings are
* LEFT_TEXT (for horizontal) and
* TOP_TEXT (for vertical)
The enumeration text_just in GRAPHICS.H
provides names for the horiz and vert
settings passed to settextjustify.
settextjustify affects text written with
outtext and can't be used with text-mode and
stream functions.
Return Value:
If invalid input is passed to settextjustify,
graphresult returns -11, and the current text
justification remains unchanged.
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | | | | |
+-----+------+---------+--------+----------+
See Also:
gettextsettings
graphresult
outtext
settextstyle
Example:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
/* function prototype */
void xat(int x, int y);
/* horizontal text justification settings */
char *hjust[] = { "LEFT_TEXT",
"CENTER_TEXT",
"RIGHT_TEXT"
};
/* vertical text justification settings */
char *vjust[] = { "LEFT_TEXT",
"CENTER_TEXT",
"RIGHT_TEXT"
};
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int midx, midy, hj, vj;
char msg[80];
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
midx = getmaxx() / 2;
midy = getmaxy() / 2;
/* loop through text justifications */
for (hj=LEFT_TEXT; hj<=RIGHT_TEXT; hj++)
for (vj=LEFT_TEXT; vj<=RIGHT_TEXT; vj++)
{
cleardevice();
/* set the text justification */
settextjustify(hj, vj);
/* create a message string */
sprintf(msg, "%s %s", hjust[hj], vjust[vj]);
/* create cross hairs on the screen */
xat(midx, midy);
/* output the message */
outtextxy(midx, midy, msg);
getch();
}
/* clean up */
closegraph();
return 0;
}
/* draw an "x" at (x, y) */
void xat(int x, int y)
{
line(x-4, y, x+4, y);
line(x, y-4, x, y+4);
}
==============
settextstyle <GRAPHICS.H>
==============
Sets the current text characteristics
Declaration:
void far settextstyle(int font, int direction, int charsize);
Remarks:
settextstyle sets the text font, the direction
in which text is displayed, and the size of
the characters.
A call to settextstyle affects all text
output by outtext and outtextxy.
font
====
One 8x8 bit-mapped font and several "stroked"
fonts are available. The 8x8 bit-mapped font,
the default, is built into the graphics
system.
The enumeration font_names, defined in
GRAPHICS.H, provides names for the
different font settings.
direction
=========
Font directions supported are horizontal text
(left to right) and vertical text (rotated
90 degrees counterclockwise).
The default direction is HORIZ_DIR.
Name | Value | Direction
-----------+-------+---------------
HORIZ_DIR | 0 | Left to right
VERT_DIR | 1 | Bottom to top
charsize
========
The size of each character can be magnified
using the charsize factor.
If charsize is non-zero, it can affect
bit-mapped or stroked characters.
A charsize value of 0 can be used only with
stroked fonts.
charsize |
value | outtext and outtextxy ...
----------+----------------------------------------------------------------
0 | Magnify the stroked font text using either the default
| character magnification factor (4) or the user-defined
| character size given by setusercharsize.
1 | Display characters from the 8x8 bit-mapped font in an
| 8x8 pixel rectangle onscreen.
2 | Display characters from the 8x8 bit-mapped font in a
| 16x16 pixel rectangle
3 | Display characters from the 8x8 bit-mapped font in a
| 24x24 pixel rectangle
... | (Up to a limit of ten times the normal size)
Always use textheight and textwidth to
determine the actual dimensions of the text.
Return Value: None
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | | | | |
+-----+------+---------+--------+----------+
See Also:
gettextsettings
graph_charsize
graphresult
installuserfont
settextjustify
Example:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
/* the names of the text styles supported */
char *fname[] = { "DEFAULT font",
"TRIPLEX font",
"SMALL font",
"SANS SERIF font",
"GOTHIC font"
};
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int style, midx, midy;
int size = 1;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
midx = getmaxx() / 2;
midy = getmaxy() / 2;
settextjustify(CENTER_TEXT, CENTER_TEXT);
/* loop through the available text styles */
for (style=DEFAULT_FONT; style<=GOTHIC_FONT; style++)
{
cleardevice();
if (style == TRIPLEX_FONT)
size = 4;
/* select the text style */
settextstyle(style, HORIZ_DIR, size);
/* output a message */
outtextxy(midx, midy, fname[style]);
getch();
}
/* clean up */
closegraph();
return 0;
}
"Stroked" Fonts
=================
Stroked fonts (as set by settextstyle) are
stored in *.CHR disk files, and only one at
a time is kept in memory.
Therefore, when you select a stroked font
(different from the last selected stroked
font), the corresponding *.CHR file must be
loaded from disk.
To avoid this loading when several stroked
fonts are used, you can link font files into
your program.
To do this, you convert them into object
files with the BGIOBJ utility, then register
them through registerbgifont, as described in
UTIL.DOC, included with your distributions
disks.
=================
setusercharsize <GRAPHICS.H>
=================
User-defined character magnification factor for stroked fonts
Declaration:
void far setusercharsize(int multx, int divx, int multy, int divy);
Remarks:
setusercharsize gives you finer control over
the size of text from stroked fonts used with
graphics functions.
The values set by setusercharsize are active
only if charsize = 0, as set by a previous
call to settextstyle.
With setusercharsize, you specify factors by
which the width and height are scaled.
* the default width is scaled by multx : divx
* the default height is scaled by multy : divy.
For example, to make text twice as wide and
50% taller than the default, set
multx = 2; divx = 1; /* 2:1 */
multy = 3; divy = 2; /* 3:2 */
Return Value:
None
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | | | | |
+-----+------+---------+--------+----------+
See Also:
gettextsettings
graphresult
settextstyle
Example:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request autodetection */
int gdriver = DETECT, gmode, errorcode;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
/* select a text style */
settextstyle(TRIPLEX_FONT, HORIZ_DIR, 4);
/* move to the text starting position */
moveto(0, getmaxy() / 2);
/* output some normal text */
outtext("Norm ");
/* make the text 1/3 the normal width */
setusercharsize(1, 3, 1, 1);
outtext("Short ");
/* make the text 3 times normal width */
setusercharsize(3, 1, 1, 1);
outtext("Wide");
/* clean up */
getch();
closegraph();
return 0;
}
=============
setviewport <GRAPHICS.H>
=============
Sets the current viewport for graphics output
Declaration:
void far setviewport(int left, int top, int right, int bottom, int clip);
Remarks:
setviewport establishes a new viewport for
graphics output.
The viewport's corners are given in absolute
screen coordinates by (left,top) and
(right,bottom).
The current position (CP) is moved to (0,0)
in the new window.
The clip argument determines whether drawings
are clipped (truncated) at the current
viewport boundaries. If clip is non-zero, all
drawings will be clipped to the current
viewport.
Return Value:
setviewport does not return.
If invalid input is passed to setviewport,
graphresult returns -11, and the current view
settings remain unchanged.
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | | | | |
+-----+------+---------+--------+----------+
See Also:
clearviewport
getviewsettings
graphresult
Example:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#define CLIP_ON 1 /* activates clipping in
viewport */
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
setcolor(getmaxcolor());
/* message in default full-screen viewport */
outtextxy(0, 0, "* <-- (0, 0) in default viewport");
/* create a smaller viewport */
setviewport(50, 50, getmaxx()-50, getmaxy()-50, CLIP_ON);
/* display some text */
outtextxy(0, 0, "* <-- (0, 0) in smaller viewport");
/* clean up */
getch();
closegraph();
return 0;
}
==============
setwritemode <GRAPHICS.H>
==============
Sets the writing mode for line drawing in graphics mode
Declaration: void far setwritemode(int mode);
Remarks:
Symbolic constants are defined for mode.
Each constant corresponds to a binary
operation between each byte in the line
and the corresponding bytes onscreen.
Constant |Value| What It Means
----------+-----+--------------------------------------------------------
COPY_PUT | 0 | Uses the assembly language MOV instruction, overwriting
| | with the line whatever is on the screen.
XOR_PUT | 1 | Uses the XOR command to combine the line with the
| | screen.
| | Two successive XOR commands will erase the line and
| | restore the screen to its original appearance.
setwritemode currently works only with line,
linerel, lineto, rectangle, and drawpoly.
Return Value:
None
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | | | | |
+-----+------+---------+--------+----------+
See Also:
drawpoly
line
linerel
lineto
putimage
rectangle
Example:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main()
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int xmax, ymax;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
xmax = getmaxx();
ymax = getmaxy();
/* select XOR drawing mode */
setwritemode(XOR_PUT);
/* draw a line */
line(0, 0, xmax, ymax);
getch();
/* erase the line by drawing over it */
line(0, 0, xmax, ymax);
getch();
/* select overwrite drawing mode */
setwritemode(COPY_PUT);
/* draw a line */
line(0, 0, xmax, ymax);
/* clean up */
getch();
closegraph();
return 0;
}
=======================
textheight, textwidth <GRAPHICS.H>
=======================
* textheight returns the height of a string in pixels
* textwidth returns the width of a string in pixels
Declaration:
* int far textheight(char far *textstring);
* int far textwidth(char far *textstring);
Remarks:
* textheight takes the current font size and
multiplication factor, and determines the
height of textstring in pixels.
* textwidth takes the string length, current
font size, and multiplication factor, and
determines the width of textstring in pixels.
These functions are useful for adjusting the
spacing between lines, computing viewport
heights, sizing a title to make it fit on a
graph or in a box, etc..
For example, with the 8x8 bit-mapped font and
a multiplication factor of 2 (set by
settextstyle), the string "Borland C++" is 16
pixels high.
Instead of doing the computations manually,
use textheight to compute the height of
strings, and use textwidth to compute their
width.
When you use these functions, no source code
modifications are required when you select
different fonts.
Return Value:
textheight returns the text height in pixels.
textwidth returns the text width in pixels.
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | | | | |
+-----+------+---------+--------+----------+
It works only with IBM PCs and compatibles
equipped with supported graphics display
adapters.
See Also:
gettextsettings
outtext
outtextxy
settextstyle
Examples:
textheight example
textwidth example
textwidth example
===================
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int x = 0, y = 0;
int i;
char msg[80];
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
y = getmaxy() / 2;
settextjustify(LEFT_TEXT, CENTER_TEXT);
for (i=1; i<11; i++)
{
/* select the text style, direction, and size */
settextstyle(TRIPLEX_FONT, HORIZ_DIR, i);
/* create a message string */
sprintf(msg, "Size: %d", i);
/* output the message */
outtextxy(x, y, msg);
/* advance to the end of the text */
x += textwidth(msg);
}
/* clean up */
getch();
closegraph();
return 0;
}
textheight example
====================
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int y = 0;
int i;
char msg[80];
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
/* draw some text on the screen */
for (i=1; i<11; i++)
{
/* select the text style, direction, and size */
settextstyle(TRIPLEX_FONT, HORIZ_DIR, i);
/* create a message string */
sprintf(msg, "Size: %d", i);
/* output the message */
outtextxy(1, y, msg);
/* advance to the next text line */
y += textheight(msg);
}
/* clean up */
getch();
closegraph();
return 0;
}