From af1c36b894f9c66007ab831fc1db83733b72b1db Mon Sep 17 00:00:00 2001 From: kohsine Date: Sun, 31 Mar 2024 18:47:45 -0400 Subject: [PATCH] dark example --- Cargo.toml | 4 +- examples/dark.rs | 118 +++++++++++++++++++++++++++++++++++++++++ examples/sqr.rs | 88 ------------------------------ src/modules/windows.rs | 9 ++-- 4 files changed, 126 insertions(+), 93 deletions(-) create mode 100644 examples/dark.rs delete mode 100644 examples/sqr.rs diff --git a/Cargo.toml b/Cargo.toml index d6ac20d..7c11c39 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,8 +15,8 @@ name = "con" path = "examples/con.rs" [[example]] -name = "sqr" -path = "examples/sqr.rs" +name = "dark" +path = "examples/dark.rs" [profile.release] opt-level = "z" diff --git a/examples/dark.rs b/examples/dark.rs new file mode 100644 index 0000000..6b75c8f --- /dev/null +++ b/examples/dark.rs @@ -0,0 +1,118 @@ +#![no_std] +#![no_main] + +use cstr_core::{cstr, CStr}; + +use kos::{ + graphics::{display_message, Color, Dot, Size}, + input::fetch_key, + threads::{exit, fetch_event, Event}, + windows::{ + define_button, define_window, end_window_draw, get_button_id, invert_pixel, + start_window_draw, WindowKind, WindowParams, CLOSE_BUTTON, + }, +}; + +const HEADER: &CStr = cstr!("Dark Mode Demo"); +const TEXT: [&CStr; 6] = [ + cstr!("Lorem ipsum dolor sit amet,"), + cstr!("semper et rutrum placerat,"), + cstr!("Integer sed diam commodo quam varius"), + cstr!("Sed finibus urna sit amet felis"), + cstr!("vestibulum elementum. Maecenas at feugiat lacus"), + cstr!("tristique et sit amet tortor."), +]; +const BTN: u32 = 42; +const WINDOW_SIZE: Size = Size { + width: 400, + height: 400, +}; + +extern crate alloc; + +fn draw_window(invert: bool) { + start_window_draw(); + + define_window( + Dot { x: 50, y: 50 }, + WINDOW_SIZE, + WindowParams { + color: Color::rgb(0xff, 0xff, 0xff), + kind: WindowKind::FixedThemed, + title: Some(HEADER), + }, + ); + + display_message(Dot { x: 10, y: 10 }, Color::rgb(0, 0, 0), TEXT[0], None); + display_message(Dot { x: 10, y: 50 }, Color::rgb(0, 0, 0), TEXT[1], None); + display_message(Dot { x: 10, y: 90 }, Color::rgb(0, 0, 0), TEXT[2], None); + display_message(Dot { x: 10, y: 130 }, Color::rgb(0, 0, 0), TEXT[3], None); + display_message(Dot { x: 10, y: 170 }, Color::rgb(0, 0, 0), TEXT[4], None); + display_message(Dot { x: 10, y: 210 }, Color::rgb(0, 0, 0), TEXT[5], None); + + define_button( + Dot { x: 10, y: 300 }, + Size { + width: 100, + height: 30, + }, + BTN, + true, + true, + Some(Color::rgb(147, 112, 219)), + ); + + display_message( + Dot { x: 20, y: 310 }, + Color::rgb(255, 255, 255), + if invert { + cstr!("Light mode") + } else { + cstr!("Dark mode") + }, + None, + ); + + if invert { + for x in 0..WINDOW_SIZE.width { + for y in 0..WINDOW_SIZE.height { + invert_pixel(Dot { x, y }) + } + } + } + + end_window_draw(); + + return; +} + +fn button_handler(invert: &mut bool) { + let btn_id = get_button_id(); + + if btn_id.is_some() { + match btn_id.unwrap() { + CLOSE_BUTTON => exit(), + BTN => { + *invert = !*invert; + draw_window(*invert); + } + _ => {} + } + } +} + +#[no_mangle] +fn kol_main() { + let mut invert = false; + + while let Some(ev) = + fetch_event((Event::Redraw as u32) | (Event::KeyPress as u32) | (Event::BtnPress as u32)) + { + match ev { + Event::Redraw => draw_window(invert), + Event::KeyPress => drop(fetch_key()), + Event::BtnPress => button_handler(&mut invert), + _ => break, + } + } +} diff --git a/examples/sqr.rs b/examples/sqr.rs deleted file mode 100644 index 5c3875c..0000000 --- a/examples/sqr.rs +++ /dev/null @@ -1,88 +0,0 @@ -#![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 92d7970..5c8ff7a 100644 --- a/src/modules/windows.rs +++ b/src/modules/windows.rs @@ -39,10 +39,13 @@ 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; +pub fn put_pixel(pos: Dot, color: Option) { 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) } + unsafe { sys::put_pixel(pos.x, pos.y, color) } +} + +pub fn invert_pixel(pos: Dot) { + unsafe { sys::put_pixel(pos.x, pos.y, 1 << 24) } } pub fn define_button(