Rename GP_Context -> GP_Pixmap
[gfxprim.git] / demos / c_simple / virtual_backend_example.c
blobb1d8199f5b89c7b6d3f1233ce491877a24bfb4cb
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 virtual backend test.
27 Virtual backned allows you to test interactively pixel types that your
28 hardware/xserver doesn't support.
32 #include <GP.h>
34 static GP_Pixel white_pixel, black_pixel, red_pixel, blue_pixel, green_pixel;
36 static void redraw(GP_Backend *backend)
38 GP_Pixmap *pixmap = backend->pixmap;
40 /* Now draw some testing patters */
41 black_pixel = GP_RGBToPixmapPixel(0x00, 0x00, 0x00, pixmap);
42 white_pixel = GP_RGBToPixmapPixel(0xff, 0xff, 0xff, pixmap);
43 red_pixel = GP_RGBToPixmapPixel(0xff, 0x00, 0x00, pixmap);
44 blue_pixel = GP_RGBToPixmapPixel(0x00, 0x00, 0xff, pixmap);
45 green_pixel = GP_RGBToPixmapPixel(0x00, 0xff, 0x00, pixmap);
47 GP_Fill(pixmap, white_pixel);
49 unsigned int i, j;
50 for (i = 0; i < 40; i++) {
51 GP_HLineXYW(pixmap, 0, i, i, black_pixel);
52 GP_HLineXYW(pixmap, 1, i + 40, i, black_pixel);
53 GP_HLineXYW(pixmap, 2, i + 80, i, black_pixel);
54 GP_HLineXYW(pixmap, 3, i + 120, i, black_pixel);
55 GP_HLineXYW(pixmap, 4, i + 160, i, black_pixel);
56 GP_HLineXYW(pixmap, 5, i + 200, i, black_pixel);
57 GP_HLineXYW(pixmap, 6, i + 240, i, black_pixel);
58 GP_HLineXYW(pixmap, 7, i + 280, i, black_pixel);
61 for (i = 0; i < 256; i++) {
62 for (j = 0; j < 256; j++) {
63 uint8_t val = 1.00 * sqrt(i*i + j*j)/sqrt(2) + 0.5;
65 GP_Pixel pix = GP_RGBToPixmapPixel(i, j, val, pixmap);
66 GP_PutPixel(pixmap, i + 60, j + 10, pix);
70 GP_Text(pixmap, NULL, 60, 270, GP_VALIGN_BELOW|GP_ALIGN_RIGHT,
71 black_pixel, white_pixel, "Lorem Ipsum dolor sit...");
73 GP_Text(pixmap, NULL, 60, 290, GP_VALIGN_BELOW|GP_ALIGN_RIGHT,
74 red_pixel, white_pixel, "Lorem Ipsum dolor sit...");
76 GP_Text(pixmap, NULL, 60, 310, GP_VALIGN_BELOW|GP_ALIGN_RIGHT,
77 green_pixel, white_pixel, "Lorem Ipsum dolor sit...");
79 GP_Text(pixmap, NULL, 60, 330, GP_VALIGN_BELOW|GP_ALIGN_RIGHT,
80 blue_pixel, white_pixel, "Lorem Ipsum dolor sit...");
82 /* Update the backend screen */
83 GP_BackendFlip(backend);
86 int main(int argc, char *argv[])
88 GP_Backend *backend;
89 const char *backend_opts = "X11:350x350";
90 int opt;
91 GP_PixelType emul_type = GP_PIXEL_UNKNOWN;
93 while ((opt = getopt(argc, argv, "b:h:p:")) != -1) {
94 switch (opt) {
95 case 'b':
96 backend_opts = optarg;
97 break;
98 case 'p':
99 emul_type = GP_PixelTypeByName(optarg);
101 if (emul_type == GP_PIXEL_UNKNOWN) {
102 fprintf(stderr, "Invalid pixel type '%s'\n", optarg);
103 return 1;
105 break;
106 case 'h':
107 GP_BackendInit("help", NULL);
108 return 0;
109 break;
110 default:
111 fprintf(stderr, "Invalid paramter '%c'\n", opt);
112 return 1;
116 /* Turn on debug messages */
117 GP_SetDebugLevel(10);
119 backend = GP_BackendInit(backend_opts, "Virtual Backend Example");
121 if (emul_type != GP_PIXEL_UNKNOWN) {
122 GP_Backend *emul;
125 * Create an emulated backend on the top of real backend.
127 * The GP_BACKEND_CALL_EXIT says that when calling exit on
128 * emulated backend, the real backend exit will be called as
129 * well.
131 emul = GP_BackendVirtualInit(backend, emul_type, GP_BACKEND_CALL_EXIT);
133 if (emul == NULL) {
134 fprintf(stderr, "Failed to create Virtual Backend\n");
135 GP_BackendExit(backend);
136 return 1;
139 /* Once created virtual backend behaves exactly like a real one */
140 backend = emul;
143 redraw(backend);
145 for (;;) {
146 if (backend->Poll)
147 GP_BackendPoll(backend);
149 usleep(1000);
151 /* Read and parse events */
152 GP_Event ev;
154 while (GP_BackendGetEvent(backend, &ev)) {
156 GP_EventDump(&ev);
158 switch (ev.type) {
159 case GP_EV_KEY:
160 switch (ev.val.key.key) {
161 case GP_KEY_ESC:
162 case GP_KEY_Q:
163 GP_BackendExit(backend);
164 return 0;
165 break;
167 break;
168 case GP_EV_SYS:
169 switch(ev.code) {
170 case GP_EV_SYS_RESIZE:
171 GP_BackendResizeAck(backend);
172 redraw(backend);
173 break;
175 break;
180 GP_BackendExit(backend);
182 return 0;