setos

拙OS
Log | Files | Refs | LICENSE

commit 2c3931802b72d7c924653b5c624c9043070d838f
parent a27eaae2b28933a45ef2d65fe2f2ac004c40a76a
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Tue,  2 Apr 2024 09:30:25 +0900

draw @

Diffstat:
Msys/include/draw.h | 9++++++---
Msys/src/kernel/draw.c | 34++++++++++++++++++++++++++++------
Msys/src/kernel/main.c | 13++++++++++++-
3 files changed, 46 insertions(+), 10 deletions(-)

diff --git a/sys/include/draw.h b/sys/include/draw.h @@ -21,12 +21,15 @@ typedef struct Image { typedef struct Window { RGBA32 *fb; - int hrez; - int vrez; + RGBA32 fg; + RGBA32 bg; + int hres; + int vres; int ppsl; // pixels per scan line. Window *children; int nchildren; } Window; -error init_root_window(RGBA32 *fb_base, int hrez, int vrez, int ppsl); +error init_root_window(RGBA32 *fb_base, int hres, int vres, int ppsl); +void pixel(Window *dst, Point p, RGBA32 col); void line(Window *dst, Point p0, Point p1, RGBA32 col); \ No newline at end of file diff --git a/sys/src/kernel/draw.c b/sys/src/kernel/draw.c @@ -4,11 +4,13 @@ Window root_window; error -init_root_window(RGBA32 *fb_base, int hrez, int vrez, int ppsl) +init_root_window(RGBA32 *fb_base, int hres, int vres, int ppsl) { root_window.fb = fb_base; - root_window.hrez = hrez; - root_window.vrez = vrez; + root_window.fg = 0x0; + root_window.bg = 0xffffea; + root_window.hres = hres; + root_window.vres = vres; root_window.ppsl = ppsl; root_window.children = NULL; root_window.nchildren = 0; @@ -16,12 +18,32 @@ init_root_window(RGBA32 *fb_base, int hrez, int vrez, int ppsl) } void +pixel(Window *dst, Point p, RGBA32 col) +{ + dst->fb[p.y * dst->ppsl + p.x] = col; +} + +void line(Window *dst, Point p0, Point p1, RGBA32 col) { + // TODO: wip. I need debug print. int x, y; - for (x = p0.x; x < p1.x; x++) { - y = (x - p0.x) * (p1.y - p0.y) / (p1.x - p0.x) + p0.y; - if (x < 0 || dst->hrez <= x || y < 0 || dst->vrez <= y) { + int dx = p1.x - p0.x; + int dy = p1.y - p0.y; + float err = 0; + float derr = (float)dx / (float)dy; + if (derr < 0) { + derr = -derr; + } + x = p0.x; + y = (x - p0.x) * (p1.y - p0.y) / (p1.x - p0.x) + p0.y; + for (; x < p1.x; x++) { + err += derr; + if (err > 0.5) { + y++; + err -= 1.0; + } + if (y < 0 || dst->vres <= y) { continue; // TODO: break if the rest is outside the window. } dst->fb[y * dst->ppsl + x] = col; // TODO: alpha diff --git a/sys/src/kernel/main.c b/sys/src/kernel/main.c @@ -2,6 +2,8 @@ #include <libc.h> #include <draw.h> +uint8 font[16] = {0x0, 0x18, 0x24, 0x42, 0x5a, 0xb5, 0xa5, 0xa5, 0xa5, 0x9a, 0x40, 0x40, 0x22, 0x1c, 0x0, 0x0}; + void kernel_main(EFI_GRAPHICS_OUTPUT_PROTOCOL *gop) { @@ -11,6 +13,15 @@ kernel_main(EFI_GRAPHICS_OUTPUT_PROTOCOL *gop) gop->Mode->Info->VerticalResolution, gop->Mode->Info->PixelsPerScanLine ); - line(&root_window, (Point) {10, 10}, (Point) {100000, 100000}, 0xff00ff); + line(&root_window, (Point) {10, 10}, (Point) {50, 15}, 0xff00ff); + for (int i = 0; i < 16; i++) { + for (int j = 0; j < 8; j++) { + if (((font[i] >> (8 - j - 1)) & 1) == 1) { + pixel(&root_window, (Point) {j, i}, root_window.fg); + } else { + pixel(&root_window, (Point) {j, i}, root_window.bg); + } + } + } for(;;); } \ No newline at end of file