//------------------------------------------------------------------- // bresdemo.cpp // // This program uses Bresenham's Algorithm to draw some lines. // // programmer: ALLAN CRUSE // written on: 29 SEP 2003 //------------------------------------------------------------------- #include // for printf(), perror() #include // for open() #include // for exit() #include // for close() #include // for mman() #define VRAM_BASE_ADDRESS 0xA0000000 #define VGA_MEMORY_LENGTH ( 4 << 20) unsigned char *vram = (unsigned char*)VRAM_BASE_ADDRESS; int hres = 640, vres = 480, display_mode = 0x4101; void exchange( int &x1, int &y1, int &x2, int &y2 ) { int temp; temp = x1; x1 = x2; x2 = temp; temp = y1; y1 = y2; y2 = temp; } void octant_odd( int x1, int y1, int x2, int y2, int color ) { // make sure x1 <= x2 if ( x1 > x2 ) exchange( x1, y1, x2, y2 ); int xinc = ( x2 > x1 ) ? 1 : -1; int yinc = ( y2 > y1 ) ? 1 : -1; int dx = (x2-x1)*xinc; int dy = (y2-y1)*yinc; int errorterm = -dx; for (int x = x1, y = y1; x <= x2; x++) { vram[ y*hres + x ] = color; errorterm += (dy+dy); if ( errorterm >= 0 ) { y += yinc; errorterm -= (dx+dx); } } } void octant_even( int x1, int y1, int x2, int y2, int color ) { // make sure y1 <= y2 if ( y1 > y2 ) exchange( x1, y1, x2, y2 ); int xinc = ( x2 > x1 ) ? 1 : -1; int yinc = ( y2 > y1 ) ? 1 : -1; int dx = (x2-x1)*xinc; int dy = (y2-y1)*yinc; int errorterm = -dy; for (int x = x1, y = y1; y <= y2; y++) { vram[ y*hres + x ] = color; errorterm += (dx+dx); if ( errorterm >= 0 ) { x += xinc; errorterm -= (dy+dy); } } } void draw_line( int x1, int y1, int x2, int y2, int color ) { int deltax = ( x1 < x2 ) ? x2-x1 : x1-x2; int deltay = ( y1 < y2 ) ? y2-y1 : y1-y2; if ( deltax > deltay ) octant_odd( x1, y1, x2, y2, color ); else octant_even( x1, y1, x2, y2, color ); } void draw_segments( int x, int y, int xcent, int ycent, int color ) { int minx = 0, miny = 0, maxx = hres-1, maxy = vres-1; int xlo, xhi, v, h; xlo = xcent-x, xhi = xcent+x; if ( xlo < minx ) xlo = minx; if ( xhi > maxx ) xhi = maxx; if ( ( v = ycent-y ) >= miny ) for (h = xlo; h <= xhi; h++) vram[ v*hres + h ] = color; if ( ( v = ycent+y ) <= maxy ) for (h = xlo; h <= xhi; h++) vram[ v*hres + h ] = color; xlo = xcent-y, xhi = xcent+y; if ( xlo < minx ) xlo = minx; if ( xhi > maxx ) xhi = maxx; if ( ( v = ycent-x ) >= miny ) for (h = xlo; h <= xhi; h++) vram[ v*hres + h ] = color; if ( ( v = ycent+x ) <= maxy ) for (h = xlo; h <= xhi; h++) vram[ v*hres + h ] = color; } void fill_circle( int x, int y, int radius, int color ) { int xcent = x, ycent = y, esum = 3 - 2*radius; y = radius, x = 0; while ( x <= y ) { draw_segments( x, y, xcent, ycent, color ); if ( esum < 0 ) { esum += 4*x + 6; ++x; } else { esum += 4*(x - y) + 10; y -= 1; ++x; } } } void draw_rectangle( int x, int y, int h, int v, int color ) { int minx = x, miny = y, maxx = x+h-1, maxy = y+v-1; do { vram[ y*hres + x ] = color; ++x; } while ( x < maxx ); do { vram[ y*hres + x ] = color; ++y; } while ( y < maxy ); do { vram[ y*hres + x ] = color; --x; } while ( x > minx ); do { vram[ y*hres + x ] = color; --y; } while ( y > miny ); } void map_system_memory( int base, int size ) { int prot = PROT_READ | PROT_WRITE; int flag = MAP_FIXED | MAP_SHARED; void *mm = (void*)base; int fd = open( "/dev/vram", O_RDWR ); if ( fd < 0 ) { perror( "/dev/vram" ); exit(1); } if ( mmap( mm, size, prot, flag, fd, 0 ) == MAP_FAILED ) { perror( "mmap" ); exit(1); } close( fd ); } int main( int argc, char **argv ) { // map display memory into application's address-space system( "/sbin/insmod vram.o" ); map_system_memory( VRAM_BASE_ADDRESS, VGA_MEMORY_LENGTH ); // enter graphics mode char command[ 40 ]; sprintf( command, " clear ; mode3 %d ", display_mode ); system( command ); // draw screen border int color = 15; draw_rectangle( 0, 0, hres, vres, color ); getchar(); // draw circular disk as background int midx = hres/2, midy = vres/2, radius = vres/4; color = 5; fill_circle( midx, midy, radius, color ); getchar(); // draw the first line-pair (yellow) color = 14; draw_line( 100, 100, hres-100, vres-100, color ); getchar(); draw_line( hres-100, 100, 100, vres-100, color ); getchar(); // draw the second line-pair (cyan) color = 11; draw_line( 300, 100, hres-300, vres-100, color ); getchar(); draw_line( hres-300, 100, 300, vres-100, color ); getchar(); // leave graphics mode system( "mode3" ); }