Exemplo n.º 1
0
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;
}
Exemplo n.º 2
0
static void main_loop(void)
{
    ALLEGRO_EVENT event;

    while (true) {
        if (al_is_event_queue_empty(event_queue))
            draw_all();

        al_wait_for_event(event_queue, &event);

        switch (event.type) {
        /* ALLEGRO_EVENT_JOYSTICK_AXIS - a joystick axis value changed.
         */
        case ALLEGRO_EVENT_JOYSTICK_AXIS:
            if (event.joystick.stick < MAX_STICKS && event.joystick.axis < MAX_AXES) {
                joys[event.joystick.stick][event.joystick.axis] = event.joystick.pos;
            }
            break;

        /* ALLEGRO_EVENT_JOYSTICK_BUTTON_DOWN - a joystick button was pressed.
         */
        case ALLEGRO_EVENT_JOYSTICK_BUTTON_DOWN:
            joys_buttons[event.joystick.button] = true;
            break;

        /* ALLEGRO_EVENT_JOYSTICK_BUTTON_UP - a joystick button was released.
         */
        case ALLEGRO_EVENT_JOYSTICK_BUTTON_UP:
            joys_buttons[event.joystick.button] = false;
            break;

        case ALLEGRO_EVENT_KEY_DOWN:
            if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE)
                return;
            break;

        /* ALLEGRO_EVENT_DISPLAY_CLOSE - the window close button was pressed.
         */
        case ALLEGRO_EVENT_DISPLAY_CLOSE:
            return;

        case ALLEGRO_EVENT_JOYSTICK_CONFIGURATION:
            al_reconfigure_joysticks();
            setup_joystick_values(al_get_joystick(0));
            break;

        /* We received an event of some type we don't know about.
         * Just ignore it.
         */
        default:
            break;
        }
    }
}
Exemplo n.º 3
0
void Framework::GetJoystickIDs()
{
#ifdef WRITE_LOG
	fprintf( LogFile, "Framework: Joysticks: Learn Joystick IDs\n" );
#endif
	// Record joystick IDs in a list for ID conversion
	joystickIDs.clear();
	for( int i = 0; i < al_get_num_joysticks(); i++ )
	{
		joystickIDs.push_back( al_get_joystick( i ) );
	}
}
Exemplo n.º 4
0
/* 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));
   }
}
Exemplo n.º 5
0
/*
 * Gets the state for a controller.
 */
XC_STATE *xc_get_state(int num)
{
	if (num < 0 || num > 7)
		return NULL;

	if (controllers[num] != NULL)
		return controllers[num];

	ALLEGRO_JOYSTICK *joy = al_get_joystick(num);
	if (joy == NULL)
		return NULL;

	XC_STATE *news = (XC_STATE*)malloc(sizeof(XC_STATE));
	xc_clear_state(news);
	news->joy = joy;
	controllers[num] = news;

	return news;
}
Exemplo n.º 6
0
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;
}
Exemplo n.º 7
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)
{
   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;
}
Exemplo n.º 9
0
void Framework::run_loop()
{
   al_start_timer(primary_timer);

   while(!shutdown_program || Display::displays.empty())
   {
      ALLEGRO_EVENT this_event, next_event;

      al_wait_for_event(event_queue, &this_event);

      current_event = &this_event;
      time_now = this_event.any.timestamp;
      get_instance()->motions.update(time_now);

      Screen::on_events(current_event);

      switch(this_event.type)
      {
      case ALLEGRO_EVENT_TIMER:
         if (this_event.timer.source == primary_timer)
            Screen::primary_timer_funcs();
         else
            Screen::timer_funcs();
         while (al_peek_next_event(event_queue, &next_event)
               && next_event.type == ALLEGRO_EVENT_TIMER
               && next_event.timer.source == this_event.timer.source)
            al_drop_next_event(event_queue);
         break;
      case ALLEGRO_EVENT_KEY_DOWN:
         if (Framework::current_event->keyboard.keycode == ALLEGRO_KEY_LSHIFT
               || Framework::current_event->keyboard.keycode == ALLEGRO_KEY_RSHIFT) Framework::key_shift++;
         if (Framework::current_event->keyboard.keycode == ALLEGRO_KEY_ALT
               || Framework::current_event->keyboard.keycode == ALLEGRO_KEY_ALTGR) Framework::key_alt++;
         if (Framework::current_event->keyboard.keycode == ALLEGRO_KEY_RCTRL
               || Framework::current_event->keyboard.keycode == ALLEGRO_KEY_LCTRL) Framework::key_ctrl++;
         if (current_event->keyboard.keycode == ALLEGRO_KEY_F1)
            drawing_profiler_graph = !drawing_profiler_graph; // toggle the profiler graph with F1
         Screen::key_down_funcs();
         break;
      case ALLEGRO_EVENT_KEY_UP:
         if (Framework::current_event->keyboard.keycode == ALLEGRO_KEY_LSHIFT
               || Framework::current_event->keyboard.keycode == ALLEGRO_KEY_RSHIFT) Framework::key_shift--;
         if (Framework::current_event->keyboard.keycode == ALLEGRO_KEY_ALT
               || Framework::current_event->keyboard.keycode == ALLEGRO_KEY_ALTGR) Framework::key_alt--;
         if (Framework::current_event->keyboard.keycode == ALLEGRO_KEY_RCTRL
               || Framework::current_event->keyboard.keycode == ALLEGRO_KEY_LCTRL) Framework::key_ctrl--;
         Screen::key_up_funcs();
         break;
      case ALLEGRO_EVENT_KEY_CHAR:
         Screen::key_char_funcs();
         break;
      case ALLEGRO_EVENT_MOUSE_BUTTON_UP:
         Screen::mouse_up_funcs();
         break;
      case ALLEGRO_EVENT_MOUSE_BUTTON_DOWN:
         Screen::mouse_down_funcs();
         break;
      case ALLEGRO_EVENT_MOUSE_WARPED:
         Screen::mouse_warp_funcs();
         break;
      case ALLEGRO_EVENT_MOUSE_AXES:
         Screen::mouse_axes_funcs();
         break;
      case ALLEGRO_EVENT_JOYSTICK_BUTTON_DOWN:
         Screen::joy_button_down_funcs();
         break;
      case ALLEGRO_EVENT_JOYSTICK_BUTTON_UP:
         Screen::joy_button_up_funcs();
         break;
      case ALLEGRO_EVENT_JOYSTICK_AXIS:
         Screen::joy_axis_funcs();
         break;
      case ALLEGRO_EVENT_MOUSE_ENTER_DISPLAY:
      case ALLEGRO_EVENT_MOUSE_LEAVE_DISPLAY:
         // currently ignored
         break;
      case ALLEGRO_EVENT_DISPLAY_SWITCH_OUT:
         Screen::display_switch_out_funcs();
         break;
      case ALLEGRO_EVENT_DISPLAY_SWITCH_IN:
         Screen::display_switch_in_funcs();
         break;
      case ALLEGRO_EVENT_NATIVE_DIALOG_CLOSE:
         //Screen::display_switch_in_funcs();
         if (textlog) close_log_window();
         break;
      case ALLEGRO_EVENT_JOYSTICK_CONFIGURATION:
         std::cout << "a joystick was added/removed" << std::endl;
         al_reconfigure_joysticks();
         // note: a bug in allegro causes a crash when al_get_joystick(0) if there
         // are 0 joysticks.  So this extra check has been added to prevent
         // the crash from occuring, though it should be corrected in future
         // versions when this bug in allegro is fixed.
         joystick = (al_get_num_joysticks() == 0) ? NULL : al_get_joystick(0);
         Screen::joy_config_funcs();
         break;
      case ALLEGRO_EVENT_MENU_CLICK:
         Screen::native_menu_click_funcs();
         break;
      case ALLEGRO_EVENT_DISPLAY_CLOSE:
         {
            Display *this_display = Display::find_display(this_event.display.source);
            if (this_display) this_display->display_close_func();
         }
         break;
      default:
         if (ALLEGRO_EVENT_TYPE_IS_USER(this_event.type)) Screen::user_event_funcs();
         else std::cout << "uncaught event [" << this_event.type << "]" << std::endl;
         break;
      }
   }
}