/*
*作者:huangea
*邮箱:huangea@outlook.com
*程序:屏保圈
*环境:基于linux 语言:C语言 代码量:+120
*/
#include<stdio.h>
#include<fcntl.h>
#include<sys/mman.h>
#include<unistd.h>
#include <math.h>
#define XC 5
#define YS 6000000
typedef unsigned int u32_t;
u32_t *p,color=0xff0000; /*宏定义:屏幕指针,颜色代码*/
void huayuan(int d_x,int d_y,int r,u32_t color,u32_t *p) /*画圈函数,实参:圆点横纵坐标,半径,像素点指针*/
{int b_x,b_y;
for(b_x=d_x-r-1;b_x<d_x+r+1;b_x++) /*函数原理:根据传参得到圆点坐标位置,只扫描距离圆点四周半径为R的正方形,两点距离满足R点亮该位置像素点*/
for(b_y=d_y-r-1;b_y<d_y+r+1;b_y++)
if(r>sqrt((d_x- b_x) * (d_x – b_x) + (d_y – b_y)*(d_y – b_y))&&sqrt((d_x- b_x) * (d_x – b_x) + (d_y – b_y)*(d_y – b_y))>r-XC)
p[b_x*1360+b_y]=color;
}
void yuan(int pot_x,int pot_y,int r,u32_t color,u32_t *p) /*轨迹圆函数,点亮圆圈后,延时显示短时间后用黑色抹除掉*/
{int j;
huayuan(pot_x,pot_y,r,color,p);
for(j=YS;j>0;j–);
huayuan(pot_x,pot_y,r,0x000000,p);
}
void jmp(int x1,int x2,int y1,int y2,int r,u32_t *p) /*碰壁跳转函数,函数原理:根据两次碰壁的坐标,计算碰壁角度,继续沿同角度反折,采用递归,死循环*/
{
int pot_x=0,pot_y=0,j;
// color += 30000; /*改变颜色代码,渐变效果,可开启*/
// printf(“\nx1=%5d y1=%5d x2=%5d y2=%5d pot_x=%5d pot_y=%5d hd1=%8f hd2=%8f \n”,x1,y1,x2,y2,pot_x,pot_y,hd1,hd2); /*程序碰壁信息,调试用*/
if(x2>=702) /*碰到屏幕下壁,下面判断左右入射*/
{
if(y1<=y2){
for(pot_y=y2;pot_y<=1360-r;pot_y++)
for(pot_x=x2;pot_x>r;pot_x–)
if(tan(((double)x2-(double)x1)/((double)y2-(double)y1))==tan(((double)x2-(double)pot_x)/((double)pot_y-(double)y2)))
{
yuan(pot_x,pot_y,r,color,p);
if(pot_x-r<=6||pot_y+r>=1359)goto jm1;
}
}
else if(y2<=y1){
for(pot_y=y2;pot_y>=r;pot_y–)
for(pot_x=x2;pot_x>=r;pot_x–)
if(tan(((double)x2-(double)x1)/((double)y1-(double)y2))==tan(((double)x2-(double)pot_x)/((double)y2-(double)pot_y)))
{
yuan(pot_x,pot_y,r,color,p);
if(pot_x-r<=0||pot_y+r<=r+r)goto jm1;
}
}
}
if(y2>=1359-r) /*碰到屏幕右壁,下面判断上下入射*/
{
if(x1>=x2){
for(pot_x=x2;pot_x>r;pot_x–)
for(pot_y=y2;pot_y>=r;pot_y–)
if(tan(((double)y2-(double)y1)/((double)x1-(double)x2))==tan(((double)y2-(double)pot_y)/((double)x2-(double)pot_x)))
{
yuan(pot_x,pot_y,r,color,p);
if(pot_x<=r+6||pot_y+r<=r)goto jm1;
}
}
else if(x2>=x1){
for(pot_x=x2;pot_x<=768-r;pot_x++)
for(pot_y=y2;pot_y>r;pot_y–)
if(tan(((double)y2-(double)y1)/((double)x2-(double)x1))==tan(((double)y2-(double)pot_y)/((double)pot_x-(double)x2)))
{
yuan(pot_x,pot_y,r,color,p);
if(pot_x>=765-r||pot_y<=r)goto jm1;
}
}
}
if(x2<=6+r) /*碰到屏幕上壁,下面判断左右入射*/
{
if(y1>=y2){
for(pot_y=y2;pot_y>=r;pot_y–)
for(pot_x=x2;pot_x<768-r;pot_x++)
if(tan(((double)y2-(double)y1)/((double)x1-(double)x2))==tan(((double)y2-(double)pot_y)/((double)x2-(double)pot_x)))
{
yuan(pot_x,pot_y,r,color,p);
if(pot_x>=702||pot_y<=r)goto jm1;
}
}
else if(y2>=y1){
for(pot_y=y2;pot_y<=1360-r;pot_y++)
for(pot_x=x2;pot_x<768-r;pot_x++)
if(tan(((double)x1-(double)x2)/((double)y2-(double)y1))==tan(((double)pot_x-(double)x2)/((double)pot_y-(double)y2)))
{
yuan(pot_x,pot_y,r,color,p);
if(pot_x>=702||pot_y>=1299)goto jm1;
}
}
}
if(y2<=r) /*碰到屏幕左壁,下面判断上下入射*/
{
if(x2<x1){
for(pot_x=x2;pot_x>=r;pot_x–)
for(pot_y=y2;pot_y<1360-r;pot_y++)
if(tan(((double)y1-(double)y2)/((double)x1-(double)x2))==tan(((double)pot_y-(double)y2)/((double)x2-(double)pot_x)))
{
yuan(pot_x,pot_y,r,color,p);
if(pot_x<=r||pot_y>1360-r)goto jm1;
}
}
else if(x1<x2){
for(pot_x=x2;pot_x<=768-r;pot_x++)
for(pot_y=y2;pot_y<=1360-r;pot_y++)
if(tan(((double)y1-(double)y2)/((double)x2-(double)x1))==tan(((double)pot_y-(double)y2)/((double)pot_x-(double)x2)))
{
yuan(pot_x,pot_y,r,color,p);
if(pot_x>=765-r||pot_y>1360-r)goto jm1;
}
}
}
goto end;
jm1: jmp(x2,pot_x,y2,pot_y,r,p);
end: ;
}
int main(void) /*主函数*/
{
int fd,r=60;
fd=open(“/dev/fb0”,O_RDWR); /*打开显卡设备,linux下先获得最高权限,否则失败*/
p=(u32_t *)mmap(NULL, 1360 * 768 * 4, PROT_READ | PROT_WRITE, MAP_SHARED,fd,0); /*framebuffer,指针P指向屏幕像素点,屏幕分辨率为1360*768 机器差异。*/
if(p == MAP_FAILED)
perror(“mmap error”);
jm: jmp(50+r,r,r,40+r,r,p); /*初始化圆圈位置和角度*/
return 0;
}
基于 Linux framebuffer 屏幕保护弹球-C语言
未经允许不得转载:huangea的博客 » 基于 Linux framebuffer 屏幕保护弹球-C语言
碰巧看到这个,真不错,我会再来关注你的。
网站看起来很不错,支持一下,有机会再来