Add joypad support via SDL to Qt build
Jeffrey Pfau jeffrey@endrift.com
Mon, 03 Feb 2014 22:28:28 -0800
4 files changed,
82 insertions(+),
5 deletions(-)
M
CMakeLists.txt
→
CMakeLists.txt
@@ -50,6 +50,7 @@ add_library(${BINARY_NAME} SHARED ${ARM_SRC} ${GBA_SRC} ${DEBUGGER_SRC} ${RENDERER_SRC} ${UTIL_SRC} ${OS_SRC})
target_link_libraries(${BINARY_NAME} m ${DEBUGGER_LIB} ${OS_LIB}) if(BUILD_SDL) + add_definitions(-DBUILD_SDL) add_subdirectory(${CMAKE_SOURCE_DIR}/src/platform/sdl ${CMAKE_BINARY_DIR}/sdl) endif()
M
src/platform/qt/CMakeLists.txt
→
src/platform/qt/CMakeLists.txt
@@ -8,6 +8,13 @@ if(APPLE)
set(CMAKE_PREFIX_PATH "/usr/local/opt/qt5") # Temporary stopgap measure endif() +if(BUILD_SDL) + find_package(SDL 1.2 REQUIRED) + set(PLATFORM_LIBRARY "${PLATFORM_LIBRARY};${SDL_LIBRARY};${SDLMAIN_LIBRARY}") + set(PLATFORM_SRC ${PLATFORM_SRC} ${CMAKE_SOURCE_DIR}/src/platform/sdl/sdl-events.c) + include_directories(${SDL_INCLUDE_DIR} ${CMAKE_SOURCE_DIR}/src/platform/sdl) +endif() + set(CMAKE_AUTOMOC ON) set(CMAKE_INCLUDE_CURRENT_DIR ON)@@ -17,10 +24,10 @@
set(SOURCE_FILES AudioDevice.cpp Display.cpp GameController.cpp Window.cpp) if(USE_GDB_STUB) - set(SOURCE_FILES ${SOURCE_FILES} GDBController.cpp GDBWindow.cpp) + set(SOURCE_FILES ${PLATFORM_SRC} ${SOURCE_FILES} GDBController.cpp GDBWindow.cpp) endif() add_executable(QGBAc WIN32 MACOSX_BUNDLE main.cpp ${SOURCE_FILES}) qt5_use_modules(QGBAc Widgets Multimedia OpenGL) -target_link_libraries(QGBAc ${OPENGL_LIBRARY} ${BINARY_NAME}) +target_link_libraries(QGBAc ${PLATFORM_LIBRARY} ${OPENGL_LIBRARY} ${BINARY_NAME})
M
src/platform/qt/GameController.cpp
→
src/platform/qt/GameController.cpp
@@ -11,8 +11,14 @@ GameController::GameController(QObject* parent)
: QObject(parent) , m_drawContext(new uint32_t[256 * 256]) , m_audioContext(nullptr) + , m_activeKeys(0) , m_rom(nullptr) { +#ifdef BUILD_SDL + SDL_Init(SDL_INIT_JOYSTICK | SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE); + GBASDLInitEvents(&m_sdlEvents); + SDL_JoystickEventState(SDL_QUERY); +#endif m_renderer = new GBAVideoSoftwareRenderer; GBAVideoSoftwareRendererCreate(m_renderer); m_renderer->outputBuffer = (color_t*) m_drawContext;@@ -46,6 +52,10 @@ }
controller->m_pauseMutex.unlock(); controller->frameAvailable(controller->m_drawContext); }; + +#ifdef BUILD_SDL + connect(this, SIGNAL(frameAvailable(const uint32_t*)), this, SLOT(testSDLEvents())); +#endif } GameController::~GameController() {@@ -129,10 +139,56 @@ }
void GameController::keyPressed(int key) { int mappedKey = 1 << key; - m_threadContext.activeKeys |= mappedKey; + m_activeKeys |= mappedKey; + updateKeys(); } void GameController::keyReleased(int key) { int mappedKey = 1 << key; - m_threadContext.activeKeys &= ~mappedKey; + m_activeKeys &= ~mappedKey; + updateKeys(); } + +void GameController::updateKeys() { + int activeKeys = m_activeKeys; +#ifdef BUILD_SDL + activeKeys |= m_activeButtons; +#endif + m_threadContext.activeKeys = activeKeys; +} + +#ifdef BUILD_SDL +void GameController::testSDLEvents() { + SDL_Joystick* joystick = m_sdlEvents.joystick; + SDL_JoystickUpdate(); + int numButtons = SDL_JoystickNumButtons(joystick); + m_activeButtons = 0; + int i; + for (i = 0; i < numButtons; ++i) { + GBAKey key = GBASDLMapButtonToKey(i); + if (key == GBA_KEY_NONE) { + continue; + } + if (SDL_JoystickGetButton(joystick, i)) { + m_activeButtons |= 1 << key; + } + } + int numHats = SDL_JoystickNumHats(joystick); + for (i = 0; i < numHats; ++i) { + int hat = SDL_JoystickGetHat(joystick, i); + if (hat & SDL_HAT_UP) { + m_activeButtons |= 1 << GBA_KEY_UP; + } + if (hat & SDL_HAT_LEFT) { + m_activeButtons |= 1 << GBA_KEY_LEFT; + } + if (hat & SDL_HAT_DOWN) { + m_activeButtons |= 1 << GBA_KEY_DOWN; + } + if (hat & SDL_HAT_RIGHT) { + m_activeButtons |= 1 << GBA_KEY_RIGHT; + } + } + updateKeys(); +} +#endif
M
src/platform/qt/GameController.h
→
src/platform/qt/GameController.h
@@ -11,6 +11,9 @@ #include "AudioDevice.h"
extern "C" { #include "gba-thread.h" +#ifdef BUILD_SDL +#include "sdl-events.h" +#endif } struct GBAAudio;@@ -47,13 +50,23 @@ void frameAdvance();
void keyPressed(int key); void keyReleased(int key); +#ifdef BUILD_SDL +private slots: + void testSDLEvents(); + private: - void setupAudio(GBAAudio* audio); + GBASDLEvents m_sdlEvents; + int m_activeButtons; +#endif + +private: + void updateKeys(); uint32_t* m_drawContext; AudioDevice* m_audioContext; GBAThread m_threadContext; GBAVideoSoftwareRenderer* m_renderer; + int m_activeKeys; QFile* m_rom; QFile* m_bios;