|
|
What you are going to learn today:
Today's the last day - and it just so happens to be unlucky number 13. But don't fret, today's tutorial is short and sweet. We'll first introduce you to graphics, file I/O, and finally a 'Where to go from here' section. It's been nice teaching you - Good luck. Shapey Shapes With BGIDon't you have a powerful computer? I mean, you have SuperVGA with that great high-rez monitor which can cram pixels up to 1024 by 768 right? So, why don't you make use of of it? Pascal allows you to fully utilze your computer's graphic utilities with the Graph unit, so read on!The first thing you must do when writing a graphics program is to initalize the graphics. You do this with the InitGraph procedure. You need to pass it three parameters, these are: first, the value of which graphics driver to use, second, the graphics mode it's in, and third, the Borland Graphics Interface (BGI) files path, or the directory where the BGI files are found. (Most likely it's C:\TP\BGI, but hey, we don't know which directory you installed Pascal to, so we can't exactly be perfect.) Note however, that you have to pass variables to the procedure, not just values, because InitGraph refers to its parameters by reference - It actually changes the values of the integers. For example: uses
Graph;
var
driverVar, modeVar: integer;
begin
driverVar := Detect;
Writeln(driverVar, ' ', modeVar);
InitGraph(driverVar, modeVar, 'C:\TP\BGI');
Writeln(driverVar, ' ', modeVar);
Readln; { To Pause The Display }
end.
If you run the above program, you'll notice that the values of driverVar and modeVar
are different before and after InitGraph. Normally, variables you pass won't be
changed by a procedure because what it does is to just copy the value. But here the
original value is changed instead. You can implement this in your procedures too -
just add the var keyword before the variable names you want to be referenced.
But enough of that. You're probably more intrigued by the code listing above. Let's start slowly, shall we? After we include the Graph unit, we go on to declare driverVar and modeVar - both integers. On the very first line, we make driverVar to be equal to Detect. Detect? What Detect? Well, Detect is just a constant representing 0 declared in unit Graph. Thus the following lines would all be the same: driverVar := Detect; driverVar := 0; driverVar := Detect + 0; driverVar := Detect - 0; driverVar := a + b + c + d - a - b - c - d;Great! Now we print out the values of driverVar and modeVar - Use InitGraph on them, and print them out again. The last line Readln is just there to pause the display so that you won't need to go to the User Screen to see it all again. Whew! After we initialize the graphics, we can now start drawing! 1. Drawing BoxesFor the drawing of boxes and other forms of rectangles, you would use the Bar command. You pass it four values - first two are the XY coordinates of the top- left corner, last two are the XY coordinates of the bottom-right corner of the rectangle. Thus, to draw a 10 by 10 square, you'll type:Bar(10, 10, 20, 20);Now that was a great square, eh? But what if you want it to fill the whole screen? Well, there are the GetMaxX and GetMaxY commands, which return the screen width and screen height in pixels. So, this is how it'll look: Bar(0, 0, GetMaxX, GetMaxY);Well, you get the idea now. It's all very simple, actually. Now, after rectangles, you have - of course, circles! (Little Round Things...) 2. Drawing CirclesCircles are generally as simple as rectangles, but you need a bit more knowledge of maths. You pass the Circle command three parameters. The first two are the X and Y coordinates of the center of the circle. The third is the radius, which is the distance between the center and the edge. So, try this out:circle(10, 10, 10);This line will draw a circle with the center at coordinates (10, 10), extending 10 pixels from the center. Nice effect, eh? But what if you want to draw an imperfect circle, otherwise known as an ellipse? Well, you'll use the ellipse command: ellipse(0, 0, GetMaxX, GetMaxY);That draws a nice big ellipse all across your screen - huge one. Now is that powerful or what? Now, after the program finishes, you've still got one last thing to do. Return the computer back to text mode. Use the command CloseGraph to do this. It isn't neccesary, but highly recommended... 3. Drawing LinesLines can be drawn using the line command. It accepts four parameters: first two are the XY coordinates of the start of the line, the next two are the XY coordinates of the end of the line. So it looks like this:Line(12, 15, 80, 90);Ahh, this draws a line from (12, 15) to (80, 90). Great work! Who said that graphics were a difficult topic? We just finished this whole thing in one chapter! Well, now it's time to embark on your typing spree. Go on. Type this program. If you dare. (Um, we would like to state that we are not responsible for anything stupid which happens during this program...) And yeah, you can change the BGI path if it is different from what is stated on the listing. Here goes:
1: program DrawTheHeck;
2:
3: uses
4: Graph;
5:
6: var
7: driverVar, modeVar: integer;
8:
9: begin
10: driverVar := Detect;
11: InitGraph(driverVar, modeVar, 'C:\TP\BGI');
12: Line(0, 0, GetMaxX, GetMaxY);
13: Bar(100, 100, GetMaxX - 100, GetMaxY - 100);
14: Readln; { This is to pause the display. }
15: CloseGraph;
16: end.
The Write CommandAs you know, the Writeln command prints a line and then moves the cursor to the next line, but what if you don't want that. Well, you use the Write command, which keep the cursor at where the line ended. Thus, the following code... begin
Write('This is ');
Write('my life!');
end.
...would output the following:
This is my life!As simple as that, you've joined two separate statements together! Wow! That is powerful, isn't it? (Actually, this is pretty basic knowledge, but never mind.) Where to go from here...From here, we have finished everything we have on our agenda. Well, here is the list of things we never taught you about, but you should definitely try learning:
email: tq97-11127@advanced.org |