From 7caa47b9fbd43b2d4cf40dcc9b96d79cc386a0ba Mon Sep 17 00:00:00 2001 From: koniifer Date: Thu, 17 Oct 2024 15:31:42 +0100 Subject: [PATCH] make surface example cooler --- sysdata/libraries/render/src/lib.hb | 40 +++---------------- sysdata/libraries/render/src/software.hb | 39 ++++++++++++++++-- sysdata/libraries/stn/src/memory.hb | 9 ++++- .../render_example/src/examples/surface.hb | 7 ++-- sysdata/system_config.toml | 12 +++--- 5 files changed, 58 insertions(+), 49 deletions(-) diff --git a/sysdata/libraries/render/src/lib.hb b/sysdata/libraries/render/src/lib.hb index 3bb56c3..9227f52 100644 --- a/sysdata/libraries/render/src/lib.hb +++ b/sysdata/libraries/render/src/lib.hb @@ -1,4 +1,3 @@ -.{memory} := @use("../../stn/src/lib.hb") software := @use("software.hb") image := @use("image.hb") @@ -7,34 +6,11 @@ mode := software init := mode.init doublebuffer := mode.doublebuffer - -Surface := struct { - buf: ^Color, - width: int, - height: int, -} - -new_surface := fn(width: int, height: int): Surface { - return .( - @inline(memory.alloc, Color, width * height * @bitcast(@sizeof(Color))), - width, - height, - ) -} - -surface_from_ptr := fn(ptr: ^Color, width: int, height: int): Surface { - return .( - ptr, - width, - height, - ) -} - -clone_surface := fn(surface: Surface): Surface { - new := new_surface(surface.width, surface.height) - @inline(memory.copy, Color, surface.buf, new.buf, @intcast(surface.width * surface.height)) - return new -} +Surface := mode.Surface +new_surface := mode.new_surface +surface_from_ptr := mode.surface_from_ptr +clone_surface := mode.clone_surface +free_surface := mode.free_surface // Colours Color := packed struct {b: u8, g: u8, r: u8, a: u8} @@ -68,10 +44,4 @@ put_vline := mode.put_vline put_hline := mode.put_hline // Display -// width := mode.width -// height := mode.height -// dimensions := mode.dimensions -// set_height := mode.set_height -// set_width := mode.set_width -// set_dimensions := mode.set_dimensions sync := mode.sync \ No newline at end of file diff --git a/sysdata/libraries/render/src/software.hb b/sysdata/libraries/render/src/software.hb index ae8905b..8cd0b74 100644 --- a/sysdata/libraries/render/src/software.hb +++ b/sysdata/libraries/render/src/software.hb @@ -1,14 +1,47 @@ .{math, memory, dt} := @use("../../stn/src/lib.hb"); -.{Color, Surface, new_surface} := @use("lib.hb"); +.{Color} := @use("lib.hb"); .{Vec2} := math +Surface := struct { + buf: ^Color, + width: int, + height: int, +} + +new_surface := fn(width: int, height: int): Surface { + return .( + @inline(memory.alloc, Color, width * height), + width, + height, + ) +} + +surface_from_ptr := fn(ptr: ^Color, width: int, height: int): Surface { + return .( + ptr, + width, + height, + ) +} + +clone_surface := fn(surface: ^Surface): Surface { + new := new_surface(surface.width, surface.height) + @inline(memory.copy, Color, surface.buf, new.buf, @intcast(surface.width * surface.height)) + return new +} + +free_surface := fn(surface: ^Surface): void { + // todo + return +} + framebuffer := @as(^Color, idk) -init := fn(double_buffer: bool): Surface { +init := fn(doublebuffer: bool): Surface { framebuffer = dt.get(^Color, "framebuffer/fb0/ptr\0") width := dt.get(int, "framebuffer/fb0/width\0") height := dt.get(int, "framebuffer/fb0/height\0") - if double_buffer { + if doublebuffer { return new_surface(width, height) } else { return .(framebuffer, width, height) diff --git a/sysdata/libraries/stn/src/memory.hb b/sysdata/libraries/stn/src/memory.hb index 9b89cbe..b51c5ad 100644 --- a/sysdata/libraries/stn/src/memory.hb +++ b/sysdata/libraries/stn/src/memory.hb @@ -1,8 +1,8 @@ PAGE_SIZE := 4096 MAX_ALLOC := 0xFF -alloc := fn($Expr: type, bytes: int): ^Expr { - pages := 1 + bytes / PAGE_SIZE +alloc := fn($Expr: type, num: int): ^Expr { + pages := 1 + @bitcast(@sizeof(Expr)) * num / PAGE_SIZE if pages <= MAX_ALLOC { return @bitcast(@inline(request_page, pages)) } @@ -19,6 +19,11 @@ alloc := fn($Expr: type, bytes: int): ^Expr { return @bitcast(ptr) } +free := fn($Expr: type, ptr: ^Expr, num: int): void { + // todo + return +} + request_page := fn(page_count: u8): ^u8 { msg := "\{00}\{01}xxxxxxxx\0" msg_page_count := msg + 1; diff --git a/sysdata/programs/render_example/src/examples/surface.hb b/sysdata/programs/render_example/src/examples/surface.hb index 191c8ad..a3b75ac 100644 --- a/sysdata/programs/render_example/src/examples/surface.hb +++ b/sysdata/programs/render_example/src/examples/surface.hb @@ -16,13 +16,14 @@ example := fn(): void { pos_inner := Vec2(int).((image.width - side) / 2, (image.height - side) / 2) color := random.range(render.Color, render.black, render.white) loop { - render.clear(image, render.black) render.clear(screen, render.black) - + // color += .(1, 1, 1, 1) render.put_filled_rect(image, pos_inner, .(side, side), color) - render.put_rect(image, .(0, 0), .(image.width - 1, image.height - 1), render.white) + render.put_rect(image, pos_inner, .(side, side), render.black) + render.put_rect(image, .(0, 0), .(image.width - 1, image.height - 1), color) render.put_surface(screen, image, pos) + render.put_rect(image, pos_inner, .(side, side), color) render.sync(screen) if pos_inner.x == 0 | pos_inner.x == image.width - side { diff --git a/sysdata/system_config.toml b/sysdata/system_config.toml index 835b3fd..e5deef9 100644 --- a/sysdata/system_config.toml +++ b/sysdata/system_config.toml @@ -31,17 +31,17 @@ resolution = "1024x768x24" # [boot.limine.ableos.modules.diskio_driver] # path = "boot:///diskio_driver.hbf" -# [boot.limine.ableos.modules.render_example] -# path = "boot:///render_example.hbf" +[boot.limine.ableos.modules.render_example] +path = "boot:///render_example.hbf" # [boot.limine.ableos.modules.serial_driver_test] # path = "boot:///serial_driver_test.hbf" -[boot.limine.ableos.modules.horizon] -path = "boot:///horizon.hbf" +# [boot.limine.ableos.modules.horizon] +# path = "boot:///horizon.hbf" -[boot.limine.ableos.modules.horizon_testing_program] -path = "boot:///horizon_testing_program.hbf" +# [boot.limine.ableos.modules.horizon_testing_program] +# path = "boot:///horizon_testing_program.hbf" # [boot.limine.ableos.modules.dt_buffer_test] # path = "boot:///dt_buffer_test.hbf"