From 447bb48501efdaa88cd2deb2c71011cb2ecd2190 Mon Sep 17 00:00:00 2001 From: sweetbread Date: Thu, 11 Jan 2024 20:01:27 +0300 Subject: [PATCH] style: Make start/end_window_draw safe and Size struct for define window --- example/hwa.rs | 45 +++++++++++++++++++++++---------------------- src/lib.rs | 19 ++++++++++++++++--- 2 files changed, 39 insertions(+), 25 deletions(-) diff --git a/example/hwa.rs b/example/hwa.rs index 9c51f96..f5fa150 100644 --- a/example/hwa.rs +++ b/example/hwa.rs @@ -3,34 +3,35 @@ use cstr_core::{cstr, CStr}; -use kos::{Color, Dot, Event, WindowKind, WindowParams, WindowTextParams}; +use kos::{Color, Dot, Event, Size, WindowKind, WindowParams, WindowTextParams}; const HEADER: &CStr = cstr!("Hey Kolibri"); const MSG: &str = "Hello from Rust!"; #[inline(always)] // for some reason function removed otherwise fn draw_window() { - unsafe { - kos::start_window_draw(); - kos::define_window( - Dot { x: 50, y: 50 }, - 300, 400, - WindowParams { - color: Color::rgb(0xff, 0xff, 0xff), - kind: WindowKind::Themed, - title: Some(HEADER), - }, - ); - kos::display_message( - Dot { x: 0, y: 10 }, - WindowTextParams { - color: Color::rgb(0x66, 0x22, 0x22), - text: MSG, - bg_color: None, - }, - ); - kos::end_window_draw(); - } + kos::start_window_draw(); + kos::define_window( + Dot { x: 50, y: 50 }, + Size { + width: 300, + height: 400, + }, + WindowParams { + color: Color::rgb(0xff, 0xff, 0xff), + kind: WindowKind::Themed, + title: Some(HEADER), + }, + ); + kos::display_message( + Dot { x: 0, y: 10 }, + WindowTextParams { + color: Color::rgb(0x66, 0x22, 0x22), + text: MSG, + bg_color: None, + }, + ); + kos::end_window_draw(); } #[no_mangle] diff --git a/src/lib.rs b/src/lib.rs index 59ff7ce..fe13558 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -35,6 +35,11 @@ pub struct Dot { pub y: u32, } +pub struct Size { + pub width: u32, + pub height: u32, +} + #[repr(u32)] pub enum WindowKind { Fixed = 0, @@ -50,13 +55,13 @@ pub struct WindowParams<'a> { pub title: Option<&'a cstr_core::CStr>, } -pub fn define_window(start: Dot, width: u32, height: u32, params: WindowParams<'_>) { +pub fn define_window(start: Dot, size: Size, params: WindowParams<'_>) { const RELATIVE_FLAG: u32 = 0x20; unsafe { sys::define_window( - start.x * 65536 + width, - start.y * 65536 + height, + start.x * 65536 + size.width, + start.y * 65536 + size.height, params.color.as_rgb_val() | (RELATIVE_FLAG | (params.title.is_some() as u32) << 4 | params.kind as u32) << 24, 0, @@ -93,6 +98,14 @@ pub fn exit() -> ! { unsafe { sys::exit() } } +pub fn start_window_draw() { + unsafe { sys::start_window_draw() } +} + +pub fn end_window_draw() { + unsafe { sys::end_window_draw() } +} + #[panic_handler] fn panic(_info: &core::panic::PanicInfo) -> ! { exit();