mirror of
https://github.com/italicsjenga/rust_minifb.git
synced 2024-12-23 19:31:30 +11:00
WIP on pure Rust version of the Mac backend
This commit is contained in:
parent
e83c66b8dd
commit
29e744bc19
22
Cargo.toml
22
Cargo.toml
|
@ -13,3 +13,25 @@ user32-sys = "0.1.2"
|
|||
winapi = "0.2.4"
|
||||
kernel32-sys = "0.1.4"
|
||||
gdi32-sys = "0.1.1"
|
||||
|
||||
[target.x86_64-apple-darwin.dependencies]
|
||||
objc = "0.1.8"
|
||||
cgl = "0.1"
|
||||
cocoa = "0.1.4"
|
||||
core-foundation = "0"
|
||||
core-graphics = "0"
|
||||
|
||||
[target.i686-pc-windows-gnu.dependencies]
|
||||
winapi = "0.2.4"
|
||||
user32-sys = "0.1.2"
|
||||
kernel32-sys = "0.1.4"
|
||||
gdi32-sys = "0.1.1"
|
||||
|
||||
[target.i686-pc-windows-msvc.dependencies]
|
||||
winapi = "0.2.4"
|
||||
user32-sys = "0.1.2"
|
||||
kernel32-sys = "0.1.4"
|
||||
gdi32-sys = "0.1.1"
|
||||
|
||||
|
||||
|
||||
|
|
36
src/lib.rs
36
src/lib.rs
|
@ -1,9 +1,43 @@
|
|||
extern crate libc;
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
#[macro_use]
|
||||
extern crate objc;
|
||||
#[cfg(target_os = "macos")]
|
||||
extern crate cgl;
|
||||
#[cfg(target_os = "macos")]
|
||||
extern crate cocoa;
|
||||
#[cfg(target_os = "macos")]
|
||||
extern crate core_foundation;
|
||||
#[cfg(target_os = "macos")]
|
||||
|
||||
/// Error that can happen while creating a window or a headless renderer.
|
||||
#[derive(Debug)]
|
||||
pub enum CreationError {
|
||||
OsError(String),
|
||||
NotSupported,
|
||||
}
|
||||
|
||||
impl CreationError {
|
||||
fn to_string(&self) -> &str {
|
||||
match *self {
|
||||
CreationError::OsError(ref text) => &text,
|
||||
CreationError::NotSupported => "Some of the requested attributes are not supported",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
pub mod windows;
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
pub use windows::*;
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
pub mod macos;
|
||||
#[cfg(target_os = "mac")]
|
||||
pub use macos::*;
|
||||
|
||||
|
||||
/*
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
|
|
85
src/macos.rs
Normal file
85
src/macos.rs
Normal file
|
@ -0,0 +1,85 @@
|
|||
#![cfg(target_os = "macos")]
|
||||
|
||||
use CreationError;
|
||||
use CreationError::OsError;
|
||||
|
||||
use libc;
|
||||
use cocoa::appkit;
|
||||
use cocoa::appkit::*;
|
||||
use cocoa::appkit::NSEventSubtype::*;
|
||||
|
||||
use cocoa::base::{id, nil};
|
||||
use objc::runtime::{Class, Object, Sel, BOOL, YES, NO};
|
||||
|
||||
use cocoa::foundation::{NSAutoreleasePool, NSDate, NSDefaultRunLoopMode, NSPoint, NSRect, NSSize,
|
||||
NSString, NSUInteger};
|
||||
|
||||
|
||||
struct Minifb {
|
||||
temp: isize,
|
||||
}
|
||||
|
||||
impl Minifb {
|
||||
pub unsafe fn new(name: &str, width: isize, height: isize) -> Result<Minifb, CreationError> {
|
||||
let app = match Self::create_app() {
|
||||
Some(app) => app,
|
||||
None => { return Err(OsError(format!("Couldn't create NSApplication"))); },
|
||||
};
|
||||
|
||||
let masks = 0u64;
|
||||
|
||||
//let masks = NSResizableWindowMask as NSUInteger |
|
||||
// NSClosableWindowMask as NSUInteger |
|
||||
// NSTitledWindowMaskas as NSUInteger;
|
||||
|
||||
let frame = NSRect::new(NSPoint::new(0., 0.), NSSize::new(width as f64, height as f64));
|
||||
|
||||
let window = IdRef::new(NSWindow::alloc(nil).initWithContentRect_styleMask_backing_defer_(
|
||||
frame,
|
||||
masks,
|
||||
NSBackingStoreBuffered,
|
||||
NO,
|
||||
));
|
||||
|
||||
if window.is_nil() {
|
||||
return Err(OsError(format!("Unable to create window")));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fn create_app() -> Option<id> {
|
||||
unsafe {
|
||||
let app = NSApp();
|
||||
if app == nil {
|
||||
None
|
||||
} else {
|
||||
app.setActivationPolicy_(NSApplicationActivationPolicyRegular);
|
||||
Some(app)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
struct IdRef(id);
|
||||
|
||||
impl IdRef {
|
||||
fn new(i: id) -> IdRef {
|
||||
IdRef(i)
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn retain(i: id) -> IdRef {
|
||||
if i != nil {
|
||||
let _: id = unsafe { msg_send![i, retain] };
|
||||
}
|
||||
IdRef(i)
|
||||
}
|
||||
|
||||
fn non_nil(self) -> Option<IdRef> {
|
||||
if self.0 == nil { None } else { Some(self) }
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -76,7 +76,6 @@ unsafe extern "system" fn wnd_proc(window: winapi::HWND,
|
|||
|
||||
user32::ReleaseDC(window, dc);
|
||||
}
|
||||
|
||||
_ => (),
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue