1 | /*
|
---|
2 | * Copyright (c) 2019 Jiri Svoboda
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup gfxdemo
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file Graphic demo
|
---|
33 | */
|
---|
34 |
|
---|
35 | #include <canvas.h>
|
---|
36 | #include <congfx/console.h>
|
---|
37 | #include <draw/surface.h>
|
---|
38 | #include <display.h>
|
---|
39 | #include <fibril.h>
|
---|
40 | #include <guigfx/canvas.h>
|
---|
41 | #include <gfx/bitmap.h>
|
---|
42 | #include <gfx/color.h>
|
---|
43 | #include <gfx/render.h>
|
---|
44 | #include <io/console.h>
|
---|
45 | #include <io/pixelmap.h>
|
---|
46 | #include <stdlib.h>
|
---|
47 | #include <str.h>
|
---|
48 | #include <window.h>
|
---|
49 |
|
---|
50 | /** Run rectangle demo on a graphic context.
|
---|
51 | *
|
---|
52 | * @param gc Graphic context
|
---|
53 | * @param w Width
|
---|
54 | * @param h Height
|
---|
55 | */
|
---|
56 | static errno_t demo_rects(gfx_context_t *gc, gfx_coord_t w, gfx_coord_t h)
|
---|
57 | {
|
---|
58 | gfx_color_t *color = NULL;
|
---|
59 | gfx_rect_t rect;
|
---|
60 | int i, j;
|
---|
61 | errno_t rc;
|
---|
62 |
|
---|
63 | for (j = 0; j < 10; j++) {
|
---|
64 | rc = gfx_color_new_rgb_i16(rand() % 0x10000, rand() % 0x10000,
|
---|
65 | rand() % 0x10000, &color);
|
---|
66 | if (rc != EOK)
|
---|
67 | return rc;
|
---|
68 |
|
---|
69 | rc = gfx_set_color(gc, color);
|
---|
70 | if (rc != EOK)
|
---|
71 | return rc;
|
---|
72 |
|
---|
73 | for (i = 0; i < 10; i++) {
|
---|
74 | rect.p0.x = rand() % (w - 1);
|
---|
75 | rect.p0.y = rand() % (h - 1);
|
---|
76 | rect.p1.x = rect.p0.x + rand() % (w - 1 - rect.p0.x);
|
---|
77 | rect.p1.y = rect.p0.y + rand() % (h - 1 - rect.p0.y);
|
---|
78 |
|
---|
79 | rc = gfx_fill_rect(gc, &rect);
|
---|
80 | if (rc != EOK)
|
---|
81 | return rc;
|
---|
82 | }
|
---|
83 |
|
---|
84 | gfx_color_delete(color);
|
---|
85 |
|
---|
86 | fibril_usleep(500 * 1000);
|
---|
87 | }
|
---|
88 |
|
---|
89 | return EOK;
|
---|
90 | }
|
---|
91 |
|
---|
92 | /** Run bitmap demo on a graphic context.
|
---|
93 | *
|
---|
94 | * @param gc Graphic context
|
---|
95 | * @param w Width
|
---|
96 | * @param h Height
|
---|
97 | */
|
---|
98 | static errno_t demo_bitmap(gfx_context_t *gc, gfx_coord_t w, gfx_coord_t h)
|
---|
99 | {
|
---|
100 | gfx_bitmap_t *bitmap;
|
---|
101 | gfx_bitmap_params_t params;
|
---|
102 | gfx_bitmap_alloc_t alloc;
|
---|
103 | int i, j;
|
---|
104 | gfx_coord2_t offs;
|
---|
105 | gfx_rect_t srect;
|
---|
106 | errno_t rc;
|
---|
107 | pixelmap_t pixelmap;
|
---|
108 |
|
---|
109 | params.rect.p0.x = 0;
|
---|
110 | params.rect.p0.y = 0;
|
---|
111 | params.rect.p1.x = w;
|
---|
112 | params.rect.p1.y = h;
|
---|
113 |
|
---|
114 | rc = gfx_bitmap_create(gc, ¶ms, NULL, &bitmap);
|
---|
115 | if (rc != EOK)
|
---|
116 | return rc;
|
---|
117 |
|
---|
118 | rc = gfx_bitmap_get_alloc(bitmap, &alloc);
|
---|
119 | if (rc != EOK)
|
---|
120 | return rc;
|
---|
121 |
|
---|
122 | /* Fill bitmap with something. In absence of anything else, use pixelmap */
|
---|
123 | pixelmap.width = w;
|
---|
124 | pixelmap.height = h;
|
---|
125 | pixelmap.data = alloc.pixels;
|
---|
126 |
|
---|
127 | for (i = 0; i < w; i++) {
|
---|
128 | for (j = 0; j < h; j++) {
|
---|
129 | pixelmap_put_pixel(&pixelmap, i, j,
|
---|
130 | PIXEL(255, (i % 30) < 3 ? 255 : 0,
|
---|
131 | (j % 30) < 3 ? 255 : 0, i / 2));
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 | for (j = 0; j < 10; j++) {
|
---|
136 | for (i = 0; i < 5; i++) {
|
---|
137 | srect.p0.x = rand() % (w - 40);
|
---|
138 | srect.p0.y = rand() % (h - 20);
|
---|
139 | srect.p1.x = srect.p0.x + rand() % (w - srect.p0.x);
|
---|
140 | srect.p1.y = srect.p0.y + rand() % (h - srect.p0.y);
|
---|
141 | offs.x = rand() % (w - srect.p1.x);
|
---|
142 | offs.y = rand() % (h - srect.p1.y);
|
---|
143 | offs.x = 0;
|
---|
144 | offs.y = 0;
|
---|
145 |
|
---|
146 | rc = gfx_bitmap_render(bitmap, &srect, &offs);
|
---|
147 | if (rc != EOK)
|
---|
148 | goto error;
|
---|
149 | fibril_usleep(500 * 1000);
|
---|
150 | }
|
---|
151 | }
|
---|
152 |
|
---|
153 | gfx_bitmap_destroy(bitmap);
|
---|
154 |
|
---|
155 | return EOK;
|
---|
156 | error:
|
---|
157 | gfx_bitmap_destroy(bitmap);
|
---|
158 | return rc;
|
---|
159 | }
|
---|
160 |
|
---|
161 | /** Run demo loop on a graphic context.
|
---|
162 | *
|
---|
163 | * @param gc Graphic context
|
---|
164 | * @param w Width
|
---|
165 | * @param h Height
|
---|
166 | */
|
---|
167 | static errno_t demo_loop(gfx_context_t *gc, gfx_coord_t w, gfx_coord_t h)
|
---|
168 | {
|
---|
169 | errno_t rc;
|
---|
170 |
|
---|
171 | while (true) {
|
---|
172 | rc = demo_rects(gc, w, h);
|
---|
173 | if (rc != EOK)
|
---|
174 | return rc;
|
---|
175 |
|
---|
176 | rc = demo_bitmap(gc, w, h);
|
---|
177 | if (rc != EOK)
|
---|
178 | return rc;
|
---|
179 | }
|
---|
180 | }
|
---|
181 |
|
---|
182 | /** Run demo on console. */
|
---|
183 | static errno_t demo_console(void)
|
---|
184 | {
|
---|
185 | console_ctrl_t *con = NULL;
|
---|
186 | console_gc_t *cgc = NULL;
|
---|
187 | gfx_context_t *gc;
|
---|
188 | errno_t rc;
|
---|
189 |
|
---|
190 | printf("Init console..\n");
|
---|
191 | con = console_init(stdin, stdout);
|
---|
192 | if (con == NULL)
|
---|
193 | return EIO;
|
---|
194 |
|
---|
195 | printf("Create console GC\n");
|
---|
196 | rc = console_gc_create(con, stdout, &cgc);
|
---|
197 | if (rc != EOK)
|
---|
198 | return rc;
|
---|
199 |
|
---|
200 | gc = console_gc_get_ctx(cgc);
|
---|
201 |
|
---|
202 | rc = demo_loop(gc, 80, 25);
|
---|
203 | if (rc != EOK)
|
---|
204 | return rc;
|
---|
205 |
|
---|
206 | rc = console_gc_delete(cgc);
|
---|
207 | if (rc != EOK)
|
---|
208 | return rc;
|
---|
209 |
|
---|
210 | return EOK;
|
---|
211 | }
|
---|
212 |
|
---|
213 | /** Run demo on canvas. */
|
---|
214 | static errno_t demo_canvas(void)
|
---|
215 | {
|
---|
216 | canvas_gc_t *cgc = NULL;
|
---|
217 | gfx_context_t *gc;
|
---|
218 | window_t *window = NULL;
|
---|
219 | pixel_t *pixbuf = NULL;
|
---|
220 | surface_t *surface = NULL;
|
---|
221 | canvas_t *canvas = NULL;
|
---|
222 | gfx_coord_t vw, vh;
|
---|
223 | errno_t rc;
|
---|
224 |
|
---|
225 | printf("Init canvas..\n");
|
---|
226 |
|
---|
227 | window = window_open("comp:0/winreg", NULL,
|
---|
228 | WINDOW_MAIN | WINDOW_DECORATED, "GFX Demo");
|
---|
229 | if (window == NULL) {
|
---|
230 | printf("Error creating window.\n");
|
---|
231 | return -1;
|
---|
232 | }
|
---|
233 |
|
---|
234 | vw = 400;
|
---|
235 | vh = 300;
|
---|
236 |
|
---|
237 | pixbuf = calloc(vw * vh, sizeof(pixel_t));
|
---|
238 | if (pixbuf == NULL) {
|
---|
239 | printf("Error allocating memory for pixel buffer.\n");
|
---|
240 | return ENOMEM;
|
---|
241 | }
|
---|
242 |
|
---|
243 | surface = surface_create(vw, vh, pixbuf, 0);
|
---|
244 | if (surface == NULL) {
|
---|
245 | printf("Error creating surface.\n");
|
---|
246 | return EIO;
|
---|
247 | }
|
---|
248 |
|
---|
249 | canvas = create_canvas(window_root(window), NULL, vw, vh,
|
---|
250 | surface);
|
---|
251 | if (canvas == NULL) {
|
---|
252 | printf("Error creating canvas.\n");
|
---|
253 | return EIO;
|
---|
254 | }
|
---|
255 |
|
---|
256 | window_resize(window, 0, 0, vw + 10, vh + 30, WINDOW_PLACEMENT_ANY);
|
---|
257 | window_exec(window);
|
---|
258 |
|
---|
259 | printf("Create canvas GC\n");
|
---|
260 | rc = canvas_gc_create(canvas, surface, &cgc);
|
---|
261 | if (rc != EOK)
|
---|
262 | return rc;
|
---|
263 |
|
---|
264 | gc = canvas_gc_get_ctx(cgc);
|
---|
265 |
|
---|
266 | rc = demo_loop(gc, 400, 300);
|
---|
267 | if (rc != EOK)
|
---|
268 | return rc;
|
---|
269 |
|
---|
270 | rc = canvas_gc_delete(cgc);
|
---|
271 | if (rc != EOK)
|
---|
272 | return rc;
|
---|
273 |
|
---|
274 | return EOK;
|
---|
275 | }
|
---|
276 |
|
---|
277 | /** Run demo on display server. */
|
---|
278 | static errno_t demo_display(void)
|
---|
279 | {
|
---|
280 | display_t *display = NULL;
|
---|
281 | gfx_context_t *gc;
|
---|
282 | display_window_t *window = NULL;
|
---|
283 | errno_t rc;
|
---|
284 |
|
---|
285 | printf("Init display..\n");
|
---|
286 |
|
---|
287 | rc = display_open(NULL, &display);
|
---|
288 | if (rc != EOK) {
|
---|
289 | printf("Error opening display.\n");
|
---|
290 | return rc;
|
---|
291 | }
|
---|
292 |
|
---|
293 | rc = display_window_create(display, &window);
|
---|
294 | if (rc != EOK) {
|
---|
295 | printf("Error creating window.\n");
|
---|
296 | return rc;
|
---|
297 | }
|
---|
298 |
|
---|
299 | rc = display_window_get_gc(window, &gc);
|
---|
300 | if (rc != EOK) {
|
---|
301 | printf("Error getting graphics context.\n");
|
---|
302 | }
|
---|
303 |
|
---|
304 | rc = demo_loop(gc, 400, 300);
|
---|
305 | if (rc != EOK)
|
---|
306 | return rc;
|
---|
307 |
|
---|
308 | rc = gfx_context_delete(gc);
|
---|
309 | if (rc != EOK)
|
---|
310 | return rc;
|
---|
311 |
|
---|
312 | return EOK;
|
---|
313 | }
|
---|
314 |
|
---|
315 | static void print_syntax(void)
|
---|
316 | {
|
---|
317 | printf("syntax: gfxdemo {canvas|console|display}\n");
|
---|
318 | }
|
---|
319 |
|
---|
320 | int main(int argc, char *argv[])
|
---|
321 | {
|
---|
322 | errno_t rc;
|
---|
323 |
|
---|
324 | if (argc < 2) {
|
---|
325 | print_syntax();
|
---|
326 | return 1;
|
---|
327 | }
|
---|
328 |
|
---|
329 | if (str_cmp(argv[1], "console") == 0) {
|
---|
330 | rc = demo_console();
|
---|
331 | if (rc != EOK)
|
---|
332 | return 1;
|
---|
333 | } else if (str_cmp(argv[1], "canvas") == 0) {
|
---|
334 | rc = demo_canvas();
|
---|
335 | if (rc != EOK)
|
---|
336 | return 1;
|
---|
337 | } else if (str_cmp(argv[1], "display") == 0) {
|
---|
338 | rc = demo_display();
|
---|
339 | if (rc != EOK)
|
---|
340 | return 1;
|
---|
341 | } else {
|
---|
342 | print_syntax();
|
---|
343 | return 1;
|
---|
344 | }
|
---|
345 | }
|
---|
346 |
|
---|
347 | /** @}
|
---|
348 | */
|
---|