File: ex_timedwait.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 (134 lines) | stat: -rw-r--r-- 3,441 bytes parent folder | download
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
124
125
126
127
128
129
130
131
132
133
134
/*
 *    Example program for the Allegro library, by Peter Wang.
 *
 *    Test timed version of al_wait_for_event().
 */


#include <allegro5/allegro.h>
#include <allegro5/allegro_font.h>

#include "common.c"

static bool test_relative_timeout(ALLEGRO_FONT *font, ALLEGRO_EVENT_QUEUE *queue);
static bool test_absolute_timeout(ALLEGRO_FONT *font, ALLEGRO_EVENT_QUEUE *queue);



int main(int argc, char **argv)
{
   ALLEGRO_DISPLAY *dpy;
   ALLEGRO_EVENT_QUEUE *queue;
   ALLEGRO_FONT *font;

   (void)argc;
   (void)argv;

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

   al_install_keyboard();

   dpy = al_create_display(640, 480);
   if (!dpy) {
      abort_example("Could not create a display.\n");
   }

   font = al_create_builtin_font();
   if (!font) {
      abort_example("Could not create builtin font.\n");
   }

   queue = al_create_event_queue();
   al_register_event_source(queue, al_get_keyboard_event_source());
   al_register_event_source(queue, al_get_display_event_source(dpy));

   while (true) {
      if (!test_relative_timeout(font, queue))
         break;
      if (!test_absolute_timeout(font, queue))
         break;
   }

   return 0;
}



static bool test_relative_timeout(ALLEGRO_FONT *font, ALLEGRO_EVENT_QUEUE *queue)
{
   ALLEGRO_EVENT event;
   float shade = 0.1;

   while (true) {
      if (al_wait_for_event_timed(queue, &event, 0.1)) {
         if (event.type == ALLEGRO_EVENT_KEY_DOWN) {
            if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) {
               return true;
            }
            else {
               shade = 0.0;
            }
         }
         else if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
            return false;
         }
      }
      else {
         /* timed out */
         shade += 0.1;
         if (shade > 1.0)
            shade = 1.0;
      }

      al_clear_to_color(al_map_rgba_f(0.5 * shade, 0.25 * shade, shade, 0));
      al_draw_textf(font, al_map_rgb_f(1., 1., 1.), 320, 240, ALLEGRO_ALIGN_CENTRE, "Relative timeout (%.2f)", shade);
      al_draw_text(font, al_map_rgb_f(1., 1., 1.), 320, 260, ALLEGRO_ALIGN_CENTRE, "Esc to move on, Space to restart.");
      al_flip_display();
   }
}



static bool test_absolute_timeout(ALLEGRO_FONT *font, ALLEGRO_EVENT_QUEUE *queue)
{
   ALLEGRO_TIMEOUT timeout;
   ALLEGRO_EVENT event;
   float shade = 0.1;
   bool ret;

   while (true) {
      al_init_timeout(&timeout, 0.1);
      while ((ret = al_wait_for_event_until(queue, &event, &timeout))) {
         if (event.type == ALLEGRO_EVENT_KEY_DOWN) {
            if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) {
               return true;
            } else {
               shade = 0.;
            }
         }
         else if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
            return false;
         }
      }

      if (!ret) {
         /* timed out */
         shade += 0.1;
         if (shade > 1.0)
            shade = 1.0;
      }

      al_clear_to_color(al_map_rgba_f(shade, 0.5 * shade, 0.25 * shade, 0));
      al_draw_textf(font, al_map_rgb_f(1., 1., 1.), 320, 240, ALLEGRO_ALIGN_CENTRE, "Absolute timeout (%.2f)", shade);
      al_draw_text(font, al_map_rgb_f(1., 1., 1.), 320, 260, ALLEGRO_ALIGN_CENTRE, "Esc to move on, Space to restart.");
      al_flip_display();
   }
}


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