
Lesson 8 - bgi Graphics
If you feel that Graphics is not important for you than skip to lesson 9 (File Handling). BGI Graphics is an advanced topic and need not be explicitly covered. It depends on your preference. Graphics lovers will surely be interested in this topic.
This lesson will cover:
Introduction to Graphics
Initialising Graphics
Graphics Statements
Advanced Graphics Statements
One might require the file egavga.bgi during this graphics tutorial. Click here if you don't have this file (or it is corrupt) and place it in the BGI directory in the Turbo Pascal installation folder.
COPYRIGHT NOTICE: It is assumed that you have bought Turbo Pascal once you have downloaded this file. This file is owned by its respective owners (Borland Company).
IMPORTANT DISCLAIMER: You will download and use this file at your own risk. We don't take any responsability for any damage whatsoever.
Introduction to Graphics
Graphics is a method of representing output material in a more complex, user-friendlier way than the crt. The crt is one of the few turbo pascal units or libraries. Turbo Pascal has a graphics library with which you can represent output material. Before you can call turbo pascal's graphics procedures and functions, you have to include the Graph unit in the Uses section (Eg. Uses Crt, Graph, Overlay, Printers etc...).
Initialising Graphics
Unlike the crt, graphics should be initialised, and it should be initialised at the beginning of the program or just before you start using graphics procedures and functions. This will help the compiler to link to the graphics file. Usually, the default graphics routines used by the Turbo Pascal's compiler is the EGAVGA.BGI. When you initialise the graphics, automatically your computer will use this file to output graphics material.
The famous piece of code, used to initialise the graphics in Turbo Pascal (NOT Dev-PASCAL! Mind you! Dev-Pascal does not accept the graph driver and graph mode be declared as integer values. If you have Dev-Pascal then change all the variable types of the 'gd,gm' to smallint! 'Gd, Gm : Smallint;') is shown below:
Program Lesson8_Program1;
Uses Crt,Graph;
Var GraphicsDriver, GraphicsMode,
ErrCode : Integer;
{two var's are needed for initialisation}
Begin
Writeln('Initialising Graphics, please wait...');
GraphicsDriver := Detect;
InitGraph(GraphicsDriver, GraphicsMode,'');
{IMPORTANT, read the following or
otherwise graphics will not work!! ;)}
(*between the inverted commas,
type in the path of the graphics BGI file
(usually 'C:\TP\BGI'),
OR
change the dir in the file menu (PRESS Alt+F)
and roll down your mouse pointer to the 'change dir'
menu; then either type the path to the BGI file,
or go to C: -> TP -> BGI*)
ErrCode := GraphResult;
If GraphResult <> grOK then { <> means 'not equal to' }
Begin
ClrScr;
Writeln('Graphics error occured: ',
GraphErrorMsg(ErrCode));
Writeln('If a file not found error is displayed above');
Writeln('then, change the dir from the current');
Writeln('location to C:\ -> TP -> BGI, '+
+'from the file menu!');
Readln;
Halt(1);
End Else
Begin
Randomize;
SetColor(Random(15) + 1); {Set text colour}
{Output text at 20 pixels from the top of the screen,
and 20 other from the left side of the screen.}
OutTextXY(20,20,'Welcome to the new generation
of Pascal Programming:');
OutTextXY(20,30,'Pascal Graphics!!');
OutTextXY(25,70,'You will learn more
graphics procedures and');
OutTextXY(25,80,'functions, later in this lesson :-)');
Readln;
End;
CloseGraph;
End.
The program above uses the statement:
InitGraph(GraphicsDriver,GraphicsMode,'');
You may find it strange what does the two inverted commas (' ') mean, in the statement shown above. It is the path which redirects the turbo pascal linker to the graphics BGI file. So, in between the inverted commas, you have to enter the path to the graphics driver, or otherwise you will receive an error message. You may leave it blank and let the compiler himself find the BGI file automatically (in the current directory), but when I did this, it never worked!! :-) If you did not change the folder address (C:\TP) after the installation, you may use this path: 'C:\TP\BGI', like this:
InitGraph(GraphicsDriver,GraphicsMode,'C:\TP\BGI');
After the 'initgraph()' statement, you should inform the user with the problem concerning a graphics error, if the EGAVGA.BGI file is not found. Hope it does not happen to you :-) !! Then, if the user is error-free, then you can start calling graphics procedures and functions from the graphics library (graph). In the example program above, I used only the 'OutTextXY()' statement and the 'SetColor()' statement. However there are many more functions other than these!
At the end of each graphic statments, you have to close the graphics section, by using the 'CloseGraph' statement.
USEFUL INFORMATION:
Note that the screen resolution is 640 by 480 pixels (compare this with that of the CRT! - the CRT has a screen resolution of 80 by 25 'pixels' only!)