]>
git.defcon.no Git - avrfbosd/blob - draw.c
6 extern uint8_t* screen_buffer
;
7 extern uint8_t hres_bytes
;
9 void sp(uint8_t x
, uint8_t y
, uint8_t color
)
11 if (color
==0) screen_buffer
[(x
/8) + (y
*hres_bytes
)] &= ~0x80 >> (x
&7);
12 else if (color
==1) screen_buffer
[(x
/8) + (y
*hres_bytes
)] |= 0x80 >> (x
&7);
13 else screen_buffer
[(x
/8) + (y
*hres_bytes
)] ^= 0x80 >> (x
&7);
16 void set_pixel(uint8_t x
, uint8_t y
, uint8_t color
)
18 if (x
>= hres_bytes
*8 || y
>= VRES
)
23 void fill(uint8_t color
)
30 memset( (void*)screen_buffer
, 0xFF, hres_bytes
*VRES
);
33 for (int i
= 0; i
< (hres_bytes
*VRES
); i
++)
34 screen_buffer
[i
] = ~screen_buffer
[i
];
37 memset( (void*)screen_buffer
, color
, hres_bytes
*VRES
);
44 Bresenham's line algorithm, optimized and simplified.
45 function line(x0, y0, x1, y1)
48 if x0 < x1 then sx := 1 else sx := -1
49 if y0 < y1 then sy := 1 else sy := -1
54 if x0 = x1 and y0 = y1 exit loop
66 void draw_line(uint8_t x0
, uint8_t y0
, uint8_t x1
, uint8_t y1
, uint8_t c
)
69 if (x0
> hres_bytes
*8 || y0
> VRES
) // || x1 > hres_bytes*8 || y1 > VRES)
72 int dx
, dy
, sx
, sy
, err
, p
;
76 if ( x0
< x1
) sx
= 1; else sx
= -1;
77 if ( y0
< y1
) sy
= 1; else sy
= -1;
83 if ( ( x0
== x1
) && ( y0
== y1
) ) return;
84 if ( x0
> hres_bytes
*8 || y0
> VRES
) return;
99 // draw_row and draw_col ...
100 // These whould really be used for optimization.
101 // As is, they exist for conveniance, not speed :P
102 void draw_col(uint8_t x
, uint8_t y0
, uint8_t y1
, uint8_t c
)
104 draw_line( x
, y0
, x
, y1
, c
);
106 void draw_row(uint8_t y
, uint8_t x0
, uint8_t x1
, uint8_t c
)
108 draw_line( x0
, y
, x1
, y
, c
);
112 void _draw_rect(uint8_t x0
, uint8_t y0
, uint8_t w
, uint8_t h
, int8_t c
, int8_t fc
)
115 for (unsigned char i
= 0; i
< h
; i
++)
116 draw_line(x0
,y0
+i
,x0
+w
,y0
+i
,c
);
118 draw_line(x0
,y0
,x0
+w
,y0
,c
);
119 draw_line(x0
,y0
,x0
,y0
+h
,c
);
120 draw_line(x0
+w
,y0
,x0
+w
,y0
+h
,c
);
121 draw_line(x0
,y0
+h
,x0
+w
,y0
+h
,c
);
124 void _draw_circle(uint8_t x0
, uint8_t y0
, uint8_t radius
, int8_t c
, int8_t fc
) {
128 int ddF_y
= -2 * radius
;
131 uint8_t pyy
= y
,pyx
= x
;
133 //there is a fill color
135 draw_row(y0
,x0
-radius
,x0
+radius
,fc
);
137 sp(x0
, y0
+ radius
,c
);
138 sp(x0
, y0
- radius
,c
);
139 sp(x0
+ radius
, y0
,c
);
140 sp(x0
- radius
, y0
,c
);
151 //there is a fill color
153 //prevent double draws on the same rows
155 draw_row(y0
+y
,x0
-x
,x0
+x
,fc
);
156 draw_row(y0
-y
,x0
-x
,x0
+x
,fc
);
158 if (pyx
!= x
&& x
!= y
) {
159 draw_row(y0
+x
,x0
-y
,x0
+y
,fc
);
160 draw_row(y0
-x
,x0
-y
,x0
+y
,fc
);
166 sp(x0
+ x
, y0
+ y
,c
);
167 sp(x0
- x
, y0
+ y
,c
);
168 sp(x0
+ x
, y0
- y
,c
);
169 sp(x0
- x
, y0
- y
,c
);
170 sp(x0
+ y
, y0
+ x
,c
);
171 sp(x0
- y
, y0
+ x
,c
);
172 sp(x0
+ y
, y0
- x
,c
);
173 sp(x0
- y
, y0
- x
,c
);
177 void draw_rect(uint8_t x0
, uint8_t y0
, uint8_t w
, uint8_t h
, uint8_t c
)
179 _draw_rect(x0
, y0
, w
, h
, c
, -1);
182 void fill_rect(uint8_t x0
, uint8_t y0
, uint8_t w
, uint8_t h
, uint8_t c
)
184 _draw_rect(x0
, y0
, w
, h
, c
, c
);
187 void draw_circle(uint8_t x0
, uint8_t y0
, uint8_t radius
, char c
)
189 _draw_circle(x0
, y0
, radius
, c
, -1);
192 void fill_circle(uint8_t x0
, uint8_t y0
, uint8_t radius
, char c
)
194 _draw_circle(x0
, y0
, radius
, c
, c
);
197 void draw_8bpp_bitmap( uint8_t pos_x
, uint8_t pos_y
, uint8_t width
, uint8_t height
, const uint8_t* image
)
199 for ( int v
= 0; v
< height
; v
++ )
201 for ( int h
= 0; h
< width
; h
++ )
203 uint8_t t
= image
[v
*width
+h
];
204 set_pixel( pos_x
+ h
, pos_y
+ v
, t
);
209 void pgm_draw_8bpp_bitmap( uint8_t pos_x
, uint8_t pos_y
, uint8_t width
, uint8_t height
, const uint8_t* image
)
211 for ( int v
= 0; v
< height
; v
++ )
213 for ( int h
= 0; h
< width
; h
++ )
215 uint8_t t
= pgm_read_byte(&(image
[v
*width
+h
]));
216 set_pixel( pos_x
+ h
, pos_y
+ v
, t
);
221 void draw_char ( uint8_t pos_x
, uint8_t pos_y
, uint8_t ch
)
223 for ( int h
= 0; h
< font_height
; h
++ )
225 uint8_t fl
= pgm_read_byte(&( font
[ch
-32][h
]));
226 fl
= fl
<<(8-font_width
);
228 for ( int p
= 0; p
< font_width
; p
++ )
233 set_pixel( pos_x
+p
, pos_y
+h
, ft
);
239 void draw_string ( uint8_t pos_x
, uint8_t pos_y
, const char *text
)
241 for (int i
= 0; text
[i
] != 0; i
++)
243 draw_char( pos_x
+(font_width
*i
)+1, pos_y
, text
[i
]);
247 void clear_screen( void )
249 memset( (void*)screen_buffer
, 0, hres_bytes
*VRES
);