Graphics
Graphics
The graphics we will be using are Borland Graphics Interface (BGI)
specific. The functionality they implement is general, but the
details of the function calls are specific to this package.
The basic idea
-
The basic idea is this:
-
- The screen is represented as a big array of pixels
- A pixel is a dot of light on the screen
- Each pixel can have 1 of 16 different colors
- Once you set a pixel to a color, it stays that color until you
change it.
- The array is 640 pixels across by 480 pixels down.
- 0,0 is in the upper left.
- The positive x direction is to the right.
- The positive y direction is down.
Drawing on the screen
The Borland Graphics Interface provides a fairly comprehensive set of
drawing functions.
For a complete list of BGI functions, execute the help file
"G:\Win95\Apps\BC5\Help\bcdos.hlp"
These functions draw the specified object with the default linestyle,
color, and fill style, as appropriate.
Functions: The list.
-
Example 1:
-
Syntax
#include
void far putpixel(int x, int y, int color);
Description1
putpixel plots a point in the color defined by color at (x,y).
Return Value
None.
-
Example2
-
Syntax
#include
void far line(int x1, int y1, int x2, int y2);
Description
line draws a line in the current color, using the current line style
and thickness between the two points specified, (x1,y1) and (x2,y2),
without updating the current position (CP).
Return Value
None.
Animation
The BGI functions don't animate the objects on the screen, they simply
allow us to draw things.
-
To animate an object, we:
- Draw it
- Pause briefly
- Erase it
- Modify it slightly
- Redraw it
By rapidly repeating this sequence of events, the object appears to
move smoothly across the screen.
-
An animation program:
-
- Initializes the graphics subsystem.
- Creates some graphics objects.
-
A graphics object:
-
- Is just an instance of a class, like any other object.
- Has variables called x and y that denote it's position on the
screen.
- Has functions called draw() or update() that are used to display
the object
- Has functions that change x and y (so that when we draw the
object, it appears to move).
- Loops, drawing, erasing, moving, and redrawing the object.
- Eventually exits, shutting down the graphics subsystem.
The program saucer.cpp shows the basic steps to
animating an object on the screen.
An interactive graphics program
The preceding example doesn't take any input from the user (except for
when to exit the program).
To be interactive, the program must take some input from the user and
modify the behaviour accordingly.
-
In an interactive graphics program:
-
- We can use getch() to get input from the user.
- Problem: getch() stops the program until the user hits a key.
- Solution: kbhit() - returns TRUE if there is input waiting, FALSE
otherwise
-
The interactive graphics program:
-
- Has a main event loop, just like the animation program, but
- Now the loop also checks to see if a key has been hit
- If a key has been hit, the program uses getch() to read the
input, and then
- Changes the behaviour of the program according to the key that
has been hit by:
- Creating objects
- Destroying objects
- Calling object member functions to modify objects.
- Etc.
The program ship.cpp shows how to
program an event loop that uses user input to modify the behaviour of
the objects.
Sample graphics programs:
Note: To compile these successfully:
1. You need the file "egavga.bgi" in the same directory as the .cc
file. This file can be found in the Borland bgi directory. The full
pathname of this file is "G:\Win95\Apps\BC5\BGI\egavga.bgi".
2. You need to change the target platform to "DOS(standard)" and set
things up to link with the BGI functions. You do this (in Borland
C++) by right-clicking in the window that has the graphics file that
you want to compile. Select "TargetExpert" in the menu that appears.
In the next window that appears, under "Platform", select
"DOS(Standard)". Also, check the box next to "BGI". Select "OK".
Now when you build the application, it will be built for the DOS
platform and everything will work properly.