Commit 152c913f authored by Jeremy Soller's avatar Jeremy Soller
Browse files

Merge branch 'cleanup' into 'master'

Code cleanup

See merge request !68
parents 8ef4433d 58ed34c7
Pipeline #9769 passed with stage
in 1 minute and 33 seconds
image: "redoxos/redoxer"
build:linux:
image: "redoxos/redoxer"
before_script:
- apt-get install cmake -y
script:
- cargo build
build:redox:
image: "redoxos/redoxer"
script:
- redoxer build
build:wasm32:
image: "rust:latest"
before_script:
- rustup toolchain add stable
- rustup target add wasm32-unknown-unknown --toolchain stable
script:
- cargo +stable build --no-default-features --target wasm32-unknown-unknown
......@@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## unreleased
* Change Rust edition from 2015 to 2018
* Change `Color` from `repr(packed)` to `repr(transparent)`
## 0.3.31
* Web support
......@@ -14,4 +19,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Add HiDPi support for sdl2 sys
* Add DropEvent (file | text) for sdl2 sys
* Add raw-window-handle implementation for sdl2
* Add TextInputEvent
\ No newline at end of file
* Add TextInputEvent
......@@ -7,6 +7,7 @@ documentation = "https://docs.rs/orbclient"
repository = "https://gitlab.redox-os.org/redox-os/orbclient"
readme = "README.md"
license = "MIT"
edition = "2018"
keywords = [
"orbital",
"redox",
......@@ -33,19 +34,19 @@ wasm-bindgen = "0.2.69"
[target.'cfg(target_arch = "wasm32")'.dependencies.web-sys]
version = "0.3"
features = [
"Node",
"Element",
"console",
"CanvasRenderingContext2d",
"Document",
"Element",
"ImageData",
"HtmlCanvasElement",
"HtmlElement",
"Window",
"CssStyleDeclaration",
"MouseEvent",
"KeyboardEvent",
"Node",
"Element",
"console",
"CanvasRenderingContext2d",
"Document",
"Element",
"ImageData",
"HtmlCanvasElement",
"HtmlElement",
"Window",
"CssStyleDeclaration",
"MouseEvent",
"KeyboardEvent",
"WheelEvent",
"FocusEvent",
"DragEvent",
......
extern crate orbclient;
// SPDX-License-Identifier: MIT
use orbclient::{Color, EventOption, Renderer, Window};
......
extern crate orbclient;
// SPDX-License-Identifier: MIT
use orbclient::{Color, EventOption, Renderer, Window};
......@@ -40,6 +40,7 @@ fn main() {
'events: loop {
for event in window.events() {
#[allow(clippy::single_match)]
match event.to_option() {
EventOption::Quit(_quit_event) => break 'events,
_ => (),
......
extern crate orbclient;
// SPDX-License-Identifier: MIT
use orbclient::{Color, EventOption, GraphicsPath, Mode, Renderer, Window};
......
......@@ -2,6 +2,7 @@
name = "simple"
version = "0.1.0"
authors = ["Florian Blasius <flovanpt@posteo.de>"]
edition = "2018"
[lib]
crate-type = ["cdylib", "rlib"]
......@@ -29,4 +30,4 @@ wasm-bindgen-test = "0.3.8"
[profile.release]
# Tell `rustc` to optimize for small code size.
opt-level = "s"
\ No newline at end of file
opt-level = "s"
......@@ -6,8 +6,6 @@ use wasm_bindgen::prelude::*;
use orbclient::{animation_loop, log, Color, EventOption, GraphicsPath, Mode, Renderer, Window};
use std::panic;
#[wasm_bindgen(start)]
pub fn start() {
console_error_panic_hook::set_once();
......
// SPDX-License-Identifier: MIT
/*
Inspired from http://blog.ivank.net/fastest-gaussian-blur.html the algorithm 4.
The struct MathColor is needed for the calculate with bigger numbers, the Color struct save the r,g,b values with a u8.
......@@ -7,7 +9,7 @@ The struct MathColor is needed for the calculate with bigger numbers, the Color
use alloc::vec::Vec;
use core::ops::{Add, AddAssign, Sub};
use Color;
use crate::color::Color;
#[derive(Copy, Clone)]
pub struct MathColor {
......
#[cfg(not(feature = "no_std"))]
use std::fmt;
// SPDX-License-Identifier: MIT
use core::fmt;
/// A color
#[derive(Copy, Clone)]
#[repr(packed)]
#[repr(transparent)]
pub struct Color {
pub data: u32,
}
......@@ -83,7 +84,6 @@ impl PartialEq for Color {
}
}
#[cfg(not(feature = "no_std"))]
impl fmt::Debug for Color {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(f, "{:#010X}", { self.data })
......@@ -92,11 +92,21 @@ impl fmt::Debug for Color {
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn partial_eq() {
use Color;
assert_eq!(true, Color::rgb(1, 2, 3) == Color::rgba(1, 2, 3, 200));
assert_eq!(false, Color::rgb(1, 2, 3) == Color::rgba(11, 2, 3, 200));
assert_eq!(true, Color::rgba(1, 2, 3, 200) == Color::rgba(1, 2, 3, 200));
assert_eq!(Color::rgb(1, 2, 3), Color::rgba(1, 2, 3, 200));
assert_ne!(Color::rgb(1, 2, 3), Color::rgba(11, 2, 3, 200));
assert_eq!(Color::rgba(1, 2, 3, 200), Color::rgba(1, 2, 3, 200));
}
#[test]
fn alignment() {
assert_eq!(4, core::mem::size_of::<Color>());
assert_eq!(8, core::mem::size_of::<[Color; 2]>());
assert_eq!(12, core::mem::size_of::<[Color; 3]>());
assert_eq!(16, core::mem::size_of::<[Color; 4]>());
assert_eq!(20, core::mem::size_of::<[Color; 5]>());
}
}
// SPDX-License-Identifier: MIT
use core::ops::{Deref, DerefMut};
use core::{char, mem, slice};
......@@ -66,6 +68,7 @@ pub struct Event {
pub b: i64,
}
#[allow(clippy::new_without_default)]
impl Event {
/// Create a null event
pub fn new() -> Event {
......
// SPDX-License-Identifier: MIT
#[cfg(feature = "no_std")]
use alloc::vec::Vec;
......@@ -15,6 +17,7 @@ pub struct GraphicsPath {
pub points: Vec<(i32, i32, PointType)>,
}
#[allow(clippy::new_without_default)]
impl GraphicsPath {
pub fn new() -> GraphicsPath {
GraphicsPath {
......@@ -61,7 +64,7 @@ impl GraphicsPath {
x += tt * (argx2 as f32);
y += tt * (argy2 as f32);
t = t + 0.01;
t += 0.01;
self.points.push((x as i32, y as i32, PointType::Connect));
}
......@@ -107,7 +110,7 @@ impl GraphicsPath {
x += ttt * (argx3 as f32);
y += ttt * (argy3 as f32);
t = t + 0.01;
t += 0.01;
self.points.push((x as i32, y as i32, PointType::Connect));
}
......
// SPDX-License-Identifier: MIT
#![crate_name = "orbclient"]
#![crate_type = "lib"]
#![cfg_attr(feature = "no_std", feature(alloc))]
......@@ -8,7 +10,7 @@ extern crate alloc;
#[cfg(not(feature = "no_std"))]
extern crate core;
pub static FONT: &'static [u8] = include_bytes!("../res/unifont.font");
pub static FONT: &[u8] = include_bytes!("../res/unifont.font");
pub use color::Color;
pub use event::*;
......
// SPDX-License-Identifier: MIT
use core::cell::Cell;
use core::cmp;
#[cfg(not(feature = "no_std"))]
use blur;
use color::Color;
use graphicspath::GraphicsPath;
use graphicspath::PointType;
use Mode;
use FONT;
use crate::blur;
use crate::color::Color;
use crate::graphicspath::GraphicsPath;
use crate::graphicspath::PointType;
use crate::Mode;
use crate::FONT;
pub trait Renderer {
/// Get width
......@@ -42,7 +44,7 @@ pub trait Renderer {
if x >= 0 && y >= 0 && x < w as i32 && y < h as i32 {
let new = color.data;
let alpha = (new >> 24) & 0xFF;
let old = unsafe { &mut data[y as usize * w as usize + x as usize].data };
let old = &mut data[y as usize * w as usize + x as usize].data;
if alpha >= 255 || replace {
*old = new;
......@@ -63,6 +65,8 @@ pub trait Renderer {
let mut y = 0;
let mut err = 0;
// https://github.com/rust-lang/rust-clippy/issues/5354
#[allow(clippy::comparison_chain)]
while x >= y {
if radius < 0 {
if parts & 1 << 0 != 0 {
......@@ -230,7 +234,7 @@ pub trait Renderer {
}
fn lines(&mut self, points: &[[i32; 2]], color: Color) {
if points.len() == 0 {
if points.is_empty() {
// when no points given, do nothing
} else if points.len() == 1 {
self.pixel(points[0][0], points[0][1], color);
......@@ -253,9 +257,8 @@ pub trait Renderer {
let mut y: i32 = 0;
for point in graphicspath.points {
match point.2 {
PointType::Connect => self.line(x, y, point.0, point.1, color),
_ => {}
if let PointType::Connect = point.2 {
self.line(x, y, point.0, point.1, color)
}
x = point.0;
y = point.1;
......@@ -362,14 +365,15 @@ pub trait Renderer {
for y in start_y..end_y {
for x in start_x..end_x {
let a = blur_data[counter as usize];
let old = unsafe { &mut data[y as usize * self_w as usize + x as usize].data };
let old = &mut data[y as usize * self_w as usize + x as usize].data;
*old = a.data;
counter = counter + 1;
counter += 1;
}
}
}
#[allow(clippy::too_many_arguments)]
#[cfg(not(feature = "no_std"))]
fn box_shadow(
&mut self,
......@@ -423,7 +427,7 @@ pub trait Renderer {
{
self.pixel(new_x_b, new_y_b, col);
}
counter = counter + 1;
counter += 1;
}
}
}
......@@ -527,7 +531,7 @@ pub trait Renderer {
let new = image_data[i].data;
let alpha = (new >> 24) & 0xFF;
if alpha > 0 && (start + k) < window_data.len() && (start + k) < stop {
let old = unsafe { &mut window_data[start + k].data };
let old = &mut window_data[start + k].data;
if alpha >= 255 {
*old = new;
} else {
......@@ -548,6 +552,7 @@ pub trait Renderer {
}
/// Draw a linear gradient in a rectangular region
#[allow(clippy::too_many_arguments)]
#[cfg(not(feature = "no_std"))]
fn linear_gradient(
&mut self,
......@@ -624,6 +629,7 @@ pub trait Renderer {
}
/// Draw a rect with rounded corners
#[allow(clippy::too_many_arguments)]
fn rounded_rect(
&mut self,
x: i32,
......
extern crate syscall;
// SPDX-License-Identifier: MIT
use std::cell::Cell;
use std::fs::File;
......@@ -6,11 +6,11 @@ use std::io::{Read, Write};
use std::os::unix::io::{AsRawFd, FromRawFd, RawFd};
use std::{env, mem, slice, thread};
use color::Color;
use event::{Event, EVENT_RESIZE};
use renderer::Renderer;
use Mode;
use WindowFlag;
use crate::color::Color;
use crate::event::{Event, EVENT_RESIZE};
use crate::renderer::Renderer;
use crate::Mode;
use crate::WindowFlag;
pub fn get_display_size() -> Result<(u32, u32), String> {
let display_path = env::var("DISPLAY").or(Err("DISPLAY not set"))?;
......
extern crate sdl2;
// SPDX-License-Identifier: MIT
use std::cell::{Cell, RefCell};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::{mem, ptr, slice};
use color::Color;
use event::*;
use renderer::Renderer;
use Mode;
use WindowFlag;
use crate::color::Color;
use crate::event::*;
use crate::renderer::Renderer;
use crate::Mode;
use crate::WindowFlag;
static SDL_USAGES: AtomicUsize = AtomicUsize::new(0);
/// SDL2 Context
......@@ -23,8 +23,8 @@ static mut EVENT_PUMP: *mut sdl2::EventPump = ptr::null_mut();
unsafe fn init() {
if SDL_USAGES.fetch_add(1, Ordering::Relaxed) == 0 {
SDL_CTX = Box::into_raw(Box::new(sdl2::init().unwrap()));
VIDEO_CTX = Box::into_raw(Box::new((&mut *SDL_CTX).video().unwrap()));
EVENT_PUMP = Box::into_raw(Box::new((&mut *SDL_CTX).event_pump().unwrap()));
VIDEO_CTX = Box::into_raw(Box::new((&*SDL_CTX).video().unwrap()));
EVENT_PUMP = Box::into_raw(Box::new((&*SDL_CTX).event_pump().unwrap()));
}
}
......@@ -45,7 +45,6 @@ pub fn get_display_size() -> Result<(u32, u32), String> {
unsafe { &*VIDEO_CTX }
.display_bounds(0)
.map(|rect| (rect.width(), rect.height()))
.map_err(|err| format!("{}", err))
}
/// A window
......@@ -190,10 +189,10 @@ impl Window {
match builder.build() {
Ok(window) => Some(Window {
x: x,
y: y,
w: w,
h: h,
x,
y,
w,
h,
t: title.to_string(),
window_async,
mode: Cell::new(Mode::Blend),
......@@ -432,14 +431,9 @@ impl Window {
};
let mods = unsafe { &mut *SDL_CTX }.keyboard().mod_state();
let shift = if mods.contains(sdl2::keyboard::Mod::CAPSMOD)
let shift = mods.contains(sdl2::keyboard::Mod::CAPSMOD)
|| mods.contains(sdl2::keyboard::Mod::LSHIFTMOD)
|| mods.contains(sdl2::keyboard::Mod::RSHIFTMOD)
{
true
} else {
false
};
|| mods.contains(sdl2::keyboard::Mod::RSHIFTMOD);
match event {
sdl2::event::Event::RenderTargetsReset { .. } => {
......@@ -447,7 +441,7 @@ impl Window {
}
sdl2::event::Event::Window { win_event, .. } => match win_event {
sdl2::event::WindowEvent::Moved(x, y) => {
events.push(MoveEvent { x: x, y: y }.to_event())
events.push(MoveEvent { x, y }.to_event())
}
sdl2::event::WindowEvent::Resized(w, h) => events.push(
ResizeEvent {
......@@ -480,18 +474,18 @@ impl Window {
if self.mouse_relative {
events.push(MouseRelativeEvent { dx: xrel, dy: yrel }.to_event())
} else {
events.push(MouseEvent { x: x, y: y }.to_event())
events.push(MouseEvent { x, y }.to_event())
}
}
sdl2::event::Event::MouseButtonDown { .. } => events.push(button_event()),
sdl2::event::Event::MouseButtonUp { .. } => events.push(button_event()),
sdl2::event::Event::MouseWheel { x, y, .. } => {
events.push(ScrollEvent { x: x, y: y }.to_event())
events.push(ScrollEvent { x, y }.to_event())
}
sdl2::event::Event::TextInput { text, .. } => {
events.push(
TextInputEvent {
character: text.chars().nth(0).unwrap(),
character: text.chars().next().unwrap(),
}
.to_event(),
);
......@@ -513,7 +507,7 @@ impl Window {
let (x, y) = self.get_mouse_position();
events.push(MouseEvent { x: x, y: y }.to_event());
events.push(MouseEvent { x, y }.to_event());
events.push(DropEvent { kind: DROP_FILE }.to_event())
}
......@@ -522,7 +516,7 @@ impl Window {
let (x, y) = self.get_mouse_position();
events.push(MouseEvent { x: x, y: y }.to_event());
events.push(MouseEvent { x, y }.to_event());
events.push(DropEvent { kind: DROP_TEXT }.to_event())
}
sdl2::event::Event::KeyUp { scancode, .. } => {
......
extern crate wasm_bindgen;
extern crate web_sys;
// SPDX-License-Identifier: MIT
use std::{
cell::{Cell, RefCell},
rc::Rc,
};
use self::wasm_bindgen::{prelude::*, Clamped, JsCast};
use self::web_sys::{Document, HtmlCanvasElement, HtmlElement, Window as WebWindow};
use wasm_bindgen::{prelude::*, Clamped, JsCast};
use web_sys::{Document, HtmlCanvasElement, HtmlElement, Window as WebWindow};
use color::Color;
use event::*;
use renderer::Renderer;
use Mode;
use WindowFlag;
use crate::color::Color;
use crate::event::*;
use crate::renderer::Renderer;
use crate::Mode;
use crate::WindowFlag;
pub fn get_display_size() -> Result<(u32, u32), String> {
let width = window()?
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment