core: Get rid of GP_Color.
[gfxprim.git] / demos / c_simple / backend_timers_example.c
blob7b7aae2a06fdd45cee4ec83cd5fe38634b4647ab
1 /*****************************************************************************
2 * This file is part of gfxprim library. *
3 * *
4 * Gfxprim is free software; you can redistribute it and/or *
5 * modify it under the terms of the GNU Lesser General Public *
6 * License as published by the Free Software Foundation; either *
7 * version 2.1 of the License, or (at your option) any later version. *
8 * *
9 * Gfxprim is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
12 * Lesser General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU Lesser General Public *
15 * License along with gfxprim; if not, write to the Free Software *
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
17 * Boston, MA 02110-1301 USA *
18 * *
19 * Copyright (C) 2009-2013 Cyril Hrubis <metan@ucw.cz> *
20 * *
21 *****************************************************************************/
25 Simple backend timers example.
29 #include <stdio.h>
30 #include <GP.h>
32 static void redraw(GP_Backend *self)
34 GP_Context *context = self->context;
35 GP_Pixel black_pixel = GP_RGBToContextPixel(0x00, 0x00, 0x00, context);
37 GP_Fill(context, black_pixel);
39 /* Update the backend screen */
40 GP_BackendFlip(self);
43 static uint32_t timer_callback(GP_Timer *self)
45 uint32_t next = random() % 10000;
47 printf("Timer %s callback called, rescheduling after %u.\n",
48 self->id, (unsigned) next);
50 return next;
53 int main(void)
55 GP_Backend *backend;
56 const char *backend_opts = "X11:100x100";
58 backend = GP_BackendInit(backend_opts, "Backend Timers Example");
60 if (backend == NULL) {
61 fprintf(stderr, "Failed to initialize backend\n");
62 return 1;
65 redraw(backend);
68 * Periodic timer with 1000ms interval. As the callback is set to NULL
69 * Timer Event is pushed to event queue upon expiration.
71 GP_TIMER_DECLARE(timer1, 0, 1000, "Timer 1", NULL, NULL);
74 * Timer with a callback, this timer gets scheduled depending on ouput
75 * from callback (0 means disable timer).
77 GP_TIMER_DECLARE(timer2, 5000, 0, "Timer 2", timer_callback, NULL);
79 GP_BackendAddTimer(backend, &timer1);
80 GP_BackendAddTimer(backend, &timer2);
82 /* Handle events */
83 for (;;) {
84 GP_Event ev;
86 GP_BackendWaitEvent(backend, &ev);
88 GP_EventDump(&ev);
90 switch (ev.type) {
91 case GP_EV_KEY:
92 switch (ev.val.val) {
93 case GP_KEY_ESC:
94 case GP_KEY_Q:
95 GP_BackendExit(backend);
96 return 0;
97 break;
99 break;
100 case GP_EV_SYS:
101 switch (ev.code) {
102 case GP_EV_SYS_RESIZE:
103 GP_BackendResizeAck(backend);
104 redraw(backend);
105 break;
106 case GP_EV_SYS_QUIT:
107 GP_BackendExit(backend);
108 return 0;
109 break;
111 break;
115 GP_BackendExit(backend);
117 return 0;