Linux 下的桌面指针时钟

Linux 下的桌面指针时钟

一.开发背景

随着科学技术的日益增加,软件行业在有了新的突破。特别是在移动设备上更有着远大的开发空间,目前,移动设备上,包括手机,笔记本,平板,发展的特别快,界面也越来越友好,给用户更新的体现。

基于Linux系统的界面的很单调的问题上了,我做了一个简单的桌面指针时钟,就像是windows上的那样,虽然不美观,但是还有点实际应用,开放源码仅供参考!

二.功能简介

与普通的C语言程序一样,本时钟在终端里运行后(也可以双击可执行文件),会出现一个界面,会从系统中得到时间,并将相应的时间所对应的角度传给相应的指针,并显示到相应的位置上。

时间是系统时间,而且是运行的时候获取,所以时钟的转动都是参考系统时间的。

滴答滴,滴答滴,时钟不停的转动,滴答滴,滴答滴小雨拍打着水花.......就像这首歌一样,时钟不停的转动!

三.开发工具

C语言 + SDL图像界面开发包(包括SDL的扩展库SDL_image SDL_gfx)。

四.开发环境

Red Hat Linux ,由于C语言的移植性很好,SDL也可以跨平台移植所以本源代码可以在几乎所有的系统上运行!本人已经在arm up6410 开发板上测试成功!但是有点卡(本人能力有限)!

五.界面显示

 

源程序在Linux系统的截图,本人能力有限,界面只能做成这样了,有能力的可以修改一下!

 

 

六.主任寄语

本人只是个菜鸟,甚至连菜鸟都称不上。就喜欢写一些这样类似的东西。希望大家运行测试一下,下面有代码,你们可以对源码进行更改,本人没有任何意见,如果你们有什么意见和建议的话 可以发邮件给我!大家相互交流相互学习!

 

 

七.源代码

 

 

#include <SDL/SDL.h>

#include "SDL/SDL_rotozoom.h"

#include "SDL/SDL_image.h"

#include "math.h"

#include "string.h"

#include <time.h>

#include"SDL/SDL_gfxPrimitives.h"

 

/*

 

初始化SDL 函数库

SDL——INITEVERTHING 代表所有的 视频音频 图片 ...

 

*/

 

SDL_Surface * screen = NULL;

SDL_Rect rect;

 

void Init()

{

 

       if(SDL_Init(SDL_INIT_EVERYTHING)==-1)

       {

              printf("SDL_INIT fault !\n");

              exit(0);

       }

      

}

// 创建屏幕 640*480 0  SDL_SWSURFACE

void Create_Screen(int width , int height ,int bpp , Uint32 flags)

{

 

 

       //SDL_Surface*screen = NULL;

       screen= SDL_SetVideoMode(width , height,  bpp ,flags);

       if(screen== NULL)

       {

              printf("SDL_SetVideoMode default!\n");

              exit(1);

       }

 

}

// 加载图片传递文件的 绝对路径

SDL_Surface * Image_Load(char *filename)

{

       SDL_Surface*image = NULL;

       image= IMG_Load(filename);

       if(image== NULL)

       {

              printf("The image load default,maybe can't find %s !!!\n",filename);

              exit(0);

      

       }

       returnimage;

}

void Free_Image(SDL_Surface * image)

{

       SDL_FreeSurface(image);

 

}

 

//用来显示 image 图片,rect 可以调节位置

int Show_Image(SDL_Surface * image,SDL_Rect*rect)

{

 

       SDL_BlitSurface(image, NULL , screen , rect);

       SDL_UpdateRect(screen, 0 , 0 , 0 , 0 );

      

     return 0;

}

 

//找到图片的中点坐标

SDL_Rect Set_Image(SDL_Surface *image,double zoom)

{

       //doublex,y ;

       SDL_Rectrect;

      

       rect.x=  (screen->w - image->w ) / 2;

       rect.y=  (screen->h - image->h ) / 2;

      

       returnrect;

      

}

 

//改变秒针所对应的角度,一秒钟走六度

SDL_Surface *Change_Angle_Sec(SDL_Surface*image ,double *angle)

{

       SDL_Surface*change = NULL ;

       SDL_Rectrect;

              if(*angle <= -360)

              {

                     *angle += 360;

              }

 

              *angle = *angle -6.0;

              change = rotozoomSurfaceXY(image, *angle,1, 1, 1);

              rect = Set_Image(change , 1);

              Show_Image(change,&rect);

              Free_Image(change);    

             

       returnchange;

}

 

SDL_Surface *Change_Angle_Min(SDL_Surface*image ,double *angle,double sec)

{

       SDL_Surface*change = NULL ;

       SDL_Rectrect;

       //秒针周一圈 分周走 六度

       if((sec== 0) || (sec == -360))

       {

              if(*angle <= -360)

                     {

                     *angle += 360;

                     }

                     *angle= *angle -6;

 

       }

              change = rotozoomSurfaceXY(image, *angle,1, 1, 1);

              rect = Set_Image(change , 1);

             

       Show_Image(change,&rect);

       Free_Image(change);

       returnchange;

}

 

SDL_Surface *Change_Angle_Hour(SDL_Surface*image ,double *angle,double min)

{

       SDL_Surface*change = NULL ;

       SDL_Rectrect;

 

       //分针周一圈 走 六度

       if(min== 360 || min ==60 || min == 120 || min ==180 || min ==240 || min == 300)

       {

              if(*angle < -360)

                     {

                            * angle += 360;

                     }

                     *angle= *angle -5;

       }

              change = rotozoomSurfaceXY(image, *angle,1, 1, 1);

              rect = Set_Image(change , 1);

             

              Show_Image(change,&rect);

             

       Free_Image(change);

       returnchange;

}

 

void Get_System_Time(double *hour,double*min,double *sec)

{

       structtm *now;

       time_tTime = time(NULL);

       now= localtime( &Time );

                            //获取当前的系统时间,然后根据当前时间设置图片的角度!

       *hour= (double)now->tm_hour;

       *min= (double)now->tm_min;

       *sec= (double)now->tm_sec;

      

       printf("TheSystem Time Now : %d : %d : %d \n",(int)*hour,(int)*min,(int)*sec);

       *sec= *sec *-6;

       *min= *min *-6;

       *hour= *hour *-30 + *min/12;

 

}

 

int main(int argc , char **argv)

{

       intflag = 1;

 

       Init();

 

       SDL_Rectrect ;

       rect.x= 80;

       rect.y=0;

 

       doublesec = 0,min = 0, hour = 0, zoom_xy = 1;

       SDL_Surface*change = NULL,*image = NULL,*image_2 = NULL,*image_3 = NULL,* background =NULL;

      

       Create_Screen(640,480,0,SDL_SWSURFACE);

       boxColor(screen,0,0,640,480,0xffffffff);

       Show_Image(screen,0);

   

       image= Image_Load("./image/sec.png");

       image_2= Image_Load("./image/min.png");

       image_3= Image_Load("./image/hour.png");

       background=Image_Load("./image/background.jpg");

       Show_Image(background, &rect);

      

       Get_System_Time(& hour,& min,& sec);//初始化时间让各个指针各就各位

 

       while(flag)

       {

              Change_Angle_Hour(image_3,&hour,min);//分别加载 hour min sec 图片 显示到屏幕上

              Change_Angle_Min(image_2,&min,sec);

              Change_Angle_Sec(image,&sec);

 

              SDL_UpdateRect(screen, 0 , 0 , 0 ,0 );

                                                        //测试代码用来显示system time !

              //printf("The Time Now : %lf : %lf :%lf \n",hour/-30 - min/-6,min/-6,sec/-6);

                                                        //延时一秒秒针走动 ! 这是函数很精确

              SDL_Delay(1000);

                                                        //刷新整个图片

              Show_Image(background , &rect);

 

              SDL_Event event;

              while(SDL_PollEvent(&event))                  //将事件加入事件队列队列

              {    

                     switch(event.type)

                     {

                            case SDL_KEYDOWN:

                                   if(event.key.keysym.sym== SDLK_ESCAPE)

                                   {

                                          flag = 0;

                                   }break;

                            case SDL_QUIT:

                                   {

                                          flag = 0;

                                   printf("\n\nquit!\n\n*************************\n");

                                   }break;

                     }

                    

               }

             

       }

      

}

 

 

Write by:douyuan888  2012/12/31

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值