Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Problem with events

This thread is locked; no one can reply to it. rss feed Print
Problem with events
DaVid95
Member #16,132
December 2015

Hello.
I decided to learn some programming in Allegro so I tried to write some elements from a tutorial. And now I have odd problem with my application, here is the code:

#SelectExpand
1#pragma once 2#include <stdio.h> 3#include "allegro5/allegro_primitives.h" 4#include "allegro5/allegro_font.h" 5#include "allegro5/allegro_ttf.h" 6 7 8int main(int argc, char **argv) 9{ 10 ALLEGRO_DISPLAY *display = NULL; 11 ALLEGRO_EVENT_QUEUE *event_queue = NULL; 12 ALLEGRO_BITMAP *bitmap = NULL; 13 ALLEGRO_FONT *font = NULL; 14 ALLEGRO_EVENT event; 15 16 if (!al_init()) { 17 fprintf(stderr, "failed to initialize allegro!\n"); 18 return -1; 19 } 20 if (!al_install_keyboard()) { 21 fprintf(stderr, "failed to initialize the keyboard!\n"); 22 return -1; 23 } 24 al_init_font_addon(); 25 al_init_ttf_addon(); 26 if (!al_install_mouse()) { 27 fprintf(stderr, "failed to initialize the mouse!\n"); 28 return -1; 29 } 30 display = al_create_display(800, 600); 31 if (!display) { 32 fprintf(stderr, "failed to create display!\n"); 33 return -1; 34 } 35 event_queue = al_create_event_queue(); 36 if (!event_queue) { 37 fprintf(stderr, "failed to create event_queue!\n"); 38 return -1; 39 } 40 font = al_load_ttf_font("Fonts/R-2014.ttf", 12, 0); 41 if (!font) 42 { 43 fprintf(stderr, "Could not load 'R-2014.ttf'.\n"); 44 return -1; 45 } 46 47 al_register_event_source(event_queue, al_get_display_event_source(display)); 48 al_register_event_source(event_queue, al_get_mouse_event_source()); 49 al_register_event_source(event_queue, al_get_keyboard_event_source()); 50 51 al_set_window_title(display, "Title!"); 52 53 al_init_primitives_addon(); 54 al_clear_to_color(al_map_rgb(255, 255, 255)); 55 bitmap = al_create_bitmap(100, 100); 56 al_set_target_bitmap(bitmap); 57 al_clear_to_color(al_map_rgb(0, 0, 255)); 58 al_set_target_bitmap(al_get_backbuffer(display)); 59 60 al_flip_display(); 61 62 int x = 0, y = 0; 63 int xx = 0, yy = 0; 64 unsigned long int frames = 0, cframes = 0, time1 = al_get_time(), time2 = 0; 65 //al_hide_mouse_cursor(display); 66 bool get_event; 67 for (;;) 68 { 69 get_event = al_get_next_event(event_queue, &event); 70 if (get_event) 71 { 72 if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) 73 { 74 break; 75 } 76 else if (event.type == ALLEGRO_EVENT_MOUSE_AXES || ALLEGRO_EVENT_MOUSE_ENTER_DISPLAY) 77 { 78 xx = event.mouse.x; 79 yy = event.mouse.y; 80 } 81 else if (event.type == ALLEGRO_EVENT_KEY_DOWN) 82 { 83 switch (event.keyboard.keycode) 84 { 85 case ALLEGRO_KEY_UP: --y; break; 86 case ALLEGRO_KEY_DOWN: ++y; break; 87 case ALLEGRO_KEY_LEFT: --x; break; 88 case ALLEGRO_KEY_RIGHT: ++x; break; 89 } 90 if (x > 800) x = -100; 91 if (x < -100) x = 800; 92 if (y > 600) y = -100; 93 if (y < -100) y = 600; 94 } 95 } 96 ++frames; 97 98 time2 = al_get_time(); 99 if (time2 != time1) 100 { 101 time1 = time2; 102 printf("%d\n", frames); 103 cframes = frames; 104 frames = 0; 105 } 106 107 if (al_is_event_queue_empty(event_queue)) 108 { 109 al_clear_to_color(al_map_rgb(0, 0, 0)); 110 al_set_target_bitmap(bitmap); 111 al_clear_to_color(al_map_rgb(255, 0, 0)); 112 al_set_target_bitmap(al_get_backbuffer(display)); 113 al_draw_bitmap(bitmap, x, y, 0); 114 115 al_draw_filled_ellipse(xx, yy, 5, 5, al_map_rgba(0, 255, 255, 255)); 116 117 al_draw_textf(font, al_map_rgb(255, 255, 255), 0, 0, 0, "%d", cframes); 118 al_flip_display(); 119 } 120 } 121 122 al_destroy_display(display); 123 al_destroy_event_queue(event_queue); 124 al_destroy_bitmap(bitmap); 125 return 0; 126}

I have problem with events, when I press arrows, a square doesn't move and a circle under mouse cursor 'teleports' to upper left corner, and I absolutely don't know why. Here is a video of the problem:

video

When I swap a part of code...:

#SelectExpand
1 //... 2 else if (event.type == ALLEGRO_EVENT_KEY_DOWN) 3 { 4 switch (event.keyboard.keycode) 5 { 6 case ALLEGRO_KEY_UP: --y; break; 7 case ALLEGRO_KEY_DOWN: ++y; break; 8 case ALLEGRO_KEY_LEFT: --x; break; 9 case ALLEGRO_KEY_RIGHT: ++x; break; 10 } 11 if (x > 800) x = -100; 12 if (x < -100) x = 800; 13 if (y > 600) y = -100; 14 if (y < -100) y = 600; 15 } 16 17 else if (event.type == ALLEGRO_EVENT_MOUSE_AXES || ALLEGRO_EVENT_MOUSE_ENTER_DISPLAY) 18 { 19 xx = event.mouse.x; 20 yy = event.mouse.y; 21 } 22 //...

... the square moves, but the circle still teleports:

video

What am I doing wrong ??? Please help.
I add that I don't want to limit fps, so the program should gets events in every frame.
And sorry if there is any language mistakes, but my english is very bad ::)

SiegeLord
Member #7,827
October 2006
avatar

This line contains the error:

else if (event.type == ALLEGRO_EVENT_MOUSE_AXES || ALLEGRO_EVENT_MOUSE_ENTER_DISPLAY)

You want it to be:

else if (event.type == ALLEGRO_EVENT_MOUSE_AXES || event.type == ALLEGRO_EVENT_MOUSE_ENTER_DISPLAY)

Make sure you understand the difference between those two :).

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

DaVid95
Member #16,132
December 2015

Yes, of course :D
I didn't notice this, thanks for the help.

Go to: