Rename GP_Context -> GP_Pixmap
[gfxprim.git] / demos / c_simple / input_example.c
blob5ac76d302a1fc1dc2cce60af22ca06e20393bfb9
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-2010 Jiri "BlueBear" Dluhos *
20 * <jiri.bluebear.dluhos@gmail.com> *
21 * *
22 * Copyright (C) 2009-2013 Cyril Hrubis <metan@ucw.cz> *
23 * *
24 *****************************************************************************/
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <unistd.h>
30 #include "GP.h"
32 static GP_Pixmap *win;
33 static GP_Backend *backend;
35 static GP_Pixel red, green, white, black;
37 static void draw_event(GP_Event *ev)
39 static GP_Size size = 0;
41 if (ev->type != GP_EV_KEY)
42 return;
44 int align = GP_ALIGN_RIGHT|GP_VALIGN_BOTTOM;
46 GP_TextClear(win, NULL, 20, 20, align, black, size);
47 size = GP_Print(win, NULL, 20, 20, align,
48 white, black, "Key=%s",
49 GP_EventKeyName(ev->val.key.key));
51 GP_BackendFlip(backend);
54 static void event_loop(void)
56 for (;;) {
57 GP_BackendWait(backend);
59 while (GP_BackendEventsQueued(backend)) {
60 GP_Event ev;
62 GP_BackendGetEvent(backend, &ev);
63 GP_EventDump(&ev);
65 switch (ev.type) {
66 case GP_EV_KEY:
67 draw_event(&ev);
69 switch (ev.val.key.key) {
70 case GP_KEY_ESC:
71 GP_BackendExit(backend);
72 exit(0);
73 break;
74 case GP_BTN_LEFT:
75 GP_HLineXXY(win, ev.cursor_x - 3,
76 ev.cursor_x + 3,
77 ev.cursor_y, red);
78 GP_VLineXYY(win, ev.cursor_x,
79 ev.cursor_y - 3,
80 ev.cursor_y + 3, red);
81 GP_BackendFlip(backend);
82 break;
83 default:
84 break;
86 break;
87 case GP_EV_REL:
88 switch (ev.code) {
89 static int size = 0;
90 case GP_EV_REL_POS:
91 if (GP_EventGetKey(&ev, GP_BTN_LEFT)) {
92 GP_PutPixel(win, ev.cursor_x,
93 ev.cursor_y, green);
95 int align = GP_ALIGN_RIGHT|GP_VALIGN_BOTTOM;
97 GP_TextClear(win, NULL, 20, 40, align,
98 black, size);
99 size = GP_Print(win, NULL, 20, 40, align,
100 white, black, "X=%3u Y=%3u dX=%3i dY=%3i",
101 ev.cursor_x, ev.cursor_y,
102 ev.val.rel.rx, ev.val.rel.ry);
103 GP_BackendFlip(backend);
104 break;
106 break;
107 case GP_EV_SYS:
108 switch (ev.code) {
109 case GP_EV_SYS_RESIZE:
110 GP_BackendResizeAck(backend);
111 break;
112 case GP_EV_SYS_QUIT:
113 GP_BackendExit(backend);
114 exit(0);
115 break;
117 break;
123 int main(int argc, char *argv[])
125 const char *backend_opts = "X11";
126 int opt;
128 while ((opt = getopt(argc, argv, "b:h")) != -1) {
129 switch (opt) {
130 case 'b':
131 backend_opts = optarg;
132 break;
133 case 'h':
134 GP_BackendInit("help", NULL);
135 return 0;
136 break;
137 default:
138 fprintf(stderr, "Invalid paramter '%c'\n", opt);
139 return 1;
143 backend = GP_BackendInit(backend_opts, "Input Test");
145 if (backend == NULL) {
146 fprintf(stderr, "Failed to initalize backend '%s'\n",
147 backend_opts);
148 return 1;
151 win = backend->pixmap;
153 red = GP_RGBToPixmapPixel(0xff, 0x00, 0x00, win);
154 green = GP_RGBToPixmapPixel(0x00, 0xff, 0x00, win);
155 white = GP_RGBToPixmapPixel(0xff, 0xff, 0xff, win);
156 black = GP_RGBToPixmapPixel(0x00, 0x00, 0x00, win);
158 GP_Fill(win, black);
159 GP_BackendFlip(backend);
161 for (;;) {
162 GP_BackendWait(backend);
163 event_loop();