gamebridge: Twitch Control #2

Merged
cadey merged 29 commits from gamebridge into master 2020-05-11 22:40:05 +00:00
5 changed files with 69 additions and 4 deletions
Showing only changes of commit 0a4f693fdc - Show all commits

View File

@ -16,5 +16,6 @@ in pkgs.mkShell {
glfw glfw
libGL libGL
clang_10 clang_10
gdb
]; ];
} }

View File

@ -3,13 +3,14 @@
#include "controller_recorded_tas.h" #include "controller_recorded_tas.h"
#include "controller_keyboard.h" #include "controller_keyboard.h"
#include "controller_sdl.h" #include "controller_sdl.h"
#include "controller_tas_recorder.h"
static struct ControllerAPI *controller_implementations[] = { static struct ControllerAPI *controller_implementations[] = {
&controller_recorded_tas, &controller_recorded_tas,
&controller_sdl, /* &controller_sdl, */
&controller_keyboard, /* &controller_keyboard, */
/* &controller_tas_recorder, */
}; };
s32 osContInit(OSMesgQueue *mq, u8 *controllerBits, OSContStatus *status) { s32 osContInit(OSMesgQueue *mq, u8 *controllerBits, OSContStatus *status) {

View File

@ -23,7 +23,10 @@ static void tas_read(OSContPad *pad) {
if (fd != 0) { if (fd != 0) {
uint8_t bytes[4] = {0}; uint8_t bytes[4] = {0};
int result = read(fd, bytes, 4); int result = read(fd, bytes, 4);
assert(result > 0); if (result < 0) {
return;
}
pad->button = (bytes[0] << 8) | bytes[1]; pad->button = (bytes[0] << 8) | bytes[1];
pad->stick_x = bytes[2]; pad->stick_x = bytes[2];
pad->stick_y = bytes[3]; pad->stick_y = bytes[3];

View File

@ -0,0 +1,52 @@
#include <stdio.h>
#include <ultra64.h>
#include <assert.h>
#include <fcntl.h>
#include "controller_api.h"
static FILE *fout;
static int counter;
#define OFFSET 0x400
#define fname "rec.m64"
static void tas_recorder_close(void) {
fclose(fout);
}
static void tas_recorder_init(void) {
if (fname == NULL) {
fout = NULL;
return;
}
unlink(fname);
printf("[tas_recorder] writing output to %s\n", fname);
fout = fopen(fname, "wb");
assert(fout != NULL);
uint8_t buf[OFFSET];
memset(buf, 0, sizeof(buf));
fwrite(buf, 1, sizeof(buf), fout);
atexit(tas_recorder_close);
}
static void tas_recorder_read(OSContPad *pad) {
if (fout == NULL) {
return;
}
uint8_t bytes[4] = {0};
int button1 = pad->button;
int button2 = pad->button;
bytes[0] = button1 >> 8;
bytes[1] = button2 & 0x00FF;
bytes[2] = pad->stick_x;
bytes[3] = pad->stick_y;
fwrite(bytes, 1, 4, fout);
}
struct ControllerAPI controller_tas_recorder = {
tas_recorder_init,
tas_recorder_read
};

View File

@ -0,0 +1,8 @@
#ifndef CONTROLLER_TAS_RECORDER_H
#define CONTROLLER_TAS_RECORDER_H
#include "controller_api.h"
extern struct ControllerAPI controller_tas_recorder;
#endif