//------------------------------------------------------------------- // vgademo.cpp // // This example demonstrates 256-color PC graphics programming // as implemented by IBM's Video Graphics Array (VGA) adapter, // still supported by SVGA devices for backward compatibility. // // programmer: ALLAN CRUSE // written on: 08 SEP 2003 //------------------------------------------------------------------- #include // for printf(), perror() #include // for open() #include // for exit() #include // for close() #include // for mman() #include // for iopl() typedef unsigned char u8; u8 *vram = (u8*)0x000A0000; int hres = 320, vres = 200; void set_pixel( int x, int y, int color ) { int posn = y*hres + x; vram[ posn ] = color; } void fill_rectangle( int x, int y, int h, int v, int color ) { int minx = x, maxx = x+h-1; int miny = y, maxy = y+v-1; for (y = miny; y <= maxy; y++) for (x = minx; x <= maxx; x++) set_pixel( x, y, color ); } void map_vram( int base, int size ) { int fd = open( "/dev/mem", O_RDWR ); if ( fd < 0 ) { perror( "/dev/mem" ); exit(1); } int prot = PROT_READ | PROT_WRITE; int flag = MAP_FIXED | MAP_SHARED; void *mm = (void*)base; if ( mmap( mm, size, prot, flag, fd, base ) == MAP_FAILED ) { perror( "mmap" ); exit(1); } close( fd ); } int main( int argc, char **argv ) { int base = (int)vram; int size = (256<<10); map_vram( base, size ); int minx, midx, maxx, miny, midy, maxy; int x, y, color, radius_squared; //---------------------------------------------- // Demonstrate VGA color graphics (VGA mode 19) //---------------------------------------------- system( "clear ; mode3 19 " ); // 320x200, 256-colors // draw screen border color = 15; // white minx = 0, maxx = hres-1; miny = 0, maxy = vres-1; x = y = 0; do { set_pixel( x, y, color ); ++x; } while ( x < maxx ); do { set_pixel( x, y, color ); ++y; } while ( y < maxy ); do { set_pixel( x, y, color ); --x; } while ( x > minx ); do { set_pixel( x, y, color ); --y; } while ( y > miny ); getchar(); // fill circle (unrescaled) midx = hres/2; midy = 23*vres/32; radius_squared = 40*40; color = 6; for (y = 4; y <= maxy-4; y++) for (x = 4; x <= maxx-4; x++) { int dx = x - midx; int dy = y - midy; int dist_squared = dx*dx + dy*dy; color = ( dist_squared < radius_squared ) ? 6 : 150; set_pixel( x, y, color ); } getchar(); // show the palette of 256-colors int h = 5, v = 5, x0 = 32, y0 = 24; for (color = 0; color < 256; color++) { x = x0 + (color % 32)*8; y = y0 + (color / 32)*8; fill_rectangle( x, y, h, v, color ); } getchar(); system( " mode3 3 " ); // standard 80x25 text mode }