Commit 87f80036 authored by Jeremy Soller's avatar Jeremy Soller

Add display size to SDL2, do init of SDL when necessary

parent 6a25f348
......@@ -13,8 +13,8 @@ name = "orbclient"
path = "src/lib.rs"
[[bin]]
name = "data"
path = "examples/data.rs"
name = "simple"
path = "examples/simple.rs"
[target.'cfg(not(target_os = "redox"))'.dependencies]
sdl2 = "0.9"
......@@ -3,23 +3,18 @@ extern crate orbclient;
use orbclient::{Color, Window, EventOption};
fn main() {
let (width, height) = orbclient::get_display_size().unwrap();
let width = 600;
let height = 600;
let mut window = Window::new(-1,
-1,
width,
height,
let mut window = Window::new((width as i32)/4,
(height as i32)/4,
width/2,
height/2,
"TITLE")
.unwrap();
window.set(Color::rgb(255, 255, 255));
window.sync();
println!("Data: {}", window.data().len());
println!("Mut Data: {}", window.data_mut().len());
'events: loop {
for event in window.events() {
match event.to_option() {
......
......@@ -10,19 +10,15 @@ pub static FONT: &'static [u8] = include_bytes!("../res/unifont.font");
pub use color::Color;
pub use event::*;
pub use window::Window;
pub use imp::*;
pub mod color;
pub mod event;
//TODO replace this with a mod.rs file
#[path="orbital/display.rs"]
pub mod display;
#[cfg(target_os = "redox")]
#[path="orbital/window.rs"]
pub mod window;
#[path="orbital/mod.rs"]
pub mod imp;
#[cfg(not(target_os = "redox"))]
#[path="sdl2/window.rs"]
pub mod window;
#[path="sdl2/mod.rs"]
pub mod imp;
#[cfg(target_os = "redox")]
pub fn get_display_size() -> (i32, i32) {
use std::fs::File;
pub fn get_display_size() -> Result<(u32, u32), String> {
match File::open("display:") {
Ok(display) => {
let path = display.path().map(|path| path.into_os_string().into_string().unwrap_or(String::new())).unwrap_or(String::new());
let res = path.split(":").nth(1).unwrap_or("");
let width = res.split("/").nth(0).unwrap_or("").parse::<i32>().unwrap_or(0);
let height = res.split("/").nth(1).unwrap_or("").parse::<i32>().unwrap_or(0);
(width, height)
let width = res.split("/").nth(0).unwrap_or("").parse::<u32>().unwrap_or(0);
let height = res.split("/").nth(1).unwrap_or("").parse::<u32>().unwrap_or(0);
Ok((width, height))
},
Err(err) => panic!("launcher: failed to get display size: {}", err)
Err(err) => Err(format!("{}", err))
}
}
#[cfg(not(target_os = "redox"))]
pub fn get_display_size() -> (i32, i32) {
panic!("launcher: failed to get display size")
}
extern crate system;
pub use self::display::*;
pub use self::window::*;
mod display;
mod window;
extern crate system;
use std::cmp;
use std::fs::File;
use std::io::*;
......@@ -7,11 +5,11 @@ use std::mem;
use std::slice;
use std::thread;
use self::system::graphics::fast_set;
use super::system::graphics::fast_set;
use super::FONT;
use super::Event;
use super::Color;
use super::super::FONT;
use super::super::Event;
use super::super::Color;
/// A window
pub struct Window {
......@@ -251,7 +249,7 @@ impl Window {
}
}
}
/// Blocking iterator over events
pub fn events(&mut self) -> EventIter {
let mut iter = EventIter {
......
use super::{init, VIDEO_CTX};
pub fn get_display_size() -> Result<(u32, u32), String> {
unsafe { init() };
unsafe { & *VIDEO_CTX }.display_bounds(0)
.map(|rect| (rect.width(), rect.height()))
.map_err(|err| format!("{}", err))
}
extern crate sdl2;
use std::ptr;
use std::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT, Ordering};
pub use self::display::*;
pub use self::window::*;
mod display;
mod window;
pub static SDL_USAGES: AtomicUsize = ATOMIC_USIZE_INIT;
/// SDL2 Context
pub static mut SDL_CTX: *mut sdl2::Sdl = ptr::null_mut();
/// Video Context
pub static mut VIDEO_CTX: *mut sdl2::VideoSubsystem = ptr::null_mut();
/// Event Pump
pub static mut EVENT_PUMP: *mut sdl2::EventPump = ptr::null_mut();
//Call this when the CTX needs to be used is created
#[inline]
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()));
}
}
......@@ -2,8 +2,9 @@ extern crate sdl2;
use std::{mem, slice};
use super::{FONT, Color};
use super::event::*;
use super::super::{FONT, Color};
use super::super::event::*;
use super::{init, SDL_CTX, VIDEO_CTX, EVENT_PUMP};
/// A window
#[allow(dead_code)]
......@@ -20,12 +21,6 @@ pub struct Window {
t: String,
/// True if the window should not wait for events
async: bool,
/// SDL2 Context
ctx: sdl2::Sdl,
/// Video Context
video_ctx: sdl2::VideoSubsystem,
/// Event Pump
event_pump: sdl2::EventPump,
/// The inner renderer
inner: sdl2::render::Renderer<'static>,
}
......@@ -38,11 +33,10 @@ impl Window {
/// Create a new window with flags
pub fn new_flags(x: i32, y: i32, w: u32, h: u32, title: &str, async: bool) -> Option<Self> {
let ctx = sdl2::init().unwrap();
let video_ctx = ctx.video().unwrap();
let event_pump = ctx.event_pump().unwrap();
//Insure that init has been called
unsafe { init() };
let mut builder = video_ctx.window(title, w, h);
let mut builder = unsafe { & *VIDEO_CTX }.window(title, w, h);
if x >= 0 || y >= 0 {
builder.position(x, y);
......@@ -56,9 +50,6 @@ impl Window {
h: h,
t: title.to_string(),
async: async,
ctx: ctx,
video_ctx: video_ctx,
event_pump: event_pump,
inner: window.renderer().software().build().unwrap(),
}),
Err(_) => None
......@@ -109,14 +100,14 @@ impl Window {
pub fn data(&self) -> &[Color] {
let window = self.inner.window().unwrap();
let surface = window.surface(&self.event_pump).unwrap();
let surface = window.surface(unsafe { & *EVENT_PUMP }).unwrap();
let bytes = surface.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 window = self.inner.window_mut().unwrap();
let surface = window.surface_mut(&self.event_pump).unwrap();
let surface = window.surface_mut(unsafe { & *EVENT_PUMP }).unwrap();
let bytes = surface.without_lock_mut().unwrap();
unsafe { slice::from_raw_parts_mut(bytes.as_mut_ptr() as *mut Color, bytes.len()/mem::size_of::<Color>()) }
}
......@@ -305,7 +296,7 @@ impl Window {
let mut events = Vec::new();
let mouse_event = || -> Event {
let mouse = self.ctx.mouse().mouse_state();
let mouse = unsafe { &mut *SDL_CTX }.mouse().mouse_state();
MouseEvent {
x: mouse.1,
y: mouse.2,
......@@ -315,7 +306,7 @@ impl Window {
}.to_event()
};
let mods = self.ctx.keyboard().mod_state();
let mods = unsafe { &mut *SDL_CTX }.keyboard().mod_state();
let shift = if mods.contains(sdl2::keyboard::CAPSMOD)
|| mods.contains(sdl2::keyboard::LSHIFTMOD)
|| mods.contains(sdl2::keyboard::RSHIFTMOD)
......@@ -359,7 +350,7 @@ impl Window {
};
if ! self.async {
let event = self.event_pump.wait_event();
let event = unsafe { &mut *EVENT_PUMP }.wait_event();
if let sdl2::event::Event::Window{..} = event {
self.sync_path();
}
......@@ -373,7 +364,7 @@ impl Window {
}
}
while let Some(event) = self.event_pump.poll_event() {
while let Some(event) = unsafe { &mut *EVENT_PUMP }.poll_event() {
if let sdl2::event::Event::Window{..} = event {
self.sync_path();
}
......
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