//------------------------------------------------------------------- // animate1.cpp // // This program shows how to synchronize vram updates with the // CRT's Vertical Retrace signal, so that drawing occurs while // the screen is blanked, thus allowing for smooth animations. // It's an initial prototype for implementing the "pong" game. // // programmer: ALLAN CRUSE // date begun: 21 AUG 2003 // completion: 17 SEP 2003 //------------------------------------------------------------------- #include // for printf(), perror() #include // for open() #include // for exit() #include // for close() #include // for mman() #include // for iopl(), inb() #define VRAM_BASE 0xB0000000 #define VRAM_SIZE (1 << 20) typedef unsigned char PEL; PEL *fb = (PEL*)VRAM_BASE; int display_mode = 0x4101; int hres = 640, vres = 480; char devname[] = "/dev/vram"; void vsync( void ) { // wait for current retrace to finish while ( ( inb( 0x3DA ) & 8 ) != 8 ); // then wait till next retrace begins while ( ( inb( 0x3DA ) & 8 ) == 8 ); } 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 { fb[ y*hres + x ] = color; ++x; } while ( x < maxx ); do { fb[ y*hres + x ] = color; ++y; } while ( y < maxy ); do { fb[ y*hres + x ] = color; --x; } while ( x > minx ); do { fb[ y*hres + x ] = color; --y; } while ( y > miny ); } void fill_rectangle( int x, int y, int h, int v, int color ) { int minx = x, miny = y, maxx = x+h-1, maxy = y+v-1; for (y = miny; y <= maxy; y++) for (x = minx; x < maxx; x++) fb[ y*hres + x ] = color; } void fill_oval( int x, int y, int h, int v, int color ) { int midh = (h+1)/2, midv = (v+1)/2; for (int r = 0; r < v; r++) for (int k = 0; k < h; k++) { int dh = k - midh; int dv = r - midv; int rr = midh * midv; dh *= midv; dv *= midh; if ( dh*dh + dv*dv < rr*rr ) fb[ (y+r)*hres + (x+k) ] = color; } } 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( devname, O_RDWR ); if ( fd < 0 ) { perror( devname ); exit(1); } if ( mmap( mm, size, prot, flag, fd, 0 ) == MAP_FAILED ) { perror( "mmap" ); exit(1); } close( fd ); } int main( int argc, char **argv ) { int speed = 2; // set the default speed // allow the user to specify an alternative speed if ( argc > 1 ) { int arg = atoi( argv[1] ); if (( arg > 0 )&( arg < 10 )) speed = arg; } // install SVGA device-driver and map in the VRAM system( "/sbin/insmod vram.o" ); map_system_memory( VRAM_BASE, VRAM_SIZE ); // allow i/o (for detecting CRT vertical retrace) if ( iopl( 3 ) ) { perror( "iopl" ); exit(1); } // enter graphics mode char command[ 40 ]; sprintf( command, "clear ; mode3 %d ", 0x4101 ); system( command ); // 640x480, 256-color, w/LFB // draw screen border int black = 0, white = 15, blue = 9, green = 10, yellow = 14; int x = 0, y = 0, wide = hres, high = vres; draw_rectangle( x, y, wide, high, white ); getchar(); // draw game wall x += 40, y += 30, wide -= 80, high -= 60; fill_rectangle( x, y, wide, high, blue ); x += 20, y += 20, wide -= 40, high -= 40; fill_rectangle( x, y, wide, high, black ); getchar(); // draw game ball int xpos = 100, ypos = 200, diam = 20; fill_oval( xpos, ypos, diam, diam, yellow ); getchar(); // draw game paddle fill_rectangle( 80, 454, 100, 8, green ); getchar(); // animate the game ball int minx = x, maxx = x+wide-diam, dx = 1; int miny = y, maxy = y+high-diam, dy = -1; int incs, xold, yold; int hits = 5; do { xold = xpos; yold = ypos; for (int incs = 0; incs < speed; incs++) { xpos += dx; ypos += dy; if (( xpos <= minx )||( xpos >= maxx )) dx = -dx; if (( ypos <= miny )||( ypos >= maxy )) dy = -dy; if ( ypos >= maxy ) --hits; } vsync(); fill_oval( xold, yold, diam, diam, black ); fill_oval( xpos, ypos, diam, diam, yellow ); } while ( hits > 0 ); // leave graphics mode system( "mode3 3" ); }