From a1482b47433877b5e834b89d023c1b110998305c Mon Sep 17 00:00:00 2001 From: Luke Street Date: Mon, 7 Feb 2022 20:48:08 -0500 Subject: [PATCH] SDL controller input --- .gitmodules | 3 + CMakeLists.txt | 17 +- Graphics/Cargo.lock | 28 +++ Graphics/Cargo.toml | 2 + Graphics/include/aurora.hpp | 20 +- Graphics/include/lib.hpp | 15 +- Graphics/lib.cpp | 22 ++- Graphics/src/lib.rs | 231 ++++++++++++++++++---- Graphics/src/shaders/textured_quad/mod.rs | 100 ++++++---- Graphics/src/zeus.rs | 17 ++ Runtime/CMain.cpp | 27 ++- Runtime/CMakeLists.txt | 2 +- Runtime/Graphics/CMoviePlayer.cpp | 2 +- Runtime/Input/CFinalInput.cpp | 83 ++++++-- Runtime/Input/CFinalInput.hpp | 15 +- Runtime/Input/CInputGenerator.cpp | 46 +++-- Runtime/Input/CInputGenerator.hpp | 189 +++++++++--------- Runtime/MP1/MP1.cpp | 2 +- extern/SDL | 1 + 19 files changed, 554 insertions(+), 268 deletions(-) create mode 160000 extern/SDL diff --git a/.gitmodules b/.gitmodules index a4c7787d0..1899f1b99 100644 --- a/.gitmodules +++ b/.gitmodules @@ -57,3 +57,6 @@ [submodule "extern/imgui"] path = extern/imgui url = https://github.com/ocornut/imgui.git +[submodule "extern/SDL"] + path = extern/SDL + url = https://github.com/libsdl-org/SDL.git diff --git a/CMakeLists.txt b/CMakeLists.txt index caec51568..792620ff3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -481,21 +481,8 @@ target_include_directories(hecl-light PRIVATE ${CMAKE_SOURCE_DIR}) target_link_libraries(hecl-full PRIVATE zeus nod) target_link_libraries(hecl-light PRIVATE zeus nod) -#bintoc(CModelShaders.common.glsl.cpp Shaders/CModelShaders.common.glsl CMODELSHADERS_COMMON_GLSL) -#bintoc(CModelShaders.vert.glsl.cpp Shaders/CModelShaders.vert.glsl CMODELSHADERS_VERT_GLSL) -#bintoc(CModelShaders.frag.glsl.cpp Shaders/CModelShaders.frag.glsl CMODELSHADERS_FRAG_GLSL) -#bintoc(CModelShaders.common.hlsl.cpp Shaders/CModelShaders.common.hlsl CMODELSHADERS_COMMON_HLSL) -#bintoc(CModelShaders.vert.hlsl.cpp Shaders/CModelShaders.vert.hlsl CMODELSHADERS_VERT_HLSL) -#bintoc(CModelShaders.frag.hlsl.cpp Shaders/CModelShaders.frag.hlsl CMODELSHADERS_FRAG_HLSL) -#bintoc(CModelShaders.common.metal.cpp Shaders/CModelShaders.common.metal CMODELSHADERS_COMMON_METAL) -#bintoc(CModelShaders.vert.metal.cpp Shaders/CModelShaders.vert.metal CMODELSHADERS_VERT_METAL) -#bintoc(CModelShaders.frag.metal.cpp Shaders/CModelShaders.frag.metal CMODELSHADERS_FRAG_METAL) -#add_library(CModelShaders -# CModelShaders.common.glsl.cpp CModelShaders.vert.glsl.cpp CModelShaders.frag.glsl.cpp -# CModelShaders.common.hlsl.cpp CModelShaders.vert.hlsl.cpp CModelShaders.frag.hlsl.cpp -# CModelShaders.common.metal.cpp CModelShaders.vert.metal.cpp CModelShaders.frag.metal.cpp) -#target_link_libraries(CModelShaders PUBLIC zeus) -#target_link_libraries(shader_CModelShaders PUBLIC CModelShaders) +add_subdirectory(extern/SDL EXCLUDE_FROM_ALL) +target_compile_options(SDL2-static PRIVATE -Wno-implicit-fallthrough) if(NOT TARGET atdna) # Import native atdna if cross-compiling diff --git a/Graphics/Cargo.lock b/Graphics/Cargo.lock index b270176a3..efa7170c6 100644 --- a/Graphics/Cargo.lock +++ b/Graphics/Cargo.lock @@ -116,6 +116,7 @@ dependencies = [ "num_enum", "pollster", "scopeguard", + "sdl2", "smallvec", "twox-hash", "wasm-bindgen-futures", @@ -1439,6 +1440,27 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "96311ef4a16462c757bb6a39152c40f58f31cd2602a40fceb937e2bc34e6cbab" +[[package]] +name = "sdl2" +version = "0.35.2" +source = "git+https://github.com/encounter/rust-sdl2.git?rev=e282ef3acd7fcb4b2ba16863a7a0a7a8439f9335#e282ef3acd7fcb4b2ba16863a7a0a7a8439f9335" +dependencies = [ + "bitflags", + "lazy_static", + "libc", + "sdl2-sys", +] + +[[package]] +name = "sdl2-sys" +version = "0.35.2" +source = "git+https://github.com/encounter/rust-sdl2.git?rev=e282ef3acd7fcb4b2ba16863a7a0a7a8439f9335#e282ef3acd7fcb4b2ba16863a7a0a7a8439f9335" +dependencies = [ + "cfg-if 1.0.0", + "libc", + "version-compare", +] + [[package]] name = "serde" version = "1.0.132" @@ -1703,6 +1725,12 @@ version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" +[[package]] +name = "version-compare" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe88247b92c1df6b6de80ddc290f3976dbdf2f5f5d3fd049a9fb598c6dd5ca73" + [[package]] name = "version_check" version = "0.9.3" diff --git a/Graphics/Cargo.toml b/Graphics/Cargo.toml index 48a659a46..ebc08d0af 100644 --- a/Graphics/Cargo.toml +++ b/Graphics/Cargo.toml @@ -29,6 +29,8 @@ smallvec = "1.7.0" scopeguard = "1.1.0" twox-hash = "1.6.2" winit = "0.26.1" +# custom sdl2 +sdl2 = { git = "https://github.com/encounter/rust-sdl2.git", rev = "e282ef3acd7fcb4b2ba16863a7a0a7a8439f9335", features = ["no-link", "hidapi"] } [dependencies.imgui] git = "https://github.com/imgui-rs/imgui-rs" diff --git a/Graphics/include/aurora.hpp b/Graphics/include/aurora.hpp index ebdacf513..491e03dc2 100644 --- a/Graphics/include/aurora.hpp +++ b/Graphics/include/aurora.hpp @@ -1,12 +1,14 @@ #pragma once #include +#include #include #include -#include namespace aurora { -enum class SpecialKey : std::uint8_t; +enum class SpecialKey : uint8_t; +enum class ControllerButton : uint8_t; +enum class ControllerAxis : uint8_t; struct WindowSize; @@ -24,14 +26,18 @@ struct AppDelegate { virtual void onAppDraw() noexcept = 0; virtual void onAppPostDraw() noexcept = 0; virtual void onAppWindowResized(const WindowSize& size) noexcept = 0; - virtual void onAppWindowMoved(std::int32_t x, std::int32_t y) noexcept = 0; + virtual void onAppWindowMoved(int32_t x, int32_t y) noexcept = 0; virtual void onAppExiting() noexcept = 0; // Input - virtual void onCharKeyDown(std::uint8_t charCode, bool is_repeat) noexcept = 0; - virtual void onCharKeyUp(std::uint8_t charCode) noexcept = 0; - virtual void onSpecialKeyDown(const SpecialKey& key, bool is_repeat) noexcept = 0; - virtual void onSpecialKeyUp(const SpecialKey& key) noexcept = 0; + virtual void onCharKeyDown(uint8_t charCode, bool isRepeat) noexcept = 0; + virtual void onCharKeyUp(uint8_t charCode) noexcept = 0; + virtual void onSpecialKeyDown(SpecialKey key, bool isRepeat) noexcept = 0; + virtual void onSpecialKeyUp(SpecialKey key) noexcept = 0; + + // Controller + virtual void onControllerButton(uint32_t idx, ControllerButton button, bool pressed) noexcept = 0; + virtual void onControllerAxis(uint32_t idx, ControllerAxis axis, int16_t value) noexcept = 0; // virtual void resized([[maybe_unused]] const WindowSize& rect, [[maybe_unused]] bool sync) noexcept {} // virtual void mouseDown([[maybe_unused]] const SWindowCoord& coord, [[maybe_unused]] EMouseButton button, diff --git a/Graphics/include/lib.hpp b/Graphics/include/lib.hpp index f6953833f..f6946dfb0 100644 --- a/Graphics/include/lib.hpp +++ b/Graphics/include/lib.hpp @@ -6,12 +6,15 @@ void App_onAppLaunched(AppDelegate& cb) noexcept; bool App_onAppIdle(AppDelegate& cb, float dt) noexcept; void App_onAppDraw(AppDelegate& cb) noexcept; void App_onAppPostDraw(AppDelegate& cb) noexcept; -void App_onAppWindowResized(AppDelegate& cb, const WindowSize& size) noexcept; -void App_onAppWindowMoved(AppDelegate& cb, std::int32_t x, std::int32_t y) noexcept; +void App_onAppWindowResized(AppDelegate& cb, const WindowSize& size) noexcept; +void App_onAppWindowMoved(AppDelegate& cb, int32_t x, int32_t y) noexcept; void App_onAppExiting(AppDelegate& cb) noexcept; // Input -void App_onCharKeyDown(AppDelegate& cb, std::uint8_t code, bool is_repeat) noexcept; -void App_onCharKeyUp(AppDelegate& cb, std::uint8_t code) noexcept; -void App_onSpecialKeyDown(AppDelegate& cb, const SpecialKey& key, bool is_repeat); -void App_onSpecialKeyUp(AppDelegate& cb, const SpecialKey& key); +void App_onCharKeyDown(AppDelegate& cb, uint8_t code, bool isRepeat) noexcept; +void App_onCharKeyUp(AppDelegate& cb, uint8_t code) noexcept; +void App_onSpecialKeyDown(AppDelegate& cb, SpecialKey key, bool isRepeat) noexcept; +void App_onSpecialKeyUp(AppDelegate& cb, SpecialKey key) noexcept; +// Controller +void App_onControllerButton(AppDelegate& cb, uint32_t idx, ControllerButton button, bool pressed) noexcept; +void App_onControllerAxis(AppDelegate& cb, uint32_t idx, ControllerAxis axis, int16_t value) noexcept; } // namespace aurora diff --git a/Graphics/lib.cpp b/Graphics/lib.cpp index c052ad913..3ec44a7c4 100644 --- a/Graphics/lib.cpp +++ b/Graphics/lib.cpp @@ -8,22 +8,30 @@ void App_onAppPostDraw(AppDelegate& cb) noexcept { cb.onAppPostDraw(); } void App_onAppWindowResized(AppDelegate& cb, const WindowSize& size) noexcept { cb.onAppWindowResized(size); } -void App_onAppWindowMoved(AppDelegate& cb, std::int32_t x, std::int32_t y) noexcept { +void App_onAppWindowMoved(AppDelegate& cb, int32_t x, int32_t y) noexcept { cb.onAppWindowMoved(x, y); } void App_onAppExiting(AppDelegate& cb) noexcept { cb.onAppExiting(); } // Input -void App_onCharKeyDown(AppDelegate& cb, std::uint8_t code, bool is_repeat) noexcept { - cb.onCharKeyDown(code, is_repeat); +void App_onCharKeyDown(AppDelegate& cb, uint8_t code, bool isRepeat) noexcept { + cb.onCharKeyDown(code, isRepeat); } -void App_onCharKeyUp(AppDelegate& cb, std::uint8_t code) noexcept { +void App_onCharKeyUp(AppDelegate& cb, uint8_t code) noexcept { cb.onCharKeyUp(code); } -void App_onSpecialKeyDown(AppDelegate& cb, const SpecialKey& key, bool is_repeat) { - cb.onSpecialKeyDown(key, is_repeat); +void App_onSpecialKeyDown(AppDelegate& cb, SpecialKey key, bool isRepeat) noexcept { + cb.onSpecialKeyDown(key, isRepeat); } -void App_onSpecialKeyUp(AppDelegate& cb, const SpecialKey& key) { +void App_onSpecialKeyUp(AppDelegate& cb, SpecialKey key) noexcept { cb.onSpecialKeyUp(key); } + +// Controller +void App_onControllerButton(AppDelegate& cb, uint32_t idx, ControllerButton button, bool pressed) noexcept { + cb.onControllerButton(idx, button, pressed); +} +void App_onControllerAxis(AppDelegate& cb, uint32_t idx, ControllerAxis axis, int16_t value) noexcept { + cb.onControllerAxis(idx, axis, value); +} } // namespace aurora diff --git a/Graphics/src/lib.rs b/Graphics/src/lib.rs index 3094bda54..708dcb47a 100644 --- a/Graphics/src/lib.rs +++ b/Graphics/src/lib.rs @@ -3,29 +3,32 @@ #![allow(unused_variables)] #![allow(unused_unsafe)] +use std::{collections::HashMap, time::Instant}; + +use sdl2::{ + controller::{Axis, Button, GameController}, + event::Event as SDLEvent, + GameControllerSubsystem, Sdl, +}; use wgpu::Backend; use winit::{ - event::{ElementState, Event, KeyboardInput, WindowEvent}, + event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent}, event_loop::ControlFlow, }; -use winit::event::VirtualKeyCode; - -use std::{num::NonZeroU8, time::Instant, collections::HashMap}; use crate::{ - ffi::WindowSize, - gpu::{create_depth_texture, create_render_texture, DeviceHolder, initialize_gpu}, - imgui::{ImGuiState, initialize_imgui}, + ffi::{SpecialKey, WindowSize}, + gpu::{create_depth_texture, create_render_texture, initialize_gpu, DeviceHolder}, + imgui::{initialize_imgui, ImGuiState}, shaders::render_into_pass, }; -use crate::ffi::SpecialKey; mod gpu; mod imgui; mod imgui_backend; mod shaders; -mod zeus; mod util; +mod zeus; #[cxx::bridge(namespace = "aurora")] mod ffi { @@ -46,8 +49,25 @@ mod ffi { // 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); + 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 { @@ -118,6 +138,35 @@ mod ffi { // 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, + } + extern "Rust" { type WindowContext; type App; @@ -131,11 +180,50 @@ mod ffi { fn set_fullscreen(v: bool); } } +impl From