示例#1
0
文件: touch.cpp 项目: kazzmir/r-tech1
Allegro5Touch::Allegro5Touch():
queue(NULL){
    if (al_install_touch_input()){
        queue = al_create_event_queue();
        if (queue != NULL){
            al_register_event_source(queue, al_get_touch_input_event_source());
        }
    }
}
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;
}
示例#3
0
int main(void)
{
   ALLEGRO_TIMER *timer;
   ALLEGRO_EVENT_QUEUE *queue;
   ALLEGRO_MONITOR_INFO info;
   int w = 640, h = 480;
   bool done = false;
   bool need_redraw = true;
   bool background = false;
   example.show_help = true;

   if (!al_init()) {
      abort_example("Failed to init Allegro.\n");
      return 1;
   }

   if (!al_init_image_addon()) {
      abort_example("Failed to init IIO addon.\n");
      return 1;
   }

   al_init_font_addon();

   al_get_num_video_adapters();
   
   al_get_monitor_info(0, &info);

   #ifdef ALLEGRO_IPHONE
   al_set_new_display_flags(ALLEGRO_FULLSCREEN_WINDOW);
   #endif
   al_set_new_display_option(ALLEGRO_SUPPORTED_ORIENTATIONS,
                             ALLEGRO_DISPLAY_ORIENTATION_ALL, ALLEGRO_SUGGEST);
   example.display = al_create_display(w, h);
   w = al_get_display_width(example.display);
   h = al_get_display_height(example.display);

   if (!example.display) {
      abort_example("Error creating display.\n");
      return 1;
   }

   if (!al_install_keyboard()) {
      abort_example("Error installing keyboard.\n");
      return 1;
   }
    
   if (!al_install_mouse()) {
        abort_example("Error installing mouse.\n");
        return 1;
    }

   example.font = al_load_font("data/fixed_font.tga", 0, 0);
   if (!example.font) {
      abort_example("Error loading data/fixed_font.tga\n");
      return 1;
   }

   example.mysha = al_load_bitmap("data/mysha256x256.png");
   if (!example.mysha) {
      abort_example("Error loading data/mysha256x256.png\n");
      return 1;
   }

   example.white = al_map_rgb_f(1, 1, 1);
   example.half_white = al_map_rgba_f(1, 1, 1, 0.5);
   example.dark = al_map_rgb(15, 15, 15);
   example.red = al_map_rgb_f(1, 0.2, 0.1);
   change_size(256);
   add_sprite();
   add_sprite();

   timer = al_create_timer(1.0 / FPS);

   queue = al_create_event_queue();
   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_timer_event_source(timer));
   
   if (al_install_touch_input())
      al_register_event_source(queue, al_get_touch_input_event_source());
   al_register_event_source(queue, al_get_display_event_source(example.display));

   al_start_timer(timer);

   while (!done) {
      float x, y;
      ALLEGRO_EVENT event;
      w = al_get_display_width(example.display);
      h = al_get_display_height(example.display);

      if (!background && need_redraw && al_is_event_queue_empty(queue)) {
         double t = -al_get_time();
         add_time();
         al_clear_to_color(al_map_rgb_f(0, 0, 0));
         redraw();
         t += al_get_time();
         example.direct_speed_measure  = t;
         al_flip_display();
         need_redraw = false;
      }

      al_wait_for_event(queue, &event);
      switch (event.type) {
         case ALLEGRO_EVENT_KEY_CHAR: /* includes repeats */
            if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE)
               done = true;
            else if (event.keyboard.keycode == ALLEGRO_KEY_UP) {
               add_sprites(1);
            }
            else if (event.keyboard.keycode == ALLEGRO_KEY_DOWN) {
               remove_sprites(1);
            }
            else if (event.keyboard.keycode == ALLEGRO_KEY_LEFT) {
               change_size(example.bitmap_size - 1);
            }
            else if (event.keyboard.keycode == ALLEGRO_KEY_RIGHT) {
               change_size(example.bitmap_size + 1);
            }
            else if (event.keyboard.keycode == ALLEGRO_KEY_F1) {
               example.show_help ^= 1;
            }
            else if (event.keyboard.keycode == ALLEGRO_KEY_SPACE) {
               example.use_memory_bitmaps ^= 1;
               change_size(example.bitmap_size);
            }
            else if (event.keyboard.keycode == ALLEGRO_KEY_B) {
               example.blending++;
               if (example.blending == 4)
                  example.blending = 0;
            }
            break;

         case ALLEGRO_EVENT_DISPLAY_CLOSE:
            done = true;
            break;

         case ALLEGRO_EVENT_DISPLAY_HALT_DRAWING:

            background = true;
            al_acknowledge_drawing_halt(event.display.source);

            break;
         
         case ALLEGRO_EVENT_DISPLAY_RESUME_DRAWING:
            background = false;
            break;
         
         case ALLEGRO_EVENT_DISPLAY_RESIZE:
            al_acknowledge_resize(event.display.source);
            break;
              
         case ALLEGRO_EVENT_TIMER:
            update();
            need_redraw = true;
            break;
         
         case ALLEGRO_EVENT_TOUCH_BEGIN:
            x = event.touch.x;
            y = event.touch.y;
            goto click;

         case ALLEGRO_EVENT_MOUSE_BUTTON_UP:
            x = event.mouse.x;
            y = event.mouse.y;
            goto click;
            
         click:
         {
            int fh = al_get_font_line_height(example.font);
            
            if (x < 80 && y >= h - fh * 10) {
               int button = (y - (h - fh * 10)) / (fh * 2);
               if (button == 0) {
                  example.use_memory_bitmaps ^= 1;
                  change_size(example.bitmap_size);
               }
               if (button == 1) {
                  example.blending++;
                  if (example.blending == 4)
                     example.blending = 0;
               }
               if (button == 3) {
                  if (x < 40)
                     remove_sprites(example.sprite_count / 2);
                  else
                     add_sprites(example.sprite_count);
               }
               if (button == 2) {
                  int s = example.bitmap_size * 2;
                  if (x < 40)
                     s = example.bitmap_size / 2;
                  change_size(s);
               }
               if (button == 4) {
                  example.show_help ^= 1;
               }
                
            }
            break;
         }
      }
   }

   al_destroy_bitmap(example.bitmap);

   return 0;
}
int main(int argc, char **argv)
{
   int num_touches = 0;
   TOUCH touches[MAX_TOUCHES];
   ALLEGRO_DISPLAY *display;
   ALLEGRO_EVENT_QUEUE *queue;
   ALLEGRO_EVENT event;

   (void)argc;
   (void)argv;

   if (!al_init()) {
      abort_example("Could not init Allegro.\n");
   }
   al_init_primitives_addon();
   if (!al_install_touch_input()) {
      abort_example("Could not init touch input.\n");
   }

   display = al_create_display(800, 600);
   if (!display) {
       abort_example("Error creating display\n");
   }
   queue = al_create_event_queue();

   al_register_event_source(queue, al_get_touch_input_event_source());
   al_register_event_source(queue, al_get_display_event_source(display));

   while (true) {
      if (al_is_event_queue_empty(queue)) {
         al_clear_to_color(al_map_rgb(255, 255, 255));
         draw_touches(num_touches, touches);
         al_flip_display();
      }

      al_wait_for_event(queue, &event);

      if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
         break;
      }
      else if (event.type == ALLEGRO_EVENT_TOUCH_BEGIN) {
         int i = num_touches;
         if (num_touches < MAX_TOUCHES) {
            touches[i].id = event.touch.id;
            touches[i].x = event.touch.x;
            touches[i].y = event.touch.y;
            num_touches++;
         }
      }
      else if (event.type == ALLEGRO_EVENT_TOUCH_END) {
         int i = find_index(event.touch.id, num_touches, touches);
         if (i >= 0 && i < num_touches) {
            touches[i] = touches[num_touches - 1];
            num_touches--;
         }
      }
      else if (event.type == ALLEGRO_EVENT_TOUCH_MOVE) {
         int i = find_index(event.touch.id, num_touches, touches);
         if (i >= 0) {
            touches[i].x = event.touch.x;
            touches[i].y = event.touch.y;
         }
      }
   }

   return 0;
}
/*
 * keyboard driver is called first, so initialize allegro here 
 */
int init_allegro(void){

    if(!al_init())
    {
#if _ANDROID_
	__android_log_print(ANDROID_LOG_INFO,"AllegroActivity","Error!, Allegro has failed to initialize.");	
#else      
        fprintf(stderr,"Error!, Allegro has failed to initialize.\n");
#endif
	return -1;
    }  else {
        //fprintf(stderr,"al_init successful\n");
    }
#if _ANDROID_    
    //al_set_new_display_flags(ALLEGRO_FULLSCREEN_WINDOW);
    al_set_new_display_flags(ALLEGRO_WINDOWED);
    //al_set_new_display_option(ALLEGRO_COLOR_SIZE,32,ALLEGRO_SUGGEST);
    //al_set_new_display_option(ALLEGRO_CAN_DRAW_INTO_BITMAP,1,ALLEGRO_REQUIRE);
#endif  
    //al_set_new_display_option(ALLEGRO_COLOR_SIZE,32,ALLEGRO_SUGGEST);
    display = al_create_display(SCREEN_WIDTH, SCREEN_HEIGHT);
    //display = al_create_display(800, 600);

    if(display == NULL)
    {
#if _ANDROID_
	__android_log_print(ANDROID_LOG_INFO,"AllegroActivity","Error!, Failed to create the display.");	
#else      
        fprintf(stderr,"Error!, Failed to create the display.");
#endif
	return -1;
    }    
    al_set_system_mouse_cursor(display,ALLEGRO_SYSTEM_MOUSE_CURSOR_DEFAULT);

    if(!al_install_keyboard())
    {
        fprintf(stderr,"Error!, Failed to install keyboard.\n");
        return -1;
    }  else {
        //fprintf(stderr,"al_install_keyboard successful\n");
    }   

    if(!al_install_mouse())
    {
        fprintf(stderr,"Error!, Failed to install mouse.");
        return -1;
    }    

    a_event_queue_k = al_create_event_queue();
    if(a_event_queue_k == NULL)
    {
       fprintf(stderr,"Error!, Failed to create the keyboard event queue.");
       return -1;
    }    
    al_register_event_source(a_event_queue_k, al_get_keyboard_event_source());

    a_event_queue_m = al_create_event_queue();
    if(a_event_queue_m == NULL)
    {
       fprintf(stderr,"Error!, Failed to create the mouse event queue.");
       return -1;
    }    
#if _ANDROID_    
//do below at touch input
#else
    al_register_event_source(a_event_queue_m, al_get_mouse_event_source());
#endif
    a_event_queue_d = al_create_event_queue();
    if(a_event_queue_d == NULL)
    {
       fprintf(stderr,"Error!, Failed to create the display event queue.");
       return -1;
    }    
    al_register_event_source(a_event_queue_d, al_get_display_event_source(display));

#if _ANDROID_
    if(!al_install_touch_input())
    {
      __android_log_print(ANDROID_LOG_INFO,"AllegroActivity","Error!, Failed to install touch_input.");	
    } else {
      __android_log_print(ANDROID_LOG_INFO,"AllegroActivity","Installed touch_input successfully.");	
      al_set_mouse_emulation_mode(ALLEGRO_MOUSE_EMULATION_5_0_x);
      
      al_register_event_source(a_event_queue_m, al_get_touch_input_mouse_emulation_event_source());
    }
#endif 

#if _ANDROID_
//allow to use al_open to read (only) files from the apk store, e.g. fonts
    al_android_set_apk_file_interface();
#endif

    return 1; //ok    
}