我想在图形窗口中打印出我的鼠标坐标,当用户点击它时,应该会出现一条消息“点击”。但问题是当用户点击它时,我收到的不是 1 条消息,而是 5-10 条消息。我知道这可能是因为我释放按钮的速度有多快。有没有办法只打印一次?
#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()