我想在圖形窗口中打印出鼠標的座標,當用戶點擊它時,應該會出現一條消息「clicked」。但問題是當用戶點擊它時,而不是1條消息,我得到5-10條消息。我明白這可能是因爲我釋放按鈕的速度有多快。有沒有辦法只打印一次?Allegro,輸出鼠標點擊
#include <allegro.h>
#include <iostream>
int main(){
allegro_init();
install_keyboard();
install_mouse();
set_color_depth(32);
set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
BITMAP *pic = load_bitmap("mouse.bmp",NULL);
BITMAP *buffer = create_bitmap(640,480);
int x = 0, y = 0;
while(!key[KEY_ESC]){
blit(buffer, screen, 0,0,0,0, buffer->w,buffer->h);
draw_sprite(buffer, pic, mouse_x, mouse_y);
blit(buffer, screen, 0,0,0,0, buffer->w, buffer->h);
clear_bitmap(buffer);
if(mouse_x!=x && mouse_y!=y){
std::cout<<mouse_x<<":"<<mouse_y<<std::endl;
}
if(mouse_b&1){
std::cout<<std::endl<<">>CLICKED<<"<<std::endl;
}
x=mouse_x, y=mouse_y;
}
destroy_bitmap(pic);
destroy_bitmap(buffer);
return 0;
}
END_OF_MAIN()