/*
SDL2+C标准库写的时钟小程序文章源自微观生活(93wg.com)微观生活-https://93wg.com/11548.html
安卓系统下测试可行文章源自微观生活(93wg.com)微观生活-https://93wg.com/11548.html
下列是完全代码文章源自微观生活(93wg.com)微观生活-https://93wg.com/11548.html
*/文章源自微观生活(93wg.com)微观生活-https://93wg.com/11548.html
//画可旋转矩形,缭绕矩形中心旋转/顺时针旋转文章源自微观生活(93wg.com)微观生活-https://93wg.com/11548.html
//相对于于数学坐标象限4/3/2/1文章源自微观生活(93wg.com)微观生活-https://93wg.com/11548.html
//参数分别是渲染器,矩形中心点坐标x0/y0坐标,矩形w宽/h高,A旋转角度/弧度值文章源自微观生活(93wg.com)微观生活-https://93wg.com/11548.html
int myRect(SDL_Renderer *render,int x0,int y0,int w,int h,double A)文章源自微观生活(93wg.com)微观生活-https://93wg.com/11548.html
{文章源自微观生活(93wg.com)微观生活-https://93wg.com/11548.html
//源矩形四个顶点坐标文章源自微观生活(93wg.com)微观生活-https://93wg.com/11548.html
double Ax,Ay,Bx,By,Cx,Cy,Dx,Dy;
//以矩形中心作为暂时坐标原点
Ax=-w/2.0;Ay=+h/2.0;
Bx=Ax;By=-h/2.0;
Cx=+w/2.0;Cy=By;
Dx=Cx;Dy=Ay;
SDL_Point p0[4]=
{
{Ax,Ay},//A
{Bx,By},//B
{Cx,Cy},//C
{Dx,Dy} //D
};
/*
逆时针旋转:
x&
y&
*/
//旋转A弧度后矩形ABCD四点新坐标
double A1x,A1y,B1x,B1y,C1x,C1y,D1x,D1y;
D1x=Dx*cos(A)-Dy*sin(A)+x0;
D1y=Dx*sin(A)+Dy*cos(A)+y0;
C1x=Cx*cos(A)-Cy*sin(A)+x0;
C1y=Cx*sin(A)+Cy*cos(A)+y0;
A1x=Ax*cos(A)-Ay*sin(A)+x0;
A1y=Ax*sin(A)+Ay*cos(A)+y0;
B1x=Bx*cos(A)-By*sin(A)+x0;
B1y=Bx*sin(A)+By*cos(A)+y0;
//旋转后的矩形四个点坐标
SDL_Point p1[5]=
{
{A1x,A1y},//A
{B1x,B1y},//B
{C1x,C1y},//C
{D1x,D1y}, //D
{A1x,A1y}//A
};
//画出旋转后矩形
SDL_RenderDrawLines(render,p1,5);
return 0;
}
//画圆函数
int myCircle(SDL_Renderer *render,int x0,int y0,int r)
{
double x1=0.0,y1=0.0;
static double TPI=6.2831853072;
double temp=0.0;
double step=0.0031415926536;
for(int i=0;temp<TPI;i++)
{
temp=i*step;
x1=x0-r*cos(temp);
y1=y0-r*sin(temp);
SDL_RenderDrawPoint(render, x1, y1);
}
return 0;
}
//画刻度
int myDraw(SDL_Renderer *render,int x0,int y0,int r)
{
double x1=0.0,y1=0.0;
static double TPI=6.2831853072;
double a1=0.1047197551;
double st=0.0,x2=0.0,y2=00;
//画刻度线60个 长度15像素
//夹角弧度
SDL_SetRenderDrawColor(render, 255, 255, 255, SDL_ALPHA_OPAQUE);
for(int j=0;j<60;j++)
{
st=j*a1;
x1=x0-r*cos(st);
y1=y0-r*sin(st);
x2=x1-15.0*cos(st);
y2=y1-15.0*sin(st);
if(j%5==0 && j!=0 && j!=15 && j!=30 && j!=45)
{
myRect(render,x1,y1,30,8,j*PI/30);
}
if(j==0) //9
{
SDL_Rect rect = {x1-15,y1-4,30 , 8 };
SDL_RenderFillRect(render, &rect);
}
else if(j==15) //12
{
SDL_Rect rect = {x1-4,y1-15,8 ,30 };
SDL_RenderFillRect(render, &rect);
}
else if(j==30) //3
{
SDL_Rect rect = {x1-10,y1-4,30, 8};
SDL_RenderFillRect(render, &rect);
}
else if(j==45) //6
{
SDL_Rect rect = {x1-4,y1-10,8 ,30};
SDL_RenderFillRect(render, &rect);
}
else
{
if(j!=5 && j!=10 && j!=20 && j!=25 && j!=35 && j!=40 &&j!=50 &&j!=55)
{
SDL_Point points[2]
{
{x1, y1},
{x2, y2}
};
//画线
SDL_RenderDrawLines(render, points, 2);
}
}
}
return 0;
}
//实时获取东8区时间
//获取时/分/秒
int getTime(int *hour,int *min,int *sec)
{
time_t now;
time(&now);
struct tm *info;
//获取GMT时间
info=gmtime(&now);
*hour=(info->tm_hour+8)%24;
*min=info->tm_min;
*sec=info->tm_sec;
return 0;
}
//动态画表针
int drawHand(SDL_Renderer *ren)
{
//时/分/秒
int h=0,m=0,s=0;
//对应弧度
float Ah,Am,As;
//获取时间
getTime(&h,&m,&s);
//当前时针偏转弧度
Ah=h*PI/6.0+m*PI/360.0;
//当前分针偏转弧度
Am=m*PI/30.0+s*PI/1800.0;
//当前秒针偏转弧度
As=s*PI/30.0;
//初始化时针在12时位置 时针长200宽12
//画时针
myRect(ren,360+100*sin(Ah),640-100*cos(Ah),12,200,Ah);
//画分针
myRect(ren,360+120*sin(Am),640-120*cos(Am),8,240,Am);
//画秒针
myRect(ren,360+140*sin(As),640-140*cos(As),4,280,As);
return 0;
}
int main(int agrs,char *argv[])
{
//定义窗口
SDL_Window *wnd=NULL;
//定义渲染器
SDL_Renderer *render=NULL;
//初始化所有子系统
SDL_Init(SDL_INIT_EVERYTHING);
// 创立窗口
wnd = SDL_CreateWindow(
// 标题
&
// 居中
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
// 宽高
350, 350,
//状况
SDL_WINDOW_SHOWN);
//检查窗口是不是创立胜利
if (wnd == NULL)
{
printf(&
return 1;
}
//渲染器创立 render=SDL_CreateRenderer(wnd,1,1);
//背风景黑色 SDL_SetRenderDrawColor(render,0,0,0,255);
//刷新屏幕
SDL_RenderClear(render);
//选择绘制色彩
SDL_SetRenderDrawColor(render, 255, 0, 0, SDL_ALPHA_OPAQUE);
while(1){
//绘制钟盘
myDraw(render,360,640,300);
SDL_SetRenderDrawColor(render, 255, 0, 0, SDL_ALPHA_OPAQUE);
myCircle(render,360,640,3);
myCircle(render,360,640,4);
SDL_SetRenderDrawColor(render, 0, 0, 255, SDL_ALPHA_OPAQUE);
myCircle(render,360,640,11);
myCircle(render,360,640,12);
drawHand(render);
SDL_RenderPresent(render);
}
//清算
SDL_DestroyRenderer(render);
SDL_DestroyWindow(wnd);
SDL_Quit();
return 0;
}
以上就是微观生活(93wg.com)关于“钟表程序(C语言+SDL2)”的详细内容,希望对大家有所帮助!
评论