bool Framework::initialize(std::string config_filename) { if (initialized) return initialized; if (!al_init()) std::cerr << "al_init() failed" << std::endl; ALLEGRO_PATH *resource_path = al_get_standard_path(ALLEGRO_RESOURCES_PATH); al_change_directory(al_path_cstr(resource_path, ALLEGRO_NATIVE_PATH_SEP)); al_destroy_path(resource_path); if (!al_install_mouse()) std::cerr << "al_install_mouse() failed" << std::endl; if (!al_install_keyboard()) std::cerr << "al_install_keyboard() failed" << std::endl; if (!al_install_joystick()) std::cerr << "al_install_joystick() failed" << std::endl; if (!al_install_audio()) std::cerr << "al_install_audio() failed" << std::endl; if (!al_init_native_dialog_addon()) std::cerr << "al_init_native_dialog_addon() failed" << std::endl; if (!al_init_primitives_addon()) std::cerr << "al_init_primitives_addon() failed" << std::endl; if (!al_init_image_addon()) std::cerr << "al_init_image_addon() failed" << std::endl; if (!al_init_font_addon()) std::cerr << "al_init_font_addon() failed" << std::endl; if (!al_init_ttf_addon()) std::cerr << "al_init_ttf_addon() failed" << std::endl; if (!al_init_acodec_addon()) std::cerr << "al_init_acodec_addon() failed" << std::endl; if (!al_reserve_samples(32)) std::cerr << "al_reserve_samples() failed" << std::endl; srand(time(NULL)); primary_timer = al_create_timer(ALLEGRO_BPS_TO_SECS(60)); al_set_new_bitmap_flags(ALLEGRO_MIN_LINEAR | ALLEGRO_MAG_LINEAR); // al_set_new_bitmap_flags(ALLEGRO_MIN_LINEAR | ALLEGRO_MAG_LINEAR | ALLEGRO_MIPMAP); builtin_font = al_create_builtin_font(); event_queue = al_create_event_queue(); al_register_event_source(event_queue, al_get_keyboard_event_source()); al_register_event_source(event_queue, al_get_mouse_event_source()); al_register_event_source(event_queue, al_get_joystick_event_source()); al_register_event_source(event_queue, al_get_timer_event_source(primary_timer)); al_register_event_source(event_queue, al_get_joystick_event_source()); al_register_event_source(event_queue, al_get_default_menu_event_source()); if (al_get_num_joysticks()) joystick = al_get_joystick(0); // make this better eventually else std::cerr << "no joystick(s) detected" << std::endl; instance = new Framework(config_filename); Attributes::create_datatype_definition( AllegroColorAttributeDatatype::IDENTIFIER, AllegroColorAttributeDatatype::to_val_func, AllegroColorAttributeDatatype::to_str_func ); initialized = true; return true; }
/* ljoy_get_joystick_state: [primary thread] * * Copy the internal joystick state to a user-provided structure. */ static void ljoy_get_joystick_state(ALLEGRO_JOYSTICK *joy_, ALLEGRO_JOYSTICK_STATE *ret_state) { ALLEGRO_JOYSTICK_LINUX *joy = (ALLEGRO_JOYSTICK_LINUX *) joy_; ALLEGRO_EVENT_SOURCE *es = al_get_joystick_event_source(); _al_event_source_lock(es); { *ret_state = joy->joystate; } _al_event_source_unlock(es); }
/* ljoy_process_new_data: [fdwatch thread] * * Process new data arriving in the joystick's fd. */ static void ljoy_process_new_data(void *data) { ALLEGRO_JOYSTICK_LINUX *joy = data; ALLEGRO_EVENT_SOURCE *es = al_get_joystick_event_source(); if (!es) { // Joystick driver not fully initialized return; } _al_event_source_lock(es); { struct input_event input_events[32]; int bytes, nr, i; while ((bytes = read(joy->fd, &input_events, sizeof input_events)) > 0) { nr = bytes / sizeof(struct input_event); for (i = 0; i < nr; i++) { int type = input_events[i].type; int code = input_events[i].code; int value = input_events[i].value; if (type == EV_KEY) { int number = map_button_number(joy, code); if (number >= 0) { if (value) joy->joystate.button[number] = 32767; else joy->joystate.button[number] = 0; ljoy_generate_button_event(joy, number, (value ? ALLEGRO_EVENT_JOYSTICK_BUTTON_DOWN : ALLEGRO_EVENT_JOYSTICK_BUTTON_UP)); } } else if (type == EV_ABS) { int number = code; if (number < TOTAL_JOYSTICK_AXES) { const AXIS_MAPPING *map = &joy->axis_mapping[number]; int stick = map->stick; int axis = map->axis; float pos = norm_pos(map, value); joy->joystate.stick[stick].axis[axis] = pos; ljoy_generate_axis_event(joy, stick, axis, pos); } } } } } _al_event_source_unlock(es); }
void AllegroEventSource::init() { m_impl = new AllegroEventSourceImpl(); assert(m_impl != nullptr && "Unable to allocate AllegroEventSourceImpl"); m_impl->event_queue = al_create_event_queue(); assert(m_impl->event_queue != nullptr && "Unable to allocate Allegro Event Queue"); ALLEGRO_EVENT_QUEUE *q = m_impl->event_queue; al_register_event_source(q, al_get_keyboard_event_source()); al_register_event_source(q, al_get_mouse_event_source()); al_register_event_source(q, al_get_joystick_event_source()); }
void init_event_things(ALLEGRO_TIMER* &timer, ALLEGRO_EVENT_QUEUE* &queue) { al_set_new_window_position(window_x, window_y); display = al_create_display(scr_w, scr_h); timer = al_create_timer(1.0 / game_fps); queue = al_create_event_queue(); al_register_event_source(queue, al_get_mouse_event_source()); al_register_event_source(queue, al_get_keyboard_event_source()); al_register_event_source(queue, al_get_joystick_event_source()); al_register_event_source(queue, al_get_display_event_source(display)); al_register_event_source(queue, al_get_timer_event_source(timer)); }
// Sets up game void setup(){ // Init allegro 5 al_init(); // Input al_install_keyboard(); al_install_mouse(); al_install_joystick(); // Fonts al_init_font_addon(); al_init_ttf_addon(); // Graphics al_init_image_addon(); al_init_primitives_addon(); // Audio al_install_audio(); al_init_acodec_addon(); al_reserve_samples( 20); // Initializing timer = al_create_timer(1.0 / MAX_FPS); display = al_create_display( SCREEN_W, SCREEN_H); // Events event_queue = al_create_event_queue(); 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_register_event_source( event_queue, al_get_joystick_event_source()); al_clear_to_color( al_map_rgb(0,0,0)); al_flip_display(); al_start_timer(timer); // Creates a random number generator (based on time) srand( time(nullptr)); // Game state stateID = STATE_NULL; nextState = STATE_NULL; // Clear settings for( int i = 0; i < 11; i++) settings[i] = false; // Clear frams array for( int i = 0; i < 100; i++) frames_array[i] = 0; }
/* ljoy_generate_button_event: [fdwatch thread] * * Helper to generate an event after a button is pressed or released. * The joystick must be locked BEFORE entering this function. */ static void ljoy_generate_button_event(ALLEGRO_JOYSTICK_LINUX *joy, int button, ALLEGRO_EVENT_TYPE event_type) { ALLEGRO_EVENT event; ALLEGRO_EVENT_SOURCE *es = al_get_joystick_event_source(); if (!_al_event_source_needs_to_generate_event(es)) return; event.joystick.type = event_type; event.joystick.timestamp = al_get_time(); event.joystick.id = (ALLEGRO_JOYSTICK *)joy; event.joystick.stick = 0; event.joystick.axis = 0; event.joystick.pos = 0.0; event.joystick.button = button; _al_event_source_emit_event(es, &event); }
/* initialises the input emulation */ void init_input() { ALLEGRO_JOYSTICK *joy; keybuf_len = 0; keybuf_mutex = al_create_mutex(); input_queue = al_create_event_queue(); al_register_event_source(input_queue, al_get_keyboard_event_source()); al_register_event_source(input_queue, al_get_display_event_source( al_get_current_display())); if (al_get_num_joysticks() > 0) { joy = al_get_joystick(0); if (joy) al_register_event_source(input_queue, al_get_joystick_event_source(joy)); } }
/* ljoy_generate_axis_event: [fdwatch thread] * * Helper to generate an event after an axis is moved. * The joystick must be locked BEFORE entering this function. */ static void ljoy_generate_axis_event(ALLEGRO_JOYSTICK_LINUX *joy, int stick, int axis, float pos) { ALLEGRO_EVENT event; ALLEGRO_EVENT_SOURCE *es = al_get_joystick_event_source(); if (!_al_event_source_needs_to_generate_event(es)) return; event.joystick.type = ALLEGRO_EVENT_JOYSTICK_AXIS; event.joystick.timestamp = al_get_time(); event.joystick.id = (ALLEGRO_JOYSTICK *)joy; event.joystick.stick = stick; event.joystick.axis = axis; event.joystick.pos = pos; event.joystick.button = 0; _al_event_source_emit_event(es, &event); }
int main(void) { ALLEGRO_DISPLAY *display; if (!al_init()) { abort_example("Could not init Allegro.\n"); return 1; } al_init_primitives_addon(); display = al_create_display(640, 480); if (!display) { abort_example("al_create_display failed\n"); return 1; } al_install_keyboard(); black = al_map_rgb(0, 0, 0); grey = al_map_rgb(0xe0, 0xe0, 0xe0); white = al_map_rgb(255, 255, 255); al_install_joystick(); event_queue = al_create_event_queue(); if (!event_queue) { abort_example("al_create_event_queue failed\n"); return 1; } if (al_get_keyboard_event_source()) { al_register_event_source(event_queue, al_get_keyboard_event_source()); } al_register_event_source(event_queue, al_get_display_event_source(display)); al_register_event_source(event_queue, al_get_joystick_event_source()); setup_joystick_values(al_get_joystick(0)); main_loop(); return 0; }
bool AllegroInput5::Init( bool keyboard, bool mouse, bool gamepad, bool touch ) { // There is a slight confusion about these flags. For now, they do not GUARANTEE that a given input method is available! // They only indicate that al_intall_*() call was successfull, with the exception of m_hadGamepad, that also checks that // at least one joystick is available. This is not quite right, but it's mostly relevant for mobile devies, so I'll fix // this later :) m_hasKeyboard = keyboard; m_hasMouse = mouse; m_hasGamepad = gamepad; m_hasTouch = touch; m_queue = al_create_event_queue(); if ( keyboard ) { if ( !al_install_keyboard() ) { GetLog().Log( CommonLog(), LL_CRITICAL, "AllegroInput: Failed to install keyboard driver" ); m_hasKeyboard = false; } else { GetLog().Log( CommonLog(), LL_INFO, "AllegroInput: Installed keyboard driver" ); al_register_event_source( m_queue, al_get_keyboard_event_source() ); } } if ( mouse ) { if ( !al_install_mouse() ) { GetLog().Log( CommonLog(), LL_CRITICAL, "AllegroInput: Failed to install mouse driver" ); m_hasMouse = false; } else { GetLog().Log( CommonLog(), LL_INFO, "AllegroInput: Installed mouse driver" ); al_register_event_source( m_queue, al_get_mouse_event_source() ); } } if ( gamepad ) { if ( !al_install_joystick() ) { GetLog().Log( CommonLog(), LL_CRITICAL, "AllegroInput: Failed to install gamepad driver" ); m_hasGamepad = false; } else { // Note: only the first joystick is supported. This should be fixed. Sometime. ALLEGRO_JOYSTICK *pJoystick = al_get_joystick( 0 ); if ( !pJoystick ) { GetLog().Log( CommonLog(), LL_CRITICAL, "AllegroInput: Joystick #0 not found" ); m_hasGamepad = false; } else { GetLog().Log( CommonLog(), LL_INFO, "AllegroInput: Installed gamepad driver" ); al_register_event_source( m_queue, al_get_joystick_event_source() ); } } } if ( touch ) { if ( !al_install_touch_input() ) { GetLog().Log( CommonLog(), LL_CRITICAL, "AllegroInput: Failed to install touch input driver" ); m_hasTouch = false; } else { GetLog().Log( CommonLog(), LL_INFO, "AllegroInput: Installed touch input driver" ); al_register_event_source( m_queue, al_get_touch_input_event_source() ); } } return true; }
int main(int argc, char **argv){ if(argc==3){ if(strcmp(argv[1],"rand")==0 && strcmp(argv[1],"map/maps.txt")!=0){ map_aleatoire(argv[2], 1000,100); printf("map cree\n"); return 0; } } /* Creation de l ecran */ ALLEGRO_DISPLAY *display = NULL; /* Creation du timer */ ALLEGRO_TIMER *timer; /* Creation de la liste d'evenement */ ALLEGRO_EVENT_QUEUE *queue; bool redraw = true; /*bool boucle_jeu=true;*/ /* Initialisation d allegro */ if(!al_init()) { fprintf(stderr, "failed to initialize allegro!\n"); return -1; } if(!al_init_image_addon()) { fprintf(stderr, "failed to initialize allegro image addon!\n"); return -1; } if(!al_init_primitives_addon()) { fprintf(stderr, "failed to initialize allegro primitive addon!\n"); return -1; } if(!al_install_mouse()) { fprintf(stderr, "failed to initialize allegro mouse !\n"); return -1; } if(!al_install_keyboard()) { fprintf(stderr, "failed to initialize allegro keyboard!\n"); return -1; } if(!al_install_joystick()){ fprintf(stderr, "failed to initialize allegro joystick!\n"); return -1; } if(!al_install_audio()) { fprintf(stderr, "failed to initialize allegro audio!\n"); /*return -1;*/ } if(!al_init_acodec_addon()){ fprintf(stderr, "failed to initialize allegro audio codec!\n"); /*return -1;*/ } if (!al_reserve_samples(2)){ fprintf(stderr, "failed to initialize allegro reserve sample 1!\n"); /*return -1;*/ } al_init_font_addon(); if(!al_init_ttf_addon()){ fprintf(stderr, "failed to initialize allegro ttf addon!\n"); return -1; } Game* game; /* Instantiation de l ecran */ display = al_create_display(DISPLAY_W,DISPLAY_H); if(!display) { fprintf(stderr, "failed to create display!\n"); return -1; } bool recommencer = true; while(recommencer){ recommencer = false; game = game_init(); al_hide_mouse_cursor(display); /* Instantiation de la liste d evenement */ queue = al_create_event_queue(); /* Instantiation du timer de jeu */ timer = al_create_timer(1.0/FPS); al_start_timer(timer); /* Enregistrement des evenement */ al_register_event_source(queue, al_get_keyboard_event_source()); al_register_event_source(queue, al_get_mouse_event_source()); al_register_event_source(queue, al_get_joystick_event_source()); al_register_event_source(queue, al_get_display_event_source(display)); al_register_event_source(queue, al_get_timer_event_source(timer)); /* ECRAN TITRE */ al_clear_to_color(al_map_rgb(0,0,0)); al_draw_bitmap_region(game->jeu->map->background,250,200,game->jeu->map->w*LENGTH_SPRITE,game->jeu->map->h*LENGTH_SPRITE,0,0,0); al_draw_text(game->jeu->font_jeu,al_map_rgb(0,0,0),280,150,ALLEGRO_ALIGN_LEFT,"Titre du jeu"); al_draw_text(game->jeu->font_jeu,al_map_rgb(0,0,0),280,190,ALLEGRO_ALIGN_LEFT,"Press enter"); if(al_is_audio_installed()){ ALLEGRO_SAMPLE* main_musique; main_musique = al_load_sample("musique/musique_titre.ogg"); if (!main_musique){ fprintf(stderr, "Erreur lors du chargement musique d'intro\n"); } al_play_sample(main_musique, 0.1, 0.0,1.0,ALLEGRO_PLAYMODE_LOOP,NULL); } al_flip_display(); bool debut = false; Event* event_titre; ALLEGRO_EVENT event_titre_event; event_titre = event_init(EVENT_PERIPH_XBOX); while(debut != true){ al_wait_for_event(queue, &event_titre_event); if(event_get_ok_court(event_titre)){ debut = true; } event_update(event_titre,&event_titre_event); } if(al_is_audio_installed()){ al_stop_samples(); } /* Ecran de chargement du premier niveau */ char num_niveau[4]; sprintf(num_niveau,"%d",game->current_map_i); al_clear_to_color(al_map_rgb(0,0,0)); if(game->size == game->current_map_i){ al_draw_text(game->jeu->font_jeu,al_map_rgb(255,255,255),280,190,ALLEGRO_ALIGN_LEFT,"Loading final stage"); }else{ al_draw_text(game->jeu->font_jeu,al_map_rgb(255,255,255),300,190,ALLEGRO_ALIGN_LEFT,"Loading level "); al_draw_text(game->jeu->font_jeu,al_map_rgb(255,255,255),440,190,ALLEGRO_ALIGN_LEFT,num_niveau); } al_flip_display(); sleep(2); al_clear_to_color(al_map_rgb(0,0,0)); al_flip_display(); /* Début de la boucle de jeu */ game->jeu->ev->son = true; map_set_frame(game->jeu->map,DISPLAY_W/2-200,100,200,DISPLAY_H-300); while(game->jeu->boucle_jeu){ game_next_map_test(game); ALLEGRO_EVENT event; al_wait_for_event(queue, &event); /* On demande a quitter le jeu */ if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE ){ game->jeu->boucle_jeu=false; } event_update(game->jeu->ev,&event); /* Evenement timer */ if(event.type == ALLEGRO_EVENT_TIMER){ redraw=true; } /* if(event.type == ALLEGRO_EVENT_DISPLAY_RESIZE){ al_acknowledge_resize(display); redraw = true; } */ /* affichage */ if(redraw && al_is_event_queue_empty(queue)){ redraw=false; game->jeu->boucle_jeu = jeu_update_event(game->jeu); jeu_draw(game->jeu); } } /* Ecran de fin du jeu */ if(game->success == true){ al_clear_to_color(al_map_rgb(0,0,0)); al_draw_text(game->jeu->font_jeu,al_map_rgb(255,255,255),400,100,ALLEGRO_ALIGN_CENTER,"Félicitation, vous avez terminés les premiers niveaux du jeu !"); al_draw_text(game->jeu->font_jeu,al_map_rgb(255,255,255),400,120,ALLEGRO_ALIGN_CENTER,"Ce jeu à été créé à l'occasion du premier projet de C de"); al_draw_text(game->jeu->font_jeu,al_map_rgb(255,255,255),400,140,ALLEGRO_ALIGN_CENTER,"la promotion 2016 de Télécom Nancy"); al_draw_text(game->jeu->font_jeu,al_map_rgb(255,255,255),400,160,ALLEGRO_ALIGN_CENTER,"par"); al_draw_text(game->jeu->font_jeu,al_map_rgb(255,255,255),400,180,ALLEGRO_ALIGN_CENTER,"Giannini Valentin et Eric Perlinski"); al_flip_display(); sleep(5); al_clear_to_color(al_map_rgb(0,0,0)); al_draw_text(game->jeu->font_jeu,al_map_rgb(255,255,255),400,150,ALLEGRO_ALIGN_CENTER,"Appuyez sur [ESC] pour quitter, ou sur [Entrer] pour recommencer"); al_flip_display(); }else{ al_clear_to_color(al_map_rgb(0,0,0)); al_draw_text(game->jeu->font_jeu,al_map_rgb(255,255,255),400,100,ALLEGRO_ALIGN_CENTER,"Vous avez épuisés toutes les vies de votre personnage !"); al_draw_text(game->jeu->font_jeu,al_map_rgb(255,255,255),400,120,ALLEGRO_ALIGN_CENTER,"Ce jeu à été créé à l'occasion du premier projet de C de"); al_draw_text(game->jeu->font_jeu,al_map_rgb(255,255,255),400,140,ALLEGRO_ALIGN_CENTER,"la promotion 2016 de Télécom Nancy"); al_draw_text(game->jeu->font_jeu,al_map_rgb(255,255,255),400,160,ALLEGRO_ALIGN_CENTER,"par"); al_draw_text(game->jeu->font_jeu,al_map_rgb(255,255,255),400,180,ALLEGRO_ALIGN_CENTER,"Gianini Valentin et Eric Perlinski"); al_flip_display(); sleep(5); al_clear_to_color(al_map_rgb(0,0,0)); al_draw_text(game->jeu->font_jeu,al_map_rgb(255,255,255),400,150,ALLEGRO_ALIGN_CENTER,"Appuyez sur [ESC] pour quitter, ou sur [Entrer] pour recommencer"); al_flip_display(); } /* TODO Chargement écran de fin */ bool action = false; Event* event_credits; ALLEGRO_EVENT event_credits_event; event_credits = event_init(EVENT_PERIPH_XBOX); while(action != true){ al_wait_for_event(queue, &event_credits_event); if(event_get_ok_court(event_credits)){ action = true; recommencer = true; }else if (event_get_menu(event_credits)){ action = true; recommencer = false; } event_update(event_credits,&event_credits_event); } } jeu_dest(game->jeu); al_destroy_display(display); return 0; }
/* dialog_message: * Sends a message to all the objects in a dialog. If any of the objects * return values other than D_O_K, returns the value and sets obj to the * object which produced it. */ int t3gui_dialog_message(T3GUI_ELEMENT *dialog, int msg, int c, int *obj) { int count, res, r, force, try; assert(dialog); force = ((msg == MSG_START) || (msg == MSG_END) || (msg >= MSG_USER)); res = D_O_K; /* If a menu spawned by a d_menu_proc object is active, the dialog engine * has effectively been shutdown for the sake of safety. This means that * we can't send the message to the other objects in the dialog. So try * first to send the message to the d_menu_proc object and, if the menu * is then not active anymore, send it to the other objects as well. */ //if (active_menu_player) { // try = 2; // menu_dialog = active_menu_player->dialog; //} //else try = 1; for (; try > 0; try--) { for (count=0; dialog[count].proc; count++) { //if ((try == 2) && (&dialog[count] != menu_dialog)) // continue; if ((force) || (!(dialog[count].flags & D_HIDDEN))) { r = t3gui_object_message(dialog+count, msg, c); if (r != D_O_K) { res |= r; if (msg != MSG_DRAW && msg != MSG_IDLE && msg != MSG_TIMER && obj) { printf("new object: %d\n", count); *obj = count; } } if ((msg == MSG_IDLE) && (dialog[count].flags & (D_DIRTY | D_HIDDEN)) == D_DIRTY) { dialog[count].flags &= ~D_DIRTY; res |= D_REDRAW; } } } //if (active_menu_player) // break; } return res; } int t3gui_do_dialog(T3GUI_ELEMENT *dialog, int focus) { return t3gui_do_dialog_interval(dialog, 0.0, focus); } int t3gui_do_dialog_interval(T3GUI_ELEMENT *dialog, double speed_sec, int focus) { T3GUI_PLAYER *player = t3gui_init_dialog(dialog, focus, 0, NULL, NULL, NULL); ALLEGRO_TIMER *timer = NULL; t3gui_listen_for_events(player, al_get_keyboard_event_source()); t3gui_listen_for_events(player, al_get_mouse_event_source()); t3gui_listen_for_events(player, al_get_joystick_event_source()); t3gui_listen_for_events(player, al_get_display_event_source(al_get_current_display())); if (speed_sec > 0.0) { timer = al_create_timer(speed_sec); t3gui_listen_for_events(player, al_get_timer_event_source(timer)); al_start_timer(timer); } ALLEGRO_EVENT_QUEUE *menu_queue = al_create_event_queue(); al_register_event_source(menu_queue, t3gui_get_player_event_source(player)); t3gui_start_dialog(player); bool in_dialog = true; bool must_draw = false; while (in_dialog) { if (must_draw) { must_draw = false; t3gui_draw_dialog(player); al_flip_display(); } al_wait_for_event(menu_queue, NULL); while (!al_is_event_queue_empty(menu_queue)) { ALLEGRO_EVENT event; al_get_next_event(menu_queue, &event); switch (event.type) { case T3GUI_EVENT_REDRAW: //printf("Redraw request\n"); must_draw = true; break; case T3GUI_EVENT_CLOSE: focus = event.user.data1; in_dialog = false; break; case T3GUI_EVENT_CANCEL: focus = -1; in_dialog = false; break; } } } t3gui_stop_dialog(player); al_destroy_timer(timer); al_destroy_event_queue(menu_queue); t3gui_shutdown_dialog(player); return focus; }
/* update_joystick: [joystick thread] * Reads in data for a single DirectInput joystick device, updates * the internal ALLEGRO_JOYSTICK_STATE structure, and generates any Allegro * events required. */ static void update_joystick(ALLEGRO_JOYSTICK_DIRECTX *joy) { DIDEVICEOBJECTDATA buffer[DEVICE_BUFFER_SIZE]; DWORD num_items = DEVICE_BUFFER_SIZE; HRESULT hr; ALLEGRO_EVENT_SOURCE *es = al_get_joystick_event_source(); /* some devices require polling */ IDirectInputDevice8_Poll(joy->device); /* get device data into buffer */ hr = IDirectInputDevice8_GetDeviceData(joy->device, sizeof(DIDEVICEOBJECTDATA), buffer, &num_items, 0); if (hr != DI_OK && hr != DI_BUFFEROVERFLOW) { if ((hr == DIERR_NOTACQUIRED) || (hr == DIERR_INPUTLOST)) { ALLEGRO_WARN("joystick device not acquired or lost\n"); } else { ALLEGRO_ERROR("unexpected error while polling the joystick\n"); } return; } /* don't bother locking the event source if there's no work to do */ /* this happens a lot for polled devices */ if (num_items == 0) return; _al_event_source_lock(es); { unsigned int i; for (i = 0; i < num_items; i++) { const DIDEVICEOBJECTDATA *item = &buffer[i]; const int dwOfs = item->dwOfs; const DWORD dwData = item->dwData; switch (dwOfs) { case DIJOFS_X: handle_axis_event(joy, &joy->x_mapping, dwData); break; case DIJOFS_Y: handle_axis_event(joy, &joy->y_mapping, dwData); break; case DIJOFS_Z: handle_axis_event(joy, &joy->z_mapping, dwData); break; case DIJOFS_RX: handle_axis_event(joy, &joy->rx_mapping, dwData); break; case DIJOFS_RY: handle_axis_event(joy, &joy->ry_mapping, dwData); break; case DIJOFS_RZ: handle_axis_event(joy, &joy->rz_mapping, dwData); break; case DIJOFS_SLIDER(0): handle_axis_event(joy, &joy->slider_mapping[0], dwData); break; case DIJOFS_SLIDER(1): handle_axis_event(joy, &joy->slider_mapping[1], dwData); break; case DIJOFS_POV(0): handle_pov_event(joy, joy->pov_mapping_stick[0], dwData); break; case DIJOFS_POV(1): handle_pov_event(joy, joy->pov_mapping_stick[1], dwData); break; case DIJOFS_POV(2): handle_pov_event(joy, joy->pov_mapping_stick[2], dwData); break; case DIJOFS_POV(3): handle_pov_event(joy, joy->pov_mapping_stick[3], dwData); break; default: /* buttons */ if ((dwOfs >= DIJOFS_BUTTON0) && (dwOfs < DIJOFS_BUTTON(joy->parent.info.num_buttons))) { int num = (dwOfs - DIJOFS_BUTTON0) / (DIJOFS_BUTTON1 - DIJOFS_BUTTON0); handle_button_event(joy, num, (dwData & 0x80)); } break; } } } _al_event_source_unlock(es); }
ALLEGRO_EVENT_SOURCE *xc_get_event_source() { return al_get_joystick_event_source(); }
int main(int argc, char **argv) { int num_joysticks; ALLEGRO_EVENT_QUEUE *queue; ALLEGRO_JOYSTICK *curr_joy; ALLEGRO_DISPLAY *display; (void)argc; (void)argv; if (!al_init()) { abort_example("Could not init Allegro.\n"); } if (!al_install_joystick()) { abort_example("Could not init joysticks.\n"); } al_install_keyboard(); al_init_primitives_addon(); open_log(); display = al_create_display(640, 480); if (!display) { abort_example("Could not create display.\n"); } queue = al_create_event_queue(); al_register_event_source(queue, al_get_keyboard_event_source()); al_register_event_source(queue, al_get_joystick_event_source()); al_register_event_source(queue, al_get_display_event_source(display)); num_joysticks = al_get_num_joysticks(); log_printf("Num joysticks: %d\n", num_joysticks); if (num_joysticks > 0) { curr_joy = al_get_joystick(0); print_joystick_info(curr_joy); } else { curr_joy = NULL; } draw(curr_joy); while (1) { ALLEGRO_EVENT event; al_wait_for_event(queue, &event); if (event.type == ALLEGRO_EVENT_KEY_DOWN && event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) { break; } else if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { break; } else if (event.type == ALLEGRO_EVENT_KEY_CHAR) { int n = event.keyboard.unichar - '0'; if (n >= 0 && n < num_joysticks) { curr_joy = al_get_joystick(n); log_printf("switching to joystick %d\n", n); print_joystick_info(curr_joy); } } else if (event.type == ALLEGRO_EVENT_JOYSTICK_CONFIGURATION) { al_reconfigure_joysticks(); num_joysticks = al_get_num_joysticks(); log_printf("after reconfiguration num joysticks = %d\n", num_joysticks); if (curr_joy) { log_printf("current joystick is: %s\n", al_get_joystick_active(curr_joy) ? "active" : "inactive"); } curr_joy = al_get_joystick(0); } else if (event.type == ALLEGRO_EVENT_JOYSTICK_AXIS) { log_printf("axis event from %p, stick %d, axis %d\n", event.joystick.id, event.joystick.stick, event.joystick.axis); } else if (event.type == ALLEGRO_EVENT_JOYSTICK_BUTTON_DOWN) { log_printf("button down event %d from %p\n", event.joystick.button, event.joystick.id); } else if (event.type == ALLEGRO_EVENT_JOYSTICK_BUTTON_UP) { log_printf("button up event %d from %p\n", event.joystick.button, event.joystick.id); } draw(curr_joy); } close_log(false); return 0; }
Framework::Framework( int Width, int Height, int Framerate, bool DropFrames ) { System = this; #ifdef WRITE_LOG LogFile = fopen( "sjkt.txt", "a" ); fprintf( LogFile, "Framework: Startup: Allegro\n" ); #endif if( !al_init() ) { fprintf( LogFile, "Framework: Error: Cannot init Allegro\n" ); quitProgram = true; return; } al_init_font_addon(); if( !al_install_keyboard() || !al_install_mouse() || !al_install_joystick() || !al_init_primitives_addon() || !al_init_ttf_addon() || !al_init_image_addon() ) { fprintf( LogFile, "Framework: Error: Cannot init Allegro plugin\n" ); quitProgram = true; return; } #ifdef NETWORK_SUPPORT #ifdef WRITE_LOG fprintf( LogFile, "Framework: Startup: Network\n" ); #endif if( enet_initialize() != 0 ) { fprintf( LogFile, "Framework: Error: Cannot init enet\n" ); quitProgram = true; return; } #endif #ifdef WRITE_LOG fprintf( LogFile, "Framework: Startup: Default Variables\n" ); #endif quitProgram = false; ProgramStages = new StageStack(); framesToProcess = 0; framesPerSecond = Framerate; enableSlowDown = DropFrames; #ifdef WRITE_LOG fprintf( LogFile, "Framework: Startup: Settings file\n" ); #endif Settings = new ConfigFile( "settings.cfg" ); #ifdef DOWNLOAD_SUPPORT #ifdef WRITE_LOG fprintf( LogFile, "Framework: Startup: Download Manager\n" ); #endif DOWNLOADS = new DownloadManager( Settings->GetQuickIntegerValue( "Downloads.Concurrent", 3 ) ); #endif #ifdef WRITE_LOG fprintf( LogFile, "Framework: Startup: Allegro Events\n" ); #endif eventAllegro = al_create_event_queue(); eventMutex = al_create_mutex_recursive(); frameTimer = al_create_timer( 1.0 / (double)framesPerSecond ); srand( (unsigned int)al_get_time() ); #ifdef WRITE_LOG fprintf( LogFile, "Framework: Startup: Display\n" ); #endif DISPLAY = new Display( Width, Height ); DISPLAY->Initialise( Settings->GetQuickIntegerValue( "Video.Width", Width ), Settings->GetQuickIntegerValue( "Video.Height", Height ), Settings->GetQuickBooleanValue( "Video.Fullscreen", false ), (DisplayScaleMode::ScaleMode)Settings->GetQuickIntegerValue( "Video.ScaleMode", DisplayScaleMode::Letterbox ) ); AUDIO = new Audio(); #ifdef WRITE_LOG fprintf( LogFile, "Framework: Startup: Register event sources\n" ); #endif RegisterEventSource( DISPLAY->GetEventSource() ); RegisterEventSource( al_get_keyboard_event_source() ); RegisterEventSource( al_get_mouse_event_source() ); RegisterEventSource( al_get_joystick_event_source() ); RegisterEventSource( al_get_timer_event_source( frameTimer ) ); #ifdef WRITE_LOG fprintf( LogFile, "Framework: Startup: Joystick IDs\n" ); #endif GetJoystickIDs(); }