summaryrefslogtreecommitdiff
path: root/kernel
diff options
context:
space:
mode:
authorAleksa Vuckovic <aleksav013@gmail.com>2022-08-04 09:00:14 +0200
committerAleksa Vuckovic <aleksav013@gmail.com>2022-08-04 09:00:14 +0200
commitf622bf0d79a7460cb160207bcd75f257deb872ea (patch)
tree983c87fc43d2afb9fc4e3d119fbefd94a3a36530 /kernel
parentd4349352a57eb00ce411b4c0542d3207357aecbe (diff)
drawing characters to fb; fixed draw_line
Diffstat (limited to 'kernel')
-rw-r--r--kernel/graphics.c58
-rw-r--r--kernel/main.c21
-rw-r--r--kernel/multiboot2.c2
3 files changed, 64 insertions, 17 deletions
diff --git a/kernel/graphics.c b/kernel/graphics.c
index da5ed5d..d27a3e3 100644
--- a/kernel/graphics.c
+++ b/kernel/graphics.c
@@ -1,38 +1,42 @@
#include <stdint.h>
#include <multiboot2.h>
#include <graphics.h>
-#include <debug.h>
+#include <font.h>
uint64_t* pixel_offset(fb_t fb, uint32_t x, uint32_t y)
{
- return (uint64_t*)((uint32_t*)fb.addr + y * fb.pitch / 4 + x);
+ return (uint64_t*)((char*)fb.addr + y * fb.pitch + x * fb.bpp / 8);
}
void fb_draw_pixel(fb_t fb, uint32_t x, uint32_t y, uint32_t col)
{
- uint64_t* fb_offset = pixel_offset(fb, x, y);
+ if (x >= fb.width || y >= fb.height) return;
+
+ uint32_t* fb_offset = (uint32_t*)pixel_offset(fb, x, y);
*fb_offset = col;
}
+/* https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm */
+
void fb_draw_line_low(fb_t fb, uint32_t x0, uint32_t y0, uint32_t x1, uint32_t y1, uint32_t col)
{
- uint32_t dx = x1 - x0;
- uint32_t dy = y1 - y0;
- uint32_t yi = 1;
+ int32_t dx = x1 - x0;
+ int32_t dy = y1 - y0;
+ int32_t yi = 1;
if (dy < 0) {
yi = -1;
dy = -dy;
}
- uint32_t D = (2 * dy) - dx;
- uint32_t y = y0;
+ int32_t D = (2 * dy) - dx;
+ int32_t y = y0;
- for (uint32_t x = x0; x < x1; x++) {
+ for (int32_t x = x0; x <= x1; x++) {
fb_draw_pixel(fb, x, y, col);
if (D > 0) {
y = y + yi;
D = D + (2 * (dy - dx));
} else {
- D = D + 2*dy;
+ D = D + 2 * dy;
}
}
@@ -56,12 +60,12 @@ void fb_draw_line_high(fb_t fb, int32_t x0, int32_t y0, int32_t x1, int32_t y1,
x = x + xi;
D = D + (2 * (dx - dy));
} else {
- D = D + 2*dx;
+ D = D + 2 * dx;
}
}
}
-uint32_t abs(int64_t val)
+int32_t abs(int32_t val)
{
if (val < 0) return -val;
return val;
@@ -81,3 +85,33 @@ void fb_draw_line(fb_t fb, int32_t x0, int32_t y0, int32_t x1, int32_t y1, int32
fb_draw_line_high(fb, x0, y0, x1, y1, col);
}
}
+
+void fb_draw_character(fb_t fb, char c, uint32_t x, uint32_t y, uint32_t char_col, uint32_t bg_col)
+{
+ uint32_t offset = 32 + c * 16;
+ for (uint32_t i = 0 ; i < 16; i++)
+ {
+ for (uint32_t j = 0 ; j < 8; j++)
+ {
+ if (font[offset + i] & (1 << (7 - j))) {
+ fb_draw_pixel(fb, x + j, y + i, char_col);
+ } else {
+ fb_draw_pixel(fb, x + j, y + i, bg_col);
+ }
+ }
+ }
+}
+
+uint32_t strlen(const char* s)
+{
+ char *p = (char*)s;
+ while (*p) p++;
+ return p - s;
+}
+
+void fb_draw_string(fb_t fb, const char* s, uint32_t x, uint32_t y, uint32_t char_col, uint32_t bg_col)
+{
+ for (uint32_t i = 0; i < strlen(s); i++) {
+ fb_draw_character(fb, s[i], x + i * 8, y, char_col, bg_col);
+ }
+}
diff --git a/kernel/main.c b/kernel/main.c
index d868b42..86b095d 100644
--- a/kernel/main.c
+++ b/kernel/main.c
@@ -7,14 +7,25 @@ int kernel_main(mb2_tag_header* multiboot_bootinfo, uint32_t multiboot_magic)
{
init_fb(multiboot_bootinfo, multiboot_magic);
- static uint32_t color = BLACK;
- for (uint32_t x = 0 ; x < fb.width; x++) {
- for (uint32_t y = 0; y < fb.height; y++) {
- fb_draw_pixel(fb, x, y, color);
+ for (int x = 0; x < fb.width; x++) {
+ for (int y = 0; y < fb.width; y++) {
+ fb_draw_pixel(fb, x , y, BLUE);
}
}
- fb_draw_line(fb, 0, 0, 100, 1200, WHITE);
+ fb_draw_line(fb, 0, 0, 100, 100, WHITE);
+ fb_draw_line(fb, 0, 0, 100, 200, WHITE);
+ fb_draw_line(fb, 0, 0, 100, 300, WHITE);
+
+ fb_draw_line(fb, 100, 100, 200, 200, YELLOW);
+ fb_draw_line(fb, 100, 100, 300, 200, YELLOW);
+ fb_draw_line(fb, 100, 100, 400, 200, YELLOW);
+
+ fb_draw_line(fb, 500, 100, 300, 500, RED);
+ fb_draw_line(fb, 300, 500, 700, 500, RED);
+ fb_draw_line(fb, 700, 500, 500, 100, RED);
+
+ fb_draw_string(fb, "aleksa vuckovic 1234", 420, 300, WHITE, BLUE);
return 0;
}
diff --git a/kernel/multiboot2.c b/kernel/multiboot2.c
index 9b2e04e..e228ce1 100644
--- a/kernel/multiboot2.c
+++ b/kernel/multiboot2.c
@@ -4,6 +4,8 @@
fb_t fb;
+/* https://www.gnu.org/software/grub/manual/multiboot2/html_node/Boot-information-format.html */
+
void init_fb(mb2_tag_header* multiboot_bootinfo, uint32_t multiboot_magic)
{
if (multiboot_magic != MB2_MAGIC) {