Skip to content

Commit

Permalink
configurable joystick in all platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
jmgk77 committed Jun 30, 2022
1 parent 6d1614d commit c1b697c
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 40 deletions.
4 changes: 2 additions & 2 deletions Makefile.linux
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
EXE = chip8

SOURCES = source/main.cpp source/chip8.cpp source/sdl1_chip8.cpp
SOURCES = source/main.cpp source/chip8.cpp source/sdl1_chip8.cpp source/config.cpp
OBJS = $(addsuffix .o, $(basename $(SOURCES)))

CXXFLAGS = -I./include -g -Wall -Wformat -O3
CXXFLAGS = -I./include -g -O3 -Wno-unused-result
LIBS = `sdl-config --libs` -lSDL_ttf

%.o:%.cpp
Expand Down
2 changes: 1 addition & 1 deletion Makefile.ps2
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
EE_BIN = chip8_ps2.elf

EE_SRCS = source/main.cpp source/chip8.cpp source/sdl1_chip8.cpp
EE_SRCS = source/main.cpp source/chip8.cpp source/sdl1_chip8.cpp source/config.cpp
EE_OBJS = $(EE_SRCS:.cpp=.o)

EE_INCS = -I$(PS2SDK)/ports/include -I./include -I$(PS2SDK)/ports/include/stlport
Expand Down
39 changes: 29 additions & 10 deletions chip8.ini
Original file line number Diff line number Diff line change
@@ -1,15 +1,34 @@
; PS3
; BUTTON_A 5
; BUTTON_B 11
; BUTTON_X 10
; BUTTON_Y 0
; BUTTON_L1 1
; BUTTON_R1 3
; BUTTON_L3 12
; BUTTON_R3 13
; BUTTON_LEFT 4
; BUTTON_DOWN 8
; BUTTON_RIGHT 6
; BUTTON_UP 2
; BUTTON_L2 7
; BUTTON_R2 9
; BUTTON_START 14

; LINUX/PS2
BUTTON_A 5
BUTTON_B 11
BUTTON_X 10
BUTTON_Y 0
BUTTON_L1 1
BUTTON_R1 3
BUTTON_L3 12
BUTTON_R3 13
BUTTON_LEFT 4
BUTTON_DOWN 8
BUTTON_RIGHT 6
BUTTON_L1 12
BUTTON_R1 13
BUTTON_L3 14
BUTTON_R3 15
BUTTON_UP 2
BUTTON_L2 7
BUTTON_R2 9
BUTTON_START 14
BUTTON_RIGHTUP 3
BUTTON_RIGHT 6
BUTTON_RIGHTDOWN 9
BUTTON_DOWN 8
BUTTON_LEFTDOWN 7
BUTTON_LEFT 4
BUTTON_LEFTUP 1
12 changes: 12 additions & 0 deletions include/defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@
#define BUTTON_START 5
#define BUTTON_L3 10
#define BUTTON_R3 11
#define BUTTON_L2 -1
#define BUTTON_R2 -1
#define BUTTON_LEFT -1
#define BUTTON_DOWN -1
#define BUTTON_RIGHT -1
#define BUTTON_UP -1
#elif defined(PS3)
#define BUTTON_A 9
#define BUTTON_B 10
Expand Down Expand Up @@ -93,4 +99,10 @@
#define BUTTON_SYS 8
#define BUTTON_L3 9
#define BUTTON_R3 10
#define BUTTON_L2 -1
#define BUTTON_R2 -1
#define BUTTON_LEFT -1
#define BUTTON_DOWN -1
#define BUTTON_RIGHT -1
#define BUTTON_UP -1
#endif
5 changes: 1 addition & 4 deletions include/sdl1_chip8.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,14 @@
#include "defs.h"

class sdl1_chip8 : public chip8 {
#ifdef PS3
public:
#else
private:
#endif
// keyboard
uint8_t sdl_keys[16] = {SDLK_x, SDLK_1, SDLK_2, SDLK_3, SDLK_q, SDLK_w,
SDLK_e, SDLK_a, SDLK_s, SDLK_d, SDLK_z, SDLK_c,
SDLK_4, SDLK_r, SDLK_f, SDLK_v};
// joystick
#ifndef PS3
// LINUX/PS2
uint8_t sdl_buttons[8] = {BUTTON_A, BUTTON_B, BUTTON_X, BUTTON_Y,
BUTTON_L1, BUTTON_R1, BUTTON_L3, BUTTON_R3};
uint8_t buttons2chip8[8] = {5, 0xB, 0xA, 0, 0xC, 0xD, 0xE, 0xF};
Expand Down
56 changes: 37 additions & 19 deletions source/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,24 @@ struct entry {
};

/* sorted according to str */
struct entry dict[] = {
"BUTTON_A", BUTTON_A, "BUTTON_B", BUTTON_B,
"BUTTON_BACK", BUTTON_BACK, "BUTTON_DOWN", BUTTON_DOWN,
"BUTTON_L1", BUTTON_L1, "BUTTON_L2", BUTTON_L2,
"BUTTON_L3", BUTTON_L3, "BUTTON_LEFT", BUTTON_LEFT,
"BUTTON_R1", BUTTON_R1, "BUTTON_R2", BUTTON_R2 "BUTTON_R3",
BUTTON_R3, "BUTTON_RIGHT", BUTTON_RIGHT, "BUTTON_START",
BUTTON_START, "BUTTON_UP", BUTTON_UP, "BUTTON_X",
BUTTON_X, "BUTTON_Y", BUTTON_Y,
struct entry dict_button[] = {
"BUTTON_A", BUTTON_A, "BUTTON_B", BUTTON_B, "BUTTON_BACK",
BUTTON_BACK, "BUTTON_DOWN", BUTTON_DOWN, "BUTTON_L1", BUTTON_L1,
"BUTTON_L2", BUTTON_L2, "BUTTON_L3", BUTTON_L3, "BUTTON_LEFT",
BUTTON_LEFT, "BUTTON_R1", BUTTON_R1, "BUTTON_R2", BUTTON_R2,
"BUTTON_R3", BUTTON_R3, "BUTTON_RIGHT", BUTTON_RIGHT, "BUTTON_START",
BUTTON_START, "BUTTON_UP", BUTTON_UP, "BUTTON_X", BUTTON_X,
"BUTTON_Y", BUTTON_Y,
};
#ifndef PS3
//
struct entry dict_hat[] = {
"BUTTON_DOWN", SDL_HAT_DOWN, "BUTTON_LEFT", SDL_HAT_LEFT,
"BUTTON_LEFTDOWN", SDL_HAT_LEFTDOWN, "BUTTON_LEFTUP", SDL_HAT_LEFTUP,
"BUTTON_RIGHT", SDL_HAT_RIGHT, "BUTTON_RIGHTDOWN", SDL_HAT_RIGHTDOWN,
"BUTTON_RIGHTUP", SDL_HAT_RIGHTUP, "BUTTON_UP", SDL_HAT_UP,
};
#endif

int compare(const void *s1, const void *s2) {
const struct entry *e1 = (const entry *)s1;
Expand All @@ -34,31 +41,42 @@ int compare(const void *s1, const void *s2) {

void read_config() {
FILE *fp;
char *line = NULL;
size_t len = 0;
ssize_t read;

fp = fopen(ROM_DIRECTORY "/chip8.ini", "r");
if (fp != NULL) {
int i = 0;
while ((read = getline(&line, &len, fp)) != -1) {
if (i > sizeof(c8.sdl_buttons)) {
continue;
}
int j = 0;

char line[128];
while (fgets(line, sizeof(line), fp) != NULL) {
char command[256];
int code;
sscanf(line, "%s %d", command, &code);

// if (code != -2) {
struct entry *result;
struct entry in;
in.str = command;
result = (entry *)bsearch(&in, dict, sizeof(dict) / sizeof(dict[0]),
sizeof dict[0], compare);
if (result) {
// check buttons
result = (entry *)bsearch(&in, dict_button,
sizeof(dict_button) / sizeof(dict_button[0]),
sizeof dict_button[0], compare);
if ((result) && (i <= (int)sizeof(c8.sdl_buttons)) && (result->n != -1)) {
c8.sdl_buttons[i] = result->n;
c8.buttons2chip8[i] = code;
i++;
}
#ifndef PS3
// check hats
result = (entry *)bsearch(&in, dict_hat,
sizeof(dict_hat) / sizeof(dict_hat[0]),
sizeof dict_hat[0], compare);
if ((result) && (j <= (int)sizeof(c8.sdl_hats)) && (result->n != -1)) {
c8.sdl_hats[j] = result->n;
c8.hat2chip8[j] = code;
j++;
}
#endif
}
fclose(fp);
}
Expand Down
5 changes: 1 addition & 4 deletions source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@
#include "usbmass_bd.h"
#endif

#ifdef PS3
extern void read_config();
#endif

sdl1_chip8 c8;

Expand Down Expand Up @@ -72,9 +70,8 @@ int main(int argc, char *argv[]) {
chdir(argv[1]);
}
#endif
#ifdef PS3

read_config();
#endif
c8.init_screen();

// fps cap
Expand Down

0 comments on commit c1b697c

Please sign in to comment.