火焰字,那应该好看的。
虽然cpu会不休息了。
为什么没人用火焰作一个时间显示的屏保。
- eexpress
- 帖子: 58428
- 注册时间: 2005-08-14 21:55
- 来自: 长沙
为什么没人用火焰作一个时间显示的屏保。
● 鸣学
- darasion
- 帖子: 1153
- 注册时间: 2007-12-05 12:25
Re: 为什么没人用火焰作一个时间显示的屏保。
屏幕保住了,CPU却烧了
- eexpress
- 帖子: 58428
- 注册时间: 2005-08-14 21:55
- 来自: 长沙
- lerosua
- 论坛版主
- 帖子: 8455
- 注册时间: 2007-11-29 9:41
- 联系:
Re: 为什么没人用火焰作一个时间显示的屏保。
液晶还用啥屏保...
- Hello World!
- 帖子: 3051
- 注册时间: 2008-06-23 15:19
- 系统: ���������
- 来自: 北欧某国
- 联系:
Re: 为什么没人用火焰作一个时间显示的屏保。
我们大家都不会做屏保。。。
哪位能把这段代码改成屏保啊?
代码: 全选
//编译参数:gcc -o ubuntu-clock ubuntu-clock.c -I/usr/X11R6/include -L/usr/X11R6/lib -lX11 -lm -lXpm
#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>
#include <X11/Xatom.h>
#include <X11/xpm.h> //绘制xpm
#include <signal.h> // for signal()
#include <sys/time.h> // struct itimeral. setitimer()
#include <math.h> //数学函数库
#include "back.xpm"
#define DUMP_ERR printf
//设为全局
Display *display;
Window win;
int screennum;
static void draw(Display *display,Window win,GC gc,int width,int height,Pixmap pxmap,XpmAttributes xpmattr);
void printMsg(int);
int main()
{
int width;
int height;
Atom protocols;
const char *strdraw = "hello World";
GC gc;
//打开显示 没有指明打开哪个显示时使用环境变量中的DESPLAY
display = XOpenDisplay(NULL);
if( !display ){
DUMP_ERR("call XOpenDisplay(%s) fail\n",XDisplayName(NULL));
return 1;
}
//得到系统当前的屏幕 (如果使用虚拟屏幕的话 一般会有几个)
// get default screen
screennum = DefaultScreen(display);
//根据屏幕的大小决定窗口的大小
height = DisplayHeight(display,screennum)/2;
width = height;
//创建窗口
// create window
win = XCreateSimpleWindow(display,
RootWindow(display,screennum),
0,0,width,height,3,
BlackPixel(display,screennum),
WhitePixel(display,screennum));
//增加这个代码是捕获程序退出事件
protocols = XInternAtom(display,"WM_DELETE_WINDOW", True);
XSetWMProtocols(display, win, &protocols, 1);
//选择窗口关心的事件
// select event
XSelectInput(display,win,
ExposureMask|KeyPressMask
|ButtonPressMask|StructureNotifyMask);
//创建GC(图形上下文)
// create gc
{
unsigned long valuemask = 0;
XGCValues values;
gc = XCreateGC(display,win,valuemask,&values);
}
// 映射窗口 只有map后窗口才会在屏幕上显示
// show window
XMapWindow(display,win);
int res = 0;
//读入pxmap
Pixmap pxmap;
XpmAttributes xpmattr;
memset(&xpmattr, 0, sizeof(xpmattr));
res = XpmCreatePixmapFromData(display, win, back_xpm, &pxmap, NULL, &xpmattr);
printf("XpmCreatePixmapFromData() return %d\n", res);
XCopyArea(display, pxmap, win, gc, (xpmattr.width-width)/2, (xpmattr.height-height)/2, xpmattr.width, xpmattr.height, 0, 0);
//设置定时器
// Register printMsg to SIGALRM
signal(SIGALRM, printMsg);
struct itimerval tick;
// Initialize struct
memset(&tick, 0, sizeof(tick));
// Timeout to run function first time
tick.it_value.tv_sec = 1; // sec
tick.it_value.tv_usec = 0; // micro sec.
// Interval time to run function
tick.it_interval.tv_sec = 1;
tick.it_interval.tv_usec = 0;
// Set timer, ITIMER_REAL : real-time to decrease timer,
// send SIGALRM when timeout
res = setitimer(ITIMER_REAL, &tick, NULL);
if (res) {
printf("Set timer failed!!\n");
}
//进入事件循环
// event loop
while(1){
XEvent event;
XNextEvent(display,&event); // 读一个事件
switch(event.type){
case Expose: // expose window 显示事件
if( event.xexpose.count != 0) break;
draw(display,win,gc,width,height,pxmap,xpmattr);
//XDrawString(display,win,gc,10,10,
//strdraw,strlen(strdraw));
break;
case ConfigureNotify: // when the window’s size change
width = event.xconfigure.width;
height = event.xconfigure.height;
break;
case ClientMessage :
if(event.xclient.data.l[0] == protocols){
DUMP_ERR("recv destroy notify\n");
XFreeGC(display,gc);
XDestroyWindow(display, win);
XCloseDisplay(display);
}
return 0;
default:
break;
}
}
}
// 使用changeGC修改属性的代码 只有valuemask被设置的属性才会根据XCGValues里面的值进行修改
static void set_gc_values(Display *display,GC gc)
{
XGCValues values;
values.line_width = 2;
values.line_style = LineSolid;
values.cap_style = CapRound;
values.join_style = JoinRound;
XChangeGC(display,gc,GCLineWidth|GCLineStyle,//|GCCapStyle|GCFillStyle,
&values);
}
// 画画的代码
static void draw(Display *display,Window win,GC gc,int width,int height,Pixmap pxmap,XpmAttributes xpmattr)
{
//清除窗口中的绘图,现在不需要了。
//XClearWindow(display,win);
//获取系统时间
long ct;
struct tm *currtime;
ct=time(&ct);
currtime=(struct tm *)localtime(&ct);
//计算表针位置,从alltraxclock.c里抄来的。
int second_hand_x = (int) (sin((currtime->tm_sec * 3.14159 * 2) / 60) * 260);
int second_hand_y = (int) (cos((currtime->tm_sec * 3.14159 * 2) / 60) * 260);
int big_hand_x = (int) (sin((currtime->tm_min * 3.14159 * 2) / 60) * 230);
int big_hand_y = (int) (cos((currtime->tm_min * 3.14159 * 2) / 60) * 230);
//计算时针的时候,要把分钟也加上。
int little_hand_x = (int) (sin(((currtime->tm_hour+currtime->tm_min/60.0) * 3.14159 * 2) / 12) * 200);
int little_hand_y = (int) (cos(((currtime->tm_hour+currtime->tm_min/60.0) * 3.14159 * 2) / 12) * 200);
//绘制xpm
XCopyArea(display, pxmap, win, gc, (xpmattr.width-width)/2, (xpmattr.height-height)/2, xpmattr.width, xpmattr.height, 0, 0);
//改变线形
XSetLineAttributes(display,gc,10,LineSolid,CapRound,JoinRound);
XSetForeground(display, gc, WhitePixel(display, screennum));
//画一个表针
XDrawLine(display,win,gc,width/2,height/2,width/2+little_hand_x,height/2-little_hand_y);
XDrawLine(display,win,gc,width/2,height/2,width/2+big_hand_x,height/2-big_hand_y);
XFillArc(display,win,gc,width/2+second_hand_x-10,height/2-second_hand_y-10,20,20,0,64*360);
}
void printMsg(int num) {
//让窗口重画
XEvent event;
memset(&event,0,sizeof(XEvent));
event.type=Expose;
XSendEvent(display,win,True,0x0,&event);
XFlush(display);
}
http://noie.name 网站改版中。
- eexpress
- 帖子: 58428
- 注册时间: 2005-08-14 21:55
- 来自: 长沙