From 27ea16d17128ddddc6521c4e31268f410cd51067 Mon Sep 17 00:00:00 2001 From: Kevin Date: Sat, 30 Mar 2024 17:36:54 -0400 Subject: [PATCH] feat: put_pixel --- Cargo.toml | 4 ++ examples/sqr.rs | 88 ++++++++++++++++++++++++++++++++++++++++++ src/modules/windows.rs | 6 +++ src/sys.rs | 4 ++ src/syscalls.S | 9 +++++ 5 files changed, 111 insertions(+) create mode 100644 examples/sqr.rs diff --git a/Cargo.toml b/Cargo.toml index 4a39710..d6ac20d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +14,10 @@ path = "examples/hwa.rs" name = "con" path = "examples/con.rs" +[[example]] +name = "sqr" +path = "examples/sqr.rs" + [profile.release] opt-level = "z" lto = "thin" diff --git a/examples/sqr.rs b/examples/sqr.rs new file mode 100644 index 0000000..5c3875c --- /dev/null +++ b/examples/sqr.rs @@ -0,0 +1,88 @@ +#![no_std] +#![no_main] + +use alloc::vec::Vec; +use cstr_core::{cstr, CStr}; + +use kos::{ + graphics::{display_message, Color, Dot, Size}, + input::fetch_key, + system::{get_lang, Lang}, + threads::{exit, fetch_event, Event}, + windows::{ + define_button, define_window, end_window_draw, get_button_id, put_pixel, start_window_draw, + WindowKind, WindowParams, CLOSE_BUTTON, + }, +}; + +const HEADER: &CStr = cstr!("Hey Kolibri"); +const MSG: &CStr = cstr!("Hello GSoC 2024"); +const BTN: u32 = 42; +const COLORS: [(u8, u8, u8); 3] = [(255, 0, 0), (0, 255, 0), (0, 0, 255)]; + +#[macro_use] +extern crate alloc; + +fn draw_window(c: usize) { + start_window_draw(); + + define_window( + Dot { x: 50, y: 50 }, + Size { + width: 500, + height: 500, + }, + WindowParams { + color: Color::rgb(0xff, 0xff, 0xff), + kind: WindowKind::Themed, + title: Some(HEADER), + }, + ); + + for i in 60..300 { + for j in 60..300 { + put_pixel(Dot { x: i, y: j }, Some(Color::rgb(255, 0, 0)), None); + } + } + + for i in 200..440 { + for j in 200..440 { + put_pixel(Dot { x: i, y: j }, None, Some(true)); + } + } + + end_window_draw(); + + return; +} + +fn button_handler(c: &mut usize) { + let btn_id = get_button_id(); + + if btn_id.is_some() { + match btn_id.unwrap() { + CLOSE_BUTTON => exit(), + BTN => { + *c += 1; + draw_window(*c); + } + _ => {} + } + } +} + +#[no_mangle] +fn kol_main() { + let mut c = 0; + + while let Some(ev) = + fetch_event((Event::Redraw as u32) | (Event::KeyPress as u32) | (Event::BtnPress as u32)) + { + match ev { + Event::Redraw => draw_window(c), + Event::KeyPress => drop(fetch_key()), + Event::BtnPress => button_handler(&mut c), + _ => break, + } + } +} diff --git a/src/modules/windows.rs b/src/modules/windows.rs index d6e5785..92d7970 100644 --- a/src/modules/windows.rs +++ b/src/modules/windows.rs @@ -39,6 +39,12 @@ pub fn define_window(start: Dot, size: Size, params: WindowParams<'_>) { } } +pub fn put_pixel(pos: Dot, color: Option, invert: Option) { + let invert: u32 = invert.unwrap_or(false) as u32; + let color: u32 = color.unwrap_or(Color::rgb(255, 255, 255)).as_rgb_val(); + unsafe { sys::put_pixel(pos.x, pos.y, invert << 24 | color) } +} + pub fn define_button( start: Dot, size: Size, diff --git a/src/sys.rs b/src/sys.rs index c0bd7e1..41188e8 100644 --- a/src/sys.rs +++ b/src/sys.rs @@ -8,6 +8,10 @@ extern "C" { #[link_name = "_define_window"] pub fn define_window(ebx: u32, ecx: u32, edx: u32, esi: u32, edi: u32); + // 1 + #[link_name = "_put_pixel"] + pub fn put_pixel(ebx: u32, ecx: u32, edx: u32); + // 2 #[link_name = "_pressed_key"] pub fn pressed_key() -> u32; diff --git a/src/syscalls.S b/src/syscalls.S index b314bf1..85570eb 100644 --- a/src/syscalls.S +++ b/src/syscalls.S @@ -7,6 +7,7 @@ section '.text' public _start_window_draw public _end_window_draw public _define_window + public _put_pixel public _display_message public _wait_event public _pressed_key @@ -50,6 +51,14 @@ _define_window: pop edi ret +_put_pixel: + mov eax, SF_PUT_PIXEL + mov ebx, dword [esp + 4 * 1] + mov ecx, dword [esp + 4 * 2] + mov edx, dword [esp + 4 * 3] + int 0x40 + ret + _display_message: push esi edi mov eax, SF_DRAW_TEXT