#include<stdio.h> #include<unistd.h> #include<sys/mman.h> #include<sys/ioctl.h> #include<fcntl.h> #include<linux/fb.h> #include<stdlib.h> #include<string.h>
unsigned char *fbp; struct fb_var_screeninfo vinfo; struct fb_fix_screeninfo finfo;
void drawpoint(unsigned int x,unsigned int y,unsigned int *color) { unsigned char *fbp0; fbp0=fbp; fbp0+=y*vinfo.xres*vinfo.bits_per_pixel/8+x*vinfo.bits_per_pixel/8; memcpy(fbp0,color,vinfo.bits_per_pixel/8); } void drawline(unsigned int x0,unsigned int y0,unsigned int x1,unsigned int y1,unsigned int *color) { unsigned int x=x0; unsigned int y=y0; int s0,s1,s2; while(x!=x1||y!=y1) { drawpoint(x,y,color); if(x0>=x1&&y0>=y1) { s0=abs((x-x0-1)*(y0-y1)-(y-y0)*(x0-x1)); s1=abs((x-x0)*(y0-y1)-(y-y0-1)*(x0-x1)); s2=abs((x-x0-1)*(y0-y1)-(y-y0-1)*(x0-x1)); s0<=s1?(s0<=s2?x=x-1:(x=x-1,y=y-1)):(s1<=s2?y=y-1:(x=x-1,y=y-1)); } else if(x0>=x1&&y0<=y1) { s0=abs((x-x0-1)*(y0-y1)-(y-y0)*(x0-x1)); s1=abs((x-x0)*(y0-y1)-(y-y0+1)*(x0-x1)); s2=abs((x-x0-1)*(y0-y1)-(y-y0+1)*(x0-x1)); s0<=s1?(s0<=s2?x=x-1:(x=x-1,y=y+1)):(s1<=s2?y=y+1:(x=x-1,y=y+1)); } else if(x0<=x1&&y0>=y1) { s0=abs((x-x0+1)*(y0-y1)-(y-y0)*(x0-x1)); s1=abs((x-x0)*(y0-y1)-(y-y0-1)*(x0-x1)); s2=abs((x-x0+1)*(y0-y1)-(y-y0-1)*(x0-x1)); s0<=s1?(s0<=s2?x=x+1:(x=x+1,y=y-1)):(s1<=s2?y=y-1:(x=x+1,y=y-1)); } else if(x0<=x1&&y0<=y1) { s0=abs((x-x0+1)*(y0-y1)-(y-y0)*(x0-x1)); s1=abs((x-x0)*(y0-y1)-(y-y0+1)*(x0-x1)); s2=abs((x-x0+1)*(y0-y1)-(y-y0+1)*(x0-x1)); s0<=s1?(s0<=s2?x=x+1:(x=x+1,y=y+1)):(s1<=s2?y=y+1:(x=x+1,y=y+1)); } } drawpoint(x,y,color); } void drawcircle(unsigned int x0,unsigned int y0,unsigned int radius,unsigned int *color) { unsigned int x,y,y1=0; int s0,s1,s2; int i=0; x=x0+radius+1; y=y0; do { s0=abs((x-x0-1)*(x-x0-1)+(y-y0)*(y-y0)-radius*radius); s1=abs((x-x0)*(x-x0)+(y-y0-1)*(y-y0-1)-radius*radius); s2=abs((x-x0-1)*(x-x0-1)+(y-y0-1)*(y-y0-1)-radius*radius); s0<=s1?(s0<=s2?x=x-1:(x=x-1,y=y-1)):(s1<=s2?y=y-1:(x=x-1,y=y-1)); if((x!=2*x0-x)&&(y1!=y)) { drawpoint(2*x0-x,y,color); drawpoint(x,2*y0-y,color); drawline(x-1,y,2*x0-x+1,y,color); } if((y!=2*y0-y)&&(y1!=y)) { drawpoint(x,y,color); drawpoint(2*x0-x,2*y0-y,color); drawline(2*x0-x+1,2*y0-y,x-1,2*y0-y,color); } y1=y; }while((x!=x0)&&(y!=(y0-radius))); }
int main() { int fd; unsigned int i=0; unsigned int color,bgcolor; long int screensize; bgcolor=0; color=0x0000ff00; if((fd=open("/dev/fb0",O_RDWR))<0) { perror("fail to open"); return 0; } if (ioctl(fd,FBIOGET_FSCREENINFO, &finfo)) { perror("fail to get"); return 0; } if (ioctl(fd,FBIOGET_VSCREENINFO, &vinfo)) { perror("fail to get"); return 0; } screensize=vinfo.xres*vinfo.yres*vinfo.bits_per_pixel/8; fbp=(unsigned char *)mmap(0,screensize,PROT_READ | PROT_WRITE,MAP_SHARED,fd, 0); for(i=0;i<screensize;i+=vinfo.bits_per_pixel/8) memcpy(fbp+i,&bgcolor,vinfo.bits_per_pixel/8); usleep(10000); drawcircle(400,400,300,&color); munmap(fbp,screensize); close(fd); return 0; }
|