Skip to content

Commit

Permalink
split ControlDeck between Ship:: and LUS:: (#758)
Browse files Browse the repository at this point in the history
* split writetopad

* split readtopad

* move mpadbuffer

* move some controldeck constructor stuff

* move the rest of the oscontpad stuff out of ship::

* move stuff back to private

* more stuff back to private

* format

* remove a couple `LUS::`

* take a controldeck in

* format

* newline

* newline

* newline
  • Loading branch information
briaguya-ai authored Dec 16, 2024
1 parent 8b5554d commit bd307e8
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 101 deletions.
14 changes: 8 additions & 6 deletions src/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ Context::~Context() {
std::shared_ptr<Context>
Context::CreateInstance(const std::string name, const std::string shortName, const std::string configFilePath,
const std::vector<std::string>& archivePaths, const std::unordered_set<uint32_t>& validHashes,
uint32_t reservedThreadCount, AudioSettings audioSettings, std::shared_ptr<Window> window) {
uint32_t reservedThreadCount, AudioSettings audioSettings, std::shared_ptr<Window> window,
std::shared_ptr<ControlDeck> controlDeck) {
if (mContext.expired()) {
auto shared = std::make_shared<Context>(name, shortName, configFilePath);
mContext = shared;
if (shared->Init(archivePaths, validHashes, reservedThreadCount, audioSettings, window)) {
if (shared->Init(archivePaths, validHashes, reservedThreadCount, audioSettings, window, controlDeck)) {
return shared;
} else {
SPDLOG_ERROR("Failed to initialize");
Expand Down Expand Up @@ -79,9 +80,10 @@ Context::Context(std::string name, std::string shortName, std::string configFile
}

bool Context::Init(const std::vector<std::string>& archivePaths, const std::unordered_set<uint32_t>& validHashes,
uint32_t reservedThreadCount, AudioSettings audioSettings, std::shared_ptr<Window> window) {
uint32_t reservedThreadCount, AudioSettings audioSettings, std::shared_ptr<Window> window,
std::shared_ptr<ControlDeck> controlDeck) {
return InitLogging() && InitConfiguration() && InitConsoleVariables() &&
InitResourceManager(archivePaths, validHashes, reservedThreadCount) && InitControlDeck() &&
InitResourceManager(archivePaths, validHashes, reservedThreadCount) && InitControlDeck(controlDeck) &&
InitCrashHandler() && InitConsole() && InitWindow(window) && InitAudio(audioSettings) && InitGfxDebugger();
}

Expand Down Expand Up @@ -228,12 +230,12 @@ bool Context::InitResourceManager(const std::vector<std::string>& archivePaths,
return true;
}

bool Context::InitControlDeck(std::vector<CONTROLLERBUTTONS_T> additionalBitmasks) {
bool Context::InitControlDeck(std::shared_ptr<ControlDeck> controlDeck) {
if (GetControlDeck() != nullptr) {
return true;
}

mControlDeck = std::make_shared<ControlDeck>(additionalBitmasks);
mControlDeck = controlDeck;

if (GetControlDeck() == nullptr) {
SPDLOG_ERROR("Failed to initialize control deck");
Expand Down
8 changes: 5 additions & 3 deletions src/Context.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ class Context {
const std::vector<std::string>& archivePaths = {},
const std::unordered_set<uint32_t>& validHashes = {},
uint32_t reservedThreadCount = 1, AudioSettings audioSettings = {},
std::shared_ptr<Window> window = nullptr);
std::shared_ptr<Window> window = nullptr,
std::shared_ptr<ControlDeck> controlDeck = nullptr);
static std::shared_ptr<Context> CreateUninitializedInstance(const std::string name, const std::string shortName,
const std::string configFilePath);
static std::string GetAppBundlePath();
Expand All @@ -39,7 +40,8 @@ class Context {
~Context();

bool Init(const std::vector<std::string>& archivePaths, const std::unordered_set<uint32_t>& validHashes,
uint32_t reservedThreadCount, AudioSettings audioSettings, std::shared_ptr<Window> window = nullptr);
uint32_t reservedThreadCount, AudioSettings audioSettings, std::shared_ptr<Window> window = nullptr,
std::shared_ptr<ControlDeck> controlDeck = nullptr);

std::shared_ptr<spdlog::logger> GetLogger();
std::shared_ptr<Config> GetConfig();
Expand All @@ -60,7 +62,7 @@ class Context {
bool InitConsoleVariables();
bool InitResourceManager(const std::vector<std::string>& archivePaths = {},
const std::unordered_set<uint32_t>& validHashes = {}, uint32_t reservedThreadCount = 1);
bool InitControlDeck(std::vector<CONTROLLERBUTTONS_T> additionalBitmasks = {});
bool InitControlDeck(std::shared_ptr<ControlDeck> controlDeck = nullptr);
bool InitCrashHandler();
bool InitAudio(AudioSettings settings);
bool InitGfxDebugger();
Expand Down
70 changes: 39 additions & 31 deletions src/controller/controldeck/ControlDeck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,10 @@

namespace Ship {

ControlDeck::ControlDeck(std::vector<CONTROLLERBUTTONS_T> additionalBitmasks)
: mPads(nullptr), mSinglePlayerMappingMode(false) {
for (int32_t i = 0; i < MAXCONTROLLERS; i++) {
mPorts.push_back(std::make_shared<ControlPort>(i, std::make_shared<Controller>(i, additionalBitmasks)));
}

ControlDeck::ControlDeck(std::vector<CONTROLLERBUTTONS_T> additionalBitmasks) : mSinglePlayerMappingMode(false) {
mDeviceIndexMappingManager = std::make_shared<ShipDeviceIndexMappingManager>();
}

ControlDeck::ControlDeck() : ControlDeck(std::vector<CONTROLLERBUTTONS_T>()) {
}

ControlDeck::~ControlDeck() {
SPDLOG_TRACE("destruct control deck");
}
Expand Down Expand Up @@ -74,28 +66,6 @@ bool ControlDeck::KeyboardGameInputBlocked() {
return AllGameInputBlocked() || ImGui::GetIO().WantCaptureKeyboard;
}

void ControlDeck::WriteToPad(OSContPad* pad) {
SDL_PumpEvents();

if (AllGameInputBlocked()) {
return;
}

mPads = pad;

for (size_t i = 0; i < mPorts.size(); i++) {
const std::shared_ptr<Controller> controller = mPorts[i]->GetConnectedController();

if (controller != nullptr) {
controller->ReadToPad(&pad[i]);
}
}
}

OSContPad* ControlDeck::GetPads() {
return mPads;
}

std::shared_ptr<Controller> ControlDeck::GetControllerByPort(uint8_t port) {
return mPorts[port]->GetConnectedController();
}
Expand All @@ -120,3 +90,41 @@ bool ControlDeck::IsSinglePlayerMappingMode() {
return mSinglePlayerMappingMode;
}
} // namespace Ship

namespace LUS {
ControlDeck::ControlDeck(std::vector<CONTROLLERBUTTONS_T> additionalBitmasks)
: Ship::ControlDeck(additionalBitmasks), mPads(nullptr) {
for (int32_t i = 0; i < MAXCONTROLLERS; i++) {
mPorts.push_back(std::make_shared<Ship::ControlPort>(i, std::make_shared<Controller>(i, additionalBitmasks)));
}
}

ControlDeck::ControlDeck() : ControlDeck(std::vector<CONTROLLERBUTTONS_T>()) {
}

OSContPad* ControlDeck::GetPads() {
return mPads;
}

void ControlDeck::WriteToPad(void* pad) {
WriteToOSContPad((OSContPad*)pad);
}

void ControlDeck::WriteToOSContPad(OSContPad* pad) {
SDL_PumpEvents();

if (AllGameInputBlocked()) {
return;
}

mPads = pad;

for (size_t i = 0; i < mPorts.size(); i++) {
const std::shared_ptr<Ship::Controller> controller = mPorts[i]->GetConnectedController();

if (controller != nullptr) {
controller->ReadToPad(&pad[i]);
}
}
}
} // namespace LUS
30 changes: 21 additions & 9 deletions src/controller/controldeck/ControlDeck.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ namespace Ship {

class ControlDeck {
public:
ControlDeck();
ControlDeck(std::vector<CONTROLLERBUTTONS_T> additionalBitmasks);
~ControlDeck();

void Init(uint8_t* controllerBits);
void WriteToPad(OSContPad* pad);
OSContPad* GetPads();
virtual void WriteToPad(void* pads) = 0;
uint8_t* GetControllerBits();
std::shared_ptr<Controller> GetControllerByPort(uint8_t port);
void BlockGameInput(int32_t blockId);
Expand All @@ -25,19 +23,33 @@ class ControlDeck {
bool KeyboardGameInputBlocked();
void SetSinglePlayerMappingMode(bool singlePlayer);
bool IsSinglePlayerMappingMode();

bool ProcessKeyboardEvent(KbEventType eventType, KbScancode scancode);

std::shared_ptr<ShipDeviceIndexMappingManager> GetDeviceIndexMappingManager();

private:
protected:
bool AllGameInputBlocked();
std::vector<std::shared_ptr<ControlPort>> mPorts = {};

private:
uint8_t* mControllerBits = nullptr;
OSContPad* mPads;
bool mSinglePlayerMappingMode;

bool AllGameInputBlocked();
std::unordered_map<int32_t, bool> mGameInputBlockers;
std::shared_ptr<ShipDeviceIndexMappingManager> mDeviceIndexMappingManager;
};
} // namespace Ship

namespace LUS {
class ControlDeck : public Ship::ControlDeck {
public:
ControlDeck();
ControlDeck(std::vector<CONTROLLERBUTTONS_T> additionalBitmasks);

OSContPad* GetPads();
void WriteToPad(void* pad) override;

private:
void WriteToOSContPad(OSContPad* pad);

OSContPad* mPads;
};
} // namespace LUS
111 changes: 62 additions & 49 deletions src/controller/controldevice/controller/Controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,55 +126,6 @@ void Controller::ReloadAllMappingsFromConfig() {
GetLED()->ReloadAllMappingsFromConfig();
}

void Controller::ReadToPad(OSContPad* pad) {
OSContPad padToBuffer = { 0 };

// Button Inputs
for (auto [bitmask, button] : mButtons) {
button->UpdatePad(padToBuffer.button);
}

// Stick Inputs
GetLeftStick()->UpdatePad(padToBuffer.stick_x, padToBuffer.stick_y);
GetRightStick()->UpdatePad(padToBuffer.right_stick_x, padToBuffer.right_stick_y);

// Gyro
GetGyro()->UpdatePad(padToBuffer.gyro_x, padToBuffer.gyro_y);

mPadBuffer.push_front(padToBuffer);
if (pad != nullptr) {
auto& padFromBuffer =
mPadBuffer[std::min(mPadBuffer.size() - 1, (size_t)CVarGetInteger(CVAR_SIMULATED_INPUT_LAG, 0))];

pad->button |= padFromBuffer.button;

if (pad->stick_x == 0) {
pad->stick_x = padFromBuffer.stick_x;
}
if (pad->stick_y == 0) {
pad->stick_y = padFromBuffer.stick_y;
}

if (pad->right_stick_x == 0) {
pad->right_stick_x = padFromBuffer.right_stick_x;
}
if (pad->right_stick_y == 0) {
pad->right_stick_y = padFromBuffer.right_stick_y;
}

if (pad->gyro_x == 0) {
pad->gyro_x = padFromBuffer.gyro_x;
}
if (pad->gyro_y == 0) {
pad->gyro_y = padFromBuffer.gyro_y;
}
}

while (mPadBuffer.size() > 6) {
mPadBuffer.pop_back();
}
}

bool Controller::ProcessKeyboardEvent(KbEventType eventType, KbScancode scancode) {
bool result = false;
for (auto [bitmask, button] : GetAllButtons()) {
Expand Down Expand Up @@ -333,3 +284,65 @@ std::vector<std::shared_ptr<ControllerMapping>> Controller::GetAllMappings() {
return allMappings;
}
} // namespace Ship

namespace LUS {
Controller::Controller(uint8_t portIndex, std::vector<CONTROLLERBUTTONS_T> additionalBitmasks)
: Ship::Controller(portIndex, additionalBitmasks) {
}

Controller::Controller(uint8_t portIndex) : Ship::Controller(portIndex, {}) {
}

void Controller::ReadToPad(void* pad) {
ReadToOSContPad((OSContPad*)pad);
}

void Controller::ReadToOSContPad(OSContPad* pad) {
OSContPad padToBuffer = { 0 };

// Button Inputs
for (auto [bitmask, button] : mButtons) {
button->UpdatePad(padToBuffer.button);
}

// Stick Inputs
GetLeftStick()->UpdatePad(padToBuffer.stick_x, padToBuffer.stick_y);
GetRightStick()->UpdatePad(padToBuffer.right_stick_x, padToBuffer.right_stick_y);

// Gyro
GetGyro()->UpdatePad(padToBuffer.gyro_x, padToBuffer.gyro_y);

mPadBuffer.push_front(padToBuffer);
if (pad != nullptr) {
auto& padFromBuffer =
mPadBuffer[std::min(mPadBuffer.size() - 1, (size_t)CVarGetInteger(CVAR_SIMULATED_INPUT_LAG, 0))];

pad->button |= padFromBuffer.button;

if (pad->stick_x == 0) {
pad->stick_x = padFromBuffer.stick_x;
}
if (pad->stick_y == 0) {
pad->stick_y = padFromBuffer.stick_y;
}

if (pad->right_stick_x == 0) {
pad->right_stick_x = padFromBuffer.right_stick_x;
}
if (pad->right_stick_y == 0) {
pad->right_stick_y = padFromBuffer.right_stick_y;
}

if (pad->gyro_x == 0) {
pad->gyro_x = padFromBuffer.gyro_x;
}
if (pad->gyro_y == 0) {
pad->gyro_y = padFromBuffer.gyro_y;
}
}

while (mPadBuffer.size() > 6) {
mPadBuffer.pop_back();
}
}
} // namespace LUS
21 changes: 18 additions & 3 deletions src/controller/controldevice/controller/Controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class Controller : public ControlDevice {
std::shared_ptr<ControllerGyro> GetGyro();
std::shared_ptr<ControllerRumble> GetRumble();
std::shared_ptr<ControllerLED> GetLED();
void ReadToPad(OSContPad* pad);
virtual void ReadToPad(void* pad) = 0;
bool HasConfig();
uint8_t GetPortIndex();
std::vector<std::shared_ptr<ControllerMapping>> GetAllMappings();
Expand All @@ -53,16 +53,31 @@ class Controller : public ControlDevice {
bool HasMappingsForShipDeviceIndex(ShipDeviceIndex lusIndex);
void MoveMappingsToDifferentController(std::shared_ptr<Controller> newController, ShipDeviceIndex lusIndex);

protected:
std::unordered_map<CONTROLLERBUTTONS_T, std::shared_ptr<ControllerButton>> mButtons;

private:
void LoadButtonMappingFromConfig(std::string id);
void SaveButtonMappingIdsToConfig();

std::unordered_map<CONTROLLERBUTTONS_T, std::shared_ptr<ControllerButton>> mButtons;
std::shared_ptr<ControllerStick> mLeftStick, mRightStick;
std::shared_ptr<ControllerGyro> mGyro;
std::shared_ptr<ControllerRumble> mRumble;
std::shared_ptr<ControllerLED> mLED;
};
} // namespace Ship

namespace LUS {
class Controller : public Ship::Controller {
public:
Controller(uint8_t portIndex);
Controller(uint8_t portIndex, std::vector<CONTROLLERBUTTONS_T> additionalBitmasks);

void ReadToPad(void* pad) override;

private:
void ReadToOSContPad(OSContPad* pad);

std::deque<OSContPad> mPadBuffer;
};
} // namespace Ship
} // namespace LUS

0 comments on commit bd307e8

Please sign in to comment.