[ Pobierz całość w formacie PDF ]

0x10Graphics640 x 3504 or 16**
0x11Graphics640 x 4802
0x12Graphics640 x 48016
0x13Graphics320 x 200256
* The vertical resolution is set using function 0x12 of Interrupt 0x10 --
something I'll never mess with.
** Only EGA adapters with 64K of video RAM display 4 colors in mode 0x10.
My favorite mode is 0x13, which is an old MCGA mode available on all VGA
adapters. It uses a rather low resolution, 320 x 200 pixels, but has a ton
of colors: 256.
More modes are available than those shown above. The table lists only the
standard VGA modes. I know that Super VGA has even more colors and
resolutions. If you get into this, consider buying a technical manual on
VGA graphics.
Set Some Dem Modes!
Wow! This chapter has way too much text. But the point's been made: Before
you do doodle with graphics, you must first set the graphics mode.
Actually, you should do several things: You should discover what graphics
mode the PC is currently in, save it, set the mode to what you want, then
reset the mode back to what it was when you're done. That's a lot of work,
but it's being nice to your user.
Borland C++ has some graphics mode setting commands, but Microsoft C and
all other C language compilers probably don't. Because of that, I'm going
to use in-line Assembly language to run the graphics basics for this
Chapter. (See Chapter 16 for more information.)
To set the mode you use the PC's Video Interrupt, 0x10, to call the BIOS.
Here's the routine:
void set_mode(unsigned char mode)
{
_asm mov ah,00
_asm mov al,mode
_asm int 10h
}
The set_mode function is called with an unsigned char value, from 0
through 255, which you would pluck from the table shown earlier in this
Lesson. That mode is saved into the AL register, the value zero put into
the AH register and the function sped off to Interrupt 10h (which is
Assembler for 0x10). Voila, ze mode is set.
Of course, fetching the current graphics mode is also important. Otherwise
you may leave your user in some low-rez dorky mode and they won't have a
clue as to how to get back to normal. (The MODE 80 command does the trick
in DOS.) Here is the function to fetch the current graphics mode:
unsigned char get_mode(void)
{
unsigned char mode;
_asm mov ah,0fh
_asm int 10h
_asm mov mode,al
return(mode);
}
The value mode is returned from Interrupt 0x10 in the AL register. That's
saved into the mode variable and returned from the get_mode function.
And now, your program:
Name: GRAFMODE.C
#include
#define NORMAL 0x03
#define LOWGRAF 0x04
#define HIGRAF 0x06
#define VGA256 0x13
unsigned char get_mode(void);
void set_mode(unsigned char mode);
void main()
{
unsigned char save;
//Save the current mode (which is probably 3)
save = get_mode();
printf("You're in mode %i now.\n",save);
getchar();
//Switch to low rez CGA mode
set_mode(LOWGRAF);
puts("Hello Low CGA Graphics Mode!");
getchar();
//High rez CGA mode
set_mode(HIGRAF);
puts("Hello High CGA Graphics Mode!");
getchar();
//Low rez MCGA colorful mode
set_mode(VGA256);
puts("Hello VGA 256 Graphics Mode!");
getchar();
//Restore mode before exiting
set_mode(save);
}
unsigned char get_mode(void)
{
unsigned char mode;
_asm mov ah,0fh //Return mode value
_asm int 10h //Video interrupt
_asm mov mode,al //Save mode
return(mode);
}
void set_mode(unsigned char mode)
{
_asm mov ah,00 //Set mode
_asm mov al,mode //mode value
_asm int 10h //Video interrupt
}
Type in the above source code, or Shift+click on this link to download a
copy.
Compile!
Run the program in a DOS window (if you're using Windows). And switch the
window over to full-screen by pressing Alt+Enter if you need to. (Windows
will change modes otherwise, which is annoying).
Now, run. You should see the following:
You're in mode 3 now
You're probably in mode 3, which is the standard 80 x 25 text mode.
Press Enter.
Clunky, huh? Press Enter.
That's the old "high" resolution mode. Ick. Press Enter
Finally you have the high color VGA image. That's the one you'll be
working with in this Lesson. Press Enter and your system will be restored
back to its original graphics mode.
Did you notice that the printf function works in graphics modes? Pretty
much so. You can mix graphics and text any time. The cursor moves the
same on a graphics screen as it does on a text screen. Just use any C
language text function to display text.
Windows is actually capable of viewing all these modes in a DOS window
on the screen. Just press Alt+Enter to switch between full-screen and
windowed mode. I prefer to work full screen when I do graphics.
Feel free to modify the program to display other modes shown in the
Table.
To see if the program really does save the graphics mode, type MODE 40
before you run it. If the program restores the 40-column mode when it's
done, then you've just done your users a favor.
If you're using DJGPP, then you'll need to use the ATT nottaion, not the
Intel Assembly notation shown here. Visit http://www.delorie.com/djgpp/
for more information. (Sorry!)
Bonus C For Dummies Lesson 18-2Lesson 18-2  Hello, Pixel Fairy!
(If you have Microsoft Visual C++, click the icon below.)
The graphics screen is a big grid. Well, the text screen is a big grid,
too. So if you've been plotting out text on the screen using rows and
columns, you should get used to plotting graphics on its grid in no time.
But with graphics, you're not plotting characters, you're plotting pixels.
All graphics, no matter how complex they look or what they do, are merely
a collection of pixels on the screen. So when you start creating graphics,
you start by plotting pixels on a grid. And since you're programming, you
can afford to have the computer do all the plotting work for you.
Every graphics program you've ever seen -- including Windows itself --
starts with a basic routine to plot pixels on the screen.
Other routines, for drawing lines and circles and such, are all
functions designed to plot pixels in a certain manner.
Yes, even animation involves plotting pixels.
It's Just a Big Grid
Pixels have three attributes associated with them: X position, Y position
and color. (Again, this is like text, where each character has a row and
column position as well as a character code value.)
Pixels have an X (horizontal) position and a Y (vertical) position. The
pixel grid starts in the upper left corner of the screen at position 0,0.
This is the same for all graphics resolutions. The maximum values for X
and Y differ from graphics mode to graphics mode. In the high color VGA
mode 0x13, the maximum X value is 320 and the maximum Y value is 200.
(Incidentally, that's the size of the graphic image shown above: 320 x 200
pixels.)
In addition to the X and Y locations, pixels also have a color value. Like [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • antman.opx.pl
  • img
    \