Outils pour utilisateurs

Outils du site


allegro:input

**Ceci est une ancienne révision du document !** ----

A PCRE internal error occured. This might be caused by a faulty plugin

===== Input ===== Cet article explique le fonction de l'Input clavier et souris. ===== Exemple basique d'Input souris ===== Building on the Basic Bitmaps example, we change the box to move with the mouse. <file c main.c> #include <stdio.h> #include <allegro5/allegro.h> const float FPS = 60; const int SCREEN_W = 640; const int SCREEN_H = 480; const int BOUNCER_SIZE = 32; int main(int argc, char **argv) { ALLEGRO_DISPLAY *display = NULL; ALLEGRO_EVENT_QUEUE *event_queue = NULL; ALLEGRO_TIMER *timer = NULL; ALLEGRO_BITMAP *bouncer = NULL; float bouncer_x = SCREEN_W / 2.0 - BOUNCER_SIZE / 2.0; float bouncer_y = SCREEN_H / 2.0 - BOUNCER_SIZE / 2.0; bool redraw = true; if(!al_init()) { fprintf(stderr, "failed to initialize allegro!\n"); return -1; } if(!al_install_mouse()) { fprintf(stderr, "failed to initialize the mouse!\n"); return -1; } timer = al_create_timer(1.0 / FPS); if(!timer) { fprintf(stderr, "failed to create timer!\n"); return -1; } display = al_create_display(SCREEN_W, SCREEN_H); if(!display) { fprintf(stderr, "failed to create display!\n"); al_destroy_timer(timer); return -1; } bouncer = al_create_bitmap(BOUNCER_SIZE, BOUNCER_SIZE); if(!bouncer) { fprintf(stderr, "failed to create bouncer bitmap!\n"); al_destroy_display(display); al_destroy_timer(timer); return -1; } al_set_target_bitmap(bouncer); al_clear_to_color(al_map_rgb(255, 0, 255)); al_set_target_bitmap(al_get_backbuffer(display)); event_queue = al_create_event_queue(); if(!event_queue) { fprintf(stderr, "failed to create event_queue!\n"); al_destroy_bitmap(bouncer); al_destroy_display(display); al_destroy_timer(timer); return -1; } al_register_event_source(event_queue, al_get_display_event_source(display)); al_register_event_source(event_queue, al_get_timer_event_source(timer)); al_register_event_source(event_queue, al_get_mouse_event_source()); al_clear_to_color(al_map_rgb(0,0,0)); al_flip_display(); al_start_timer(timer); while(1) { ALLEGRO_EVENT ev; al_wait_for_event(event_queue, &ev); if(ev.type == ALLEGRO_EVENT_TIMER) { redraw = true; } else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { break; } else if(ev.type == ALLEGRO_EVENT_MOUSE_AXES || ev.type == ALLEGRO_EVENT_MOUSE_ENTER_DISPLAY) { bouncer_x = ev.mouse.x; bouncer_y = ev.mouse.y; } else if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) { break; } if(redraw && al_is_event_queue_empty(event_queue)) { redraw = false; al_clear_to_color(al_map_rgb(0,0,0)); al_draw_bitmap(bouncer, bouncer_x, bouncer_y, 0); al_flip_display(); } } al_destroy_bitmap(bouncer); al_destroy_timer(timer); al_destroy_display(display); al_destroy_event_queue(event_queue); return 0; }</file> ==== Détails ==== On ne détaille ici que les nouveautés. <code c> if(!al_install_mouse()) { fprintf(stderr, "failed to initialize the mouse!\n"); return -1; }</code> Initialise la gestion de la souris. **al_install_mouse** renvoie false en cas d'échec, dans ce cas on quitte le programme. <code c> al_register_event_source(event_queue, al_get_mouse_event_source());</code> Dirige la source d'event de la souris vers notre queue d'events. Nécessaire pour obtenir les events liés à la souris. <code c> else if(ev.type == ALLEGRO_EVENT_MOUSE_AXES || ev.type == ALLEGRO_EVENT_MOUSE_ENTER_DISPLAY) { bouncer_x = ev.mouse.x; bouncer_y = ev.mouse.y; }</code> Un event de type **ALLEGRO_EVENT_MOUSE_AXES** est généré sur un déplacement de la souris par l'utilisateur, et un event de type **ALLEGRO_EVENT_MOUSE_ENTER_DISPLAY** est généré quand la souris entre dans notre display. Ici on met à jour la position de notre boite aux coordonnées de la souris. <code c> else if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) { break; }</code> Termine le programme en cas d'appui sur n'importe quel bouton de la souris. C'était plutôt simple non? ===== Exemple basique d'Input clavier ===== Building upon the Basic Bitmaps example, we will learn to use the keyboard events. <file c main.c> #include <stdio.h> #include <allegro5/allegro.h> const float FPS = 60; const int SCREEN_W = 640; const int SCREEN_H = 480; const int BOUNCER_SIZE = 32; enum MYKEYS { KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT }; int main(int argc, char **argv) { ALLEGRO_DISPLAY *display = NULL; ALLEGRO_EVENT_QUEUE *event_queue = NULL; ALLEGRO_TIMER *timer = NULL; ALLEGRO_BITMAP *bouncer = NULL; float bouncer_x = SCREEN_W / 2.0 - BOUNCER_SIZE / 2.0; float bouncer_y = SCREEN_H / 2.0 - BOUNCER_SIZE / 2.0; bool key[4] = { false, false, false, false }; bool redraw = true; bool doexit = false; if(!al_init()) { fprintf(stderr, "failed to initialize allegro!\n"); return -1; } if(!al_install_keyboard()) { fprintf(stderr, "failed to initialize the keyboard!\n"); return -1; } timer = al_create_timer(1.0 / FPS); if(!timer) { fprintf(stderr, "failed to create timer!\n"); return -1; } display = al_create_display(SCREEN_W, SCREEN_H); if(!display) { fprintf(stderr, "failed to create display!\n"); al_destroy_timer(timer); return -1; } bouncer = al_create_bitmap(BOUNCER_SIZE, BOUNCER_SIZE); if(!bouncer) { fprintf(stderr, "failed to create bouncer bitmap!\n"); al_destroy_display(display); al_destroy_timer(timer); return -1; } al_set_target_bitmap(bouncer); al_clear_to_color(al_map_rgb(255, 0, 255)); al_set_target_bitmap(al_get_backbuffer(display)); event_queue = al_create_event_queue(); if(!event_queue) { fprintf(stderr, "failed to create event_queue!\n"); al_destroy_bitmap(bouncer); al_destroy_display(display); al_destroy_timer(timer); return -1; } al_register_event_source(event_queue, al_get_display_event_source(display)); al_register_event_source(event_queue, al_get_timer_event_source(timer)); al_register_event_source(event_queue, al_get_keyboard_event_source()); al_clear_to_color(al_map_rgb(0,0,0)); al_flip_display(); al_start_timer(timer); while(!doexit) { ALLEGRO_EVENT ev; al_wait_for_event(event_queue, &ev); if(ev.type == ALLEGRO_EVENT_TIMER) { if(key[KEY_UP] && bouncer_y >= 4.0) { bouncer_y -= 4.0; } if(key[KEY_DOWN] && bouncer_y <= SCREEN_H - BOUNCER_SIZE - 4.0) { bouncer_y += 4.0; } if(key[KEY_LEFT] && bouncer_x >= 4.0) { bouncer_x -= 4.0; } if(key[KEY_RIGHT] && bouncer_x <= SCREEN_W - BOUNCER_SIZE - 4.0) { bouncer_x += 4.0; } redraw = true; } else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { break; } else if(ev.type == ALLEGRO_EVENT_KEY_DOWN) { switch(ev.keyboard.keycode) { case ALLEGRO_KEY_UP: key[KEY_UP] = true; break; case ALLEGRO_KEY_DOWN: key[KEY_DOWN] = true; break; case ALLEGRO_KEY_LEFT: key[KEY_LEFT] = true; break; case ALLEGRO_KEY_RIGHT: key[KEY_RIGHT] = true; break; } } else if(ev.type == ALLEGRO_EVENT_KEY_UP) { switch(ev.keyboard.keycode) { case ALLEGRO_KEY_UP: key[KEY_UP] = false; break; case ALLEGRO_KEY_DOWN: key[KEY_DOWN] = false; break; case ALLEGRO_KEY_LEFT: key[KEY_LEFT] = false; break; case ALLEGRO_KEY_RIGHT: key[KEY_RIGHT] = false; break; case ALLEGRO_KEY_ESCAPE: doexit = true; break; } } if(redraw && al_is_event_queue_empty(event_queue)) { redraw = false; al_clear_to_color(al_map_rgb(0,0,0)); al_draw_bitmap(bouncer, bouncer_x, bouncer_y, 0); al_flip_display(); } } al_destroy_bitmap(bouncer); al_destroy_timer(timer); al_destroy_display(display); al_destroy_event_queue(event_queue); return 0; }</file> ==== Détails ==== <code c>enum MYKEYS { KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT };</code> Define ourselves some key constants to use with our key state array. <code c>bool key[4] = { false, false, false, false };</code> Define our key state array. This is where we'll store the state of the keys we're interested in. <code c> bool doexit = false;</code> Define a flag to short circuit our while loop. <code c> if(!al_install_keyboard()) { fprintf(stderr, "failed to initialize the keyboard!\n"); return -1; }</code> Initialize the keyboard subsystem. **al_install_keyboard** will return false if it fails, we exit if that happens. <code c> al_register_event_source(event_queue, al_get_keyboard_event_source());</code> Register the keyboard event source with our event queue. Without this we will never get any keyboard events. <code c> while(!doexit)</code> Change up our while loop to exit should **doexit** be true. Gives us a cheap way to exit. <code c> if(key[KEY_UP] && bouncer_y >= 4.0) { bouncer_y -= 4.0; } if(key[KEY_DOWN] && bouncer_y <= SCREEN_H - BOUNCER_SIZE - 4.0) { bouncer_y += 4.0; } if(key[KEY_LEFT] && bouncer_x >= 4.0) { bouncer_x -= 4.0; } if(key[KEY_RIGHT] && bouncer_x <= SCREEN_W - BOUNCER_SIZE - 4.0) { bouncer_x += 4.0; }</code> Here we check to see if one of our keys are down, and move our box in the direction of the key press. To save a bit of space and effort, we're going to go through both key events in parallel. <code c> else if(ev.type == ALLEGRO_EVENT_KEY_DOWN) {</code> The **ALLEGRO_EVENT_KEY_DOWN** event is generated whenever a key is pressed. <code c> else if(ev.type == ALLEGRO_EVENT_KEY_UP) {</code> The **ALLEGRO_EVENT_KEY_UP** event is generated whenever a key is released. <code c> switch(ev.keyboard.keycode) {</code> We switch on the **keycode** member of the keyboard event structure. The **keycode** member contains an integer mapping to an allegro keycode. <code c> case ALLEGRO_KEY_*: key[KEY_*] = true; || key[KEY_*] = false; break;</code> With both the key press and release events, we store the state of the pressed key into our **key[]** array. <code c> case ALLEGRO_KEY_ESC: doexit = true; break;</code> Here we set our **doexit** flag to true, to cause the loop to exit the next time through. Whew. A bit longer than the mouse example, but still pretty simple. [[allegro:bitmaps|Précédent]] << [[allegro:start|Sommaire]] >> [[allegro:threads|Suivant]]

allegro/input.1323097864.txt.gz · Dernière modification: 2011/12/05 16:11 par mrhide