Example #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;
  }
}
Example #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;
        }
    }
}
Example #3
0
//à utiliser pour permettre de brancher un controlleur en route
bool xc_reconfigure()
{
	return al_reconfigure_joysticks();
}
Example #4
0
int main( int argc, char* argv[] )
{
	ALLEGRO_EVENT e;
	ALLEGRO_TIMER* t;
	int64_t framesToUpdate = 0;

	if( !al_init() )
	{
		return -1;
	}
	
	al_init_font_addon();
	if( !al_install_keyboard() || !al_install_mouse() || !al_init_primitives_addon() || !al_init_ttf_addon() || !al_init_image_addon() )
	{
		return -1;
	}

#if NETWORK_SUPPORT != 0
	if( !install_network() )
	{
		return -1;
	}
#endif

#if HTTP_SUPPORT
	if( !install_http() )
	{
		return -1;
	}
#ifdef PANDORA
	Downloads = new HttpManager(2);
#else
	Downloads = new HttpManager(6);
#endif
#endif

#if EXIT_IF_NO_AUDIO != 0

	if( !al_install_audio() || !al_init_acodec_addon() )
	{
		return -1;
	}

	voice = al_create_voice(44100, ALLEGRO_AUDIO_DEPTH_INT16, ALLEGRO_CHANNEL_CONF_2);
	if (!voice)
		return 1;
	mixer = al_create_mixer(44100, ALLEGRO_AUDIO_DEPTH_FLOAT32, ALLEGRO_CHANNEL_CONF_2);
	if (!mixer)
		return 1;
	if (!al_attach_mixer_to_voice(mixer, voice))
		return 1;

#else

	if( al_install_audio() )
	{
		if( al_init_acodec_addon() )
		{
			voice = al_create_voice(44100, ALLEGRO_AUDIO_DEPTH_INT16, ALLEGRO_CHANNEL_CONF_2);
			if( voice != 0 )
			{
				mixer = al_create_mixer(44100, ALLEGRO_AUDIO_DEPTH_FLOAT32, ALLEGRO_CHANNEL_CONF_2);
				if( mixer != 0 )
					al_attach_mixer_to_voice(mixer, voice);
			}
		}
	}

#endif // EXIT_IF_NO_AUDIO

	// Random number is guarenteed to be random
	srand( 5 );

	GameStack = new StageStack();
	CurrentConfiguration = new Configuration();

	if( CurrentConfiguration->FullScreen )
		al_set_new_display_flags( ALLEGRO_FULLSCREEN_WINDOW );

	al_set_new_display_option(ALLEGRO_VSYNC, 1, ALLEGRO_SUGGEST);

	bool foundMode = false;
	int fallbackW = 640;
	int fallbackH = 480;

	if( CurrentConfiguration->ForceResolution )
	{
		foundMode = true;
	} else {
		for( int modeIdx = 0; modeIdx < al_get_num_display_modes(); modeIdx++ )
		{
			if( al_get_display_mode( modeIdx, &ScreenMode ) != NULL )
			{
				if( ScreenMode.width == CurrentConfiguration->ScreenWidth && ScreenMode.height == CurrentConfiguration->ScreenHeight )
				{
					foundMode = true;
				} else {
					fallbackW = ScreenMode.width;
					fallbackH = ScreenMode.height;
				}
			}

			if( foundMode )
				break;
		}
	}

	if( foundMode )
	{
		Screen = al_create_display( CurrentConfiguration->ScreenWidth, CurrentConfiguration->ScreenHeight );
	} else {
		Screen = al_create_display( fallbackW, fallbackH );
		CurrentConfiguration->ScreenWidth = fallbackW;
		CurrentConfiguration->ScreenHeight = fallbackH;
	}

	al_hide_mouse_cursor( Screen );

	t = al_create_timer( 1.0 / SCREEN_FPS );
  if( t == NULL )
    Quit = true;
  al_start_timer( t );

	EventQueue = al_create_event_queue();
	al_register_event_source( EventQueue, al_get_display_event_source( Screen ) );
	al_register_event_source( EventQueue, al_get_keyboard_event_source() );
	al_register_event_source( EventQueue, al_get_mouse_event_source() );
	al_register_event_source( EventQueue, al_get_timer_event_source( t ) );
#if NETWORK_SUPPORT != 0
	al_register_event_source( EventQueue, get_network_event_source() );
#endif
#if HTTP_SUPPORT
	Downloads->urlDownloads = CurrentConfiguration->MaxConcurrentDownloads;
	al_register_event_source( EventQueue, get_http_event_source() );
#endif

	Fonts = new FontManager();
	Images = new ImageManager();
	Audio = new SoundManager();

	al_set_blender( ALLEGRO_ADD, ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA );

	GameStack->Push( (Stage*)new BootUp() );

	while( !Quit )
	{
		if( GameStack->IsEmpty() )
		{
			Quit = true;
		} else {
			while( al_get_next_event( EventQueue, &e ) )
			{
#if HTTP_SUPPORT
				Downloads->Event( &e );
#endif
				switch( e.type )
				{
					case ALLEGRO_EVENT_DISPLAY_CLOSE:
						Quit = true;
						break;
					case ALLEGRO_EVENT_JOYSTICK_CONFIGURATION:
						al_reconfigure_joysticks();
						break;
					case ALLEGRO_EVENT_TIMER:
						if( e.timer.source == t )
							framesToUpdate++;
						else if( !GameStack->IsEmpty() )
							GameStack->Current()->Event( &e );
						break;
					default:
						if( !GameStack->IsEmpty() )
							GameStack->Current()->Event( &e );
						switch( e.type )
						{
#if HTTP_SUPPORT
							case ALLEGRO_EVENT_HTTP:
#endif
#if NETWORK_SUPPORT
							case ALLEGRO_EVENT_NETWORK_CONNECTION:
							case ALLEGRO_EVENT_NETWORK_RECEIVEPACKET:
							case ALLEGRO_EVENT_NETWORK_DISCONNECTION:
#endif
							case ALLEGRO_EVENT_BUTTON_CLICK:
							case ALLEGRO_EVENT_MOUSEEX_MOVE:
							case ALLEGRO_EVENT_MOUSEEX_DOWN:
							case ALLEGRO_EVENT_MOUSEEX_UP:
							case ALLEGRO_EVENT_MOUSEEX_CLICK:
							case ALLEGRO_EVENT_MOUSEEX_DOUBLECLICK:
							case ALLEGRO_EVENT_MOUSEEX_BOXED:
							case ALLEGRO_EVENT_MOUSEEX_WHEEL:
								al_unref_user_event( &e.user );
								break;
						}
						break;
				}
			}

			if( framesToUpdate > 0 )
			{
				for( int frmUp = 0; frmUp < framesToUpdate; frmUp++ )
				{
					if( !GameStack->IsEmpty() )
						GameStack->Current()->Update();
				}
				framesToUpdate = 0;
			}

			al_clear_to_color( al_map_rgb( 128, 128, 128 ) );
			if( !GameStack->IsEmpty() )
				GameStack->Current()->Render();
			al_flip_display();

			Images->Tidy();
			Fonts->Tidy();
			Audio->Tidy();
		}
	}

	while( !GameStack->IsEmpty() )
	{
		GameStack->Pop();
	}

	delete Downloads;
	delete Fonts;
	delete Images;
	delete Audio;

	al_destroy_event_queue( EventQueue );
	al_destroy_display( Screen );

#if HTTP_SUPPORT
	uninstall_http();
#endif
#if NETWORK_SUPPORT != 0
	uninstall_network();
#endif
	al_uninstall_keyboard();
	al_uninstall_mouse();
	al_shutdown_primitives_addon();
	al_shutdown_ttf_addon();
	al_shutdown_image_addon();
	al_uninstall_audio();
	al_shutdown_font_addon();

	return 0;
}
Example #5
0
Framework::Framework()
{
#ifdef WRITE_LOG
  printf( "Framework: Startup\n" );
#endif

  // Init Allegro
	if( !al_init() )
	{
		return;
	}
	al_init_font_addon();
	if( !al_install_keyboard() || !al_install_mouse() || !al_init_primitives_addon() || !al_init_ttf_addon() || !al_init_image_addon() )
	{
		return;
	}
	if( al_install_joystick() )
  {
    al_reconfigure_joysticks();
  }

  audioInitialised = false;
  InitialiseAudioSystem();

  std::string selectedLanguage;
  quitProgram = false;
  ProgramStages = new StageStack();
  framesToProcess = 0;
  Settings = new ConfigFile( "settings.cfg" );

  eventQueue = al_create_event_queue();

  InitialiseDisplay();

	al_register_event_source( eventQueue, al_get_keyboard_event_source() );
	al_register_event_source( eventQueue, al_get_mouse_event_source() );

	fpsTimer = al_create_timer( 1.0 / (float)FRAMES_PER_SECOND );
	al_register_event_source( eventQueue, al_get_timer_event_source( fpsTimer ) );
	al_start_timer( fpsTimer );

	al_hide_mouse_cursor( displaySurface );


  imageMgr = new ImageManager( this );
  fontMgr = new FontManager( this );
  audioMgr = new AudioManager( this );
  int maxDownloads = 4;
  if( Settings->KeyExists( "Downloads.MaxConcurrentDownloads" ) )
  {
    Settings->GetIntegerValue( "Downloads.MaxConcurrentDownloads", &maxDownloads );
  }
  downloadMgr = new DownloadManager( this, maxDownloads );
  networkMgr = new NetworkManager( this );
  languageMgr = new LanguageManager();
  if( Settings->KeyExists( "Application.Language" ) )
  {
    Settings->GetStringValue( "Application.Language", &selectedLanguage );
    languageMgr->SetActiveLanguage( selectedLanguage );
  }

  NativePlatform = new Platform();

  SystemFramework = this;

  extraEventsMutex = al_create_mutex();
}
Example #6
0
void Framework::ProcessEvents()
{
  ALLEGRO_EVENT e;
  FwEvent* fwev;

#ifdef WRITE_LOG
  printf( "Framework: ProcessEvents.AllegroEvents\n" );
#endif
  while( al_get_next_event( eventQueue, &e ) )
  {
    switch( e.type )
    {
      case ALLEGRO_EVENT_DISPLAY_CLOSE:
        quitProgram = true;
        break;
      case ALLEGRO_EVENT_JOYSTICK_CONFIGURATION:
        al_reconfigure_joysticks();
        break;
      case ALLEGRO_EVENT_TIMER:
        if( e.timer.source == fpsTimer )
        {
          framesToProcess++;
        } else {
          fwev = new FwEvent( &e );
          ProgramStages->Current()->Event( fwev );
          delete fwev;
        }
        break;
      default:
        fwev = new FwEvent( &e );
        ProgramStages->Current()->Event( fwev );
        delete fwev;
    }
    if( ProgramStages->IsEmpty() )
    {
      quitProgram = true;
      return;
    }
  }

#ifdef WRITE_LOG
  printf( "Framework: ProcessEvents.ExtraEvents\n" );
#endif
  al_lock_mutex( extraEventsMutex );
  while( extraEvents.size() > 0 )
  {
    fwev = extraEvents.front();
    extraEvents.pop_front();
    if( fwev->Type == EVENT_DOWNLOAD_COMPLETE )
    {
      downloadMgr->Event( fwev );
    }
    ProgramStages->Current()->Event( fwev );
    delete fwev;
    if( ProgramStages->IsEmpty() )
    {
      quitProgram = true;
      return;
    }
  }
  al_unlock_mutex( extraEventsMutex );

#ifdef WRITE_LOG
  printf( "Framework: ProcessEvents.Update\n" );
#endif
  while( framesToProcess > 0 )
  {
    if( ProgramStages->IsEmpty() )
    {
      quitProgram = true;
      return;
    }
    ProgramStages->Current()->Update();
    imageMgr->Update();
    fontMgr->Update();
    audioMgr->Update();
    downloadMgr->Update();
    networkMgr->Update();
    framesToProcess--;
  }

#ifdef WRITE_LOG
  printf( "Framework: ProcessEvents.Render\n" );
#endif
  if( !ProgramStages->IsEmpty() )
  {
    ProgramStages->Current()->Render();
    al_flip_display();
  }

}
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;
}
Example #8
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;
		}
	}
}
Example #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;
      }
   }
}