Beispiel #1
0
// Universal update
void update(){
  // Event checking
  ALLEGRO_EVENT ev;
  al_wait_for_event( event_queue, &ev);

  // Timer
  if( ev.type == ALLEGRO_EVENT_TIMER){
    // Change state (if needed)
    change_state();

    // Update listeners
    k_listener.update();
    m_listener.update();
    j_listener.update();

    // Update state
    currentState -> update();

    // Debug console toggle
    if( keyListener::keyPressed[ALLEGRO_KEY_F12])
      settings[SETTING_DEBUG] = (settings[SETTING_DEBUG] + 1) % 2;
  }
  // Exit
  else if( ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE){
    closing = true;
  }
  // Keyboard
  else if( ev.type == ALLEGRO_EVENT_KEY_DOWN || ev.type == ALLEGRO_EVENT_KEY_UP){
    k_listener.on_event( ev.type, ev.keyboard.keycode);
  }
  // Joystick
  else if( ev.type == ALLEGRO_EVENT_JOYSTICK_BUTTON_DOWN || ev.type == ALLEGRO_EVENT_JOYSTICK_BUTTON_UP){
    j_listener.on_event( ev.type, ev.joystick.button);
  }
  // Joystick plugged or unplugged
  else if( ev.type == ALLEGRO_EVENT_JOYSTICK_CONFIGURATION){
    al_reconfigure_joysticks();
    joystick_enabled = (al_get_num_joysticks() > 0);
  }

  // Queue empty? Lets draw
  if( al_is_event_queue_empty(event_queue)){
    al_clear_to_color( al_map_rgb(0,0,0));
    currentState -> draw();
    al_flip_display();

    // Update fps buffer
    for( int i = 99; i > 0; i--)
      frames_array[i] = frames_array[i - 1];
    frames_array[0] = (1.0/(al_get_time() - old_time));
    old_time = al_get_time();

    int fps_total = 0;
    for( int i = 0; i < 100; i++)
      fps_total += frames_array[i];

    // FPS = average
    fps = fps_total/100;
  }
}
Beispiel #2
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;
}
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 ) );
	}
}
Beispiel #4
0
bool gamepadConnected()
{
	bool ret = false;

	/* FIXME: 1 here means ignore accelerometer */
	if (al_is_joystick_installed() && al_get_num_joysticks() > 1) {
		ret = true;
	}

	if (!ret) {
		ret = isAndroidConsole();
	}

	return ret;
}
Beispiel #5
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));
   }
}
Beispiel #6
0
void    Inputs_Joystick_Init(void)
{
    int     i;
    int     num_joy;
    bool    found = FALSE;

    for (i = 0; i < Inputs.Sources_Max; i++)
    {
        t_input_src *src = Inputs.Sources[i];
        if (src->type == INPUT_SRC_TYPE_JOYPAD)
        {
            found = TRUE;
            break;
        }
    }
    if (found == FALSE)
        return;

    // There is at least one joypad so we'll launch initialization
    ConsolePrint(Msg_Get(MSG_Inputs_Joy_Init));

    if (!al_install_joystick() || ((num_joy = al_get_num_joysticks()) == 0))
    {
        ConsolePrint(Msg_Get(MSG_Inputs_Joy_Init_None));
		ConsolePrint("\n");
        return;
    }
    ConsolePrintf (Msg_Get(MSG_Inputs_Joy_Init_Found), num_joy);
    ConsolePrint("\n");

    // Flag available devices "connected and ready"
    for (i = 0; i < Inputs.Sources_Max; i++)
    {
        t_input_src *src = Inputs.Sources[i];
        if (src->type == INPUT_SRC_TYPE_JOYPAD)
            if (src->Connection_Port < num_joy)
                src->Connected_and_Ready = 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;
}
void Framework::TranslateAllegroEvents()
{
	ALLEGRO_EVENT e;
	Event* fwE;

	while( al_get_next_event( eventAllegro, &e ) )
	{
		switch( e.type )
		{
			case ALLEGRO_EVENT_DISPLAY_CLOSE:
				fwE = new Event();
				fwE->Type = EVENT_WINDOW_CLOSED;
				PushEvent( fwE );
				break;
			case ALLEGRO_EVENT_JOYSTICK_CONFIGURATION:
				al_reconfigure_joysticks();
				GetJoystickIDs();
				break;
			case ALLEGRO_EVENT_TIMER:
				if( e.timer.source == frameTimer )
				{
					if( enableSlowDown )
					{
						// Slow the game down, never process more than one update per frame
						framesToProcess = 1;
					} else {
						framesToProcess++;
					}
				} else {
					fwE = new Event();
					fwE->Type = EVENT_TIMER_TICK;
					fwE->Data.Timer.TimerObject = (void*)e.timer.source;
					PushEvent( fwE );
				}
				break;
			case ALLEGRO_EVENT_JOYSTICK_AXIS:
				fwE = new Event();
				fwE->Type = EVENT_JOYSTICK_AXIS;
				fwE->Data.Joystick.ID = -1;
				for( int i = 0; i < al_get_num_joysticks(); i++ )
				{
					if( joystickIDs.at( i ) == e.joystick.id )
					{
						fwE->Data.Joystick.ID = i;
						break;
					}
				}
				fwE->Data.Joystick.Stick = e.joystick.stick;
				fwE->Data.Joystick.Axis = e.joystick.axis;
				fwE->Data.Joystick.Position = e.joystick.pos;
				PushEvent( fwE );
				break;
			case ALLEGRO_EVENT_JOYSTICK_BUTTON_DOWN:
				fwE = new Event();
				fwE->Type = EVENT_JOYSTICK_BUTTON_DOWN;
				fwE->Data.Joystick.ID = -1;
				for( int i = 0; i < al_get_num_joysticks(); i++ )
				{
					if( joystickIDs.at( i ) == e.joystick.id )
					{
						fwE->Data.Joystick.ID = i;
						break;
					}
				}
				fwE->Data.Joystick.Button = e.joystick.button;
				PushEvent( fwE );
				break;
			case ALLEGRO_EVENT_JOYSTICK_BUTTON_UP:
				fwE = new Event();
				fwE->Type = EVENT_JOYSTICK_BUTTON_UP;
				fwE->Data.Joystick.ID = -1;
				for( int i = 0; i < al_get_num_joysticks(); i++ )
				{
					if( joystickIDs.at( i ) == e.joystick.id )
					{
						fwE->Data.Joystick.ID = i;
						break;
					}
				}
				fwE->Data.Joystick.Button = e.joystick.button;
				PushEvent( fwE );
				break;
			case ALLEGRO_EVENT_KEY_DOWN:
				fwE = new Event();
				fwE->Type = EVENT_KEY_DOWN;
				fwE->Data.Keyboard.KeyCode = e.keyboard.keycode;
				fwE->Data.Keyboard.UniChar = e.keyboard.unichar;
				fwE->Data.Keyboard.Modifiers = e.keyboard.modifiers;
				PushEvent( fwE );
				break;
			case ALLEGRO_EVENT_KEY_UP:
				fwE = new Event();
				fwE->Type = EVENT_KEY_UP;
				fwE->Data.Keyboard.KeyCode = e.keyboard.keycode;
				fwE->Data.Keyboard.UniChar = e.keyboard.unichar;
				fwE->Data.Keyboard.Modifiers = e.keyboard.modifiers;
				PushEvent( fwE );
				break;
			case ALLEGRO_EVENT_KEY_CHAR:
				fwE = new Event();
				fwE->Type = EVENT_KEY_PRESS;
				fwE->Data.Keyboard.KeyCode = e.keyboard.keycode;
				fwE->Data.Keyboard.UniChar = e.keyboard.unichar;
				fwE->Data.Keyboard.Modifiers = e.keyboard.modifiers;
				PushEvent( fwE );
				break;
			case ALLEGRO_EVENT_MOUSE_AXES:
				fwE = new Event();
				fwE->Type = EVENT_MOUSE_MOVE;
				fwE->Data.Mouse.X = DISPLAY->ScreenXToGameX(e.mouse.x);
				fwE->Data.Mouse.Y = DISPLAY->ScreenYToGameY(e.mouse.y);
				fwE->Data.Mouse.DeltaX = DISPLAY->ScreenXToGameX(e.mouse.dx);
				fwE->Data.Mouse.DeltaY = DISPLAY->ScreenYToGameY(e.mouse.dy);
				fwE->Data.Mouse.WheelVertical = e.mouse.dz;
				fwE->Data.Mouse.WheelHorizontal = e.mouse.dw;
				fwE->Data.Mouse.Button = e.mouse.button;
				PushEvent( fwE );
				break;
			case ALLEGRO_EVENT_MOUSE_BUTTON_DOWN:
				fwE = new Event();
				fwE->Type = EVENT_MOUSE_DOWN;
				fwE->Data.Mouse.X = DISPLAY->ScreenXToGameX(e.mouse.x);
				fwE->Data.Mouse.Y = DISPLAY->ScreenYToGameY(e.mouse.y);
				fwE->Data.Mouse.DeltaX = DISPLAY->ScreenXToGameX(e.mouse.dx);
				fwE->Data.Mouse.DeltaY = DISPLAY->ScreenYToGameY(e.mouse.dy);
				fwE->Data.Mouse.WheelVertical = e.mouse.dz;
				fwE->Data.Mouse.WheelHorizontal = e.mouse.dw;
				fwE->Data.Mouse.Button = e.mouse.button;
				PushEvent( fwE );
				break;
			case ALLEGRO_EVENT_MOUSE_BUTTON_UP:
				fwE = new Event();
				fwE->Type = EVENT_MOUSE_UP;
				fwE->Data.Mouse.X = DISPLAY->ScreenXToGameX(e.mouse.x);
				fwE->Data.Mouse.Y = DISPLAY->ScreenYToGameY(e.mouse.y);
				fwE->Data.Mouse.DeltaX = DISPLAY->ScreenXToGameX(e.mouse.dx);
				fwE->Data.Mouse.DeltaY = DISPLAY->ScreenYToGameY(e.mouse.dy);
				fwE->Data.Mouse.WheelVertical = e.mouse.dz;
				fwE->Data.Mouse.WheelHorizontal = e.mouse.dw;
				fwE->Data.Mouse.Button = e.mouse.button;
				PushEvent( fwE );
				break;
			case ALLEGRO_EVENT_DISPLAY_RESIZE:
				fwE = new Event();
				fwE->Type = EVENT_WINDOW_RESIZE;
				fwE->Data.Display.X = 0;
				fwE->Data.Display.Y = 0;
				fwE->Data.Display.Width = e.display.width;
				fwE->Data.Display.Height = e.display.height;
				fwE->Data.Display.Active = true;
				PushEvent( fwE );
				break;
			case ALLEGRO_EVENT_DISPLAY_SWITCH_IN:
				fwE = new Event();
				fwE->Type = EVENT_WINDOW_ACTIVATE;
				fwE->Data.Display.X = 0;
				fwE->Data.Display.Y = 0;
				fwE->Data.Display.Width = e.display.width;
				fwE->Data.Display.Height = e.display.height;
				fwE->Data.Display.Active = true;
				PushEvent( fwE );
				break;
			case ALLEGRO_EVENT_DISPLAY_SWITCH_OUT:
				fwE = new Event();
				fwE->Type = EVENT_WINDOW_DEACTIVATE;
				fwE->Data.Display.X = 0;
				fwE->Data.Display.Y = 0;
				fwE->Data.Display.Width = e.display.width;
				fwE->Data.Display.Height = e.display.height;
				fwE->Data.Display.Active = false;
				PushEvent( fwE );
				break;
			case ALLEGRO_EVENT_AUDIO_STREAM_FINISHED:
				fwE = new Event();
				fwE->Type = EVENT_AUDIO_STREAM_FINISHED;
				PushEvent( fwE );
				break;
			default:
				fwE = new Event();
				fwE->Type = EVENT_UNDEFINED;
				PushEvent( fwE );
				break;
		}
	}
}
Beispiel #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;
      }
   }
}