From 307e07d6513f9524b7fcab5134494bd175465cd1 Mon Sep 17 00:00:00 2001 From: Hannes Schulze Date: Thu, 30 Jan 2025 16:43:06 +0100 Subject: handle input devices in the c++ application --- lib/nes/app/input/input-device.hh | 44 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 lib/nes/app/input/input-device.hh (limited to 'lib/nes/app/input/input-device.hh') diff --git a/lib/nes/app/input/input-device.hh b/lib/nes/app/input/input-device.hh new file mode 100644 index 0000000..9ac7423 --- /dev/null +++ b/lib/nes/app/input/input-device.hh @@ -0,0 +1,44 @@ +#pragma once + +#include "nes/sys/types/button-mask.hh" + +namespace nes +{ + /// An abstract input device which can be used to play games. + class input_device + { + public: + virtual ~input_device() = default; + + input_device(input_device const&) = delete; + input_device(input_device&&) = delete; + auto operator=(input_device const&) -> input_device& = delete; + auto operator=(input_device&&) -> input_device& = delete; + + virtual auto read_key_up() -> bool = 0; + virtual auto read_key_down() -> bool = 0; + virtual auto read_key_left() -> bool = 0; + virtual auto read_key_right() -> bool = 0; + virtual auto read_key_a() -> bool = 0; + virtual auto read_key_b() -> bool = 0; + virtual auto read_key_select() -> bool = 0; + virtual auto read_key_start() -> bool = 0; + + auto read_mask() -> button_mask + { + auto res = button_mask{}; + if (read_key_up()) { res.add(buttons::up); } + if (read_key_down()) { res.add(buttons::down); } + if (read_key_left()) { res.add(buttons::left); } + if (read_key_right()) { res.add(buttons::right); } + if (read_key_a()) { res.add(buttons::a); } + if (read_key_b()) { res.add(buttons::b); } + if (read_key_select()) { res.add(buttons::select); } + if (read_key_start()) { res.add(buttons::start); } + return res; + } + + protected: + explicit input_device() = default; + }; +} // namespace nes \ No newline at end of file -- cgit v1.3.1