close
c++ 圖形編程 打造簡易的時鐘 allegro
//因為OLED要設計一個傳統時鐘
//特地把從前學的程式找出來復習一遍
//參考網站
http://www.connellybarnes.com/documents/quick_reference.html
#include <allegro.h>
#include <math.h>
#include <ctime>
#define orx 320
#define ory 240
#define PI 3.1415926
#define PI_DIV2 1.57
#define SS_LEN 180
#define MM_LEN 150
#define HH_LEN 120
#define BCOLOR makecol(0, 0, 0)
#define WCOLOR makecol(255, 255, 255)
//BITMAP *buffer; //This will be our temporary bitmap for double buffering
void init(void) {
int depth, res;
//Call allegro_init() at the very start of all programs.
allegro_init();
depth = desktop_color_depth(); //8,15,16,24,32
if (depth == 0) depth = 32;
set_color_depth(depth);
//set_color_depth(8);
res = set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
if (res != 0) {
allegro_message(allegro_error);
exit(-1);
}
install_timer();
install_keyboard();
install_mouse();
/* add other initializations here */
}
void deinit(void) {
clear_keybuf();
/* add other deinitializations here */
}
void draw_clk_init(void)
{
unsigned short int i,j,x1,x2,y1,y2;
float fk;
acquire_screen();
//Set the background color of the window to light gray.
clear_to_color( screen, WCOLOR ); //white screen
putpixel( screen, 5, 5, BCOLOR );
//setbkcolor(1);
circle(screen, orx, ory, 200, BCOLOR);
circle(screen, orx, ory, 205, BCOLOR);
circle(screen, orx, ory, 5, BCOLOR);
for(i=0; i<60; i++)
{
//fk=i*6*pi/180;
fk=i*PI/30;
if(i%5==0)
{
if(i%15==0) j=15;
else j=10;
}
else j=5;
x1 = (unsigned short)(200 *cos(fk)) + orx;
y1 = (unsigned short)(200 *sin(fk)) + ory;
x2 = (unsigned short)((200-j)*cos(fk)) + orx;
y2 = (unsigned short)((200-j)*sin(fk)) + ory;
line(screen, x1, y1, x2, y2, BCOLOR);
}
release_screen();
}
void d(unsigned char rr, unsigned char xx, unsigned char uu, int sc)
{
//All graphics commands take integer arguments.
unsigned short int dx,dy;
double fk=xx*uu*PI/180 - PI_DIV2;
//clock-wise
dx=(unsigned short)(rr*cos( fk )) + orx;
dy=(unsigned short)(rr*sin( fk )) + ory;
acquire_screen();
line(screen, orx, ory, dx, dy, sc) ;
release_screen();
}
int main() {
int hh=13,mm=61,ss=61;
time_t t;
tm *tmp;
init();
draw_clk_init();
while (!key[KEY_ESC]) {
/* put your code here */
t=time(NULL);
tmp = localtime(&t);
if (hh != tmp->tm_hour)
{
//clear old
d(HH_LEN,hh,30,WCOLOR);
//new
hh=tmp->tm_hour;
d(HH_LEN,hh,30,BCOLOR);
}
if (mm != tmp->tm_min)
{
//clear old
d(MM_LEN,mm,6,WCOLOR);
//if(mm==hh)
{
d(HH_LEN,hh,30,BCOLOR);
}
mm=tmp->tm_min;
d(MM_LEN,mm,6,BCOLOR);
}
if (ss != tmp->tm_sec)
{
//clear old
d(SS_LEN, ss, 6, WCOLOR); //(radus, sec, 360/60, color)
//if(ss==hh)
{ d(HH_LEN, hh, 30, BCOLOR); }
if(ss==mm) { d(MM_LEN, mm, 6, BCOLOR); }
//draw new
ss=tmp->tm_sec;
d(SS_LEN,ss,6,BCOLOR);
circle(screen, orx, ory, 5, BCOLOR);
}
} //EOF while (!key[KEY_ESC])
deinit();
return 0;
}
//for allegro use only
END_OF_MAIN()
全站熱搜
留言列表