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 <draw/surface.h>
|
---|
37 | #include <fibril.h>
|
---|
38 | #include <gfx/backend/canvas.h>
|
---|
39 | #include <gfx/backend/console.h>
|
---|
40 | #include <gfx/color.h>
|
---|
41 | #include <gfx/render.h>
|
---|
42 | #include <io/console.h>
|
---|
43 | #include <stdlib.h>
|
---|
44 | #include <str.h>
|
---|
45 | #include <window.h>
|
---|
46 |
|
---|
47 | /** Run rectangle demo on a graphic context.
|
---|
48 | *
|
---|
49 | * @param gc Graphic context
|
---|
50 | */
|
---|
51 | static errno_t demo_rects(gfx_context_t *gc, int w, int h)
|
---|
52 | {
|
---|
53 | gfx_color_t *color = NULL;
|
---|
54 | gfx_rect_t rect;
|
---|
55 | int i;
|
---|
56 | errno_t rc;
|
---|
57 |
|
---|
58 | while (true) {
|
---|
59 | rc = gfx_color_new_rgb_i16(rand() % 0x10000, rand() % 0x10000,
|
---|
60 | rand() % 0x10000, &color);
|
---|
61 | if (rc != EOK)
|
---|
62 | return rc;
|
---|
63 |
|
---|
64 | rc = gfx_set_color(gc, color);
|
---|
65 | if (rc != EOK)
|
---|
66 | return rc;
|
---|
67 |
|
---|
68 | for (i = 0; i < 10; i++) {
|
---|
69 | rect.p0.x = rand() % (w - 1);
|
---|
70 | rect.p0.y = rand() % (h - 1);
|
---|
71 | rect.p1.x = rect.p0.x + rand() % (w - 1 - rect.p0.x);
|
---|
72 | rect.p1.y = rect.p0.y + rand() % (h - 1 - rect.p0.y);
|
---|
73 |
|
---|
74 | rc = gfx_fill_rect(gc, &rect);
|
---|
75 | if (rc != EOK)
|
---|
76 | return rc;
|
---|
77 | }
|
---|
78 |
|
---|
79 | gfx_color_delete(color);
|
---|
80 |
|
---|
81 | fibril_usleep(500 * 1000);
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | /** Run demo on console. */
|
---|
86 | static errno_t demo_console(void)
|
---|
87 | {
|
---|
88 | console_ctrl_t *con = NULL;
|
---|
89 | console_gc_t *cgc = NULL;
|
---|
90 | gfx_context_t *gc;
|
---|
91 | errno_t rc;
|
---|
92 |
|
---|
93 | printf("Init console..\n");
|
---|
94 | con = console_init(stdin, stdout);
|
---|
95 | if (con == NULL)
|
---|
96 | return EIO;
|
---|
97 |
|
---|
98 | printf("Create console GC\n");
|
---|
99 | rc = console_gc_create(con, stdout, &cgc);
|
---|
100 | if (rc != EOK)
|
---|
101 | return rc;
|
---|
102 |
|
---|
103 | gc = console_gc_get_ctx(cgc);
|
---|
104 |
|
---|
105 | rc = demo_rects(gc, 80, 25);
|
---|
106 | if (rc != EOK)
|
---|
107 | return rc;
|
---|
108 |
|
---|
109 | rc = console_gc_delete(cgc);
|
---|
110 | if (rc != EOK)
|
---|
111 | return rc;
|
---|
112 |
|
---|
113 | return EOK;
|
---|
114 | }
|
---|
115 |
|
---|
116 | /** Run demo on canvas. */
|
---|
117 | static errno_t demo_canvas(void)
|
---|
118 | {
|
---|
119 | console_ctrl_t *con = NULL;
|
---|
120 | canvas_gc_t *cgc = NULL;
|
---|
121 | gfx_context_t *gc;
|
---|
122 | window_t *window = NULL;
|
---|
123 | pixel_t *pixbuf = NULL;
|
---|
124 | surface_t *surface = NULL;
|
---|
125 | canvas_t *canvas = NULL;
|
---|
126 | int vw, vh;
|
---|
127 | errno_t rc;
|
---|
128 |
|
---|
129 | printf("Init canvas..\n");
|
---|
130 | con = console_init(stdin, stdout);
|
---|
131 | if (con == NULL)
|
---|
132 | return EIO;
|
---|
133 |
|
---|
134 | window = window_open("comp:0/winreg", NULL,
|
---|
135 | WINDOW_MAIN | WINDOW_DECORATED, "GFX Demo");
|
---|
136 | if (window == NULL) {
|
---|
137 | printf("Error creating window.\n");
|
---|
138 | return -1;
|
---|
139 | }
|
---|
140 |
|
---|
141 | vw = 400;
|
---|
142 | vh = 300;
|
---|
143 |
|
---|
144 | pixbuf = calloc(vw * vh, sizeof(pixel_t));
|
---|
145 | if (pixbuf == NULL) {
|
---|
146 | printf("Error allocating memory for pixel buffer.\n");
|
---|
147 | return -1;
|
---|
148 | }
|
---|
149 |
|
---|
150 | surface = surface_create(vw, vh, pixbuf, 0);
|
---|
151 | if (surface == NULL) {
|
---|
152 | printf("Error creating surface.\n");
|
---|
153 | return -1;
|
---|
154 | }
|
---|
155 |
|
---|
156 | canvas = create_canvas(window_root(window), NULL, vw, vh,
|
---|
157 | surface);
|
---|
158 | if (canvas == NULL) {
|
---|
159 | printf("Error creating canvas.\n");
|
---|
160 | return -1;
|
---|
161 | }
|
---|
162 |
|
---|
163 | // sig_connect(&canvas->keyboard_event, NULL, wnd_keyboard_event);
|
---|
164 |
|
---|
165 | window_resize(window, 0, 0, vw + 10, vh + 30, WINDOW_PLACEMENT_ANY);
|
---|
166 | window_exec(window);
|
---|
167 |
|
---|
168 | printf("Create canvas GC\n");
|
---|
169 | rc = canvas_gc_create(canvas, surface, &cgc);
|
---|
170 | if (rc != EOK)
|
---|
171 | return rc;
|
---|
172 |
|
---|
173 | gc = canvas_gc_get_ctx(cgc);
|
---|
174 |
|
---|
175 | rc = demo_rects(gc, 400, 300);
|
---|
176 | if (rc != EOK)
|
---|
177 | return rc;
|
---|
178 |
|
---|
179 | rc = canvas_gc_delete(cgc);
|
---|
180 | if (rc != EOK)
|
---|
181 | return rc;
|
---|
182 |
|
---|
183 | return EOK;
|
---|
184 | }
|
---|
185 |
|
---|
186 | static void print_syntax(void)
|
---|
187 | {
|
---|
188 | printf("syntax: gfxdemo {canvas|console}\n");
|
---|
189 | }
|
---|
190 |
|
---|
191 | int main(int argc, char *argv[])
|
---|
192 | {
|
---|
193 | errno_t rc;
|
---|
194 |
|
---|
195 | if (argc < 2) {
|
---|
196 | print_syntax();
|
---|
197 | return 1;
|
---|
198 | }
|
---|
199 |
|
---|
200 | if (str_cmp(argv[1], "console") == 0) {
|
---|
201 | rc = demo_console();
|
---|
202 | if (rc != EOK)
|
---|
203 | return 1;
|
---|
204 | } else if (str_cmp(argv[1], "canvas") == 0) {
|
---|
205 | rc = demo_canvas();
|
---|
206 | if (rc != EOK)
|
---|
207 | return 1;
|
---|
208 | } else {
|
---|
209 | print_syntax();
|
---|
210 | return 1;
|
---|
211 | }
|
---|
212 | }
|
---|
213 |
|
---|
214 | /** @}
|
---|
215 | */
|
---|