From 8286f006cfee1dd72f327158e5b7ba8de2b45075 Mon Sep 17 00:00:00 2001 From: Luke Street Date: Wed, 9 Feb 2022 00:13:19 -0500 Subject: [PATCH] Automatically remap controllers; separate cxxbridge build files --- Graphics/build.rs | 12 +- Graphics/src/cxxbridge.rs | 200 ++++++++++++ Graphics/src/imgui/cxxbridge.rs | 25 ++ Graphics/src/{imgui.rs => imgui/mod.rs} | 23 +- Graphics/src/lib.rs | 362 +++------------------- Graphics/src/sdl.rs | 137 ++++++++ Graphics/src/shaders/aabb/mod.rs | 1 + Graphics/src/shaders/cxxbridge.rs | 253 +++++++++++++++ Graphics/src/shaders/mod.rs | 264 +--------------- Graphics/src/shaders/model/mod.rs | 4 +- Graphics/src/shaders/movie_player/mod.rs | 16 +- Graphics/src/shaders/texture.rs | 2 +- Graphics/src/shaders/textured_quad/mod.rs | 70 +++-- Graphics/src/util.rs | 14 +- Graphics/src/zeus.rs | 49 +-- 15 files changed, 743 insertions(+), 689 deletions(-) create mode 100644 Graphics/src/cxxbridge.rs create mode 100644 Graphics/src/imgui/cxxbridge.rs rename Graphics/src/{imgui.rs => imgui/mod.rs} (77%) create mode 100644 Graphics/src/sdl.rs create mode 100644 Graphics/src/shaders/cxxbridge.rs diff --git a/Graphics/build.rs b/Graphics/build.rs index 28d380c5c..9fe2bc411 100644 --- a/Graphics/build.rs +++ b/Graphics/build.rs @@ -31,24 +31,24 @@ fn main() { let cxx_flag = "-std=c++17"; #[cfg(all(unix, not(any(target_os = "macos", target_os = "ios"))))] let cxx_flag = "-std=gnu++17"; - cxx_build::bridge("src/lib.rs") + cxx_build::bridge("src/cxxbridge.rs") .include("include") .include(zeus_include.clone()) .flag(cxx_flag) .compile("aurora"); - println!("cargo:rerun-if-changed=src/lib.rs"); - cxx_build::bridge("src/shaders/mod.rs") + println!("cargo:rerun-if-changed=src/cxxbridge.rs"); + cxx_build::bridge("src/shaders/cxxbridge.rs") .include("include") .include(zeus_include.clone()) .flag(cxx_flag) .compile("aurora_shaders"); - println!("cargo:rerun-if-changed=src/shaders/mod.rs"); - cxx_build::bridge("src/imgui.rs") + println!("cargo:rerun-if-changed=src/shaders/cxxbridge.rs"); + cxx_build::bridge("src/imgui/cxxbridge.rs") .include("include") .include(zeus_include.clone()) .include(imgui_include.clone()) .include(imgui_engine_include.clone()) .flag(cxx_flag) .compile("aurora_imgui"); - println!("cargo:rerun-if-changed=src/imgui.rs"); + println!("cargo:rerun-if-changed=src/imgui/cxxbridge.rs"); } diff --git a/Graphics/src/cxxbridge.rs b/Graphics/src/cxxbridge.rs new file mode 100644 index 000000000..6ac100252 --- /dev/null +++ b/Graphics/src/cxxbridge.rs @@ -0,0 +1,200 @@ +use sdl2::controller::{Axis, Button}; + +use crate::{ + app_run, get_args, get_backend, get_backend_string, get_dxt_compression_supported, + get_window_size, + sdl::{get_controller_player_index, set_controller_player_index}, + set_fullscreen, set_window_title, App, WindowContext, +}; + +#[cxx::bridge(namespace = "aurora")] +pub(crate) mod ffi { + unsafe extern "C++" { + include!("aurora.hpp"); + pub(crate) type AppDelegate; + } + + unsafe extern "C++" { + include!("lib.hpp"); + pub(crate) fn App_onAppLaunched(cb: Pin<&mut AppDelegate>); + pub(crate) fn App_onAppIdle(cb: Pin<&mut AppDelegate>, dt: f32) -> bool; + pub(crate) fn App_onAppDraw(cb: Pin<&mut AppDelegate>); + pub(crate) fn App_onAppPostDraw(cb: Pin<&mut AppDelegate>); + pub(crate) fn App_onAppWindowResized(cb: Pin<&mut AppDelegate>, size: &WindowSize); + pub(crate) fn App_onAppWindowMoved(cb: Pin<&mut AppDelegate>, x: i32, y: i32); + pub(crate) fn App_onAppExiting(cb: Pin<&mut AppDelegate>); + // Input + pub(crate) fn App_onCharKeyDown(cb: Pin<&mut AppDelegate>, code: u8, is_repeat: bool); + pub(crate) fn App_onCharKeyUp(cb: Pin<&mut AppDelegate>, code: u8); + pub(crate) fn App_onSpecialKeyDown( + cb: Pin<&mut AppDelegate>, + key: SpecialKey, + is_repeat: bool, + ); + pub(crate) fn App_onSpecialKeyUp(cb: Pin<&mut AppDelegate>, key: SpecialKey); + // Controller + pub(crate) fn App_onControllerButton( + cb: Pin<&mut AppDelegate>, + idx: u32, + button: ControllerButton, + pressed: bool, + ); + pub(crate) fn App_onControllerAxis( + cb: Pin<&mut AppDelegate>, + idx: u32, + axis: ControllerAxis, + value: i16, + ); + } + + pub struct Window { + pub(crate) inner: Box, + } + + pub struct WindowSize { + pub width: u32, + pub height: u32, + } + + #[derive(Debug)] + pub enum Backend { + Invalid, + Vulkan, + Metal, + D3D12, + D3D11, + OpenGL, + WebGPU, + } + + pub enum ElementState { + Pressed, + Released, + } + + pub enum MouseButton { + Left, + Right, + Middle, + Other, + } + + pub enum SpecialKey { + None = 0, + F1 = 1, + F2 = 2, + F3 = 3, + F4 = 4, + F5 = 5, + F6 = 6, + F7 = 7, + F8 = 8, + F9 = 9, + F10 = 10, + F11 = 11, + F12 = 12, + Esc = 13, + Enter = 14, + Backspace = 15, + Insert = 16, + Delete = 17, + Home = 18, + End = 19, + PgUp = 20, + PgDown = 21, + Left = 22, + Right = 23, + Up = 24, + Down = 25, + Tab = 26, + } + + pub struct KeyboardInput { + pub scancode: u32, + pub state: ElementState, + // pub + } + + pub enum ControllerButton { + A, + B, + X, + Y, + Back, + Guide, + Start, + LeftStick, + RightStick, + LeftShoulder, + RightShoulder, + DPadUp, + DPadDown, + DPadLeft, + DPadRight, + Other, + MAX, + } + pub enum ControllerAxis { + LeftX, + LeftY, + RightX, + RightY, + TriggerLeft, + TriggerRight, + MAX, + } + pub struct Icon { + pub data: Vec, + pub width: u32, + pub height: u32, + } + + extern "Rust" { + type WindowContext; + type App; + fn app_run(mut delegate: UniquePtr, icon: Icon); + fn get_args() -> Vec; + fn get_window_size() -> WindowSize; + fn set_window_title(title: &CxxString); + fn get_dxt_compression_supported() -> bool; + fn get_backend() -> Backend; + fn get_backend_string() -> &'static str; + fn set_fullscreen(v: bool); + fn get_controller_player_index(which: u32) -> i32; + fn set_controller_player_index(which: u32, index: i32); + } +} +impl From