From 093526721ccf786029417ff63fd5ccee95575b4e Mon Sep 17 00:00:00 2001 From: ManDude <7569514+ManDude@users.noreply.github.com> Date: Mon, 16 Aug 2021 14:57:15 +0100 Subject: [PATCH] save key mapping to memory! --- game/graphics/gfx.cpp | 17 +++++++++++++ game/graphics/gfx.h | 2 ++ game/kernel/kmachine.cpp | 2 ++ game/system/newpad.cpp | 12 +++------ game/system/newpad.h | 4 +++ goal_src/engine/game/main.gc | 2 +- goal_src/engine/target/logic-target.gc | 2 +- goal_src/kernel-defs.gc | 2 ++ goal_src/kernel/gkernel-h.gc | 4 ++- goal_src/kernel/gstate.gc | 8 +++--- goal_src/pc_debug/pc-pad-utils.gc | 35 +++++++++++++++++--------- 11 files changed, 62 insertions(+), 28 deletions(-) diff --git a/game/graphics/gfx.cpp b/game/graphics/gfx.cpp index 122fffaaf..d2d8ea9f2 100644 --- a/game/graphics/gfx.cpp +++ b/game/graphics/gfx.cpp @@ -141,6 +141,23 @@ void input_mode_set(u32 enable) { } } +void input_mode_save() { + if (Pad::input_mode_get() == (u64)Pad::InputModeStatus::Enabled) { + lg::error("Can't save controller mapping while mapping controller."); + } else if (Pad::input_mode_get() == (u64)Pad::InputModeStatus::Disabled) { + g_settings.pad_mapping_info_backup = g_settings.pad_mapping_info; // copy to backup + g_settings.pad_mapping_info = Pad::g_input_mode_mapping; // set current mapping + } +} + +s64 get_mapped_button(s64 pad, s64 button) { + if (pad < 0 || pad > Pad::CONTROLLER_COUNT || button < 0 || button > 16) { + lg::error("Invalid parameters to get_mapped_button({}, {})", pad, button); + return -1; + } + return (s64)g_settings.pad_mapping_info.pad_mapping[pad][button]; +} + int PadIsPressed(Pad::Button button, int port) { return Pad::IsPressed(g_settings.pad_mapping_info, button, port); } diff --git a/game/graphics/gfx.h b/game/graphics/gfx.h index 92ca95fff..207ceda8c 100644 --- a/game/graphics/gfx.h +++ b/game/graphics/gfx.h @@ -72,6 +72,8 @@ void texture_upload_now(const u8* tpage, int mode, u32 s7_ptr); void texture_relocate(u32 destination, u32 source, u32 format); void poll_events(); void input_mode_set(u32 enable); +void input_mode_save(); +s64 get_mapped_button(s64 pad, s64 button); int PadIsPressed(Pad::Button button, int port); diff --git a/game/kernel/kmachine.cpp b/game/kernel/kmachine.cpp index 8a0d1f08a..581886063 100644 --- a/game/kernel/kmachine.cpp +++ b/game/kernel/kmachine.cpp @@ -717,6 +717,8 @@ void InitMachine_PCPort() { make_function_symbol_from_c("__pc-texture-relocate", (void*)pc_texture_relocate); // pad stuff + make_function_symbol_from_c("pc-pad-get-mapped-button", (void*)Gfx::get_mapped_button); + make_function_symbol_from_c("pc-pad-input-map-save!", (void*)Gfx::input_mode_save); make_function_symbol_from_c("pc-pad-input-mode-set", (void*)Gfx::input_mode_set); make_function_symbol_from_c("pc-pad-input-mode-get", (void*)Pad::input_mode_get); make_function_symbol_from_c("pc-pad-input-key-get", (void*)Pad::input_mode_get_key); diff --git a/game/system/newpad.cpp b/game/system/newpad.cpp index 58a912f22..463c0b840 100644 --- a/game/system/newpad.cpp +++ b/game/system/newpad.cpp @@ -22,18 +22,12 @@ std::unordered_map g_key_status; std::unordered_map g_buffered_key_status; // input mode for controller mapping -// this enum is also in pc-pad-utils.gc -enum class InputModeStatus { - Disabled, - Enabled, - Canceled -}; InputModeStatus input_mode = InputModeStatus::Disabled; u64 input_mode_pad = 0; u64 input_mode_key = -1; u64 input_mode_mod = 0; u64 input_mode_index = 0; -MappingInfo input_mode_mapping; +MappingInfo g_input_mode_mapping; void ForceClearKeys() { g_key_status.clear(); @@ -54,8 +48,8 @@ void OnKeyPress(int key) { return; } input_mode_key = key; - MapButton(input_mode_mapping, (Button)(input_mode_index++), input_mode_pad, key); - if (input_mode_index > (u64)Button::Max) { + MapButton(g_input_mode_mapping, (Button)(input_mode_index++), input_mode_pad, key); + if (input_mode_index >= (u64)Button::Max) { ExitInputMode(false); } return; diff --git a/game/system/newpad.h b/game/system/newpad.h index fa09694a9..489e9077c 100644 --- a/game/system/newpad.h +++ b/game/system/newpad.h @@ -72,6 +72,10 @@ void DefaultMapping(MappingInfo& mapping); int IsPressed(MappingInfo& mapping, Button button, int pad); void MapButton(MappingInfo& mapping, Button button, int pad, int key); +// this enum is also in pc-pad-utils.gc +enum class InputModeStatus { Disabled, Enabled, Canceled }; + +extern MappingInfo g_input_mode_mapping; void EnterInputMode(); void ExitInputMode(bool); u64 input_mode_get(); diff --git a/goal_src/engine/game/main.gc b/goal_src/engine/game/main.gc index ebf5babe2..0ea948e50 100644 --- a/goal_src/engine/game/main.gc +++ b/goal_src/engine/game/main.gc @@ -1277,7 +1277,7 @@ ) ) (set! *run* #t) - (let ((new-dproc (make-function-process *4k-dead-pool* *display-pool* process 'display display-loop))) + (let ((new-dproc (make-function-process *4k-dead-pool* *display-pool* process display-loop :name 'display))) (set! *dproc* (the process (as-process new-dproc))) ) (cond diff --git a/goal_src/engine/target/logic-target.gc b/goal_src/engine/target/logic-target.gc index 8a1a77030..9c6752462 100644 --- a/goal_src/engine/target/logic-target.gc +++ b/goal_src/engine/target/logic-target.gc @@ -32,7 +32,7 @@ (set! (-> *level* border?) #f) (set! (-> *setting-control* default border-mode) #f) (stop game-mode) - (let ((v1-3 (make-init-process *target-dead-pool* *target-pool* target 'target init-target cont))) + (let ((v1-3 (make-init-process *target-dead-pool* *target-pool* target init-target cont))) (if v1-3 (set! *target* (the target (-> v1-3 0 self))) (set! *target* #f) diff --git a/goal_src/kernel-defs.gc b/goal_src/kernel-defs.gc index 2faedcc56..f95997c80 100644 --- a/goal_src/kernel-defs.gc +++ b/goal_src/kernel-defs.gc @@ -272,6 +272,8 @@ (define-extern pc-pad-input-mode-get (function int)) (define-extern pc-pad-input-key-get (function int)) (define-extern pc-pad-input-index-get (function int)) +(define-extern pc-pad-input-map-save! (function none)) +(define-extern pc-pad-get-mapped-button (function int int int)) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;; vm functions diff --git a/goal_src/kernel/gkernel-h.gc b/goal_src/kernel/gkernel-h.gc index 023742fd1..42ee5ee5c 100644 --- a/goal_src/kernel/gkernel-h.gc +++ b/goal_src/kernel/gkernel-h.gc @@ -415,7 +415,9 @@ ) (defmacro ppointer->handle (pproc) - `(new 'static 'handle :process ,pproc :pid (-> ,pproc 0 pid)) + `(let ((the-process ,pproc)) + (new 'static 'handle :process the-process :pid (-> the-process 0 pid)) + ) ) (defmacro process->handle (proc) diff --git a/goal_src/kernel/gstate.gc b/goal_src/kernel/gstate.gc index 5cdf6aeb9..fb1f107db 100644 --- a/goal_src/kernel/gstate.gc +++ b/goal_src/kernel/gstate.gc @@ -80,14 +80,14 @@ There are several ways to "go" ) ) -(defmacro make-function-process (dead-pool new-pool proc-type proc-name func &key (stack-size #x4000) &key (stack *kernel-dram-stack*) &rest args) +(defmacro make-function-process (dead-pool new-pool proc-type func &key (name #f) &key (stack-size #x4000) &key (stack *kernel-dram-stack*) &rest args) "Start a new process that runs a function on its main thread. Returns a pointer to the new process (or #f? on error)." (with-gensyms (new-proc) `(let ((,new-proc (the-as ,proc-type (get-process ,dead-pool ,proc-type ,stack-size)))) (when ,new-proc - ((method-of-type ,proc-type activate) ,new-proc ,new-pool ,proc-name ,stack) + ((method-of-type ,proc-type activate) ,new-proc ,new-pool ,(if name name `(quote ,proc-type)) ,stack) (run-next-time-in-process ,new-proc ,func ,@args) (-> ,new-proc ppointer) ) @@ -95,14 +95,14 @@ There are several ways to "go" ) ) -(defmacro make-init-process (dead-pool new-pool proc-type proc-name func &key (stack-size #x4000) &key (stack *kernel-dram-stack*) &rest args) +(defmacro make-init-process (dead-pool new-pool proc-type func &key (name #f) &key (stack-size #x4000) &key (stack *kernel-dram-stack*) &rest args) "Start a new process and run an init function on it. Returns a pointer to the new process, or #f (or is it 0?) if something goes wrong." (with-gensyms (new-proc) `(let ((,new-proc (the-as ,proc-type (get-process ,dead-pool ,proc-type ,stack-size)))) (when ,new-proc - ((method-of-type ,proc-type activate) ,new-proc ,new-pool ,proc-name ,stack) + ((method-of-type ,proc-type activate) ,new-proc ,new-pool ,(if name name `(quote ,proc-type)) ,stack) (run-now-in-process ,new-proc ,func ,@args) (-> ,new-proc ppointer) ) diff --git a/goal_src/pc_debug/pc-pad-utils.gc b/goal_src/pc_debug/pc-pad-utils.gc index 453599da5..71e72bf0e 100644 --- a/goal_src/pc_debug/pc-pad-utils.gc +++ b/goal_src/pc_debug/pc-pad-utils.gc @@ -19,7 +19,8 @@ (cond (*debug-segment* -(deftype pc-pad-proc-list (basic) +;; a structure to hold the handles used in this file +(deftype pc-pad-proc-list (structure) ((show handle) (input handle) ) @@ -28,6 +29,7 @@ (set! (-> *pc-pad-proc-list* show) (the handle #f)) (set! (-> *pc-pad-proc-list* input) (the handle #f)) +;; a pc pad process (deftype pc-pad-proc (process) ((state-time seconds) (input-index uint64) @@ -69,20 +71,16 @@ (if (not (handle->process (-> *pc-pad-proc-list* show))) (let ((procp - (make-function-process *nk-dead-pool* *active-pool* pc-pad-proc 'pc-pad-show + (make-function-process *nk-dead-pool* *active-pool* pc-pad-proc :name 'pc-pad-show (lambda :behavior pc-pad-proc () (stack-size-set! (-> self main-thread) 512) (loop - (let ((cpad (-> *cpad-list* cpads 0))) - (format *stdcon* "~3Lcpad 0~%~0L") - (format *stdcon* "~Tbutton0: ~X~%" (-> cpad button0)) - (format *stdcon* "~Tbutton0-abs 0: ~X~%" (-> cpad button0-abs 0)) - (format *stdcon* "~Tbutton0-abs 1: ~X~%" (-> cpad button0-abs 1)) - (format *stdcon* "~Tbutton0-abs 2: ~X~%" (-> cpad button0-abs 2)) - (format *stdcon* "~Tbutton0-rel 0: ~X~%" (-> cpad button0-rel 0)) - (format *stdcon* "~Tbutton0-rel 1: ~X~%" (-> cpad button0-rel 1)) - (format *stdcon* "~Tbutton0-rel 2: ~X~%" (-> cpad button0-rel 2)) + (dotimes (ii 2) + (format *stdcon* "~3Lcpad ~D~0L~%" ii) + (dotimes (i 16) + (format *stdcon* " ~S: ~32L~D~0L~%" (-> *pc-pad-button-names* i) (pc-pad-get-mapped-button ii i)) ) + ) (suspend) ) ) @@ -151,7 +149,20 @@ (defstate pc-pi-done (pc-pad-proc) + :enter (behavior () (set! (-> self input-index) 16)) :code (behavior () + (pc-pad-input-map-save!) + (set-state-time) + (until (time-passed? PC_PAD_INPUT_NOTICE_TIME) + (with-dma-buffer-add-bucket ((buf (current-display-frame debug-buf)) + (current-display-frame bucket-group) + (bucket-id debug-draw1)) + (draw-string-xy (string-format "MAPPED ~D TO ~S!" + (pc-pad-input-key-get) (-> *pc-pad-button-names* (1- (-> self input-index)))) + buf 256 96 (font-color green) (font-flags shadow kerning large middle)) + ) + (suspend) + ) (set-state-time) (until (time-passed? PC_PAD_INPUT_NOTICE_TIME) (with-dma-buffer-add-bucket ((buf (current-display-frame debug-buf)) @@ -214,7 +225,7 @@ (if (not (handle->process (-> *pc-pad-proc-list* input))) (let ((procp - (make-init-process *nk-dead-pool* *active-pool* pc-pad-proc 'pc-pad-input + (make-init-process *nk-dead-pool* *active-pool* pc-pad-proc :name 'pc-pad-input (lambda :behavior pc-pad-proc () (pc-pad-input-mode-set #t) (go pc-pi-mapping-button)