File: ex_touch_input.c

package info (click to toggle)
allegro5 2%3A5.2.10.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 10,820 kB
  • sloc: ansic: 109,795; cpp: 12,976; objc: 4,592; java: 2,845; python: 2,595; javascript: 1,238; sh: 1,008; makefile: 40; xml: 27; pascal: 24
file content (123 lines) | stat: -rw-r--r-- 3,120 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <allegro5/allegro.h>
#include <allegro5/allegro_primitives.h>

#include "common.c"

#define MAX_TOUCHES 16

typedef struct TOUCH TOUCH;
struct TOUCH {
   int id;
   int x;
   int y;
};

static void draw_touches(int num, TOUCH touches[])
{
   int i;

   for (i = 0; i < num; i++) {
      int x = touches[i].x;
      int y = touches[i].y;
      al_draw_circle(x, y, 50, al_map_rgb(255, 0, 0), 4);
   }
}

static int find_index(int id, int num, TOUCH touches[])
{
   int i;

   for (i = 0; i < num; i++) {
      if (touches[i].id == id) {
         return i;
      }
   }

   return -1;
}

int main(int argc, char **argv)
{
   int num_touches = 0;
   bool quit = false;
   bool background = false;
   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 (!quit) {
      if (!background && 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);
      switch (event.type) {
         case ALLEGRO_EVENT_DISPLAY_CLOSE:
            quit = true;
            break;
         case 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++;
            }
            break;
         }
         case 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--;
            }
            break;
         }
         case 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;
            }
            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;
            al_acknowledge_drawing_resume(event.display.source);
            break;
         case ALLEGRO_EVENT_DISPLAY_RESIZE:
            al_acknowledge_resize(event.display.source);
            break;
      }
   }

   return 0;
}

/* vim: set sts=3 sw=3 et: */