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