]> git.karo-electronics.de Git - oswald.git/blob - ui/oswald_graphics.c
238d1eabf53ccbd3e8dbfce0cb27da30d8ba21ff
[oswald.git] / ui / oswald_graphics.c
1 #include "oswald.h"
2 #include "oswald_strings.h"
3 #include "oswald_fonts.h"
4 #include "oswald_hal.h"
5
6 #include "oswald_graphics.h"
7
8
9 void oswald_draw_bitmap_opts(const unsigned int xstart, const unsigned int ystart, const unsigned int xoff, const unsigned int yoff, const unsigned int width, const unsigned int height, const unsigned int bmp_width, const unsigned int bmp_height, const void *bmp)
10 {
11         unsigned int x, y;
12         uint8_t *cb;
13
14         if (bmp == NULL)
15                 return;
16
17         //g_printerr("dbmp %d,%d off %d,%d\n", xstart, ystart, width, height);
18         cb = (uint8_t *)bmp;
19         //g_printerr("dat %02x %02x %02x\n", (uint8_t)cb[0], (uint8_t)cb[1], (uint8_t)cb[2]);
20         // we only draw set pixel, unset pixel remain as they are
21         for (y=yoff; y<bmp_height && y<height; y++) {
22                 for (x=xoff; x<bmp_width && x<width; x++) {
23                         cb = (uint8_t *)(bmp + (y * ((bmp_width / 8) + ((bmp_width % 8) ? 1 : 0))) + (x / 8));
24                         // g_printerr("dat %02x %02x %02x\n", (uint8_t)cb[0], (uint8_t)cb[1], (uint8_t)cb[2]);
25                         if (*cb & (1 << (x % 8))) {
26                                 hal_lcd_set_pixel((xstart + x) - xoff, (ystart + y) - yoff, TRUE);
27                                 // g_printerr("X");
28                         } /*else {
29                                 g_printerr(".");
30                         }*/
31                 }
32                 //g_printerr("\n");
33         }
34 }
35
36 #if 0
37 /*inline*/ void oswald_draw_bitmap(const unsigned int xstart, const unsigned int ystart, const unsigned int width, const unsigned int height, const void *bmp)
38 {
39         unsigned int x, y;
40         uint8_t *cb;
41
42         // we only draw set pixel, unset pixel remain as they are
43         for (y=0; y<height; y++) {
44                 for (x=0; x<width; x++) {
45                         cb = (uint8_t *)(bmp + (y * ((width / 8) + ((width % 8) ? 1 : 0))) + (x / 8));
46                         if (*cb & (1 << (x % 8)))
47                                 hal_lcd_set_pixel((xstart + x), (ystart + y), TRUE);
48                 }
49         }
50 }
51 #else
52 void oswald_draw_bitmap(const unsigned int xstart, const unsigned int ystart, const unsigned int width, const unsigned int height, const void *bmp)
53 {
54         // seems we are triggering a MSPGCC compiler bug here...
55         // if we do not do this trick then bmp becomes 0x00 when passed a livel higher!
56         volatile unsigned int num;
57
58         num = (unsigned int) bmp;
59
60         oswald_draw_bitmap_opts(xstart, ystart, 0, 0, width, height, width, height, bmp);
61 }
62
63 void oswald_draw_bitmap_size(const unsigned int xstart, const unsigned int ystart, const unsigned int width, const unsigned int height, const unsigned int bmp_width, const unsigned int bmp_height, const void *bmp)
64 {
65         // seems we are triggering a MSPGCC compiler bug here...
66         // if we do not do this trick then bmp becomes 0x00 when passed a livel higher!
67         volatile unsigned int num;
68
69         num = (unsigned int) bmp;
70
71         oswald_draw_bitmap_opts(xstart, ystart, 0, 0, width, height, bmp_width, bmp_height, bmp);
72 }
73 #endif
74
75 void oswald_draw_line(const uint8_t xstart, const uint8_t ystart, const uint8_t xend, const uint8_t yend)
76 {
77         int x, y, t, dx, dy, incx, incy, pdx, pdy, ddx, ddy, es, el, err;
78  
79         dx = xend - xstart;
80         dy = yend - ystart;
81  
82         incx = (dx >= 0) ? 1 : -1;
83         incy = (dy >= 0) ? 1 : -1;
84
85         if (dx<0)
86                 dx = -dx;
87         if (dy<0)
88                 dy = -dy;
89  
90         if (dx>dy) {
91                 pdx = incx; pdy = 0;
92                 ddx=incx; ddy=incy;
93                 es =dy;   el =dx;
94         } else {
95                 pdx=0;    pdy=incy;
96                 ddx=incx; ddy=incy;
97                 es =dx;   el =dy;
98         }
99  
100         x = xstart;
101         y = ystart;
102         err = el/2;
103         hal_lcd_set_pixel(x, y, TRUE);
104  
105         for (t = 0; t < el; ++t) {
106                 err -= es; 
107                 if (err < 0) {
108                         err += el;
109                         x += ddx;
110                         y += ddy;
111                 } else {
112                         x += pdx;
113                         y += pdy;
114                 }
115                 hal_lcd_set_pixel(x, y, TRUE);
116         }
117         // hal_lcd_update_display();
118 }
119
120 void oswald_draw_line_ww(const uint8_t xstart, const uint8_t ystart, const uint8_t xend, const uint8_t yend, const uint8_t thickness)
121 {
122         int i, x, y, t, dx, dy, incx, incy, pdx, pdy, ddx, ddy, es, el, err;
123  
124         dx = xend - xstart;
125         dy = yend - ystart;
126  
127         incx = (dx >= 0) ? 1 : -1;
128         incy = (dy >= 0) ? 1 : -1;
129
130         if (dx<0)
131                 dx = -dx;
132         if (dy<0)
133                 dy = -dy;
134  
135         if (dx>dy) {
136                 pdx = incx;
137                 pdy = 0;
138                 ddx=incx;
139                 ddy=incy;
140                 es =dy;
141                 el =dx;
142         } else {
143                 pdx=0;
144                 pdy=incy;
145                 ddx=incx;
146                 ddy=incy;
147                 es =dx;
148                 el =dy;
149         }
150  
151         x = xstart;
152         y = ystart;
153         err = el/2;
154         hal_lcd_set_pixel(x, y, TRUE);
155         for (i=1; i<thickness; i++) {
156                 hal_lcd_set_pixel(x-i, y, TRUE);
157                 hal_lcd_set_pixel(x+i, y, TRUE);
158                 hal_lcd_set_pixel(x, y-i, TRUE);
159                 hal_lcd_set_pixel(x, y+i, TRUE);
160         }
161  
162         for (t = 0; t < el; ++t) {
163                 err -= es; 
164                 if (err < 0) {
165                         err += el;
166                         x += ddx;
167                         y += ddy;
168                 } else {
169                         x += pdx;
170                         y += pdy;
171                 }
172                 hal_lcd_set_pixel(x, y, TRUE);
173                 for (i=1; i<thickness; i++) {
174                         hal_lcd_set_pixel(x-i, y, TRUE);
175                         hal_lcd_set_pixel(x+i, y, TRUE);
176                         hal_lcd_set_pixel(x, y-i, TRUE);
177                         hal_lcd_set_pixel(x, y+i, TRUE);
178                 }
179         }
180         // hal_lcd_update_display();
181 }
182
183 uint8_t oswald_write_character(const uint8_t x, const uint8_t y, const oswald_font_face face, const uint8_t Character)
184 {
185 #if 0
186         u8t CharacterHeight = GetCharacterHeight();
187         u8t CharacterWidth = GetCharacterWidth(Character);
188         u16t bitmap[MAX_FONT_ROWS];
189         register lx, ly;
190
191         GetCharacterBitmap(Character, bitmap);
192
193         // printf("cw=%d ch=%d\n", CharacterWidth, CharacterHeight);
194         for (ly=0; ly<CharacterHeight; ly++) {
195                 for (lx=0; lx<CharacterWidth; lx++) {
196                         if (bitmap[ly] & (1<<lx)) {
197                                 hal_lcd_set_pixel(lx+x, ly+y, TRUE);
198                                 // printf(".");
199                         } /*else {
200                                 hal_lcd_set_pixel(lx+x, ly+y, FALSE);
201                                 // printf(" ");
202                         }*/
203                 }
204                 // printf("\n");
205         }
206
207         return CharacterWidth + GetFontSpacing();
208 #else
209         uint8_t *cdata = oswald_fonts[face].data;
210         uint8_t cwidth;
211         int csize;
212
213         if (Character == 32) // space / blank
214                 return oswald_fonts[face].width / 2;
215
216         csize = ((oswald_fonts[face].width / 8) + ((oswald_fonts[face].width % 8) ? 1 : 0)) * oswald_fonts[face].height;
217         if (oswald_fonts[face].font_type == FONT_TYPE_PROPORTIONAL)
218                 csize += 1;
219
220         //csize += (oswald_fonts[face].height / 8) + ((oswald_fonts[face].height % 8) ? 1 : 0);
221
222         // g_printerr("fp = 0x%08lx cdata = 0x%08lx\n", font_7x12, cdata);
223
224         cdata = (cdata + ((int)csize * (int)Character));
225
226         //g_printerr("%02x\n", oswald_fonts[face].data[0][0]);
227         //g_printerr("char %02x face %d %dx%d csize %d\n", Character, face, oswald_fonts[face].width, oswald_fonts[face].height, csize);
228         //g_printerr("char %02x %02x %02x\n", (uint8_t)cdata[0], (uint8_t)cdata[1], (uint8_t)cdata[2]);
229
230         // oswald_draw_bitmap(x, y, oswald_fonts[face].height, oswald_fonts[face].height, cdata);
231         if (oswald_fonts[face].font_type == FONT_TYPE_MONOSPACE) {
232                 cwidth = oswald_fonts[face].width;
233                 oswald_draw_bitmap(x, y, ((oswald_fonts[face].width / 8) + ((oswald_fonts[face].width % 8) ? 1 : 0))*8, oswald_fonts[face].height, (uint8_t *)cdata);
234         }
235         if (oswald_fonts[face].font_type == FONT_TYPE_PROPORTIONAL) {
236                 cwidth = cdata[0];
237                 cdata++;
238                 oswald_draw_bitmap_size(x, y, cwidth+1, oswald_fonts[face].height, ((oswald_fonts[face].width / 8) + ((oswald_fonts[face].width % 8) ? 1 : 0))*8, oswald_fonts[face].height, (uint8_t *)cdata);
239         }
240         // oswald_draw_bitmap_offset(x, y, (oswald_fonts[face].width % 8) > 0 ? (8-(oswald_fonts[face].width % 8)) : 0, 0, ((oswald_fonts[face].width / 8) + ((oswald_fonts[face].width % 8) ? 1 : 0))*8, oswald_fonts[face].height, cdata);
241
242         return cwidth;
243 #endif
244 }
245
246 void oswald_write_string(const uint8_t x, const uint8_t y, const oswald_font_face face, char *str)
247 {
248         uint8_t lx, i, strl;
249
250         strl = oswald_strlen(str);
251         if (strl == 0)
252                 return;
253
254         lx = x;
255         for (i=0; i<strl; i++) {
256                 lx += oswald_write_character(lx, y, face, str[i]);
257         }
258 }
259
260
261 void oswald_write_number(const uint8_t x, const uint8_t y, const oswald_font_face face, const int16_t number)
262 {
263         uint8_t lx, i, strl;
264         char str[8];
265
266         itoa(number, str, 10);
267         strl = oswald_strlen(str);
268         if (strl == 0)
269                 return;
270
271         lx = x;
272         for (i=0; i<strl; i++) {
273                 lx += oswald_write_character(lx, y, face, str[i]);
274         }
275 }
276
277