]>
git.defcon.no Git - avrfbosd/blob - draw.c
5 extern uint8_t* screen_buffer
;
6 extern uint8_t hres_bytes
;
8 void sp(uint8_t x
, uint8_t y
, uint8_t color
)
10 if (color
==0) screen_buffer
[(x
/8) + (y
*hres_bytes
)] &= ~0x80 >> (x
&7);
11 else if (color
==1) screen_buffer
[(x
/8) + (y
*hres_bytes
)] |= 0x80 >> (x
&7);
12 else screen_buffer
[(x
/8) + (y
*hres_bytes
)] ^= 0x80 >> (x
&7);
15 void set_pixel(uint8_t x
, uint8_t y
, uint8_t color
)
17 if (x
>= hres_bytes
*8 || y
>= VRES
)
22 void fill(uint8_t color
)
26 for (int i
= 0; i
< (hres_bytes
*VRES
); i
++)
30 for (int i
= 0; i
< (hres_bytes
*VRES
); i
++)
31 screen_buffer
[i
] = 0xFF;
34 for (int i
= 0; i
< (hres_bytes
*VRES
); i
++)
35 screen_buffer
[i
] = ~screen_buffer
[i
];
42 Bresenham's line algorithm, optimized and simplified.
43 function line(x0, y0, x1, y1)
46 if x0 < x1 then sx := 1 else sx := -1
47 if y0 < y1 then sy := 1 else sy := -1
52 if x0 = x1 and y0 = y1 exit loop
64 void draw_line(uint8_t x0
, uint8_t y0
, uint8_t x1
, uint8_t y1
, uint8_t c
)
67 if (x0
> hres_bytes
*8 || y0
> VRES
) // || x1 > hres_bytes*8 || y1 > VRES)
70 int dx
, dy
, sx
, sy
, err
, p
;
74 if ( x0
< x1
) sx
= 1; else sx
= -1;
75 if ( y0
< y1
) sy
= 1; else sy
= -1;
81 if ( ( x0
== x1
) && ( y0
== y1
) ) return;
82 if ( x0
> hres_bytes
*8 || y0
> VRES
) return;
97 // draw_row and draw_col ...
98 // These whould really be used for optimization.
99 // As is, they exist for conveniance, not speed :P
100 void draw_col(uint8_t x
, uint8_t y0
, uint8_t y1
, uint8_t c
)
102 draw_line( x
, y0
, x
, y1
, c
);
104 void draw_row(uint8_t y
, uint8_t x0
, uint8_t x1
, uint8_t c
)
106 draw_line( x0
, y
, x1
, y
, c
);
110 void _draw_rect(uint8_t x0
, uint8_t y0
, uint8_t w
, uint8_t h
, int8_t c
, int8_t fc
)
113 for (unsigned char i
= 0; i
< h
; i
++)
114 draw_line(x0
,y0
+i
,x0
+w
,y0
+i
,c
);
116 draw_line(x0
,y0
,x0
+w
,y0
,c
);
117 draw_line(x0
,y0
,x0
,y0
+h
,c
);
118 draw_line(x0
+w
,y0
,x0
+w
,y0
+h
,c
);
119 draw_line(x0
,y0
+h
,x0
+w
,y0
+h
,c
);
122 void _draw_circle(uint8_t x0
, uint8_t y0
, uint8_t radius
, int8_t c
, int8_t fc
) {
126 int ddF_y
= -2 * radius
;
129 uint8_t pyy
= y
,pyx
= x
;
131 //there is a fill color
133 draw_row(y0
,x0
-radius
,x0
+radius
,fc
);
135 sp(x0
, y0
+ radius
,c
);
136 sp(x0
, y0
- radius
,c
);
137 sp(x0
+ radius
, y0
,c
);
138 sp(x0
- radius
, y0
,c
);
149 //there is a fill color
151 //prevent double draws on the same rows
153 draw_row(y0
+y
,x0
-x
,x0
+x
,fc
);
154 draw_row(y0
-y
,x0
-x
,x0
+x
,fc
);
156 if (pyx
!= x
&& x
!= y
) {
157 draw_row(y0
+x
,x0
-y
,x0
+y
,fc
);
158 draw_row(y0
-x
,x0
-y
,x0
+y
,fc
);
164 sp(x0
+ x
, y0
+ y
,c
);
165 sp(x0
- x
, y0
+ y
,c
);
166 sp(x0
+ x
, y0
- y
,c
);
167 sp(x0
- x
, y0
- y
,c
);
168 sp(x0
+ y
, y0
+ x
,c
);
169 sp(x0
- y
, y0
+ x
,c
);
170 sp(x0
+ y
, y0
- x
,c
);
171 sp(x0
- y
, y0
- x
,c
);
175 void draw_rect(uint8_t x0
, uint8_t y0
, uint8_t w
, uint8_t h
, uint8_t c
)
177 _draw_rect(x0
, y0
, w
, h
, c
, -1);
180 void fill_rect(uint8_t x0
, uint8_t y0
, uint8_t w
, uint8_t h
, uint8_t c
)
182 _draw_rect(x0
, y0
, w
, h
, c
, c
);
185 void draw_circle(uint8_t x0
, uint8_t y0
, uint8_t radius
, char c
)
187 _draw_circle(x0
, y0
, radius
, c
, -1);
190 void fill_circle(uint8_t x0
, uint8_t y0
, uint8_t radius
, char c
)
192 _draw_circle(x0
, y0
, radius
, c
, c
);
195 void draw_8bpp_bitmap( uint8_t pos_x
, uint8_t pos_y
, uint8_t width
, uint8_t height
, const uint8_t* image
)
197 for ( int v
= 0; v
< height
; v
++ )
199 for ( int h
= 0; h
< width
; h
++ )
201 uint8_t t
= image
[v
*width
+h
];
202 set_pixel( pos_x
+ h
, pos_y
+ v
, t
);
207 void pgm_draw_8bpp_bitmap( uint8_t pos_x
, uint8_t pos_y
, uint8_t width
, uint8_t height
, const uint8_t* image
)
209 for ( int v
= 0; v
< height
; v
++ )
211 for ( int h
= 0; h
< width
; h
++ )
213 uint8_t t
= pgm_read_byte(&(image
[v
*width
+h
]));
214 set_pixel( pos_x
+ h
, pos_y
+ v
, t
);
219 void draw_char ( uint8_t pos_x
, uint8_t pos_y
, uint8_t ch
)
221 for ( int h
= 0; h
< font_height
; h
++ )
223 uint8_t fl
= pgm_read_byte(&( font
[ch
-32][h
]));
224 fl
= fl
<<(8-font_width
);
226 for ( int p
= 0; p
< font_width
; p
++ )
231 set_pixel( pos_x
+p
, pos_y
+h
, ft
);
237 void draw_string ( uint8_t pos_x
, uint8_t pos_y
, const char *text
)
239 for (int i
= 0; text
[i
] != 0; i
++)
241 draw_char( pos_x
+(font_width
*i
)+1, pos_y
, text
[i
]);