Browse Source

Add gamepad logic

Implement gamepad logic for macOS.
master
TheoryOfNekomata 1 year ago
parent
commit
55901c8e2a
5 changed files with 91 additions and 17 deletions
  1. +56
    -13
      CMakeLists.txt
  2. +4
    -1
      README.md
  3. +0
    -1
      TODO.md
  4. +1
    -0
      dependencies.txt
  5. +30
    -2
      src/packages/game/input/IZ_joystick.c

+ 56
- 13
CMakeLists.txt View File

@@ -7,11 +7,28 @@ set(CMAKE_C_STANDARD 11)

if (WIN32)
# TODO set arch on target instead of generator
set(PLATFORM WINDOWS)
if (CMAKE_SIZEOF_VOID_P EQUAL 8)
set(PROJECT_ARCH x64)
add_definitions(
-DIZ_WIN64
)
else ()
set(PROJECT_ARCH x86)
add_definitions(
-DIZ_WIN32
)
endif ()
elseif(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
set(PLATFORM MACOS)
add_definitions(
-DIZ_MACOS
)
else()
set(PLATFORM UNIX)
add_definitions(
-DIZ_UNIX
)
endif ()

add_definitions(
@@ -40,14 +57,26 @@ include_directories(
"${PROJECT_SOURCE_DIR}/dependencies/sqlite"
)

link_directories(
"${PROJECT_SOURCE_DIR}/dependencies/SDL2/lib/${PROJECT_ARCH}"
"${PROJECT_SOURCE_DIR}/dependencies/SDL2_image/lib/${PROJECT_ARCH}"
"${PROJECT_SOURCE_DIR}/dependencies/SDL2_ttf/lib/${PROJECT_ARCH}"
"${PROJECT_SOURCE_DIR}/dependencies/portmidi/Release"
"${PROJECT_SOURCE_DIR}/dependencies/libwebsockets/build/lib/Release"
"${PROJECT_SOURCE_DIR}/dependencies/openssl/${PROJECT_ARCH}/lib"
)
if (WIN32)
link_directories(
"${PROJECT_SOURCE_DIR}/dependencies/SDL2/lib/${PROJECT_ARCH}"
"${PROJECT_SOURCE_DIR}/dependencies/SDL2_image/lib/${PROJECT_ARCH}"
"${PROJECT_SOURCE_DIR}/dependencies/SDL2_ttf/lib/${PROJECT_ARCH}"
"${PROJECT_SOURCE_DIR}/dependencies/portmidi/Release"
"${PROJECT_SOURCE_DIR}/dependencies/libwebsockets/build/lib/Release"
"${PROJECT_SOURCE_DIR}/dependencies/openssl/${PROJECT_ARCH}/lib"
)
elseif(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
include_directories(
/usr/local/include
/usr/local/include/SDL2
/usr/local/include/openssl
)

link_directories(
/usr/local/lib
)
endif()

add_executable(
game
@@ -96,17 +125,31 @@ add_executable(
src/packages/net/core/IZ_websocket.c
src/packages/net/IZ_net_client.c src/packages/net/IZ_net_client.h src/packages/game/IZ_app_net.c src/packages/game/IZ_app_net.h src/packages/game/IZ_app_video.c src/packages/game/IZ_app_video.h src/packages/game/IZ_subsystem.h src/packages/game/IZ_app_input.c src/packages/game/IZ_app_input.h src/packages/game/IZ_app_config.c src/packages/game/IZ_app_config.h src/packages/game/asset/IZ_asset.c src/packages/game/asset/IZ_asset.h)

target_link_libraries(
game
if (WIN32)
target_link_libraries(
game
SDL2main
SDL2
SDL2_image
SDL2_ttf
portmidi
libcrypto
libssl
websockets
)
elseif(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
target_link_libraries(
game
SDL2main
SDL2
SDL2_image
SDL2_ttf
portmidi
libcrypto
libssl
# libcrypto
# libssl
websockets
)
)
endif()

add_executable(
game-test-geometry


+ 4
- 1
README.md View File

@@ -20,7 +20,10 @@ A run-and-gun shooter inspired by Metal Slug.

1. Clone this repo.
2. Clone the repositories under `dependencies.txt`, including SDL2.
3. Build the SDL2 dependency using the [build instructions for macOS](https://wiki.libsdl.org/SDL2/Installation#macos).
3. Build the SDL, SDL_image, and SDL_ttf dependencies using the [build instructions for macOS](https://wiki.libsdl.org/SDL2/Installation#macos).
> **Note:** You may need to download other tools to build the libraries.

> **Note:** Clone the submodules under SDL_ttf to download freetype and harfbuzz
4. Build the following dependencies first:
- `portmidi`
- `libwebsockets` (follow the [instructions on building LWS](https://libwebsockets.org/lws-api-doc-master/html/md_README_8build.html))


+ 0
- 1
TODO.md View File

@@ -4,4 +4,3 @@
- [ ] Proof-of-concept for fast SVG rendering (we're going to use SVG instead of Spine)
- [ ] Improve logging (remove SDL/lws dependency)
- [ ] Unify memset/memcpy/free/malloc functions (remove SDL/lws dependency)
- [ ] Consider using `_s` functions?

+ 1
- 0
dependencies.txt View File

@@ -1,5 +1,6 @@
https://github.com/libsdl-org/SDL
https://github.com/libsdl-org/SDL_image
https://github.com/libsdl-org/SDL_ttf
https://github.com/compuphase/minIni
https://github.com/grassator/bdd-for-c
https://github.com/PortMidi/portmidi


+ 30
- 2
src/packages/game/input/IZ_joystick.c View File

@@ -8,7 +8,8 @@ bool IZ_JoystickIsValidAxisThreshold(u16 value) {

void IZ_JoystickHandleDeviceEvents(IZ_JoystickState* state, SDL_Event e) {
if (e.type == SDL_JOYDEVICEADDED) {
if (SDL_NumJoysticks() <= IZ_PLAYERS && !state->device) {
u8 joysticks_count = SDL_NumJoysticks();
if (joysticks_count <= IZ_PLAYERS && !state->device) {
state->device = SDL_JoystickOpen(e.jdevice.which);
}
return;
@@ -106,7 +107,34 @@ void IZ_JoystickHandleButtonEvents(IZ_JoystickState* state, IZ_Action* action, S
u8 control_index;

for (control_index = 4; control_index < IZ_CONTROLS; control_index += 1) {
if (e.jbutton.button == state->config.control_mapping[control_index]) {
u8 normalized_button = e.jbutton.button;

/*
* ZL ZR
* L R
*
* U SLCT STRT 3
* L R 2 0
* D 1
*
*
*
*/

// TODO test with XInput, make compatible with Apple

if (IZ_MACOS) {
//printf("%d\n", e.jbutton.button);
if (e.jbutton.button == 2) {
normalized_button = 4;
} else if (e.jbutton.button == 6) {
normalized_button = 11;
} else if (e.jbutton.button == 4) {
normalized_button = 10;
}
}

if (normalized_button == state->config.control_mapping[control_index]) {
const u16 bitflag = (0x1 << control_index);

if (e.type == SDL_JOYBUTTONDOWN) {


Loading…
Cancel
Save