I want to print out the coordinates of my mouse in the graphical window, and when the user clicks it, there should appear a message "clicked". But the problem is when the user does click it, instead of 1 message, I get around 5-10 messages. I understand it's probably because of how fast I release the button. Is there a way to print just one time?
#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()