Add joypad support via SDL to Qt build

This commit is contained in:
Jeffrey Pfau 2014-02-03 22:28:28 -08:00
parent edc6de0467
commit b691c93416
4 changed files with 82 additions and 5 deletions

View File

@ -50,6 +50,7 @@ add_library(${BINARY_NAME} SHARED ${ARM_SRC} ${GBA_SRC} ${DEBUGGER_SRC} ${RENDER
target_link_libraries(${BINARY_NAME} m ${DEBUGGER_LIB} ${OS_LIB}) target_link_libraries(${BINARY_NAME} m ${DEBUGGER_LIB} ${OS_LIB})
if(BUILD_SDL) if(BUILD_SDL)
add_definitions(-DBUILD_SDL)
add_subdirectory(${CMAKE_SOURCE_DIR}/src/platform/sdl ${CMAKE_BINARY_DIR}/sdl) add_subdirectory(${CMAKE_SOURCE_DIR}/src/platform/sdl ${CMAKE_BINARY_DIR}/sdl)
endif() endif()

View File

@ -8,6 +8,13 @@ if(APPLE)
set(CMAKE_PREFIX_PATH "/usr/local/opt/qt5") # Temporary stopgap measure set(CMAKE_PREFIX_PATH "/usr/local/opt/qt5") # Temporary stopgap measure
endif() 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_AUTOMOC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON) set(CMAKE_INCLUDE_CURRENT_DIR ON)
@ -17,10 +24,10 @@ find_package(OpenGL REQUIRED)
set(SOURCE_FILES AudioDevice.cpp Display.cpp GameController.cpp Window.cpp) set(SOURCE_FILES AudioDevice.cpp Display.cpp GameController.cpp Window.cpp)
if(USE_GDB_STUB) 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() endif()
add_executable(QGBAc WIN32 MACOSX_BUNDLE main.cpp ${SOURCE_FILES}) add_executable(QGBAc WIN32 MACOSX_BUNDLE main.cpp ${SOURCE_FILES})
qt5_use_modules(QGBAc Widgets Multimedia OpenGL) 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})

View File

@ -11,8 +11,14 @@ GameController::GameController(QObject* parent)
: QObject(parent) : QObject(parent)
, m_drawContext(new uint32_t[256 * 256]) , m_drawContext(new uint32_t[256 * 256])
, m_audioContext(nullptr) , m_audioContext(nullptr)
, m_activeKeys(0)
, m_rom(nullptr) , 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; m_renderer = new GBAVideoSoftwareRenderer;
GBAVideoSoftwareRendererCreate(m_renderer); GBAVideoSoftwareRendererCreate(m_renderer);
m_renderer->outputBuffer = (color_t*) m_drawContext; m_renderer->outputBuffer = (color_t*) m_drawContext;
@ -46,6 +52,10 @@ GameController::GameController(QObject* parent)
controller->m_pauseMutex.unlock(); controller->m_pauseMutex.unlock();
controller->frameAvailable(controller->m_drawContext); controller->frameAvailable(controller->m_drawContext);
}; };
#ifdef BUILD_SDL
connect(this, SIGNAL(frameAvailable(const uint32_t*)), this, SLOT(testSDLEvents()));
#endif
} }
GameController::~GameController() { GameController::~GameController() {
@ -129,10 +139,56 @@ void GameController::frameAdvance() {
void GameController::keyPressed(int key) { void GameController::keyPressed(int key) {
int mappedKey = 1 << key; int mappedKey = 1 << key;
m_threadContext.activeKeys |= mappedKey; m_activeKeys |= mappedKey;
updateKeys();
} }
void GameController::keyReleased(int key) { void GameController::keyReleased(int key) {
int mappedKey = 1 << 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

View File

@ -11,6 +11,9 @@
extern "C" { extern "C" {
#include "gba-thread.h" #include "gba-thread.h"
#ifdef BUILD_SDL
#include "sdl-events.h"
#endif
} }
struct GBAAudio; struct GBAAudio;
@ -47,13 +50,23 @@ public slots:
void keyPressed(int key); void keyPressed(int key);
void keyReleased(int key); void keyReleased(int key);
#ifdef BUILD_SDL
private slots:
void testSDLEvents();
private: private:
void setupAudio(GBAAudio* audio); GBASDLEvents m_sdlEvents;
int m_activeButtons;
#endif
private:
void updateKeys();
uint32_t* m_drawContext; uint32_t* m_drawContext;
AudioDevice* m_audioContext; AudioDevice* m_audioContext;
GBAThread m_threadContext; GBAThread m_threadContext;
GBAVideoSoftwareRenderer* m_renderer; GBAVideoSoftwareRenderer* m_renderer;
int m_activeKeys;
QFile* m_rom; QFile* m_rom;
QFile* m_bios; QFile* m_bios;