char far *vgamemory = (char *)0xA0000000L;or (on old compilators)
char far *vgamemory = (char *)MK_FP(0xA000, 0);But under DJGPP isn't so simple because the memory is protected, so you can't access directly to all the memory of the system. The best solution is to disable the protection of the DOS memory by calling the procedure
__djgpp_nearptr_enable()
. There are a simple
code that shows how to access at the DOS memory:
#include <sys/nearptr.h> __djgpp_nearptr_enable(); vgamemory=(char *)(0xa0000 + __djgpp_conventional_base); __djgpp_nearptr_disable();The header
nearptr.h
is necessary for the prototipes of the functions. We should
use __djgpp_conventional_base
to get the right address of the DOS memory (the
protected mode is very intricate). At the end of the program, use __djgpp_nearptr_disable()
to get the program back to regular protected mode. But you must remember that with this
code you disable the protection of the memory, so you can damage the DOS area and block the
system. Use the program info
(you must download the file txi390b.zip) for more
informations.#define TEXT_MODE 0x03 #define VGA_MODE 0x13 void setmode(short mode) { union REGS regs; regs.h.ah = 0x00; // Procedure 0x00: Set Video Mode regs.h.al = mode; // The requested video mode int86(0x10,®s,®s) // Call the BIOS }If you set the most significant bit of the register
al
(use 0x93 instead of 0x13)
the video memory is not cleared. If you don't like to use the BIOS you can download the file
modes.zip that explain
how to set the video mode without the BIOS. 320y = 256y + 64y 
and next  offset = (y<<8) + (y<<6)
+ x 
.x-x1=m(y-y1)
, but is a bit slow. sizex/sizey=countx/county sizex and sizey are the sizes of the rectangle that contain the line countx and county are the counters impiegated by the algorithmso we have:
county * sizex calculatex= -------------- sizey countx * sizey calculatey= -------------- sizexBut the Bresenham's algorithm transform the equations for drawing the lines without the multiplications and the divisions. See the demo program for the code of the algorithm.
drawline()
function and draw a rectangle
more faster. We know the structure of the video memory, so is simple to design an algorithm for
a rectangle.draw sizex pixels from (x1, y1) onerow: jump 320-sizex pixels draw a pixel jump sizex-2 (intsizex) pixels draw a pixel while row drawed < sizey-2 jump 320-sizex pixel draw sizex pixelsSee the demo program for the code of the algorithm.
0..63
, that specify the intensity of Red, Green and Blue. Whit the mixing
of these colors we can make the wanted color. The VGA card have 3 register for change the
palette: RGB_READ for telling the index of the color to read, RGB_WRITE for telling the index
of the color to write and RGB_DATA for read/write the RGB value. RGB_RESET is used for prepare
the VGA card (isn't necessary). The following code shows how to setting the RGB value of the
color 15 (WHITE):
#define RGB_RESET 0x03C6 #define RGB_READ 0x03C7 #define RGB_WRITE 0x03C8 #define RGB_DATA 0x03C9 void setwhite(void) { outp(RGB_RESET, 0xFF); // Prepare the VGA card outp(RGB_WRITE, WHITE); // Tell that we want to write the color 15 (WHITE) outp(RGB_DATA, 64); // Red value outp(RGB_DATA, 64); // Green value outp(RGB_DATA, 64); // Blue value }For setting all the 256 colors simply tell to the VGA card that we want to write the color 0, and next write the RGB of all the colors:
void setpalette(char *palette) { register int i; outp(RGB_RESET, 0xFF); // Prepare the VGA card outp(RGB_WRITE, 0); // Tell that we want to write the entire palette for(i=0;i<256;i++) { outp(RGB_DATA, palette[i*3]); // Red value outp(RGB_DATA, palette[i*3+1]); // Green value outp(RGB_DATA, palette[i*3+2]); // Blue value } }For reading the RGB value of a color or of all the colors simply write the index to RGB_READ and read the values from RGB_DATA.See the demo program for the code.
void waitretrace(void) { // Wait vertical retrace while(inportb(0x03DA)&0x08); // Wait refresh while(!(inportb(0x03DA)&0x08)); }