|
| 1 | +#pragma once |
| 2 | + |
| 3 | +// Base controller class |
| 4 | +// by eien86 |
| 5 | + |
| 6 | +#include <cstdint> |
| 7 | +#include <string> |
| 8 | +#include <sstream> |
| 9 | + |
| 10 | +class Controller |
| 11 | +{ |
| 12 | +public: |
| 13 | + |
| 14 | + enum controller_t { none, joypad, fourscore1, fourscore2 }; |
| 15 | + |
| 16 | + typedef uint32_t port_t; |
| 17 | + |
| 18 | + struct input_t |
| 19 | + { |
| 20 | + bool power = false; |
| 21 | + bool reset = false; |
| 22 | + port_t port1 = 0; |
| 23 | + port_t port2 = 0; |
| 24 | + }; |
| 25 | + |
| 26 | + inline bool parseInputString(const std::string& input) |
| 27 | + { |
| 28 | + // Parse valid flag |
| 29 | + bool isValid = true; |
| 30 | + |
| 31 | + // Converting input into a stream for parsing |
| 32 | + std::istringstream ss(input); |
| 33 | + |
| 34 | + // Start separator |
| 35 | + if (ss.get() != '|') isValid = false; |
| 36 | + |
| 37 | + // Parsing console inputs |
| 38 | + isValid &= parseConsoleInputs(_input.reset, _input.power, ss); |
| 39 | + |
| 40 | + // Parsing controller 1 inputs |
| 41 | + isValid &= parseControllerInputs(_controller1Type, _input.port1, ss); |
| 42 | + |
| 43 | + // Parsing controller 1 inputs |
| 44 | + isValid &= parseControllerInputs(_controller2Type, _input.port2, ss); |
| 45 | + |
| 46 | + // End separator |
| 47 | + if (ss.get() != '|') isValid = false; |
| 48 | + |
| 49 | + // If its not the end of the stream, then extra values remain and its invalid |
| 50 | + ss.get(); |
| 51 | + if (ss.eof() == false) isValid = false; |
| 52 | + |
| 53 | + // Returning valid flag |
| 54 | + return isValid; |
| 55 | + }; |
| 56 | + |
| 57 | + inline void setController1Type(const controller_t type) { _controller1Type = type; } |
| 58 | + inline void setController2Type(const controller_t type) { _controller2Type = type; } |
| 59 | + |
| 60 | + inline bool getPowerButtonState() { return _input.power; } |
| 61 | + inline bool getResetButtonState() { return _input.reset; } |
| 62 | + inline port_t getController1Code() { return _input.port1; } |
| 63 | + inline port_t getController2Code() { return _input.port2; } |
| 64 | + |
| 65 | + private: |
| 66 | + |
| 67 | + static bool parseJoyPadInput(uint8_t& code, std::istringstream& ss) |
| 68 | + { |
| 69 | + // Currently read character |
| 70 | + char c; |
| 71 | + |
| 72 | + // Cleaning code |
| 73 | + code = 0; |
| 74 | + |
| 75 | + // Up |
| 76 | + c = ss.get(); |
| 77 | + if (c != '.' && c != 'U') return false; |
| 78 | + if (c == 'U') code |= 0b00010000; |
| 79 | + |
| 80 | + // Down |
| 81 | + c = ss.get(); |
| 82 | + if (c != '.' && c != 'D') return false; |
| 83 | + if (c == 'D') code |= 0b00100000; |
| 84 | + |
| 85 | + // Left |
| 86 | + c = ss.get(); |
| 87 | + if (c != '.' && c != 'L') return false; |
| 88 | + if (c == 'L') code |= 0b01000000; |
| 89 | + |
| 90 | + // Right |
| 91 | + c = ss.get(); |
| 92 | + if (c != '.' && c != 'R') return false; |
| 93 | + if (c == 'R') code |= 0b10000000; |
| 94 | + |
| 95 | + // Start |
| 96 | + c = ss.get(); |
| 97 | + if (c != '.' && c != 'S') return false; |
| 98 | + if (c == 'S') code |= 0b00001000; |
| 99 | + |
| 100 | + // Select |
| 101 | + c = ss.get(); |
| 102 | + if (c != '.' && c != 's') return false; |
| 103 | + if (c == 's') code |= 0b00000100; |
| 104 | + |
| 105 | + // B |
| 106 | + c = ss.get(); |
| 107 | + if (c != '.' && c != 'B') return false; |
| 108 | + if (c == 'B') code |= 0b00000010; |
| 109 | + |
| 110 | + // A |
| 111 | + c = ss.get(); |
| 112 | + if (c != '.' && c != 'A') return false; |
| 113 | + if (c == 'A') code |= 0b00000001; |
| 114 | + |
| 115 | + return true; |
| 116 | + } |
| 117 | + |
| 118 | + static bool parseControllerInputs(const controller_t type, port_t& port, std::istringstream& ss) |
| 119 | + { |
| 120 | + // Parse valid flag |
| 121 | + bool isValid = true; |
| 122 | + |
| 123 | + // If no controller assigned then, its port is all zeroes. |
| 124 | + if (type == controller_t::none) { port = 0; return true; } |
| 125 | + |
| 126 | + // Controller separator |
| 127 | + if (ss.get() != '|') isValid = false; |
| 128 | + |
| 129 | + // If normal joypad, parse its code now |
| 130 | + if (type == controller_t::joypad) |
| 131 | + { |
| 132 | + // Storage for joypad's code |
| 133 | + uint8_t code = 0; |
| 134 | + |
| 135 | + // Parsing joypad code |
| 136 | + isValid &= parseJoyPadInput(code, ss); |
| 137 | + |
| 138 | + // Pushing input code into the port |
| 139 | + port = code; |
| 140 | + |
| 141 | + // Adding joypad signature |
| 142 | + port |= ~0xFF; |
| 143 | + } |
| 144 | + |
| 145 | + // If its fourscore, its like two joypads separated by a | |
| 146 | + if (type == controller_t::fourscore1 || type == controller_t::fourscore2) |
| 147 | + { |
| 148 | + // Storage for joypad's code |
| 149 | + uint8_t code1 = 0; |
| 150 | + uint8_t code2 = 0; |
| 151 | + |
| 152 | + // Parsing joypad code1 |
| 153 | + isValid &= parseJoyPadInput(code1, ss); |
| 154 | + |
| 155 | + // Separator |
| 156 | + if (ss.get() != '|') return false; |
| 157 | + |
| 158 | + // Parsing joypad code1 |
| 159 | + isValid &= parseJoyPadInput(code2, ss); |
| 160 | + |
| 161 | + // Creating code |
| 162 | + port = code1; |
| 163 | + port |= (uint32_t)0 | code2 << 8; |
| 164 | + if (type == controller_t::fourscore1) port |= (uint32_t)0 | 1 << 19; |
| 165 | + if (type == controller_t::fourscore2) port |= (uint32_t)0 | 1 << 18; |
| 166 | + port |= (uint32_t)0 | 1 << 24; |
| 167 | + port |= (uint32_t)0 | 1 << 25; |
| 168 | + port |= (uint32_t)0 | 1 << 26; |
| 169 | + port |= (uint32_t)0 | 1 << 27; |
| 170 | + port |= (uint32_t)0 | 1 << 28; |
| 171 | + port |= (uint32_t)0 | 1 << 29; |
| 172 | + port |= (uint32_t)0 | 1 << 30; |
| 173 | + port |= (uint32_t)0 | 1 << 31; |
| 174 | + } |
| 175 | + // Return valid flag |
| 176 | + return isValid; |
| 177 | + } |
| 178 | + |
| 179 | + static bool parseConsoleInputs(bool& reset, bool& power, std::istringstream& ss) |
| 180 | + { |
| 181 | + // Parse valid flag |
| 182 | + bool isValid = true; |
| 183 | + |
| 184 | + // Currently read character |
| 185 | + char c; |
| 186 | + |
| 187 | + // Power trigger |
| 188 | + c = ss.get(); |
| 189 | + if (c != '.' && c != 'P') isValid = false; |
| 190 | + if (c == 'P') power = true; |
| 191 | + if (c == '.') power = false; |
| 192 | + |
| 193 | + // Reset trigger |
| 194 | + c = ss.get(); |
| 195 | + if (c != '.' && c != 'r') isValid = false; |
| 196 | + if (c == 'r') reset = true; |
| 197 | + if (c == '.') reset = false; |
| 198 | + |
| 199 | + // Return valid flag |
| 200 | + return isValid; |
| 201 | + } |
| 202 | + |
| 203 | + input_t _input; |
| 204 | + controller_t _controller1Type; |
| 205 | + controller_t _controller2Type; |
| 206 | +}; |
0 commit comments