**
在背景图上使圆运动,并调整圆运动速度(标志位)
**
代码如下:
#include<unistd.h>
#include<fcntl.h>
#include<string.h>
#include<stdio.h>
#include<sys/mman.h>
#include<math.h>
#include<linux/input.h>
#include<sys/stat.h>
#include<sys/types.h>
#define LC_PATH "/dev/fb0"
#define SIG 800*480
int main()
{
int n = 1;
int weight = 0;
int height = 0;
int speed = 0;
int bmpchang = open("./jieni.bmp", O_RDWR);
int lcd_fd = open("/dev/fb0",O_RDWR);
lseek(bmpchang, 18 ,SEEK_SET); //获取图片的宽高
read(bmpchang, &weight, 4);
lseek(bmpchang, 22 ,SEEK_SET);
read(bmpchang, &height, 4);
char bmpbuf[weight*height*3];
int lcdbuf[weight*height];
int tempbuf[weight*height];
lseek(bmpchang, 54, SEEK_SET); //跳过图片头信息字节
int laji = (4-(weight*3)%4)%4; //图片字节不能被4整除时,加入的垃圾字节数
for(int i = 0; i < height; i++)
{
read(bmpchang, &bmpbuf[weight*i*3],weight*3);
lseek(bmpchang, laji, SEEK_CUR); //在字节读入时,跳过垃圾字节
}
for(int i = 0; i < weight*height; i++)
{
lcdbuf[i] = 0x00<<24 | bmpbuf[i*3+2]<<16 | bmpbuf[i*3+1]<<8 | bmpbuf[i*3];
}
for(int i = 0; i < weight; i++)
{
for(int j = 0; j < height; j++)
{
tempbuf[(height-1-j)*weight+i] = lcdbuf[j*weight+i]; //像素点反转
}
}
//内存映射
int *mmap_bmp = mmap(NULL, 800*480*4, PROT_READ|PROT_WRITE, MAP_SHARED, lcd_fd, 0);
/*标志位实现自由图片移动,未实现*/
// int MIDLEJ = ((480-height/n)/2);
// int MIDLEI = ((800-weight/n)/2);
// int RL = 100;
// int biaozhij=1;
// int biaozhii=1;
printf("请输入移动速度:");
scanf("%d",&speed);
putchar('\n');
for(int i=0; i<height/n; i++) //图片的高度h、宽度、缩放倍数n
{
for(int j=weight*n*i, k=0; j<(weight*n*i+weight); j+=n,k++) //循环决定每行该取的像素点多少
{
//任意位置(480-height/n))、(800-weight/n))、缩小倍数n
}
}
unsigned int *mem_p;
int k=0;
int g=0;
int MIDLEY = 240;
int MIDLEX = 400;
int R = 100;
int biaozhiy=1;
int biaozhix=1;
int MIDLEJ = 200;
int MIDLEI = 350;
int RL = 80;
int biaozhij=1;
int biaozhii=1;
int fb0 = open(LC_PATH,O_RDWR);
mem_p = (unsigned int*)mmap(NULL,SIG*4,PROT_READ | PROT_WRITE, MAP_SHARED, fb0, 0); //映射圆
while(1)
{
for (int y = 0; y < 480; y++)
{
for (int x = 0; x < 800; x++)
{
//画圆
k = ((x-MIDLEX)*(x-MIDLEX))+((y-MIDLEY)*(y-MIDLEY));
if (k<=R*R)
{
*(mem_p+800*y+x) = 0xFFFF00;
}
else
{
*(mmap_bmp+800*y+x) = tempbuf[y*weight+x]; //将图片放在背景
}
}
}
/*错误示范,圆在外面,会发生圆变透明,不停闪烁*/
for (int i = 0; i < 480; i++)
{
for (int j = 0; j < 800; j++)
{
k = ((j-MIDLEI)*(j-MIDLEI))+((i-MIDLEJ)*(i-MIDLEJ));
if (k<=RL*RL)
{
*(mem_p+800*i+j) = 0x0000FF;
}
}
}
//大圆重置标志位
if ((MIDLEY+R)>479)
{
biaozhiy=2;
}
if ((MIDLEX+R)>799)
{
biaozhix=2;
}
if ((MIDLEY-R)<0)
{
biaozhiy=1;
}
if ((MIDLEX-R)<0)
{
biaozhix=1;
}
//小圆重置标志位
if ((MIDLEI+RL)>799)
{
biaozhii=2;
}
if ((MIDLEJ+RL)>479)
{
biaozhij=2;
}
if ((MIDLEI-RL)<0)
{
biaozhii=1;
}
if ((MIDLEJ-RL)<0)
{
biaozhij=1;
}
//改变大圆中心点
if (biaozhix==1)
{
MIDLEX+=speed;
}
if (biaozhix==2)
{
MIDLEX-=speed;
}
if (biaozhiy==1)
{
MIDLEY+=speed;
}
if (biaozhiy==2)
{
MIDLEY-=speed;
}
//改变小圆中心点
if (biaozhii==1)
{
MIDLEI+=speed;
}
if (biaozhii==2)
{
MIDLEI-=speed;
}
if (biaozhij==1)
{
MIDLEJ+=speed;
}
if (biaozhij==2)
{
MIDLEJ-=speed;
}
}
munmap(mmap_bmp, 800*480*4); //解除内存映射
close(bmpchang); //关闭文件
close(lcd_fd);
munmap(mem_p, SIG*4); //取消映射
close(fb0);
return 0;
}