1 /* This is a small demo of the high-performance GUIX graphics framework. */
2
3 #include <stdio.h>
4 #include "gx_api.h"
5
6 #include "guix_shapes_resources.h"
7 #include "guix_shapes_specifications.h"
8
9 GX_WINDOW *pShapesScreen;
10 GX_WINDOW *pTextWindow;
11 GX_WINDOW *pShapesWindow;
12 GX_RESOURCE_ID line_color = GX_COLOR_ID_GRAY;
13 GX_RESOURCE_ID fill_color = GX_COLOR_ID_TEAL;
14 INT radius = 150;
15 GX_BOOL anti_aliased = GX_TRUE;
16 GX_BOOL round_end = GX_TRUE;
17 GX_BOOL solid_fill = GX_TRUE;
18 GX_BOOL pixelmap_fill = GX_FALSE;
19 INT brush_width = 2;
20 INT start_angle = 60;
21 INT end_angle = 90;
22 INT angle_off = 0;
23 INT ellipse_b = 100;
24
25 #define CIRCLE 0
26 #define ARC 1
27 #define PIE 2
28 #define POLYGON 3
29 #define ELLIPSE 4
30 #define RECTANGLE 5
31
32 #define ARC_TIMER 5
33 #define PIE_TIMER 6
34
35 #define ARC_TICKS 2
36 #define PIE_TICKS 10
37
38
39 INT draw_shape = CIRCLE;
40 INT pre_shape;
41
42 /* Define the ThreadX demo thread control block and stack. */
43
44 TX_THREAD demo_thread;
45 UCHAR demo_thread_stack[4096];
46 TX_BYTE_POOL memory_pool;
47 GX_WINDOW_ROOT *root;
48
49 /* Define prototypes. */
50 VOID demo_thread_entry(ULONG thread_input);
51 extern UINT win32_graphics_driver_setup_24xrgb(GX_DISPLAY *display);
52 VOID *memory_allocate(ULONG size);
53 VOID memory_free(VOID *mem);
54 UINT string_length_get(GX_CONST GX_CHAR* input_string, UINT max_string_length);
55
56 /* define some memory for storage of rotated text pixelmap */
57 #define ROTATION_MEMORY_SIZE (DISPLAY_1_X_RESOLUTION * DISPLAY_1_Y_RESOLUTION)
58 GX_UBYTE rotation_memory[ROTATION_MEMORY_SIZE];
59
main(int argc,char ** argv)60 int main(int argc, char ** argv)
61 {
62 tx_kernel_enter();
63 return(0);
64 }
65
memory_allocate(ULONG size)66 VOID *memory_allocate(ULONG size)
67 {
68 VOID *memptr;
69
70 if (tx_byte_allocate(&memory_pool, &memptr, size, TX_NO_WAIT) == TX_SUCCESS)
71 {
72 return memptr;
73 }
74 return NULL;
75 }
76
memory_free(VOID * mem)77 void memory_free(VOID *mem)
78 {
79 tx_byte_release(mem);
80 }
81
82
tx_application_define(void * first_unused_memory)83 VOID tx_application_define(void *first_unused_memory)
84 {
85 /* Create the main demo thread. */
86 tx_thread_create(&demo_thread, "GUIX Demo Thread", demo_thread_entry,
87 0, demo_thread_stack, sizeof(demo_thread_stack),
88 1, 1, TX_NO_TIME_SLICE, TX_AUTO_START);
89 }
90
91
demo_thread_entry(ULONG thread_input)92 VOID demo_thread_entry(ULONG thread_input)
93 {
94 /* create byte pool for needle rotation to use */
95 tx_byte_pool_create(&memory_pool, "scratchpad", rotation_memory,
96 ROTATION_MEMORY_SIZE);
97
98 /* Initialize GUIX. */
99 gx_system_initialize();
100
101 gx_studio_display_configure(DISPLAY_1, win32_graphics_driver_setup_24xrgb,
102 LANGUAGE_ENGLISH, DISPLAY_1_DEFAULT_THEME, &root);
103
104 /* install our memory allocator and de-allocator */
105 gx_system_memory_allocator_set(memory_allocate, memory_free);
106
107 /* create the main screen */
108 gx_studio_named_widget_create("Shapes_Screen", (GX_WIDGET *) root, (GX_WIDGET **) &pShapesScreen);
109
110 /* create the main screen */
111 gx_studio_named_widget_create("Text_Rotaion_Window", GX_NULL, (GX_WIDGET **)&pTextWindow);
112
113 /* get a pointer to polygon window */
114 pShapesWindow = &(((SHAPES_SCREEN_CONTROL_BLOCK *)pShapesScreen)->Shapes_Screen_Shapes_Window);
115
116 /* Show the root window to make it and patients screen visible. */
117 gx_widget_show(root);
118
119 /* let GUIX run */
120 gx_system_start();
121 }
122
update_prompt_value(GX_PIXELMAP_PROMPT * pp,INT value)123 VOID update_prompt_value(GX_PIXELMAP_PROMPT *pp, INT value)
124 {
125 static GX_CHAR str[10];
126 GX_STRING string;
127
128 if(pp)
129 {
130 gx_utility_ltoa(value, str, 10);
131 string.gx_string_ptr = str;
132 string.gx_string_length = string_length_get(str, sizeof(str) - 1);
133 gx_prompt_text_set_ext((GX_PROMPT *) pp, &string);
134 }
135 }
136
ShapesScreen_event_handler(GX_WINDOW * window,GX_EVENT * myevent)137 UINT ShapesScreen_event_handler(GX_WINDOW *window, GX_EVENT *myevent)
138 {
139 UINT status = 0;
140 GX_PIXELMAP_PROMPT *prompt;
141 GX_WINDOW *pGraphicsWin = &(((SHAPES_SCREEN_CONTROL_BLOCK *)pShapesScreen)->Shapes_Screen_graphics_window);
142
143 switch (myevent->gx_event_type)
144 {
145 case GX_EVENT_SHOW:
146 gx_window_event_process(window, myevent);
147 break;
148
149 case GX_SIGNAL(ID_RADIUS_SLIDER, GX_EVENT_SLIDER_VALUE):
150 radius = myevent->gx_event_payload.gx_event_longdata;
151 prompt = &(((SHAPES_SCREEN_CONTROL_BLOCK *)pShapesScreen)->Shapes_Screen_radius_val);
152 update_prompt_value(prompt, radius);
153 gx_system_dirty_mark(pGraphicsWin);
154 break;
155
156 case GX_SIGNAL(ID_BRUSH_WIDTH, GX_EVENT_SLIDER_VALUE):
157 brush_width = myevent->gx_event_payload.gx_event_longdata;
158 prompt = &(((SHAPES_SCREEN_CONTROL_BLOCK *)pShapesScreen)->Shapes_Screen_brush_width_val);
159 update_prompt_value(prompt, brush_width);
160 gx_system_dirty_mark(pGraphicsWin);
161 break;
162
163 case GX_SIGNAL(ID_ANTI_ALIASED, GX_EVENT_TOGGLE_ON):
164 anti_aliased = GX_TRUE;
165 gx_system_dirty_mark(pGraphicsWin);
166 break;
167
168 case GX_SIGNAL(ID_ANTI_ALIASED, GX_EVENT_TOGGLE_OFF):
169 anti_aliased = GX_FALSE;
170 gx_system_dirty_mark(pGraphicsWin);
171 break;
172
173 case GX_SIGNAL(ID_ROUND_END, GX_EVENT_TOGGLE_ON):
174 round_end = GX_TRUE;
175 gx_system_dirty_mark(pGraphicsWin);
176 break;
177
178 case GX_SIGNAL(ID_ROUND_END, GX_EVENT_TOGGLE_OFF):
179 round_end = GX_FALSE;
180 gx_system_dirty_mark(pGraphicsWin);
181 break;
182
183 case GX_SIGNAL(ID_SOLID_FILL, GX_EVENT_TOGGLE_ON):
184 solid_fill = GX_TRUE;
185 gx_system_dirty_mark(pGraphicsWin);
186 break;
187
188 case GX_SIGNAL(ID_SOLID_FILL, GX_EVENT_TOGGLE_OFF):
189 solid_fill = GX_FALSE;
190 gx_system_dirty_mark(pGraphicsWin);
191 break;
192
193 case GX_SIGNAL(ID_PIXELMAP_FILL, GX_EVENT_TOGGLE_ON):
194 pixelmap_fill = GX_TRUE;
195 gx_system_dirty_mark(pGraphicsWin);
196 break;
197
198 case GX_SIGNAL(ID_PIXELMAP_FILL, GX_EVENT_TOGGLE_OFF):
199 pixelmap_fill = GX_FALSE;
200 gx_system_dirty_mark(pGraphicsWin);
201 break;
202
203 case GX_SIGNAL(ID_CIRCLE, GX_EVENT_RADIO_SELECT):
204 if (pShapesWindow->gx_widget_parent == GX_NULL)
205 {
206 gx_widget_detach(pTextWindow);
207 gx_widget_attach(pShapesScreen, pShapesWindow);
208 gx_system_dirty_mark(pShapesScreen);
209 }
210 draw_shape = CIRCLE;
211 gx_system_dirty_mark(pGraphicsWin);
212 break;
213
214 case GX_SIGNAL(ID_ELLIPSE, GX_EVENT_RADIO_SELECT):
215 if (pShapesWindow->gx_widget_parent == GX_NULL)
216 {
217 gx_widget_detach(pTextWindow);
218 gx_widget_attach(pShapesScreen, pShapesWindow);
219 gx_system_dirty_mark(pShapesScreen);
220 }
221 draw_shape = ELLIPSE;
222 gx_system_dirty_mark(pGraphicsWin);
223 break;
224
225 case GX_SIGNAL(ID_ARC, GX_EVENT_RADIO_SELECT):
226 if (pShapesWindow->gx_widget_parent == GX_NULL)
227 {
228 gx_widget_detach(pTextWindow);
229 gx_widget_attach(pShapesScreen, pShapesWindow);
230 gx_system_dirty_mark(pShapesScreen);
231 }
232 draw_shape = ARC;
233 end_angle = start_angle;
234 gx_system_timer_start(pGraphicsWin, ARC_TIMER, ARC_TICKS, ARC_TICKS);
235 break;
236
237 case GX_SIGNAL(ID_PIE, GX_EVENT_RADIO_SELECT):
238 if (pShapesWindow->gx_widget_parent == GX_NULL)
239 {
240 gx_widget_detach(pTextWindow);
241 gx_widget_attach(pShapesScreen, pShapesWindow);
242 gx_system_dirty_mark(pShapesScreen);
243 }
244 draw_shape = PIE;
245 gx_system_timer_start(pGraphicsWin, PIE_TIMER, PIE_TICKS, PIE_TICKS);
246 gx_system_dirty_mark(pGraphicsWin);
247 break;
248
249 case GX_SIGNAL(ID_POLYGON, GX_EVENT_RADIO_SELECT):
250 if (pShapesWindow->gx_widget_parent == GX_NULL)
251 {
252 gx_widget_detach(pTextWindow);
253 gx_widget_attach(pShapesScreen, pShapesWindow);
254 gx_system_dirty_mark(pShapesScreen);
255 }
256 draw_shape = POLYGON;
257 gx_system_dirty_mark(pGraphicsWin);
258 break;
259
260 case GX_SIGNAL(ID_RECTANGLE, GX_EVENT_RADIO_SELECT):
261 if (pShapesWindow->gx_widget_parent == GX_NULL)
262 {
263 gx_widget_detach(pTextWindow);
264 gx_widget_attach(pShapesScreen, pShapesWindow);
265 gx_system_dirty_mark(pShapesScreen);
266 }
267 draw_shape = RECTANGLE;
268 gx_system_dirty_mark(pGraphicsWin);
269 break;
270
271 case GX_EVENT_TIMER:
272 if (myevent->gx_event_payload.gx_event_timer_id == ARC_TIMER)
273 {
274 /* Update arc parameter. */
275 end_angle += 2;
276 if (end_angle == 360)
277 {
278 gx_system_timer_stop(pGraphicsWin, ARC_TIMER);
279 }
280 }
281 else if (myevent->gx_event_payload.gx_event_timer_id == PIE_TIMER)
282 {
283 /* Update pie parameter. */
284 angle_off += 2;
285 if (angle_off >= 360)
286 {
287 angle_off = 0;
288 }
289 }
290 gx_system_dirty_mark(pGraphicsWin);
291 break;
292
293 case GX_SIGNAL(ID_TEXT, GX_EVENT_RADIO_SELECT):
294 if (pTextWindow->gx_widget_parent == GX_NULL)
295 {
296 gx_widget_detach(pShapesWindow);
297 gx_widget_attach( pShapesScreen, pTextWindow);
298 gx_system_dirty_mark(pShapesScreen);
299 }
300 break;
301
302 default:
303 status = gx_window_event_process(window, myevent);
304 break;
305 }
306 return status;
307 }
308
graphics_draw(GX_WINDOW * window)309 VOID graphics_draw(GX_WINDOW *window)
310 {
311 ULONG brush_style = 0;
312 GX_RECTANGLE rect;
313 GX_POINT rectangle[4] = { { 208, 63 },{ 274, 63 },{ 274, 163 },{ 208, 163 } };
314 GX_POINT pentagon[5] = { { 315, 103 },{ 360, 63 },{ 405, 103 },{ 385, 163 },{ 335, 163 } };
315 GX_POINT concave[6] = { { 70, 63 },{ 110, 93 },{ 150, 63 },{ 150, 163 },{ 110, 123 },{ 70, 163 } };
316 GX_POINT star[10] = { { 188, 227 },{ 227, 227 },{ 238, 187 },{ 252, 227 },{ 288, 227 },{ 259, 253 },{ 271, 294 },{ 241, 270 },{ 207, 293 },{ 218, 253 } };
317 GX_POINT self_intersection[8] = { { 118, 316 },{ 197, 410 },{ 274, 316 },{ 342, 410 },{ 342, 316 },{ 272, 410 },{ 197, 316 },{ 118, 410 } };
318 INT xcenter;
319 INT ycenter;
320
321 gx_window_draw((GX_WINDOW*)window);
322
323 if (anti_aliased)
324 {
325 brush_style |= GX_BRUSH_ALIAS;
326 }
327
328 if (solid_fill)
329 {
330 brush_style |= GX_BRUSH_SOLID_FILL;
331 }
332
333 if (pixelmap_fill)
334 {
335 brush_style |= GX_BRUSH_PIXELMAP_FILL;
336 gx_context_pixelmap_set(GX_PIXELMAP_ID_FISH);
337 }
338
339 if (round_end)
340 {
341 brush_style |= GX_BRUSH_ROUND;
342 }
343
344 gx_context_brush_define(line_color, fill_color, brush_style);
345 gx_context_brush_width_set(brush_width);
346
347 xcenter = window -> gx_widget_size.gx_rectangle_left + ((window -> gx_widget_size.gx_rectangle_right - window -> gx_widget_size.gx_rectangle_left + 1) >> 1);
348 ycenter = window -> gx_widget_size.gx_rectangle_top + ((window -> gx_widget_size.gx_rectangle_bottom - window -> gx_widget_size.gx_rectangle_top + 1) >> 1);
349
350 switch (draw_shape)
351 {
352 case CIRCLE:
353 gx_context_brush_define(GX_COLOR_ID_LIGHT_GRAY, GX_COLOR_ID_GREEN, brush_style);
354 gx_canvas_circle_draw(xcenter, ycenter, radius);
355 break;
356
357 case ARC:
358 gx_context_brush_define(GX_COLOR_ID_LIGHT_GRAY, GX_COLOR_ID_PURPLE, brush_style);
359 gx_canvas_arc_draw(xcenter, ycenter, radius, start_angle, end_angle);
360 break;
361
362 case PIE:
363 gx_context_brush_define(GX_COLOR_ID_LIGHT_GRAY, GX_COLOR_ID_INDIAN_RED, brush_style);
364 gx_canvas_pie_draw(xcenter, ycenter, radius, 60 + angle_off, 150 + angle_off);
365
366 gx_context_brush_define(GX_COLOR_ID_LIGHT_GRAY, GX_COLOR_ID_YELLOW, brush_style);
367 gx_canvas_pie_draw(xcenter, ycenter, radius, 150 + angle_off, 200 + angle_off);
368
369 gx_context_brush_define(GX_COLOR_ID_LIGHT_GRAY, GX_COLOR_ID_GREEN, brush_style);
370 gx_canvas_pie_draw(xcenter, ycenter, radius, 200 + angle_off, 280 + angle_off);
371
372 gx_context_brush_define(GX_COLOR_ID_LIGHT_GRAY, GX_COLOR_ID_PURPLE, brush_style);
373 gx_canvas_pie_draw(xcenter, ycenter, radius, 280 + angle_off, 60 + angle_off);
374 break;
375
376 case RECTANGLE:
377 gx_context_line_color_set(GX_COLOR_ID_LIGHT_TEAL);
378 gx_context_fill_color_set(GX_COLOR_ID_GREEN);
379 rect = window->gx_widget_size;
380 gx_utility_rectangle_resize(&rect, -10);
381 gx_canvas_rectangle_draw(&rect);
382
383 gx_utility_rectangle_resize(&rect, -30);
384 gx_context_line_color_set(GX_COLOR_ID_INDIAN_RED);
385 gx_context_fill_color_set(GX_COLOR_ID_PURPLE);
386 gx_canvas_rectangle_draw(&rect);
387
388 gx_utility_rectangle_resize(&rect, -30);
389 gx_context_line_color_set(GX_COLOR_ID_LIGHT_GRAY);
390 gx_context_fill_color_set(GX_COLOR_ID_BLACK);
391 gx_canvas_rectangle_draw(&rect);
392 break;
393
394 case POLYGON:
395 gx_context_brush_define(GX_COLOR_ID_LIGHT_GRAY, GX_COLOR_ID_GREEN, brush_style);
396 gx_canvas_polygon_draw(rectangle, 4);
397 gx_canvas_polygon_draw(pentagon, 5);
398 gx_canvas_polygon_draw(concave, 6);
399 gx_canvas_polygon_draw(star, 10);
400 gx_canvas_polygon_draw(self_intersection, 8);
401 break;
402
403 case ELLIPSE:
404 gx_context_brush_define(GX_COLOR_ID_LIGHT_GRAY, GX_COLOR_ID_SKY_BLUE, brush_style);
405 gx_canvas_ellipse_draw(xcenter, ycenter, radius, ellipse_b);
406
407 if (radius > 50)
408 {
409 gx_context_brush_define(GX_COLOR_ID_LIGHT_GRAY, GX_COLOR_ID_BLACK, brush_style);
410 gx_canvas_ellipse_draw(xcenter, ycenter, radius - 50, ellipse_b);
411 }
412
413 if (radius > 100)
414 {
415 gx_context_brush_define(GX_COLOR_ID_LIGHT_GRAY, GX_COLOR_ID_SKY_BLUE, brush_style);
416 gx_canvas_ellipse_draw(xcenter, ycenter, radius - 100, ellipse_b);
417 }
418 break;
419 }
420 }
421
422 /******************************************************************************************/
423 /* Calculate string length. */
424 /******************************************************************************************/
string_length_get(GX_CONST GX_CHAR * input_string,UINT max_string_length)425 UINT string_length_get(GX_CONST GX_CHAR* input_string, UINT max_string_length)
426 {
427 UINT length = 0;
428
429 if (input_string)
430 {
431 /* Traverse the string. */
432 for (length = 0; input_string[length]; length++)
433 {
434 /* Check if the string length is bigger than the max string length. */
435 if (length >= max_string_length)
436 {
437 break;
438 }
439 }
440 }
441
442 return length;
443 }
444
445