core: Get rid of GP_Color.
[gfxprim.git] / demos / c_simple / virtual_backend_example.c
blob5ef5a8efc9e38e53a499113d0d8703dbd27ae516
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 int main(int argc, char *argv[])
36 GP_Backend *backend;
37 GP_Context *context;
38 GP_Pixel white_pixel, black_pixel, red_pixel, blue_pixel, green_pixel;
39 const char *backend_opts = "X11:350x350";
40 int opt;
41 GP_PixelType emul_type = GP_PIXEL_UNKNOWN;
43 while ((opt = getopt(argc, argv, "b:h:p:")) != -1) {
44 switch (opt) {
45 case 'b':
46 backend_opts = optarg;
47 break;
48 case 'p':
49 emul_type = GP_PixelTypeByName(optarg);
51 if (emul_type == GP_PIXEL_UNKNOWN) {
52 fprintf(stderr, "Invalid pixel type '%s'\n", optarg);
53 return 1;
55 break;
56 case 'h':
57 GP_BackendInit("help", NULL);
58 return 0;
59 break;
60 default:
61 fprintf(stderr, "Invalid paramter '%c'\n", opt);
62 return 1;
66 /* Turn on debug messages */
67 GP_SetDebugLevel(10);
69 backend = GP_BackendInit(backend_opts, "Virtual Backend Example");
71 if (emul_type != GP_PIXEL_UNKNOWN) {
72 GP_Backend *emul;
75 * Create an emulated backend on the top of real backend.
77 * The GP_BACKEND_CALL_EXIT says that when calling exit on
78 * emulated backend, the real backend exit will be called as
79 * well.
81 emul = GP_BackendVirtualInit(backend, emul_type, GP_BACKEND_CALL_EXIT);
83 if (emul == NULL) {
84 fprintf(stderr, "Failed to create Virtual Backend\n");
85 GP_BackendExit(backend);
86 return 1;
89 /* Once created virtual backend behaves exactly like a real one */
90 backend = emul;
93 context = backend->context;
95 /* Now draw some testing patters */
96 black_pixel = GP_RGBToContextPixel(0x00, 0x00, 0x00, context);
97 white_pixel = GP_RGBToContextPixel(0xff, 0xff, 0xff, context);
98 red_pixel = GP_RGBToContextPixel(0xff, 0x00, 0x00, context);
99 blue_pixel = GP_RGBToContextPixel(0x00, 0x00, 0xff, context);
100 green_pixel = GP_RGBToContextPixel(0x00, 0xff, 0x00, context);
102 GP_Fill(context, white_pixel);
104 unsigned int i, j;
105 for (i = 0; i < 40; i++) {
106 GP_HLineXYW(context, 0, i, i, black_pixel);
107 GP_HLineXYW(context, 1, i + 40, i, black_pixel);
108 GP_HLineXYW(context, 2, i + 80, i, black_pixel);
109 GP_HLineXYW(context, 3, i + 120, i, black_pixel);
110 GP_HLineXYW(context, 4, i + 160, i, black_pixel);
111 GP_HLineXYW(context, 5, i + 200, i, black_pixel);
112 GP_HLineXYW(context, 6, i + 240, i, black_pixel);
113 GP_HLineXYW(context, 7, i + 280, i, black_pixel);
116 for (i = 0; i < 256; i++) {
117 for (j = 0; j < 256; j++) {
118 uint8_t val = 1.00 * sqrt(i*i + j*j)/sqrt(2) + 0.5;
120 GP_Pixel pix = GP_RGBToContextPixel(i, j, val, context);
121 GP_PutPixel(context, i + 60, j + 10, pix);
125 GP_Text(context, NULL, 60, 270, GP_VALIGN_BELOW|GP_ALIGN_RIGHT,
126 black_pixel, white_pixel, "Lorem Ipsum dolor sit...");
128 GP_Text(context, NULL, 60, 290, GP_VALIGN_BELOW|GP_ALIGN_RIGHT,
129 red_pixel, white_pixel, "Lorem Ipsum dolor sit...");
131 GP_Text(context, NULL, 60, 310, GP_VALIGN_BELOW|GP_ALIGN_RIGHT,
132 green_pixel, white_pixel, "Lorem Ipsum dolor sit...");
134 GP_Text(context, NULL, 60, 330, GP_VALIGN_BELOW|GP_ALIGN_RIGHT,
135 blue_pixel, white_pixel, "Lorem Ipsum dolor sit...");
137 /* Update the backend screen */
138 GP_BackendFlip(backend);
140 for (;;) {
141 if (backend->Poll)
142 GP_BackendPoll(backend);
144 usleep(1000);
146 /* Read and parse events */
147 GP_Event ev;
149 while (GP_BackendGetEvent(backend, &ev)) {
151 GP_EventDump(&ev);
153 switch (ev.type) {
154 case GP_EV_KEY:
155 switch (ev.val.key.key) {
156 case GP_KEY_ESC:
157 case GP_KEY_Q:
158 GP_BackendExit(backend);
159 return 0;
160 break;
166 GP_BackendExit(backend);
168 return 0;