Rename GP_Context -> GP_Pixmap
[gfxprim.git] / demos / c_simple / backend_example.c
blob6c828834787139f87661f55b18246a95999fa221
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 example.
29 #include <stdio.h>
30 #include <GP.h>
32 static void redraw(GP_Backend *self)
34 GP_Pixmap *pixmap = self->pixmap;
35 GP_Pixel white_pixel, black_pixel;
37 black_pixel = GP_RGBToPixmapPixel(0x00, 0x00, 0x00, pixmap);
38 white_pixel = GP_RGBToPixmapPixel(0xff, 0xff, 0xff, pixmap);
40 GP_Fill(pixmap, black_pixel);
41 GP_Line(pixmap, 0, 0, pixmap->w - 1, pixmap->h - 1, white_pixel);
42 GP_Line(pixmap, 0, pixmap->h - 1, pixmap->w - 1, 0, white_pixel);
44 /* Update the backend screen */
45 GP_BackendFlip(self);
48 int main(int argc, char *argv[])
50 GP_Backend *backend;
51 const char *backend_opts = "X11:100x100";
52 int opt;
54 while ((opt = getopt(argc, argv, "b:h")) != -1) {
55 switch (opt) {
56 case 'b':
57 backend_opts = optarg;
58 break;
59 case 'h':
60 GP_BackendInit(NULL, NULL);
61 return 0;
62 break;
63 default:
64 fprintf(stderr, "Invalid paramter '%c'\n", opt);
65 return 1;
69 backend = GP_BackendInit(backend_opts, "Backend Example");
71 if (backend == NULL) {
72 fprintf(stderr, "Failed to initialize backend\n");
73 return 1;
76 redraw(backend);
78 /* Handle events */
79 for (;;) {
80 GP_Event ev;
82 GP_BackendWaitEvent(backend, &ev);
84 GP_EventDump(&ev);
86 switch (ev.type) {
87 case GP_EV_KEY:
88 switch (ev.val.val) {
89 case GP_KEY_ESC:
90 case GP_KEY_Q:
91 GP_BackendExit(backend);
92 return 0;
93 break;
95 break;
96 case GP_EV_SYS:
97 switch (ev.code) {
98 case GP_EV_SYS_RESIZE:
99 GP_BackendResizeAck(backend);
100 redraw(backend);
101 break;
102 case GP_EV_SYS_QUIT:
103 GP_BackendExit(backend);
104 return 0;
105 break;
107 break;
111 GP_BackendExit(backend);
113 return 0;