add gfx and winit

This commit is contained in:
Michael Zhang 2021-01-15 12:49:00 -06:00
parent 073952598d
commit da7b86ab31
Signed by: michael
GPG key ID: BDA47A31A3C8EE6B
7 changed files with 74 additions and 19 deletions

16
Cargo.lock generated
View file

@ -935,7 +935,9 @@ dependencies = [
name = "framework"
version = "0.1.0"
dependencies = [
"ndk-glue",
"anyhow",
"bass-sys",
"gfx-hal",
"winit",
]
@ -943,7 +945,9 @@ dependencies = [
name = "framework-example-2048"
version = "0.1.0"
dependencies = [
"anyhow",
"framework",
"ndk-glue",
]
[[package]]
@ -995,6 +999,16 @@ dependencies = [
"log",
]
[[package]]
name = "gfx-hal"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "18d0754f5b7a43915fd7466883b2d1bb0800d7cc4609178d0b27bf143b9e5123"
dependencies = [
"bitflags",
"raw-window-handle",
]
[[package]]
name = "gfx_core"
version = "0.9.2"

View file

@ -8,4 +8,11 @@ edition = "2018"
crate-type = ["lib", "cdylib"]
[dependencies]
anyhow = "1.0.38"
framework = { path = "../framework" }
[target.'cfg(target_os = "android")'.dependencies]
ndk-glue = "0.2.1"
[package.metadata.android]
apk_label = "2048"

View file

@ -0,0 +1,13 @@
use anyhow::Result;
use framework::Game;
pub fn real_main() -> Result<()> {
let game = Game::init()?;
game.run();
Ok(())
}
#[cfg_attr(target_os = "android", ndk_glue::main(backtrace = "on"))]
pub fn android_main() {
real_main();
}

View file

@ -1,3 +1,5 @@
use framework_example_2048::real_main;
fn main() {
println!("Hello, world!");
real_main();
}

View file

@ -5,7 +5,7 @@ authors = ["Michael Zhang <mail@mzhang.io>"]
edition = "2018"
[dependencies]
anyhow = "1.0.38"
gfx-hal = "0.6.0"
winit = { version = "0.24.0", features = ["web-sys"] }
[target.'cfg(target_os = "android")'.dependencies]
ndk-glue = "0.2.1"
bass-sys = { path = "../bass-sys" }

View file

@ -1,3 +1,10 @@
use anyhow::Result;
use winit::{
event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::{Window, WindowBuilder},
};
pub struct ObjectWrapper {
id: usize,
inner: Box<dyn Object>,
@ -9,21 +16,38 @@ pub trait Object {
fn draw(&self);
}
pub struct Context {
}
pub struct Context {}
pub struct Game {
event_loop: EventLoop<()>,
window: Window,
objects: Vec<ObjectWrapper>,
}
impl Game {
pub fn run<F>(mut self, func: F)
where
F: Fn(),
{
loop {
for object in self.objects.iter_mut() {
pub fn init() -> Result<Self> {
let event_loop = EventLoop::new();
let window = WindowBuilder::new().build(&event_loop)?;
Ok(Game {
objects: vec![],
event_loop,
window,
})
}
pub fn run(mut self) {
let window_id = self.window.id();
self.event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait;
match event {
Event::WindowEvent {
event: WindowEvent::CloseRequested,
window_id,
} if window_id == window_id => *control_flow = ControlFlow::Exit,
_ => (),
}
}
});
}
}

View file

@ -2,8 +2,3 @@ mod game;
mod renderer;
pub use crate::game::Game;
#[cfg_attr(target_os = "android", ndk_glue::main(backtrace = "on"))]
pub fn android_main() {
println!("hello, world!");
}