Add demo application.
[bertos.git] / gfx / win_test.c
1 /**
2  * \file
3  * <!--
4  * Copyright 2006 Develer S.r.l. (http://www.develer.com/)
5  * This file is part of DevLib - See README.devlib for information.
6  * -->
7  *
8  * \version $Id$
9  *
10  * \author Bernardo Innocenti <bernie@develer.com>
11  *
12  * \brief Windowing system test.
13  */
14
15 /*#*
16  *#* $Log$
17  *#* Revision 1.1  2006/01/23 23:14:29  bernie
18  *#* Implement simple, but impressive windowing system.
19  *#*
20  *#*/
21
22 #include <emul/emul.h>
23 #include <drv/lcd_gfx.h>
24 #include <gfx/gfx.h>
25 #include <gfx/win.h>
26
27 static void magic(struct Bitmap *bitmap, coord_t x, coord_t y)
28 {
29         static const coord_t coords[] = { 120, 34, 90, 90, 30, 90, 0, 34, 60, 0, 90, 90, 0, 34, 120, 34, 30, 90, 60, 0 };
30         unsigned int i;
31
32         gfx_moveTo(bitmap, coords[countof(coords)-2]/2 + x, coords[countof(coords)-1]/3 + y);
33         for (i = 0; i < countof(coords); i += 2)
34                 gfx_lineTo(bitmap, coords[i]/2 + x, coords[i+1]/3 + y);
35 }
36
37 int main(int argc, char *argv[])
38 {
39         emul_init(&argc, argv);
40         lcd_init();
41
42         const coord_t small_left = 45, small_top = 30, small_width = 50, small_height = 30;
43         const coord_t large_left = -10, large_top = 10, large_width = 85, large_height = 41;
44
45         Window root_win, small_win, large_win;
46         Bitmap small_bm, large_bm;
47         uint8_t small_raster[RASTER_SIZE(small_width, small_height)];
48         uint8_t large_raster[RASTER_SIZE(large_width, large_height)];
49
50         win_create(&root_win,  &lcd_bitmap);
51
52         gfx_bitmapInit(&large_bm, large_raster, large_width, large_height);
53         win_create(&large_win, &large_bm);
54         win_open(&large_win, &root_win);
55         win_move(&large_win, large_left, large_top);
56
57         gfx_bitmapInit(&small_bm, small_raster, small_width, small_height);
58         win_create(&small_win, &small_bm);
59         win_open(&small_win, &root_win);
60         win_move(&small_win, small_left, small_top);
61
62
63         coord_t x = 0, y = LCD_WIDTH / 2;
64         coord_t xdir = +1, ydir = -1;
65         coord_t xdir_large = +1;
66         coord_t ydir_small = +1;
67         int raise_counter = 0;
68         int i;
69         Bitmap *bm;
70
71         for(;;)
72         {
73                 /* Background animation */
74                 bm = &lcd_bitmap;
75                 gfx_bitmapClear(bm);
76 /*              gfx_setClipRect(bm, 0, 0, bm->width, bm->height);
77                 gfx_rectDraw(bm, 10, 10, bm->width-10, bm->height-10);
78                 gfx_setClipRect(bm, 11, 11, bm->width-11, bm->height-11);
79 */              magic(bm, x, y);
80                 x += xdir;
81                 y += ydir;
82                 if (x >= bm->width)  xdir = -1;
83                 if (x <= -50)        xdir = +1;
84                 if (y >= bm->height) ydir = -1;
85                 if (y <= -50)        ydir = +1;
86
87                 /* Large window animation */
88                 bm = large_win.bitmap;
89                 gfx_bitmapClear(bm);
90                 for (i = 0; i < bm->height / 2; i += 2)
91                         gfx_rectDraw(bm, 0 + i, 0 + i, bm->width - i, bm->height - i);
92
93
94                 /* Small window animation */
95                 bm = small_win.bitmap;
96                 gfx_bitmapClear(bm);
97                 gfx_rectDraw(bm, 0, 0, bm->width, bm->height);
98                 gfx_line(bm, 0, 0, bm->width, bm->height);
99                 gfx_line(bm, bm->width, 0, 0, bm->height);
100
101                 /* Move windows around */
102                 win_move(&large_win, large_win.geom.xmin + xdir_large, large_top);
103                 if (large_win.geom.xmin < -20) xdir_large = +1;
104                 if (large_win.geom.xmin > RECT_WIDTH(&root_win.geom) - 5) xdir_large = -1;
105
106                 win_move(&small_win, small_left, small_win.geom.ymin + ydir_small);
107                 if (small_win.geom.ymin < -20) ydir_small = +1;
108                 if (small_win.geom.ymin > RECT_HEIGHT(&root_win.geom) - 5) ydir_small = -1;
109
110                 ++raise_counter;
111                 if (raise_counter % 997 == 0)
112                         win_raise(&small_win);
113                 else if (raise_counter % 731 == 0)
114                         win_raise(&large_win);
115
116                 win_compose(&root_win);
117                 lcd_blit_bitmap(root_win.bitmap);
118                 emul_idle();
119                 usleep(10000);
120         }
121
122         emul_cleanup();
123         return 0;
124 }