kazuya takase
vef03****@nifty*****
2003年 7月 20日 (日) 13:16:45 JST
渋川さんへ、 本日の実績(2/2)を送ります。 test サンプルに追加(kt-clock.c:時計もどき)お願いします。 あまり、サンプルを増やさないほうが良いですか? ◎「kt-clock.c の概要」 30秒間だけ動作(END_TIME で変更可能)する時計もどきです。 「X Window で遊ぶ本」の中の時計プログラムを改造しました。 外周から内周へ向けて、秒・分・時を表していますが、ちょっと 不思議な時計です。(左回りで、0時が真下!!) ベースにしたプログラムは「シグナル」を使っていましたが、 Be OS(POSIX)へのポーティングが不完全の為、sleepでごまかし ました。 <疑問点> (1)毎秒、XCreateGC を呼ぶ必要があるのか? (2)時々描画(一部)されない時がある。 (3)描画される色がたまに変化する。 //----- start /* kt-clock.c */ #include <X11/Xlib.h> #include <math.h> //----- #define WIN_WIDTH 100 #define WIN_HEIGHT 100 //----- #define END_TIME 30 //----- clock timer end (sec) int ti = 1; //----- clock timer sleep (sec) //----- Display *w_dis; Window w_win; GC w_gc; //----- draw_hand( double th, int r_start, int r_end ) { int x_st, x_end, y_st, y_end; x_st = WIN_WIDTH / 2 + r_start * cos( th ); x_end = WIN_WIDTH / 2 + r_end * cos( th ); y_st = WIN_WIDTH / 2 + r_start * sin( th ); y_end = WIN_WIDTH / 2 + r_end * sin( th ); XDrawLine( w_dis, w_win, w_gc, x_st, y_st, x_end, y_end ); } //----- int drawFace( viod ) { time_t ti; struct tm *now; time( &ti ); now = localtime( &ti ); XClearWindow( w_dis, w_win ); XDrawArc( w_dis, w_win, w_gc, 0, 0, 99, 99, 0, 360 * 64 ); XDrawArc( w_dis, w_win, w_gc, 20, 20, 59, 59, 0, 360 * 64 ); XDrawArc( w_dis, w_win, w_gc, 30, 30, 39, 39, 0, 360 * 64 ); XDrawArc( w_dis, w_win, w_gc, 40, 40, 19, 19, 0, 360 * 64 ); draw_hand( M_PI / 2 - ( now->tm_hour ) * 2 * M_PI / 12, 10, 20 ); draw_hand( M_PI / 2 - ( now->tm_min ) * 2 * M_PI / 60, 20, 30 ); draw_hand( M_PI / 2 - ( now->tm_sec ) * 2 * M_PI / 60, 30, 50 ); XFlush( w_dis ); //----- printf( "[%02dh:%02dt:%02ds]\n", now->tm_hour, now->tm_min, now->tm_sec ); } //----- main int main( int argc, char *argv[] ) { XSetWindowAttributes w_att; XEvent w_eve; char w_title[] = "kt-clock"; char w_icon_title[] = "ICON!"; int i; w_dis = XOpenDisplay( NULL ); w_win = XCreateSimpleWindow( w_dis, RootWindow( w_dis, 0 ),10 ,100 , WIN_WIDTH, WIN_HEIGHT, 2, BlackPixel( w_dis, 0 ), WhitePixel( w_dis, 0 )); XSetStandardProperties( w_dis, w_win, w_title, w_icon_title, None, argv, argc, NULL ); w_att.override_redirect = True; XChangeWindowAttributes( w_dis, w_win, CWOverrideRedirect, &w_att ); XSelectInput( w_dis, w_win, ExposureMask ); XMapWindow( w_dis, w_win ); do{ XNextEvent( w_dis, &w_eve); }while( w_eve.type != Expose ); //----- for( i = 0; i < END_TIME; i++ ) { w_gc = XCreateGC( w_dis, w_win, 0, 0 ); drawFace( ); sleep( ti ); } //----- XDestroyWindow( w_dis , w_win ); XCloseDisplay( w_dis ); return( 0 ); } //----- end ------------------ kt ( kazuya takase )