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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "gfx_font.h"
#include "log.h"
#include "multiboot.h"
#include "panic.h"
#include "gfx.h"
uint8_t *framebuffer = NULL;
uint16_t fb_width;
uint16_t fb_height;
/* this is BYTES per pixel */
uint8_t fb_bpp;
uint16_t fb_stride;
const uint8_t *gfx_bpp = &fb_bpp;
const uint16_t *gfx_width = &fb_width;
const uint16_t *gfx_height = &fb_height;
static int cursor_x, cursor_y;
uint32_t _gfx_fgcol, _gfx_bgcol;
void gfx_set_background(uint32_t col)
{
_gfx_bgcol = col;
}
uint32_t gfx_get_background(void)
{
return _gfx_bgcol;
}
void gfx_set_foreground(uint32_t col)
{
_gfx_fgcol = col;
}
uint32_t gfx_get_foreground(void)
{
return _gfx_fgcol;
}
void gfx_drawpixel(int x, int y)
{
((uint32_t*)framebuffer)[y * fb_width + x] = _gfx_fgcol;
}
/* implemented in assembly now */
/*
void gfx_clear(uint32_t col)
{
uint8_t *p = framebuffer;
uint8_t *stop = framebuffer + fb_width * fb_height * fb_bpp;
while(p < stop)
{
*(uint32_t*)p = col;
p += fb_bpp;
}
}
*/
void gfx_reset(void)
{
_gfx_fgcol = VGA_RGBPACK(0xff, 0xff, 0xff);
_gfx_bgcol = VGA_RGBPACK(0, 0, 0);
gfx_clear();
cursor_y = 0;
cursor_x = 0;
}
void gfx_drawchar(int x, int y, char c)
{
uint8_t *line_addr = framebuffer + (x * fb_bpp) + (y * fb_stride);
const uint32_t bg = _gfx_bgcol;
const uint32_t fg = _gfx_fgcol;
for(int i = 0; i < FONT_HEIGHT; ++i)
{
uint8_t mask_table[8] = {128, 64, 32, 16, 8, 4, 2, 1};
for(int j = 0; j < 8; ++j)
{
if(gfx_font[c][i] & mask_table[j])
((uint32_t*)line_addr)[j] = fg;
}
line_addr += fb_stride;
}
}
void gfx_putchar(char ch)
{
if(ch != '\n')
{
gfx_drawchar(cursor_x, cursor_y, ch);
cursor_x += FONT_WIDTH;
if(cursor_x >= fb_width)
{
cursor_x = 0;
cursor_y += FONT_HEIGHT;
if(cursor_y >= fb_height)
{
gfx_clear();
cursor_y = 0;
}
}
}
else
{
cursor_x = 0;
cursor_y += FONT_HEIGHT;
if(cursor_y >= fb_height)
{
gfx_clear();
cursor_y = 0;
}
}
}
void gfx_puts(const char* str)
{
while(*str)
{
gfx_putchar(*str++);
}
}
/* implemented in assembly */
#if 0
void gfx_hline(int x1, int x2, int y)
{
/* make sure x1 is to the left of x2 */
if(x2 < x1)
{
int temp = x1;
x1 = x2;
x2 = temp;
}
uint8_t *base = framebuffer + y * fb_stride;
uint8_t *dest = base + x1 * fb_bpp;
uint8_t *stop = base + x2 * fb_bpp;
const uint32_t col = _gfx_fgcol;
while(dest < stop)
{
*(uint32_t*)dest = col;
dest += fb_bpp;
}
}
void gfx_vline(int y1, int y2, int x)
{
/* make sure y1 is above y2 */
if(y2 < y1)
{
int temp = y1;
y1 = y2;
y2 = temp;
}
uint8_t *dest = framebuffer + y1 * fb_stride + x * fb_bpp;
uint8_t *stop = framebuffer + y2 * fb_stride + x * fb_bpp;
const uint32_t col = _gfx_fgcol;
while(dest < stop)
{
*(uint32_t*)dest = col;
dest += fb_stride;
}
}
#endif
#define SWAP(x, y, tmp) (tmp=x;x=y;y=tmp;)
void gfx_fillrect(int x, int y, int w, int h)
{
for(int i = 0; i < h; ++i)
{
gfx_hline(x, x + w, y + i);
}
}
bool gfx_init(struct vbe_info_t *vbe_mode_info)
{
framebuffer = (uint8_t*)vbe_mode_info->physbase;
fb_width = vbe_mode_info->Xres;
fb_height = vbe_mode_info->Yres;
fb_bpp = vbe_mode_info->bpp / 8;
fb_stride = fb_bpp * fb_width;
if(fb_bpp != 4)
{
printf("WARNING: BPP != 32, falling back to text mode...\n");
return false;
}
gfx_reset();
set_putchar(gfx_putchar);
set_puts(gfx_puts);
return true;
}
|