Commit ff633b89 authored by Jeremy Soller's avatar Jeremy Soller

Add data function for arbitrary pixel operations

parent 35eb54c1
......@@ -2,7 +2,7 @@
name = "orbclient"
description = "The Orbital Client Library"
repository = "https://github.com/redox-os/orbclient"
version = "0.1.3"
version = "0.1.4"
license-file = "LICENSE"
readme = "README.md"
authors = ["Jeremy Soller <jackpot51@gmail.com>"]
......@@ -11,10 +11,6 @@ authors = ["Jeremy Soller <jackpot51@gmail.com>"]
name = "orbclient"
path = "src/lib.rs"
[[bin]]
name = "example"
path = "src/example.rs"
[target.i686-unknown-linux-gnu.dependencies]
sdl2 = "0.9"
......
......@@ -7,12 +7,12 @@ pub struct Color {
impl Color {
/// Create a new color from RGB
pub fn rgb(r: u8, g: u8, b: u8) -> Self {
pub const fn rgb(r: u8, g: u8, b: u8) -> Self {
Color { data: 0xFF000000 | ((r as u32) << 16) | ((g as u32) << 8) | (b as u32) }
}
/// Set the alpha
pub fn rgba(r: u8, g: u8, b: u8, a: u8) -> Self {
pub const fn rgba(r: u8, g: u8, b: u8, a: u8) -> Self {
Color { data: ((a as u32) << 24) | ((r as u32) << 16) | ((g as u32) << 8) | (b as u32) }
}
}
extern crate orbclient;
use std::cmp::max;
use std::env;
use orbclient::{BmpFile, Color, EventOption, Window};
fn main() {
let path = match env::args().nth(1) {
Some(arg) => arg,
None => "res/redox.bmp".to_string(),
};
let bmp = BmpFile::from_path(&path);
let mut window = Window::new(-1,
-1,
max(32, bmp.width() as u32),
max(32, bmp.height() as u32),
&path)
.unwrap();
window.set(Color::rgb(0, 0, 0));
window.image(0, 0, bmp.width() as u32, bmp.height() as u32, &bmp);
window.sync();
loop {
for event in window.events() {
println!("{:?}", event.to_option());
if let EventOption::Quit(_) = event.to_option() {
return;
}
}
}
}
#![crate_name="orbclient"]
#![crate_type="lib"]
#![feature(associated_consts)]
#![feature(const_fn)]
#![feature(box_syntax)]
#![feature(deprecated)]
......@@ -13,15 +14,11 @@ pub static FONT: &'static [u8] = include_bytes!("../res/unifont.font");
pub use bmp::BmpFile;
pub use color::Color;
pub use event::*;
pub use point::Point;
pub use size::Size;
pub use window::Window;
pub mod bmp;
pub mod color;
pub mod event;
pub mod point;
pub mod size;
#[cfg(target_os = "redox")]
#[path="orbital/window.rs"]
......
......@@ -99,6 +99,14 @@ impl Window {
// TODO
}
pub fn data(&self) -> &[Color] {
&self.data
}
pub fn data_mut(&mut self) -> &mut [Color] {
&mut self.data
}
/// Draw a pixel
pub fn pixel(&mut self, x: i32, y: i32, color: Color) {
if x >= 0 && y >= 0 && x < self.w as i32 && y < self.h as i32 {
......
use std::ops::{Add, Sub};
/// A point
#[derive(Copy, Clone, Debug, Default)]
pub struct Point {
pub x: i32,
pub y: i32,
}
impl Point {
/// Create a new point
pub fn new(x: i32, y: i32) -> Self {
Point { x: x, y: y }
}
}
impl Add for Point {
type Output = Point;
fn add(self, other: Point) -> Self::Output {
Point {
x: self.x + other.x,
y: self.y + other.y,
}
}
}
impl Sub for Point {
type Output = Point;
fn sub(self, other: Point) -> Self::Output {
Point {
x: self.x - other.x,
y: self.y - other.y,
}
}
}
extern crate sdl2;
use std::{mem, slice};
use super::{FONT, Color};
use super::event::*;
......@@ -97,6 +99,16 @@ impl Window {
// TODO
}
pub fn data(&self) -> &[Color] {
let bytes = self.inner.surface().unwrap().without_lock().unwrap();
unsafe { slice::from_raw_parts(bytes.as_ptr() as *const Color, bytes.len()/mem::size_of::<Color>()) }
}
pub fn data_mut(&mut self) -> &mut [Color] {
let bytes = self.inner.surface_mut().unwrap().without_lock_mut().unwrap();
unsafe { slice::from_raw_parts_mut(bytes.as_mut_ptr() as *mut Color, bytes.len()/mem::size_of::<Color>()) }
}
/// Draw a pixel
pub fn pixel(&mut self, x: i32, y: i32, color: Color) {
self.inner.set_blend_mode(sdl2::render::BlendMode::Blend);
......
use std::cmp;
use std::ops::{Add, Sub};
/// A size
#[derive(Copy, Clone, Debug, Default)]
pub struct Size {
pub width: u32,
pub height: u32,
}
impl Size {
/// Create a new size
pub fn new(width: u32, height: u32) -> Self {
Size {
width: width,
height: height,
}
}
}
impl Add for Size {
type Output = Size;
fn add(self, other: Size) -> Self::Output {
Size {
width: self.width + other.width,
height: self.height + other.height,
}
}
}
impl Sub for Size {
type Output = Size;
fn sub(self, other: Size) -> Self::Output {
Size {
width: self.width - cmp::min(self.width, other.width),
height: self.height - cmp::min(self.height, other.height),
}
}
}
Markdown is supported
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