Skip to content

Commit

Permalink
get in game
Browse files Browse the repository at this point in the history
  • Loading branch information
briaguya-ai committed Jan 5, 2025
1 parent e1b595e commit c16d8d8
Show file tree
Hide file tree
Showing 11 changed files with 206 additions and 101 deletions.
47 changes: 25 additions & 22 deletions src/audio/SDLAudioPlayer.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "SDLAudioPlayer.h"
#include <spdlog/spdlog.h>

// todo: https://wiki.libsdl.org/SDL3/README/migration#sdl_audioh

namespace Ship {

SDLAudioPlayer::~SDLAudioPlayer() {
Expand All @@ -9,35 +11,36 @@ SDLAudioPlayer::~SDLAudioPlayer() {
}

bool SDLAudioPlayer::DoInit() {
if (SDL_Init(SDL_INIT_AUDIO) != 0) {
SPDLOG_ERROR("SDL init error: %s\n", SDL_GetError());
return false;
}
SDL_AudioSpec want, have;
SDL_zero(want);
want.freq = this->GetSampleRate();
want.format = SDL_AUDIO_S16;
want.channels = 2;
want.samples = this->GetSampleLength();
want.callback = NULL;
mDevice = SDL_OpenAudioDevice(NULL, 0, &want, &have, 0);
if (mDevice == 0) {
SPDLOG_ERROR("SDL_OpenAudio error: {}", SDL_GetError());
return false;
}
SDL_PauseAudioDevice(mDevice, 0);
// if (SDL_Init(SDL_INIT_AUDIO) != 0) {
// SPDLOG_ERROR("SDL init error: %s\n", SDL_GetError());
// return false;
// }
// SDL_AudioSpec want, have;
// SDL_zero(want);
// want.freq = this->GetSampleRate();
// want.format = SDL_AUDIO_S16;
// want.channels = 2;
// want.samples = this->GetSampleLength();
// want.callback = NULL;
// mDevice = SDL_OpenAudioDevice(NULL, 0, &want, &have, 0);
// if (mDevice == 0) {
// SPDLOG_ERROR("SDL_OpenAudio error: {}", SDL_GetError());
// return false;
// }
// SDL_PauseAudioDevice(mDevice, 0);
return true;
}

int SDLAudioPlayer::Buffered() {
// 4 is sizeof(int16_t) * num_channels (2 for stereo)
return SDL_GetQueuedAudioSize(mDevice) / 4;
// return SDL_GetQueuedAudioSize(mDevice) / 4;
return 0;
}

void SDLAudioPlayer::Play(const uint8_t* buf, size_t len) {
if (Buffered() < 6000) {
// Don't fill the audio buffer too much in case this happens
SDL_QueueAudio(mDevice, buf, len);
}
// if (Buffered() < 6000) {
// // Don't fill the audio buffer too much in case this happens
// SDL_QueueAudio(mDevice, buf, len);
// }
}
} // namespace Ship
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ std::shared_ptr<ControllerLEDMapping> LEDMappingFactory::CreateLEDMappingFromSDL
}

auto controller = SDL_OpenGamepad(sdlIndex);
if (SDL_GameControllerHasLED(controller)) {
// todo: SDL_GameControllerHasLED() - replaced with SDL_PROP_GAMEPAD_CAP_RGB_LED_BOOLEAN
// if (SDL_GameControllerHasLED(controller)) {
if (false) {
sdlControllersWithLEDs[lusIndex] = SDL_OpenGamepad(sdlIndex);
} else {
SDL_CloseGamepad(controller);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ std::shared_ptr<ControllerRumbleMapping> RumbleMappingFactory::CreateRumbleMappi
}

auto controller = SDL_OpenGamepad(sdlIndex);
bool hasRumble = SDL_GameControllerHasRumble(controller);
// todo: SDL_GameControllerHasRumble() - replaced with SDL_PROP_GAMEPAD_CAP_RUMBLE_BOOLEAN
// bool hasRumble = SDL_GameControllerHasRumble(controller);
bool hasRumble = false;

if (hasRumble) {
sdlControllersWithRumble[lusIndex] = SDL_OpenGamepad(sdlIndex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ void SDLLEDMapping::SetLEDColor(Color_RGB8 color) {
return;
}

if (!SDL_GameControllerHasLED(mController)) {
// todo: SDL_GameControllerHasLED() - replaced with SDL_PROP_GAMEPAD_CAP_RGB_LED_BOOLEAN
// if (!SDL_GameControllerHasLED(mController)) {
if (true) {
return;
}

Expand Down
17 changes: 12 additions & 5 deletions src/controller/controldevice/controller/mapping/sdl/SDLMapping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,20 @@ int32_t SDLMapping::GetCurrentSDLDeviceIndex() {
return -1;
}

for (int32_t i = 0; i < SDL_NumJoysticks(); i++) {
SDL_Joystick* joystick = SDL_OpenJoystick(i);
if (SDL_GetJoystickID(joystick) == SDL_GetJoystickID(SDL_GetGamepadJoystick(mController))) {
int i, numJoysticks;
SDL_JoystickID *joysticks = SDL_GetJoysticks(&numJoysticks);
if (joysticks) {
for (i = 0; i < numJoysticks; ++i) {
SDL_JoystickID instanceId = joysticks[i];
SDL_Joystick* joystick = SDL_OpenJoystick(instanceId);
if (SDL_GetJoystickID(joystick) == SDL_GetJoystickID(SDL_GetGamepadJoystick(mController))) {
SDL_CloseJoystick(joystick);
SDL_free(joysticks);
return i;
}
SDL_CloseJoystick(joystick);
return i;
}
SDL_CloseJoystick(joystick);
SDL_free(joysticks);
}

// didn't find one
Expand Down
37 changes: 28 additions & 9 deletions src/controller/deviceindex/ControllerDisconnectedWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,17 @@ int32_t ControllerDisconnectedWindow::GetSDLIndexFromSDLInput() {
int32_t sdlDeviceIndex = -1;

std::unordered_map<int32_t, SDL_Gamepad*> sdlControllers;
for (auto i = 0; i < SDL_NumJoysticks(); i++) {
if (SDL_IsGamepad(i)) {
sdlControllers[i] = SDL_OpenGamepad(i);

int i, numJoysticks;
SDL_JoystickID *joysticks = SDL_GetJoysticks(&numJoysticks);
if (joysticks) {
for (i = 0; i < numJoysticks; ++i) {
SDL_JoystickID instanceId = joysticks[i];
if (SDL_IsGamepad(instanceId)) {
sdlControllers[i] = SDL_OpenGamepad(instanceId);
}
}
SDL_free(joysticks);
}

for (auto [controllerIndex, controller] : sdlControllers) {
Expand Down Expand Up @@ -94,11 +101,17 @@ void ControllerDisconnectedWindow::DrawKnownControllerDisconnected() {
Hide();
}

int i, numJoysticks;
uint8_t connectedSdlControllerCount = 0;
for (auto i = 0; i < SDL_NumJoysticks(); i++) {
if (SDL_IsGamepad(i)) {
connectedSdlControllerCount++;
SDL_JoystickID *joysticks = SDL_GetJoysticks(&numJoysticks);
if (joysticks) {
for (i = 0; i < numJoysticks; ++i) {
SDL_JoystickID instanceId = joysticks[i];
if (SDL_IsGamepad(instanceId)) {
connectedSdlControllerCount++;
}
}
SDL_free(joysticks);
}

if (connectedSdlControllerCount != 0 &&
Expand All @@ -115,11 +128,17 @@ void ControllerDisconnectedWindow::DrawKnownControllerDisconnected() {
void ControllerDisconnectedWindow::DrawUnknownOrMultipleControllersDisconnected() {
ImGui::Text("Controller(s) disconnected.");

int i, numJoysticks;
uint8_t connectedSdlControllerCount = 0;
for (auto i = 0; i < SDL_NumJoysticks(); i++) {
if (SDL_IsGamepad(i)) {
connectedSdlControllerCount++;
SDL_JoystickID *joysticks = SDL_GetJoysticks(&numJoysticks);
if (joysticks) {
for (i = 0; i < numJoysticks; ++i) {
SDL_JoystickID instanceId = joysticks[i];
if (SDL_IsGamepad(instanceId)) {
connectedSdlControllerCount++;
}
}
SDL_free(joysticks);
}

if (connectedSdlControllerCount != 0 &&
Expand Down
27 changes: 21 additions & 6 deletions src/controller/deviceindex/ControllerReorderingWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,17 @@ int32_t ControllerReorderingWindow::GetSDLIndexFromSDLInput() {
int32_t sdlDeviceIndex = -1;

std::unordered_map<int32_t, SDL_Gamepad*> sdlControllers;
for (auto i = 0; i < SDL_NumJoysticks(); i++) {
if (SDL_IsGamepad(i)) {
sdlControllers[i] = SDL_OpenGamepad(i);

int i, numJoysticks;
SDL_JoystickID *joysticks = SDL_GetJoysticks(&numJoysticks);
if (joysticks) {
for (i = 0; i < numJoysticks; ++i) {
SDL_JoystickID instanceId = joysticks[i];
if (SDL_IsGamepad(instanceId)) {
sdlControllers[i] = SDL_OpenGamepad(instanceId);
}
}
SDL_free(joysticks);
}

for (auto [controllerIndex, controller] : sdlControllers) {
Expand Down Expand Up @@ -65,11 +72,19 @@ void ControllerReorderingWindow::DrawElement() {

// if we don't have more than one controller, just close the window
std::vector<int32_t> connectedSdlControllerIndices;
for (auto i = 0; i < SDL_NumJoysticks(); i++) {
if (SDL_IsGamepad(i)) {
connectedSdlControllerIndices.push_back(i);

int i, numJoysticks;
SDL_JoystickID *joysticks = SDL_GetJoysticks(&numJoysticks);
if (joysticks) {
for (i = 0; i < numJoysticks; ++i) {
SDL_JoystickID instanceId = joysticks[i];
if (SDL_IsGamepad(instanceId)) {
connectedSdlControllerIndices.push_back(i);
}
}
SDL_free(joysticks);
}

if (connectedSdlControllerIndices.size() <= 1) {
Context::GetInstance()->GetControlDeck()->GetDeviceIndexMappingManager()->InitializeMappingsMultiplayer(
connectedSdlControllerIndices);
Expand Down
27 changes: 19 additions & 8 deletions src/controller/deviceindex/ShipDeviceIndexMappingManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,15 @@ void ShipDeviceIndexMappingManager::InitializeSDLMappingsForPort(uint8_t n64port
return;
}

char guidString[33]; // SDL_GUID_LENGTH + 1 for null terminator
SDL_JoystickGetGUIDString(SDL_JoystickGetDeviceGUID(sdlIndex), guidString, sizeof(guidString));
std::string sdlControllerName = SDL_GameControllerNameForIndex(sdlIndex) != nullptr
? SDL_GameControllerNameForIndex(sdlIndex)
: "Game Controller";
// todo: move a bunch of stuff in here over to use instance ids
// char guidString[33]; // SDL_GUID_LENGTH + 1 for null terminator
// SDL_GetJoystickGUIDForID()
// SDL_GUIDToString(SDL_JoystickGetDeviceGUID(sdlIndex), guidString, sizeof(guidString));
// std::string sdlControllerName = SDL_GameControllerNameForIndex(sdlIndex) != nullptr
// ? SDL_GameControllerNameForIndex(sdlIndex)
// : "Game Controller";
char guidString[33] = "guidString";
std::string sdlControllerName = "controllerName";

// find all lus indices with this guid
std::vector<ShipDeviceIndex> matchingGuidLusIndices;
Expand Down Expand Up @@ -173,10 +177,17 @@ void ShipDeviceIndexMappingManager::InitializeMappingsSinglePlayer() {
}

std::vector<int32_t> connectedSdlControllerIndices;
for (auto i = 0; i < SDL_NumJoysticks(); i++) {
if (SDL_IsGamepad(i)) {
connectedSdlControllerIndices.push_back(i);

int i, numJoysticks;
SDL_JoystickID *joysticks = SDL_GetJoysticks(&numJoysticks);
if (joysticks) {
for (i = 0; i < numJoysticks; ++i) {
SDL_JoystickID instanceId = joysticks[i];
if (SDL_IsGamepad(instanceId)) {
connectedSdlControllerIndices.push_back(i);
}
}
SDL_free(joysticks);
}

mShipDeviceIndexToPhysicalDeviceIndexMappings.clear();
Expand Down
Loading

0 comments on commit c16d8d8

Please sign in to comment.