Skip to content

Commit

Permalink
Merge branch 'main' of github.com:marekmaskarinec/tophat
Browse files Browse the repository at this point in the history
  • Loading branch information
marekmaskarinec committed Apr 10, 2024
2 parents a1cf0a7 + bb5048b commit 74771a7
Show file tree
Hide file tree
Showing 12 changed files with 712 additions and 6 deletions.
8 changes: 6 additions & 2 deletions .github/workflows/package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ jobs:
steps:
- uses: actions/checkout@v2
- name: Install packages
run: sudo apt install -y openssh-client mesa-common-dev xorg-dev libxi-dev libxcursor-dev mingw-w64 mingw-w64-common
run: |
sudo apt update
sudo apt install -y openssh-client mesa-common-dev xorg-dev libxi-dev libxcursor-dev mingw-w64 mingw-w64-common
- name: Install SSH key
uses: shimataro/ssh-key-action@v2
with:
Expand Down Expand Up @@ -53,7 +55,9 @@ jobs:
- uses: actions/checkout@v2
- uses: mymindstorm/setup-emsdk@v14
- name: Install packages
run: sudo apt install -y openssh-client mesa-common-dev xorg-dev libxi-dev libxcursor-dev mingw-w64
run: |
sudo apt update
sudo apt install -y openssh-client mesa-common-dev xorg-dev libxi-dev libxcursor-dev mingw-w64
- name: Update submodules
run: |
git submodule init
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ if(MSVC)
string(STRIP "${gitver}" gitver)
target_compile_definitions(tophat PUBLIC -DTH_VERSION=\"${ver}\" -DTH_GITVER=\"${gitver}\")

target_link_libraries(tophat gdi32 opengl32 user32 ${umka_lib_path})
target_link_libraries(tophat xinput gdi32 opengl32 user32 ${umka_lib_path})
else()
message("TODO(skejeton): This script only works with MSVC. If you are on Linux or WSL/MSYS2 run `make` instead.")
endif()
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ SHORT_PLATFORM:= $(shell (X=`uname -s`; echo $${X:0:10}))
ifeq ($(PLATFORM), Linux)
LDLIBS += -lm -lX11 -ldl -lGL -lpthread -fPIC -lXi -lXcursor
CROSS_CC=x86_64-w64-mingw32-gcc
CROSS_FLAGS = -lopengl32 -lgdi32 -DNO_OPENGL_HEADERS
CROSS_FLAGS = -lopengl32 -lgdi32 -lxinput -DNO_OPENGL_HEADERS
TARGET=tophat
UMKA_BIN = ./lib/umka/build/umka
else
ifeq ($(SHORT_PLATFORM), MINGW64_NT)
LDLIBS += -lm -lopengl32 -lgdi32 -Wl,-Bstatic -lpthread
LDLIBS += -lm -lopengl32 -lgdi32 -Wl,-Bstatic -lpthread -lxinput
TARGET=tophat.exe
DEFS += -DNO_OPENGL_HEADERS
UMKA_BIN = ./lib/umka/build/umka.exe
Expand Down
2 changes: 2 additions & 0 deletions src/audio.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ th_audio_load(th_sound **out, char *path, uint32_t flags)
if (ma_sound_init_from_file(&thg->audio_engine, path,
MA_SOUND_FLAG_DECODE | MA_SOUND_FLAG_NO_SPATIALIZATION | flags, NULL, NULL,
&s->inst) != MA_SUCCESS) {
umkaDecRef(thg->umka, s);
return th_err_io;
}

Expand All @@ -58,6 +59,7 @@ th_sound_copy(th_sound **out, th_sound *s)
if (ma_sound_init_copy(&thg->audio_engine, &s->inst,
MA_SOUND_FLAG_DECODE | MA_SOUND_FLAG_NO_SPATIALIZATION, NULL,
&r->inst) != MA_SUCCESS) {
umkaDecRef(thg->umka, r);
return 1;
}

Expand Down
130 changes: 130 additions & 0 deletions src/bindings.c
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,126 @@ umth_input_get_mouse_scroll(UmkaStackSlot *p, UmkaStackSlot *r)
*(fu *)(p[1].ptrVal) = thg->mouse_wheel.x;
}

void
umth_input_gamepad_get_gamepads(UmkaStackSlot *p, UmkaStackSlot *r)
{
((int64_t *)p[0].ptrVal)[0] = thg->gamepad[0].connected ? 0 : -1;
((int64_t *)p[0].ptrVal)[1] = thg->gamepad[1].connected ? 1 : -1;
((int64_t *)p[0].ptrVal)[2] = thg->gamepad[2].connected ? 2 : -1;
((int64_t *)p[0].ptrVal)[3] = thg->gamepad[3].connected ? 3 : -1;
}

void
umth_input_gamepad_get_gamepad(UmkaStackSlot *p, UmkaStackSlot *r)
{
for (int i = 0; i < 4; i++) {
if (thg->gamepad[i].connected) {
r->intVal = i;
return;
}
}

r->intVal = -1;
}

void
umth_input_gamepad_is_pressed(UmkaStackSlot *p, UmkaStackSlot *r)
{
int button = p[0].intVal;
int gamepad = p[1].intVal;

if (gamepad < 0 || gamepad >= 4) {
r->intVal = 0;
return;
}

r->intVal = thg->gamepad[gamepad].buttons[button].pressed;
}

void
umth_input_gamepad_is_just_pressed(UmkaStackSlot *p, UmkaStackSlot *r)
{
int button = p[0].intVal;
int gamepad = p[1].intVal;

if (gamepad < 0 || gamepad >= 4) {
r->intVal = 0;
return;
}

r->intVal = thg->gamepad[gamepad].buttons[button].just_pressed;
}

void
umth_input_gamepad_is_just_released(UmkaStackSlot *p, UmkaStackSlot *r)
{
int button = p[0].intVal;
int gamepad = p[1].intVal;

if (gamepad < 0 || gamepad >= 4) {
r->intVal = 0;
return;
}

r->intVal = thg->gamepad[gamepad].buttons[button].just_released;
}

void
umth_input_gamepad_pressure(UmkaStackSlot *p, UmkaStackSlot *r)
{
int button = p[0].intVal;
int gamepad = p[1].intVal;

if (gamepad < 0 || gamepad >= 4) {
r->realVal = 0;
return;
}

r->realVal = thg->gamepad[gamepad].buttons[button].pressure;
}

void
umth_input_gamepad_stick(UmkaStackSlot *p, UmkaStackSlot *r)
{
th_vf2 *out = p[0].ptrVal;
int stick = p[1].intVal;
int gamepad = p[2].intVal;

if (gamepad < 0 || gamepad >= 4) {
*out = (th_vf2){0};
return;
}

switch (stick) {
case 0: *out = thg->gamepad[gamepad].left_stick; break;
case 1: *out = thg->gamepad[gamepad].right_stick; break;
}
}

void
umth_input_gamepad_rumble(UmkaStackSlot *p, UmkaStackSlot *r)
{
int gamepad = p[2].intVal;
float left = p[1].realVal;
float right = p[0].realVal;

if (gamepad < 0 || gamepad >= 4) {
return;
}

if (left < 0)
left = 0;
if (right < 0)
right = 0;
if (left > 0.99)
left = 0.99;
if (right > 0.99)
right = 0.99;

thg->gamepad[gamepad].rumble_left = left;
thg->gamepad[gamepad].rumble_right = right;
}

///////////////////////
// entities
// draws an entity
Expand Down Expand Up @@ -1204,6 +1324,16 @@ _th_umka_bind(void *umka)
umkaAddFunc(umka, "umth_input_get_str", &umth_input_get_str);
umkaAddFunc(umka, "umth_input_get_mouse_delta", &umth_input_get_mouse_delta);
umkaAddFunc(umka, "umth_input_get_mouse_scroll", &umth_input_get_mouse_scroll);
umkaAddFunc(umka, "umth_input_gamepad_get_gamepads", &umth_input_gamepad_get_gamepads);
umkaAddFunc(umka, "umth_input_gamepad_get_gamepad", &umth_input_gamepad_get_gamepad);
umkaAddFunc(umka, "umth_input_gamepad_is_pressed", &umth_input_gamepad_is_pressed);
umkaAddFunc(
umka, "umth_input_gamepad_is_just_pressed", &umth_input_gamepad_is_just_pressed);
umkaAddFunc(
umka, "umth_input_gamepad_is_just_released", &umth_input_gamepad_is_just_released);
umkaAddFunc(umka, "umth_input_gamepad_pressure", &umth_input_gamepad_pressure);
umkaAddFunc(umka, "umth_input_gamepad_stick", &umth_input_gamepad_stick);
umkaAddFunc(umka, "umth_input_gamepad_rumble", &umth_input_gamepad_rumble);

// entities
umkaAddFunc(umka, "umth_ent_draw", &umth_ent_draw);
Expand Down
2 changes: 2 additions & 0 deletions src/image.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ th_image_create_render_target(th_render_target **out, int width, int height, int
t->image->smp = sg_make_sampler(&smp_desc);
th_err err = assert_image(t->image);
if (err) {
umkaDecRef(thg->umka, t);
return err;
}

Expand Down Expand Up @@ -204,6 +205,7 @@ th_load_image(th_image **out, char *path)
th_err err = gen_tex(img, (uint32_t *)data);
stbi_image_free(data);
if (err) {
umkaDecRef(thg->umka, img);
return err;
}

Expand Down
85 changes: 85 additions & 0 deletions src/input.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#include "tophat.h"
#include <string.h>
#ifdef _WIN32
#include <math.h>
#include <xinput.h>
#endif

extern th_global *thg;

Expand Down Expand Up @@ -63,3 +67,84 @@ th_input_cycle()
memset(thg->just_released, 0, 512 * sizeof(uu));
memset(thg->press_repeat, 0, 512 * sizeof(uu));
}

// Deadzone and drift fix
static float
i_fix(float value)
{
if (fabs(value) < 0.08)
return 0;
return value;
}

static void
i_update_button(th_gamepad_button *btn, float value)
{
btn->just_pressed = !btn->pressed && value > 0.5f;
btn->just_released = btn->pressed && value <= 0.5f;
btn->pressed = value > 0.5f;
btn->pressure = value;
}

void
th_input_update_gamepads()
{
#ifdef _WIN32
XINPUT_STATE state;
for (int i = 0; i < 4; i++) {
state = (XINPUT_STATE){0};

th_generic_gamepad *gp = &thg->gamepad[i];

if (XInputGetState(i, &state) == ERROR_SUCCESS) {
XINPUT_VIBRATION vib = {0};
vib.wLeftMotorSpeed = (gp->rumble_left * 65535);
vib.wRightMotorSpeed = (gp->rumble_right * 65535);
XInputSetState(i, &vib);

gp->rumble_left = 0;
gp->rumble_right = 0;

gp->connected = true;

XINPUT_GAMEPAD xgp = state.Gamepad;

i_update_button(&gp->dpad_up, (xgp.wButtons & XINPUT_GAMEPAD_DPAD_UP) > 0);
i_update_button(
&gp->dpad_down, (xgp.wButtons & XINPUT_GAMEPAD_DPAD_DOWN) > 0);
i_update_button(
&gp->dpad_left, (xgp.wButtons & XINPUT_GAMEPAD_DPAD_LEFT) > 0);
i_update_button(
&gp->dpad_right, (xgp.wButtons & XINPUT_GAMEPAD_DPAD_RIGHT) > 0);

i_update_button(&gp->start, (xgp.wButtons & XINPUT_GAMEPAD_START) > 0);
i_update_button(&gp->select, (xgp.wButtons & XINPUT_GAMEPAD_BACK) > 0);

i_update_button(
&gp->left_stick_press, (xgp.wButtons & XINPUT_GAMEPAD_LEFT_THUMB) > 0);
i_update_button(&gp->right_stick_press,
(xgp.wButtons & XINPUT_GAMEPAD_RIGHT_THUMB) > 0);

i_update_button(&gp->LB, (xgp.wButtons & XINPUT_GAMEPAD_LEFT_SHOULDER) > 0);
i_update_button(
&gp->RB, (xgp.wButtons & XINPUT_GAMEPAD_RIGHT_SHOULDER) > 0);

i_update_button(&gp->A, (xgp.wButtons & XINPUT_GAMEPAD_A) > 0);
i_update_button(&gp->B, (xgp.wButtons & XINPUT_GAMEPAD_B) > 0);
i_update_button(&gp->X, (xgp.wButtons & XINPUT_GAMEPAD_X) > 0);
i_update_button(&gp->Y, (xgp.wButtons & XINPUT_GAMEPAD_Y) > 0);

i_update_button(&gp->LT, (float)xgp.bLeftTrigger / 255.0f);
i_update_button(&gp->RT, (float)xgp.bRightTrigger / 255.0f);

gp->left_stick.x = i_fix((float)xgp.sThumbLX / 32768.0f);
gp->left_stick.y = i_fix((float)xgp.sThumbLY / 32768.0f);

gp->right_stick.x = i_fix((float)xgp.sThumbRX / 32768.0f);
gp->right_stick.y = i_fix((float)xgp.sThumbRY / 32768.0f);
} else {
gp->connected = false;
}
}
#endif
}
Loading

0 comments on commit 74771a7

Please sign in to comment.