Beispiel #1
0
void Mouse::Init() {

    SetImage(_image, _focus.GetX(), _focus.GetY());
    _numButtons = al_get_mouse_num_buttons();

    al_get_mouse_state(&_curState);

}
Beispiel #2
0
int main(void)
{
   ALLEGRO_DISPLAY *display;
   ALLEGRO_BITMAP *cursor;
   ALLEGRO_EVENT_QUEUE *queue;
   ALLEGRO_EVENT event;
   ALLEGRO_FONT *font;
   int mx = 0;
   int my = 0;
   int mz = 0;
   int mw = 0;
   int mmx = 0;
   int mmy = 0;
   int mmz = 0;
   int mmw = 0;
   bool in = true;
   bool buttons[NUM_BUTTONS] = {false};
   int i;
   float p = 0.0;
   ALLEGRO_COLOR black;

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

   al_init_primitives_addon();
   al_install_mouse();
   al_install_keyboard();
   al_init_image_addon();
   al_init_font_addon();
   
   actual_buttons = al_get_mouse_num_buttons();
   if (actual_buttons > NUM_BUTTONS)
      actual_buttons = NUM_BUTTONS;

   al_set_new_display_flags(ALLEGRO_RESIZABLE);
   display = al_create_display(640, 480);
   if (!display) {
      abort_example("Error creating display\n");
   }

   al_hide_mouse_cursor(display);

   cursor = al_load_bitmap("data/cursor.tga");
   if (!cursor) {
      abort_example("Error loading cursor.tga\n");
   }

   font = al_load_font("data/fixed_font.tga", 1, 0);
   if (!font) {
      abort_example("data/fixed_font.tga not found\n");
   }
   
   black = al_map_rgb_f(0, 0, 0);

   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_display_event_source(display));

   while (1) {
      if (al_is_event_queue_empty(queue)) {
         al_clear_to_color(al_map_rgb(0xff, 0xff, 0xc0));
         for (i = 0; i < actual_buttons; i++) {
            draw_mouse_button(i, buttons[i]);
         }
         al_draw_bitmap(cursor, mx, my, 0);
         al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_INVERSE_ALPHA);
         al_draw_textf(font, black, 5, 5, 0, "dx %i, dy %i, dz %i, dw %i", mmx, mmy, mmz, mmw);
         al_draw_textf(font, black, 5, 25, 0, "x %i, y %i, z %i, w %i", mx, my, mz, mw);
         al_draw_textf(font, black, 5, 45, 0, "p = %g", p);
         al_draw_textf(font, black, 5, 65, 0, "%s", in ? "in" : "out");
         al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_INVERSE_ALPHA);
         mmx = mmy = mmz = 0;
         al_flip_display();
      }

      al_wait_for_event(queue, &event);
      switch (event.type) {
         case ALLEGRO_EVENT_MOUSE_AXES:
            mx = event.mouse.x;
            my = event.mouse.y;
            mz = event.mouse.z;
            mw = event.mouse.w;
            mmx = event.mouse.dx;
            mmy = event.mouse.dy;
            mmz = event.mouse.dz;
            mmw = event.mouse.dw;
            p = event.mouse.pressure;
            break;

         case ALLEGRO_EVENT_MOUSE_BUTTON_DOWN:
            if (event.mouse.button-1 < NUM_BUTTONS) {
               buttons[event.mouse.button-1] = true;
            }
            p = event.mouse.pressure;
            break;

         case ALLEGRO_EVENT_MOUSE_BUTTON_UP:
            if (event.mouse.button-1 < NUM_BUTTONS) {
               buttons[event.mouse.button-1] = false;
            }
            p = event.mouse.pressure;
            break;

         case ALLEGRO_EVENT_MOUSE_ENTER_DISPLAY:
            in = true;
            break;

         case ALLEGRO_EVENT_MOUSE_LEAVE_DISPLAY:
            in = false;
            break;

         case ALLEGRO_EVENT_KEY_DOWN:
            if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) {
               goto done;
            }
            break;

         case ALLEGRO_EVENT_DISPLAY_RESIZE:
            al_acknowledge_resize(event.display.source);
            break;

         case ALLEGRO_EVENT_DISPLAY_CLOSE:
            goto done;
      }
   }

done:

   al_destroy_event_queue(queue);

   return 0;
}
void game_loop(void)
{
    float bouncer_x = screenW / 2.0 - squareSize / 2.0 + 50;
    float bouncer_y = screenH / 2.0 - squareSize / 2.0;
    float crosshair_x = screenW / 2.0 - crosshairSize / 2.0;
    float crosshair_y = screenH / 2.0 - crosshairSize / 2.0;
    float bouncer_dx = 4, bouncer_dy = -4.0;
    ALLEGRO_FONT *font = al_load_ttf_font("pirulen.ttf",72,0 );
    int textCounter = 0;
    int hit = 0;
    int miss = 0;
    int counter = 0;
    int onGround = 1;
    bool key[5] = {0, 0, 0, 0, 0};
    bool redraw = true;
    al_start_timer(timer);

    printf("mouse axes: %d" , al_get_mouse_num_buttons());
 
    while (!done) {
        ALLEGRO_EVENT event;
        al_wait_for_event(event_queue, &event);
 
        if (event.type == ALLEGRO_EVENT_TIMER) 
        {
               if(bouncer_x < 0 || bouncer_x > screenW - squareSize) {
                  bouncer_dx = -bouncer_dx;
               }
       
               if(bouncer_y < 0 || bouncer_y > screenH - squareSize) {
                  bouncer_dy = -bouncer_dy;
               }
       
               bouncer_x += bouncer_dx;
               bouncer_y += bouncer_dy;

               if (textCounter)
               {
                  textCounter = textCounter + 1;
                  if (textCounter == 60)
                  {
                     textCounter = 0;
                  }
               }
               redraw = true;
         }

        //key pressed down
        else if (event.type == ALLEGRO_EVENT_KEY_DOWN) 
        {
            switch (event.keyboard.keycode)
            {
                case ALLEGRO_KEY_ESCAPE:
                done = true;

                case ALLEGRO_KEY_W:
                key[keyW] = true;
                break;

                case ALLEGRO_KEY_A:
                key[keyA] = true;
                break;

                case ALLEGRO_KEY_S:
                key[keyS] = true;
                break;

                case ALLEGRO_KEY_D:
                key[keyD] = true;
                break;

                case ALLEGRO_KEY_LSHIFT:
                key[keyShift] = true;
                break;
            }
        }

        //key released
        else if (event.type == ALLEGRO_EVENT_KEY_UP)
        {
            switch (event.keyboard.keycode)
            {
                case ALLEGRO_KEY_W:
                key[keyW] = false;
                break;

                case ALLEGRO_KEY_A:
                key[keyA] = false;
                break;

                case ALLEGRO_KEY_S:
                key[keyS] = false;
                break;

                case ALLEGRO_KEY_D:
                key[keyD] = false;
                break;

                case ALLEGRO_KEY_LSHIFT:
                key[keyShift] = false;
                break;
            }
        }
        
        else if(event.type == ALLEGRO_EVENT_MOUSE_AXES || ALLEGRO_EVENT_MOUSE_ENTER_DISPLAY)
        {
           crosshair_x = event.mouse.x - crosshairSize/2;
           crosshair_y = event.mouse.y - crosshairSize/2;
        }
        

        //shooting
         if (event.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN)
        {
           if (bounding_box_collision(bouncer_x, bouncer_y, squareSize, squareSize, event.mouse.x - 1, event.mouse.y - 1, 2, 2))
           {
              fprintf(stderr, "hit! \n");
              hit = 1;
              miss = 0;
              textCounter = 1;
           }
           else
           {
              fprintf(stderr, "Miss! \n");
              miss = 1;
              hit = 0;
              textCounter = 1;
           }
        }

        /*
        //Mouse Test
         if(event.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) {
                      done = 1;
                            }
        */

        if (redraw && al_is_event_queue_empty(event_queue)) {
            redraw = false;

            //Clear screen to green
            al_clear_to_color(al_map_rgb(0, 255, 0));

            //hit text
            if (hit && textCounter)
            {
               al_draw_text(font, al_map_rgb(255,255,255), 640/2, (480/4),ALLEGRO_ALIGN_CENTRE, "Hit!");
            }

            //Miss text
            if (miss && textCounter)
            {
               al_draw_text(font, al_map_rgb(255,255,255), 640/2, (480/4),ALLEGRO_ALIGN_CENTRE, "Miss!");
            }

               al_draw_bitmap(bouncer, bouncer_x, bouncer_y, 0);
            al_draw_bitmap(crosshair, crosshair_x, crosshair_y, 0);
            //update_graphics();
            al_flip_display();
        }
    }

}
Beispiel #4
0
VALUE rbal_mouse_buttons(VALUE rself) {  
  return RBH_UINT_NUM(al_get_mouse_num_buttons());
}
Beispiel #5
0
static int allua_mouse_get_num_buttons(lua_State * L)
{
   lua_pushinteger(L, al_get_mouse_num_buttons());
   return 1;
}