Allowing the execution of pre-decoded inputs

This commit is contained in:
SergioMartin86 2024-07-21 15:11:25 +02:00
parent 998a01febc
commit 67c93e664e
2 changed files with 34 additions and 19 deletions

View File

@ -26,7 +26,7 @@ public:
port_t port2 = 0;
};
inline bool parseInputString(const std::string& input)
inline bool parseInputString(const std::string& input, input_t* decoded) const
{
// Parse valid flag
bool isValid = true;
@ -38,13 +38,13 @@ public:
if (ss.get() != '|') isValid = false;
// Parsing console inputs
isValid &= parseConsoleInputs(_input.reset, _input.power, ss);
isValid &= parseConsoleInputs(decoded->reset, decoded->power, ss);
// Parsing controller 1 inputs
isValid &= parseControllerInputs(_controller1Type, _input.port1, ss);
isValid &= parseControllerInputs(_controller1Type, decoded->port1, ss);
// Parsing controller 1 inputs
isValid &= parseControllerInputs(_controller2Type, _input.port2, ss);
isValid &= parseControllerInputs(_controller2Type, decoded->port2, ss);
// End separator
if (ss.get() != '|') isValid = false;
@ -60,11 +60,6 @@ public:
inline void setController1Type(const controller_t type) { _controller1Type = type; }
inline void setController2Type(const controller_t type) { _controller2Type = type; }
inline bool getPowerButtonState() { return _input.power; }
inline bool getResetButtonState() { return _input.reset; }
inline port_t getController1Code() { return _input.port1; }
inline port_t getController2Code() { return _input.port2; }
private:
static bool parseJoyPadInput(uint8_t& code, std::istringstream& ss)
@ -204,7 +199,6 @@ public:
return isValid;
}
input_t _input;
controller_t _controller1Type;
controller_t _controller2Type;

View File

@ -16,22 +16,43 @@ class NESInstanceBase
NESInstanceBase() = default;
virtual ~NESInstanceBase() = default;
inline void advanceState(const std::string &move)
inline void advanceState(const std::string &input)
{
bool isInputValid = _controller.parseInputString(move);
if (isInputValid == false) JAFFAR_THROW_LOGIC("Move provided cannot be parsed: '%s'\n", move.c_str());
// Storage for the decoded input
quickNES::Controller::input_t decodedInput;
// Getting decoded input from the input string
decodeInput(input, &decodedInput);
// Calling advance state with the decoded input
advanceState(&decodedInput);
}
inline void advanceState(const void* decodedInputBuffer)
{
// Casting decoded input to the right type
const auto decodedInput = (quickNES::Controller::input_t*) decodedInputBuffer;
// Parsing power
if (_controller.getPowerButtonState() == true) JAFFAR_THROW_LOGIC("Power button pressed, but not supported: '%s'\n", move.c_str());
if (decodedInput->power == true) JAFFAR_THROW_LOGIC("Power button pressed, but not supported.");
// Parsing reset
if (_controller.getResetButtonState() == true) doSoftReset();
if (decodedInput->reset == true) doSoftReset();
// Parsing Controllers
const auto controller1 = _controller.getController1Code();
const auto controller2 = _controller.getController2Code();
// Running specified inputs
advanceStateImpl(decodedInput->port1, decodedInput->port2);
}
advanceStateImpl(controller1, controller2);
inline size_t getDecodedInputSize() const
{
return sizeof(quickNES::Controller::input_t);
}
inline void decodeInput(const std::string &input, void* decodedInputBuffer) const
{
const auto decodedInput = (quickNES::Controller::input_t*) decodedInputBuffer;
bool isInputValid = _controller.parseInputString(input, decodedInput);
if (isInputValid == false) JAFFAR_THROW_LOGIC("Move provided cannot be parsed: '%s'\n", input.c_str());
}
inline void setController1Type(const std::string& type)