71: How do I get started
with graphics programming?
A:
(* This simple test shows the rudiments of getting started with
Turbo
Pascal graphics programming *)
uses Crt, Graph;
var grDriver : integer;
grMode : integer;
ErrCode : integer;
i, j : integer;
xm, ym : integer;
const CharSize : integer = 3;
begin
{ Request graphics driver autodetection }
grDriver := Detect;
{ Initialize graphics system and put hardware into graphics
mode }
{ The relevant .bgi driver is needed in the current directory
for example egavga.bgi }
InitGraph (grDriver, grMode, ' ');
{ Return an error code for the previous graphic operation }
ErrCode := GraphResult;
{ Test for initialialization success }
if ErrCode <> grOk then begin
Writeln ('Graphics error:', GraphErrorMsg(ErrCode)); halt; end;
{ Clear the output device and home the current pointer }
ClearDevice;
{}
{ Use your own coordinates }
xm := Round (GetMaxX / 100.0);
ym := Round (GetMaxY / 100.0);
{}
{ Set the current line width and style, optional }
SetLineStyle (SolidLn, 0, ThickWidth);
{ Set the drawing color }
SetColor (Yellow);
{ Draw a line }
Line (70*xm, 50*ym, 90*xm, 80*ym);
{}
{ Drawing bars }
{ Set the fill pattern and color }
SetFillStyle (SolidFill, Red);
Bar (0, 0, 25*xm, 25*ym);
{}
SetColor (Magenta);
SetFillStyle (SolidFill, Blue);
Bar3D (30*xm, 20*ym, 50*xm, 60*ym, 8*xm, TopOn);
{}
{ Writing text in the graphics mode }
{ Set the drawing color }
SetColor (LightCyan);
{ Set the current background color }
SetBkColor (Black);
{ Set style for text output in graphics mode }
SetTextStyle(DefaultFont, HorizDir, CharSize);
OutTextXY (0, 80*ym, 'Press any key');
{}
repeat until KeyPressed;
{}
{ Restore the original screen mode before graphics was
initialized }
RestoreCrtMode;
writeln ('That''s all folks');
{ Shut down the graphics system }
CloseGraph;
end.
For an example what you can do with graphics, see
111673 Oct 8 1993 ftp://garbo.uwasa.fi/pc/ts/tsdemo16.zip
tsdemo16.zip Assorted graphics demonstrations of functions etc
(or whatever is the current version).