Skip to content

Commit

Permalink
Improve ex_timedwait
Browse files Browse the repository at this point in the history
  • Loading branch information
SiegeLordEx authored and SiegeLord committed Sep 15, 2024
1 parent e38b93b commit 5114ee6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
2 changes: 1 addition & 1 deletion examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ example(ex_shader_target ${IMAGE} ${DATA_IMAGES} ${DATA_SHADERS})
example(ex_subbitmap ${IMAGE} ${PRIM} ${DATA_IMAGES})
example(ex_threads ${PRIM})
example(ex_threads2)
example(ex_timedwait)
example(ex_timedwait ${FONT})
example(ex_timer ${FONT} ${PRIM})
example(ex_timer_pause)
example(ex_touch_input ${PRIM})
Expand Down
47 changes: 36 additions & 11 deletions examples/ex_timedwait.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,60 @@


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

#include "common.c"

static void test_relative_timeout(ALLEGRO_EVENT_QUEUE *queue);
static void test_absolute_timeout(ALLEGRO_EVENT_QUEUE *queue);
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("Unable to set any graphic mode\n");
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));

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

return 0;
}



static void test_relative_timeout(ALLEGRO_EVENT_QUEUE *queue)
static bool test_relative_timeout(ALLEGRO_FONT *font, ALLEGRO_EVENT_QUEUE *queue)
{
ALLEGRO_EVENT event;
float shade = 0.1;
Expand All @@ -53,12 +68,15 @@ static void test_relative_timeout(ALLEGRO_EVENT_QUEUE *queue)
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;
return true;
}
else {
shade = 0.1;
shade = 0.0;
}
}
else if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
return false;
}
}
else {
/* timed out */
Expand All @@ -68,13 +86,15 @@ static void test_relative_timeout(ALLEGRO_EVENT_QUEUE *queue)
}

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 void test_absolute_timeout(ALLEGRO_EVENT_QUEUE *queue)
static bool test_absolute_timeout(ALLEGRO_FONT *font, ALLEGRO_EVENT_QUEUE *queue)
{
ALLEGRO_TIMEOUT timeout;
ALLEGRO_EVENT event;
Expand All @@ -86,11 +106,14 @@ static void test_absolute_timeout(ALLEGRO_EVENT_QUEUE *queue)
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;
return true;
} else {
shade = 0.1;
shade = 0.;
}
}
else if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
return false;
}
}

if (!ret) {
Expand All @@ -101,6 +124,8 @@ static void test_absolute_timeout(ALLEGRO_EVENT_QUEUE *queue)
}

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();
}
}
Expand Down

0 comments on commit 5114ee6

Please sign in to comment.