-
Notifications
You must be signed in to change notification settings - Fork 1
/
fbdraw.h
101 lines (92 loc) · 2.33 KB
/
fbdraw.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#ifndef _FBDRAW_H_
#define _FBDRAW_H_
#include <stddef.h>
#include <stdint.h>
#include "misc.h"
#include "bitmap.h"
struct fbdraw_info {
uint8_t *mem;
unsigned long size;
unsigned int xres;
unsigned int yres;
unsigned int bits_per_pixel;
unsigned int line_length;
};
inline void fbdraw_pixel(unsigned x, unsigned int y,
uint32_t color, struct fbdraw_info *fb)
{
if ((x >= fb->xres) || (y >= fb->yres))
return;
uint8_t *fbp8 = fb->mem;
int pos = y * fb->line_length + x * fb->bits_per_pixel / 8;
fbp8 += pos;
uint16_t *fbp16 = (uint16_t *) fbp8;
uint32_t *fbp32 = (uint32_t *) fbp8;
switch (fb->bits_per_pixel) {
case 32:
*fbp32 = color;
break;
case 16:
*fbp16 = color;
break;
default:
*fbp8 = color;
break;
}
}
inline void fbdraw_xline(unsigned x0, unsigned x1, unsigned int y,
uint32_t color, struct fbdraw_info *fb)
{
unsigned int xmin = MIN(x0, x1);
unsigned int xmax = MAX(x0, x1);
for (; xmin <= xmax; xmin++)
fbdraw_pixel(xmin, y, color, fb);
}
inline void fbdraw_yline(unsigned y0, unsigned y1, unsigned int x,
uint32_t color, struct fbdraw_info *fb)
{
unsigned int ymin = MIN(y0, y1);
unsigned int ymax = MAX(y0, y1);
for (; ymin <= ymax; ymin++)
fbdraw_pixel(x, ymin, color, fb);
}
inline void fbdraw_rect(unsigned int x, unsigned int y,
unsigned int w, unsigned int h,
uint32_t color, struct fbdraw_info *fb)
{
// draw up
fbdraw_xline(x, x + w, y, color, fb);
// draw down
fbdraw_xline(x, x + w, y + h, color, fb);
// draw left
fbdraw_yline(y, y + h, x, color, fb);
// draw right
fbdraw_yline(y, y + h, x + w, color, fb);
}
inline void fbdraw_rect_solid(unsigned int x, unsigned int y,
unsigned int w, unsigned int h,
uint32_t color, struct fbdraw_info *fb)
{
unsigned int ycur;
for (ycur = y; ycur <= (y + h); ycur++)
fbdraw_xline(x, x + w, ycur, color, fb);
}
inline void fbdraw_bitmap(unsigned int x, unsigned int y,
struct bitmap *bm, uint32_t * colorfg,
uint32_t * colorbg, struct fbdraw_info *fb)
{
uint8_t *bmdata = bm->data;
unsigned int bx, by;
for (by = 0; by < bm->h; by++) {
for (bx = 0; bx < bm->w; bx++) {
if ((*(bmdata + by) & 1 << bx) && colorfg != NULL) {
fbdraw_pixel(bx + x, by + y,
*colorfg, fb);
} else if (colorbg != NULL) {
fbdraw_pixel(bx + x, by + y,
*colorbg, fb);
}
}
}
}
#endif