Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions cmake/dependencies/common.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@ if (CMAKE_GENERATOR MATCHES "Visual Studio")
endif()

#=================== ImGui ===================
set(sdl_gamepad_patch_file ${CMAKE_CURRENT_SOURCE_DIR}/cmake/dependencies/patches/sdl-gamepad-fix.patch)
# set(sdl_gamepad_patch_file ${CMAKE_CURRENT_SOURCE_DIR}/cmake/dependencies/patches/sdl-gamepad-fix.patch)

# Applies the patch or checks if it has already been applied successfully previously. Will error otherwise.
set(sdl_apply_patch_if_needed git apply ${sdl_gamepad_patch_file} ${git_hide_output} || git apply --reverse --check ${sdl_gamepad_patch_file})
# set(sdl_apply_patch_if_needed git apply ${sdl_gamepad_patch_file} ${git_hide_output} || git apply --reverse --check ${sdl_gamepad_patch_file})
FetchContent_Declare(
ImGui
GIT_REPOSITORY https://github.com/ocornut/imgui.git
GIT_TAG v1.90.6-docking
PATCH_COMMAND ${sdl_apply_patch_if_needed}
GIT_TAG v1.91.6-docking
)
FetchContent_MakeAvailable(ImGui)
list(APPEND ADDITIONAL_LIB_INCLUDES ${imgui_SOURCE_DIR} ${imgui_SOURCE_DIR}/backends)
Expand All @@ -37,7 +36,7 @@ target_sources(ImGui
target_sources(ImGui
PRIVATE
${imgui_SOURCE_DIR}/backends/imgui_impl_opengl3.cpp
${imgui_SOURCE_DIR}/backends/imgui_impl_sdl2.cpp
${imgui_SOURCE_DIR}/backends/imgui_impl_sdl3.cpp
)

target_include_directories(ImGui PUBLIC ${imgui_SOURCE_DIR} ${imgui_SOURCE_DIR}/backends PRIVATE ${SDL2_INCLUDE_DIRS})
Expand Down
4 changes: 2 additions & 2 deletions cmake/dependencies/linux.cmake
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#=================== ImGui ===================
find_package(SDL2 REQUIRED)
target_link_libraries(ImGui PUBLIC SDL2::SDL2)
find_package(SDL3 REQUIRED)
target_link_libraries(ImGui PUBLIC SDL3::SDL3)

if (USE_OPENGLES)
target_link_libraries(ImGui PUBLIC ${OPENGL_GLESv2_LIBRARY})
Expand Down
2 changes: 1 addition & 1 deletion include/libultraship/classes.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
#include "audio/WasapiAudioPlayer.h"
#endif
#include "audio/SDLAudioPlayer.h"
#ifdef __APPLE__
#ifdef SDL_PLATFORM_APPLE
#include "utils/AppleFolderManager.h"
#endif
#endif
Expand Down
10 changes: 5 additions & 5 deletions src/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <tchar.h>
#endif

#ifdef __APPLE__
#ifdef SDL_PLATFORM_APPLE
#include "utils/AppleFolderManager.h"
#include <unistd.h>
#include <pwd.h>
Expand Down Expand Up @@ -375,7 +375,7 @@ std::string Context::GetShortName() {

std::string Context::GetAppBundlePath() {
#if defined(__ANDROID__)
const char* externaldir = SDL_AndroidGetExternalStoragePath();
const char* externaldir = SDL_GetAndroidExternalStoragePath();
if (externaldir != NULL) {
return externaldir;
}
Expand All @@ -389,7 +389,7 @@ std::string Context::GetAppBundlePath() {
#ifdef NON_PORTABLE
return CMAKE_INSTALL_PREFIX;
#else
#ifdef __APPLE__
#ifdef SDL_PLATFORM_APPLE
FolderManager folderManager;
return folderManager.getMainBundlePath();
#endif
Expand All @@ -416,7 +416,7 @@ std::string Context::GetAppBundlePath() {

std::string Context::GetAppDirectoryPath(std::string appName) {
#if defined(__ANDROID__)
const char* externaldir = SDL_AndroidGetExternalStoragePath();
const char* externaldir = SDL_GetAndroidExternalStoragePath();
if (externaldir != NULL) {
return externaldir;
}
Expand All @@ -427,7 +427,7 @@ std::string Context::GetAppDirectoryPath(std::string appName) {
return std::string(home) + "/Documents";
#endif

#if defined(__APPLE__)
#if defined(SDL_PLATFORM_APPLE)
if (char* fpath = std::getenv("SHIP_HOME")) {
if (fpath[0] == '~') {
const char* home = getenv("HOME") ? getenv("HOME") : getpwuid(getuid())->pw_dir;
Expand Down
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
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

todo


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 = AUDIO_S16SYS;
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
2 changes: 1 addition & 1 deletion src/audio/SDLAudioPlayer.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once
#include "AudioPlayer.h"
#include <SDL2/SDL.h>
#include <SDL3/SDL.h>

namespace Ship {
class SDLAudioPlayer : public AudioPlayer {
Expand Down
4 changes: 2 additions & 2 deletions src/config/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "utils/StringHelper.h"
#include "Context.h"

#ifdef __APPLE__
#ifdef SDL_PLATFORM_APPLE
#include "graphic/Fast3D/gfx_metal.h"
#endif

Expand Down Expand Up @@ -252,7 +252,7 @@ WindowBackend Config::GetWindowBackend() {
#ifdef ENABLE_DX11
return WindowBackend::FAST3D_DXGI_DX11;
#endif
#ifdef __APPLE__
#ifdef SDL_PLATFORM_APPLE
if (Metal_IsSupported()) {
return WindowBackend::FAST3D_SDL_METAL;
}
Expand Down
6 changes: 3 additions & 3 deletions src/controller/controldevice/controller/Controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
#include <memory>
#include <algorithm>
#include "public/bridge/consolevariablebridge.h"
#if __APPLE__
#include <SDL_events.h>
#if SDL_PLATFORM_APPLE
#include <SDL3/SDL_events.h>
#else
#include <SDL2/SDL_events.h>
#include <SDL3/SDL_events.h>
#endif
#include <spdlog/spdlog.h>
#include "utils/StringHelper.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ AxisDirectionMappingFactory::CreateDefaultSDLAxisDirectionMappings(ShipDeviceInd
std::shared_ptr<ControllerAxisDirectionMapping>
AxisDirectionMappingFactory::CreateAxisDirectionMappingFromSDLInput(uint8_t portIndex, StickIndex stickIndex,
Direction direction) {
std::unordered_map<ShipDeviceIndex, SDL_GameController*> sdlControllers;
std::unordered_map<ShipDeviceIndex, SDL_Gamepad*> sdlControllers;
std::shared_ptr<ControllerAxisDirectionMapping> mapping = nullptr;
for (auto [lusIndex, indexMapping] :
Context::GetInstance()->GetControlDeck()->GetDeviceIndexMappingManager()->GetAllDeviceIndexMappings()) {
Expand All @@ -171,17 +171,17 @@ AxisDirectionMappingFactory::CreateAxisDirectionMappingFromSDLInput(uint8_t port

auto sdlIndex = sdlIndexMapping->GetSDLDeviceIndex();

if (!SDL_IsGameController(sdlIndex)) {
if (!SDL_IsGamepad(sdlIndex)) {
// this SDL device isn't a game controller
continue;
}

sdlControllers[lusIndex] = SDL_GameControllerOpen(sdlIndex);
sdlControllers[lusIndex] = SDL_OpenGamepad(sdlIndex);
}

for (auto [lusIndex, controller] : sdlControllers) {
for (int32_t button = SDL_CONTROLLER_BUTTON_A; button < SDL_CONTROLLER_BUTTON_MAX; button++) {
if (SDL_GameControllerGetButton(controller, static_cast<SDL_GameControllerButton>(button))) {
for (int32_t button = SDL_GAMEPAD_BUTTON_SOUTH; button < SDL_GAMEPAD_BUTTON_COUNT; button++) {
if (SDL_GetGamepadButton(controller, static_cast<SDL_GamepadButton>(button))) {
mapping = std::make_shared<SDLButtonToAxisDirectionMapping>(lusIndex, portIndex, stickIndex, direction,
button);
break;
Expand All @@ -192,9 +192,9 @@ AxisDirectionMappingFactory::CreateAxisDirectionMappingFromSDLInput(uint8_t port
break;
}

for (int32_t i = SDL_CONTROLLER_AXIS_LEFTX; i < SDL_CONTROLLER_AXIS_MAX; i++) {
const auto axis = static_cast<SDL_GameControllerAxis>(i);
const auto axisValue = SDL_GameControllerGetAxis(controller, axis) / 32767.0f;
for (int32_t i = SDL_GAMEPAD_AXIS_LEFTX; i < SDL_GAMEPAD_AXIS_COUNT; i++) {
const auto axis = static_cast<SDL_GamepadAxis>(i);
const auto axisValue = SDL_GetGamepadAxis(controller, axis) / 32767.0f;
int32_t axisDirection = 0;
if (axisValue < -0.7f) {
axisDirection = NEGATIVE;
Expand All @@ -213,7 +213,7 @@ AxisDirectionMappingFactory::CreateAxisDirectionMappingFromSDLInput(uint8_t port
}

for (auto [i, controller] : sdlControllers) {
SDL_GameControllerClose(controller);
SDL_CloseGamepad(controller);
}

return mapping;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,73 +172,73 @@ ButtonMappingFactory::CreateDefaultSDLButtonMappings(ShipDeviceIndex shipDeviceI
switch (bitmask) {
case BTN_A:
mappings.push_back(
std::make_shared<SDLButtonToButtonMapping>(shipDeviceIndex, portIndex, BTN_A, SDL_CONTROLLER_BUTTON_A));
std::make_shared<SDLButtonToButtonMapping>(shipDeviceIndex, portIndex, BTN_A, SDL_GAMEPAD_BUTTON_SOUTH));
break;
case BTN_B:
mappings.push_back(
std::make_shared<SDLButtonToButtonMapping>(shipDeviceIndex, portIndex, BTN_B, SDL_CONTROLLER_BUTTON_B));
std::make_shared<SDLButtonToButtonMapping>(shipDeviceIndex, portIndex, BTN_B, SDL_GAMEPAD_BUTTON_EAST));
break;
case BTN_L:
if (!isGameCube) {
mappings.push_back(std::make_shared<SDLButtonToButtonMapping>(shipDeviceIndex, portIndex, BTN_L,
SDL_CONTROLLER_BUTTON_LEFTSHOULDER));
SDL_GAMEPAD_BUTTON_LEFT_SHOULDER));
}
break;
case BTN_R:
mappings.push_back(std::make_shared<SDLAxisDirectionToButtonMapping>(shipDeviceIndex, portIndex, BTN_R,
SDL_CONTROLLER_AXIS_TRIGGERRIGHT, 1));
SDL_GAMEPAD_AXIS_RIGHT_TRIGGER, 1));
break;
case BTN_Z:
mappings.push_back(std::make_shared<SDLAxisDirectionToButtonMapping>(shipDeviceIndex, portIndex, BTN_Z,
SDL_CONTROLLER_AXIS_TRIGGERLEFT, 1));
SDL_GAMEPAD_AXIS_LEFT_TRIGGER, 1));
break;
case BTN_START:
mappings.push_back(std::make_shared<SDLButtonToButtonMapping>(shipDeviceIndex, portIndex, BTN_START,
SDL_CONTROLLER_BUTTON_START));
SDL_GAMEPAD_BUTTON_START));
break;
case BTN_CUP:
mappings.push_back(std::make_shared<SDLAxisDirectionToButtonMapping>(shipDeviceIndex, portIndex, BTN_CUP,
SDL_CONTROLLER_AXIS_RIGHTY, -1));
SDL_GAMEPAD_AXIS_RIGHTY, -1));
break;
case BTN_CDOWN:
mappings.push_back(std::make_shared<SDLAxisDirectionToButtonMapping>(shipDeviceIndex, portIndex, BTN_CDOWN,
SDL_CONTROLLER_AXIS_RIGHTY, 1));
SDL_GAMEPAD_AXIS_RIGHTY, 1));
if (isGameCube) {
mappings.push_back(std::make_shared<SDLButtonToButtonMapping>(shipDeviceIndex, portIndex, BTN_CDOWN,
SDL_CONTROLLER_BUTTON_RIGHTSHOULDER));
SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER));
}
break;
case BTN_CLEFT:
mappings.push_back(std::make_shared<SDLAxisDirectionToButtonMapping>(shipDeviceIndex, portIndex, BTN_CLEFT,
SDL_CONTROLLER_AXIS_RIGHTX, -1));
SDL_GAMEPAD_AXIS_RIGHTX, -1));
if (isGameCube) {
mappings.push_back(std::make_shared<SDLButtonToButtonMapping>(shipDeviceIndex, portIndex, BTN_CLEFT,
SDL_CONTROLLER_BUTTON_Y));
SDL_GAMEPAD_BUTTON_NORTH));
}
break;
case BTN_CRIGHT:
mappings.push_back(std::make_shared<SDLAxisDirectionToButtonMapping>(shipDeviceIndex, portIndex, BTN_CRIGHT,
SDL_CONTROLLER_AXIS_RIGHTX, 1));
SDL_GAMEPAD_AXIS_RIGHTX, 1));
if (isGameCube) {
mappings.push_back(std::make_shared<SDLButtonToButtonMapping>(shipDeviceIndex, portIndex, BTN_CRIGHT,
SDL_CONTROLLER_BUTTON_X));
SDL_GAMEPAD_BUTTON_WEST));
}
break;
case BTN_DUP:
mappings.push_back(std::make_shared<SDLButtonToButtonMapping>(shipDeviceIndex, portIndex, BTN_DUP,
SDL_CONTROLLER_BUTTON_DPAD_UP));
SDL_GAMEPAD_BUTTON_DPAD_UP));
break;
case BTN_DDOWN:
mappings.push_back(std::make_shared<SDLButtonToButtonMapping>(shipDeviceIndex, portIndex, BTN_DDOWN,
SDL_CONTROLLER_BUTTON_DPAD_DOWN));
SDL_GAMEPAD_BUTTON_DPAD_DOWN));
break;
case BTN_DLEFT:
mappings.push_back(std::make_shared<SDLButtonToButtonMapping>(shipDeviceIndex, portIndex, BTN_DLEFT,
SDL_CONTROLLER_BUTTON_DPAD_LEFT));
SDL_GAMEPAD_BUTTON_DPAD_LEFT));
break;
case BTN_DRIGHT:
mappings.push_back(std::make_shared<SDLButtonToButtonMapping>(shipDeviceIndex, portIndex, BTN_DRIGHT,
SDL_CONTROLLER_BUTTON_DPAD_RIGHT));
SDL_GAMEPAD_BUTTON_DPAD_RIGHT));
break;
}

Expand All @@ -247,7 +247,7 @@ ButtonMappingFactory::CreateDefaultSDLButtonMappings(ShipDeviceIndex shipDeviceI

std::shared_ptr<ControllerButtonMapping>
ButtonMappingFactory::CreateButtonMappingFromSDLInput(uint8_t portIndex, CONTROLLERBUTTONS_T bitmask) {
std::unordered_map<ShipDeviceIndex, SDL_GameController*> sdlControllers;
std::unordered_map<ShipDeviceIndex, SDL_Gamepad*> sdlControllers;
std::shared_ptr<ControllerButtonMapping> mapping = nullptr;
for (auto [lusIndex, indexMapping] :
Context::GetInstance()->GetControlDeck()->GetDeviceIndexMappingManager()->GetAllDeviceIndexMappings()) {
Expand All @@ -260,17 +260,17 @@ ButtonMappingFactory::CreateButtonMappingFromSDLInput(uint8_t portIndex, CONTROL

auto sdlIndex = sdlIndexMapping->GetSDLDeviceIndex();

if (!SDL_IsGameController(sdlIndex)) {
if (!SDL_IsGamepad(sdlIndex)) {
// this SDL device isn't a game controller
continue;
}

sdlControllers[lusIndex] = SDL_GameControllerOpen(sdlIndex);
sdlControllers[lusIndex] = SDL_OpenGamepad(sdlIndex);
}

for (auto [lusIndex, controller] : sdlControllers) {
for (int32_t button = SDL_CONTROLLER_BUTTON_A; button < SDL_CONTROLLER_BUTTON_MAX; button++) {
if (SDL_GameControllerGetButton(controller, static_cast<SDL_GameControllerButton>(button))) {
for (int32_t button = SDL_GAMEPAD_BUTTON_SOUTH; button < SDL_GAMEPAD_BUTTON_COUNT; button++) {
if (SDL_GetGamepadButton(controller, static_cast<SDL_GamepadButton>(button))) {
mapping = std::make_shared<SDLButtonToButtonMapping>(lusIndex, portIndex, bitmask, button);
break;
}
Expand All @@ -280,9 +280,9 @@ ButtonMappingFactory::CreateButtonMappingFromSDLInput(uint8_t portIndex, CONTROL
break;
}

for (int32_t i = SDL_CONTROLLER_AXIS_LEFTX; i < SDL_CONTROLLER_AXIS_MAX; i++) {
const auto axis = static_cast<SDL_GameControllerAxis>(i);
const auto axisValue = SDL_GameControllerGetAxis(controller, axis) / 32767.0f;
for (int32_t i = SDL_GAMEPAD_AXIS_LEFTX; i < SDL_GAMEPAD_AXIS_COUNT; i++) {
const auto axis = static_cast<SDL_GamepadAxis>(i);
const auto axisValue = SDL_GetGamepadAxis(controller, axis) / 32767.0f;
int32_t axisDirection = 0;
if (axisValue < -0.7f) {
axisDirection = NEGATIVE;
Expand All @@ -301,7 +301,7 @@ ButtonMappingFactory::CreateButtonMappingFromSDLInput(uint8_t portIndex, CONTROL
}

for (auto [i, controller] : sdlControllers) {
SDL_GameControllerClose(controller);
SDL_CloseGamepad(controller);
}

return mapping;
Expand Down
Loading
Loading